125 lines
3.3 KiB
Plaintext
125 lines
3.3 KiB
Plaintext
import pygame, sys, random
|
|
|
|
def draw_floor():
|
|
screen.blit(floor_surface,(floor_x_pos,900))
|
|
screen.blit(floor_surface,(floor_x_pos + 576,900))
|
|
|
|
def check_collision():
|
|
for pipe in pipe_list:
|
|
if bird_rect.colliderect(pipe):
|
|
return False
|
|
|
|
if bird_rect.top <= -100 or bird_rect.bottom >= 900:
|
|
return False
|
|
|
|
return True
|
|
|
|
def rotate_bird(bird):
|
|
new_bird = pygame.transform.rotozoom(bird,-bird_movement * 3,1)
|
|
return new_bird
|
|
|
|
def bird_animation():
|
|
new_bird = bird_frames[bird_index]
|
|
new_bird_rect = new_bird.get_rect(center = (100,bird_rect.centery))
|
|
return new_bird,new_bird_rect
|
|
|
|
def score_display(game_state):
|
|
if game_state == 'main_game':
|
|
score_surface = game_font.render(str(int(score)),True,(255,255,255))
|
|
score_rect = score_surface.get_rect(center = (288,100))
|
|
screen.blit(score_surface,score_rect)
|
|
if game_state == 'game_over':
|
|
score_surface = game_font.render(f'Score: {int(score)}' ,True,(255,255,255))
|
|
score_rect = score_surface.get_rect(center = (288,100))
|
|
screen.blit(score_surface,score_rect)
|
|
|
|
high_score_surface = game_font.render(f'High score: {int(high_score)}',True,(255,255,255))
|
|
high_score_rect = high_score_surface.get_rect(center = (288,850))
|
|
screen.blit(high_score_surface,high_score_rect)
|
|
|
|
def update_score(score, high_score):
|
|
if score > high_score:
|
|
high_score = score
|
|
return high_score
|
|
|
|
pygame.mixer.pre_init(frequency = 44100, size = 16, channels = 1, buffer = 512)
|
|
pygame.init()
|
|
screen = pygame.display.set_mode((576,1024))
|
|
clock = pygame.time.Clock()
|
|
game_font = pygame.font.Font('04B_19.TTF',40)
|
|
|
|
gravity = 0.25
|
|
bird_movement = 0
|
|
game_active = True
|
|
score = 0
|
|
high_score = 0
|
|
|
|
bg_surface = pygame.image.load('background-day.png').convert()
|
|
bg_surface = pygame.transform.scale2x(bg_surface)
|
|
|
|
floor_surface = pygame.image.load('base.png').convert()
|
|
floor_surface = pygame.transform.scale2x(floor_surface)
|
|
floor_x_pos = 0
|
|
|
|
bird_downflap = pygame.transform.scale2x(pygame.image.load( 'bluebird-downflap.png').convert_alpha())
|
|
bird_midflap = pygame.transform.scale2x(pygame.image.load( 'bluebird-midflap.png').convert_alpha())
|
|
bird_upflap = pygame.transform.scale2x(pygame.image.load( 'bluebird-upflap.png').convert_alpha())
|
|
bird_frames = [bird_downflap,bird_midflap,bird_upflap]
|
|
bird_index = 0
|
|
bird_surface = bird_frames[bird_index]
|
|
bird_rect = bird_surface.get_rect(center = (100,512))
|
|
|
|
BIRDFLAP = pygame.USEREVENT + 1
|
|
pygame.time.set_timer(BIRDFLAP,200)
|
|
|
|
|
|
pipe_list = []
|
|
|
|
|
|
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 -= 8
|
|
if event.key == pygame.K_SPACE and game_active == False:
|
|
game_active = True
|
|
bird_rect.center = (100,512)
|
|
bird_movement = 0
|
|
score = 0
|
|
|
|
|
|
|
|
if event.type == BIRDFLAP:
|
|
if bird_index < 2:
|
|
bird_index += 1
|
|
else:
|
|
bird_index = 0
|
|
|
|
bird_surface,bird_rect = bird_animation()
|
|
|
|
screen.blit(bg_surface,(0,0))
|
|
|
|
if game_active:
|
|
|
|
bird_movement += gravity
|
|
rotated_bird = rotate_bird(bird_surface)
|
|
bird_rect.centery += bird_movement
|
|
screen.blit(rotated_bird,bird_rect)
|
|
game_active = check_collision()
|
|
|
|
score += 0.01
|
|
score_display('main_game')
|
|
else:
|
|
score_display('game_over')
|
|
|
|
floor_x_pos -= 1
|
|
draw_floor()
|
|
if floor_x_pos <= -576:
|
|
floor_x_pos = 0
|
|
|
|
pygame.display.update()
|
|
clock.tick(120) |