5.7 플래피버드 게임 추가기능 구현하기 (파이프 위치 랜덤화)
import random
TITLE = 'Flappy Bird'
WIDTH = 400
HEIGHT = 708
GRAVITY = 0.3
drop_speed = 0
GAP = 140
PIPE_SPEED = -3
bird_alive = True
score = 0
# Actor 객체들
flappy_bird = Actor('bird1', (75, 350))
top_pipe = Actor('top', (350,0))
bottom_pipe = Actor('bottom', (350, top_pipe.height + GAP))
def draw():
screen.blit('background', (0, 0))
flappy_bird.draw()
top_pipe.draw()
bottom_pipe.draw()
screen.draw.text(
str(score),
color = 'white',
midtop = (WIDTH/2, 10),
fontsize = 70,
shadow = (1, 1)
)
def reset_pipes():
random_y = random.randint(-100, 100)
top_pipe.y = random_y
bottom_pipe.y = top_pipe.height + GAP + random_y
top_pipe.x = WIDTH
bottom_pipe.x = WIDTH
def update():
global drop_speed, bird_alive, score
drop_speed += GRAVITY
flappy_bird.y += drop_speed
top_pipe.x += PIPE_SPEED
bottom_pipe.x += PIPE_SPEED
# 파이프의 무한순환
if top_pipe.right < 0 or bottom_pipe.right < 0:
reset_pipes()
if bird_alive == True:
score += 1
# 파이프와의 충돌
if flappy_bird.colliderect(top_pipe) or flappy_bird.colliderect(bottom_pipe):
flappy_bird.image = "birddead"
bird_alive = False
# 게임의 재시작
if flappy_bird.y > HEIGHT or flappy_bird.y < 0:
flappy_bird.image = "bird1"
bird_alive = True
flappy_bird.center = (75, 350)
drop_speed = 0
reset_pipes()
score = 0
def on_mouse_down():
global drop_speed
if bird_alive == True:
drop_speed = -6.5Last updated