Em bị lỗi name is not defined ở line 7, mọi người chỉ em với ạ
import pygame, sys, random
def draw_floor():
screen.blit(floor, (floor_x_pos,600))
screen.blit(floor, (floor_x_pos+432,600))
def create_pipe():
random_pipe_pos = random.choice(pipe_height)
bottom_pipe = pipe_surface.get_rect(midtop = (500, random_pipe_pos))
top_pipe = pipe_surface.get_rect(midtop = (500, random_pipe_pos - 650))
return bottom_pipe, top_pipe
def move_pipe(pipes):
for pipe in pipes:
pipe.centerx -= 5
return pipes
def draw_pipe(pipes):
for pipe in pipes:
if pipe.bottom >= 600:
screen.blit(pipe_surface, pipe)
else:
flip_pipe = pygame.transform.flip(pipe_surface, False, True)
screen.blit(flip_pipe, pipe)
def rotate_bird(bird1):
new_bird = pygame.transform.rotozoom(bird1,-bird_movement*3, 1)
return new_bird
def check_collision(pipes):
for pipe in pipes:
if bird_rect.colliderect(pipe):
return False
if bird_rect.top <= -75 or bird_rect.bottom >= 650:
return False
return True
def bird_animation():
new_bird = bird_list[bird_index]
new_bird_rect = new_bird.get_rect(center = (100, bird_rect.centery))
return new_bird, new_bird_rect
pygame.init()
screen = pygame.display.set_mode((432,768))
clock = pygame.time.Clock()
gravity = 0.25
bird_movement = 0
game_active = True
#chen background
bg = pygame.image.load('assets/background-night.png').convert()
bg = pygame.transform.scale2x(bg)
#chen san
floor = pygame.image.load('assets/floor.png').convert()
floor = pygame.transform.scale2x(floor)
floor_x_pos = 0
bird_down = pygame.image.load('assets/yellowbird-downflap.png').convert_alpha()
bird_mid = pygame.image.load('assets/yellowbird-midflap.png').convert_alpha()
bird_up = pygame.image.load('assets/yellowbird-upflap.png').convert_alpha()
bird_list = [bird_down, bird_mid, bird_up]
bird_index = 0
bird = bird_list[bird_index]
#tao chim
bird = pygame.image.load('assets/yellowbird-midflap.png').convert_alpha()
bird = pygame.transform.scale2x(bird)
bird_rect = bird.get_rect(center = (100, 384))
birdflap = pygame.USEREVENT + 1
pygame.time.set_timer(birdflap, 200)
#pipe_surface = pygame.image.load('assets/pipe-green.png').convert()
#pipe_surface = pygame.transform.scale2x(pipe_surface)
pipe_list = []
#timer
spawnpipe = pygame.USEREVENT
pygame.time.set_timer(spawnpipe, 1200)
pipe_height = [200,300,400]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE and game_active:
bird_movement = 0
bird_movement = -11
if event.key == pygame.K_SPACE and game_active == False:
game_active = True
pipe_list.clear()
bird_rect.center = (100,384)
bird_movement = 0
if event.type == spawnpipe:
pipe_list.extend(create_pipe())
if event.type == birdflap:
if bird_index < 2:
bird_index += 1
else:
bird_index =0
bird , bird_rect = bird_animation()
screen.blit(bg,(0,0))
if game_active:
screen.blit(bg, (0,0))
#chim
bird_movement += gravity
rotated_bird = rotate_bird(bird)
bird_rect.centery += bird_movement
screen.blit(rotated_bird, bird_rect)
game_active = check_collision(pipe_list)
pipe_list = move_pipe(pipe_list)
draw_pipe(pipe_list)
floor_x_pos -=1
draw_floor()
if floor_x_pos <= -432:
floor_x_pos = 0
pygame.display.update()
clock.tick(120)