Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, sys, random
- def draw_floor():
- screen.blit(floor_surface, (floor_x_pos, 400))
- screen.blit(floor_surface, (floor_x_pos + 288, 400))
- def create_pipe():
- random_pipe_pos = random.choice(pipe_height)
- bottom_pipe = pipe_surface.get_rect(midtop=(350, random_pipe_pos))
- top_pipe = pipe_surface.get_rect(midbottom=(350, random_pipe_pos - 120))
- return bottom_pipe, top_pipe
- def move_pipes(pipes):
- for pipe in pipes:
- pipe.centerx -= 2.5
- return pipes
- def draw_pipes(pipes):
- for pipe in pipes:
- if pipe.bottom >= 512:
- screen.blit(pipe_surface, pipe)
- def check_collision(pipes):
- for pipe in pipes:
- if bird_rect.colliderect(pipe):
- death_sound.play()
- return False
- else:
- # 2 boolean values for flip function: 1st is if x flip and 2nd is if y flip (boolean)
- flip_pipe = pygame.transform.flip(pipe_surface, False, True)
- screen.blit(flip_pipe, pipe)
- if bird_rect.top <= -50 or bird_rect.bottom >= 400:
- return False
- return True
- def rotate_bird(bird):
- new_bird = pygame.transform.rotozoom(bird, -bird_movement * 7, 1)
- return new_bird
- def bird_animation():
- new_bird = bird_frames[bird_index]
- new_bird_rect = new_bird.get_rect(center=(50, 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, (0, 0, 255))
- score_rect = score_surface.get_rect(center=(144, 50))
- screen.blit(score_surface, score_rect)
- if game_state == 'game over':
- score_surface = game_font.render(f'Score: {int(score)}', True, (0, 0, 255))
- score_rect = score_surface.get_rect(center=(144, 50))
- screen.blit(score_surface, score_rect)
- high_score_surface = game_font.render(f'High Score: {int(high_score)}', True, (0, 0, 255))
- high_score_rect = high_score_surface.get_rect(center=(144, 350))
- 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=1024)
- pygame.init()
- screen = pygame.display.set_mode((288, 512)) # half of this (x,y) is 288,512; double is 576,1024
- game_font = pygame.font.Font('freesansbold.ttf', 30)
- # Important game variables
- # we will add gravity to bird movement every frame
- # and this bird movement will move the bird rectangle down
- # if bird rectangle goes down, then we put bird_surface on different position
- # game variables
- gravity = 0.125
- bird_movement = 0
- game_active = True
- score = 0
- high_score = 0
- bg_surface = pygame.image.load('Images/background-day.png').convert()
- # bg_surface = pygame.transform.scale2x(bg_surface) #(if screen is 576,1024)
- floor_surface = pygame.image.load('Images/base.png')
- # floor_surface = pygame.transform.scale2x(floor_surface)
- FPS = pygame.time.Clock()
- floor_x_pos = 0
- # animation
- bird_downflap = pygame.image.load('Images/yellowbird-downflap.png').convert_alpha()
- bird_midflap = pygame.image.load('Images/yellowbird-midflap.png').convert_alpha()
- bird_upflap = pygame.image.load('Images/yellowbird-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=(50, 256))
- # to create a new USEREVENT, add +1,+2 etc
- BIRDFLAP = pygame.USEREVENT + 1
- pygame.time.set_timer(BIRDFLAP, 200)
- # bird_surface = pygame.image.load('Images/yellowbird-midflap.png').convert_alpha()
- # using rect (rectangle) function center = x,y is coordinate of center of rectangle of bird
- # bird_rect = bird_surface.get_rect(center=(50, 256))
- pipe_surface = pygame.image.load('Images/pipe-red.png').convert()
- pipe_list = []
- # how to use timer: 1200 is always in milliseconds. 1 sec = 1000 milliseconds
- SPAWNPIPE = pygame.USEREVENT
- pygame.time.set_timer(SPAWNPIPE, 1200)
- pipe_height = [200, 250, 300, 350]
- game_over_surface = pygame.image.load('Images/message.png').convert_alpha()
- game_over_rect = game_over_surface.get_rect(center=(144, 256))
- # sounds
- flap_sound = pygame.mixer.Sound('Sounds/sfx_wing.wav')
- death_sound = pygame.mixer.Sound('Sounds/sfx_hit.wav')
- score_sound = pygame.mixer.Sound('Sounds/sfx_point.wav')
- score_sound_countdown = 100
- 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 -= 4.1
- flap_sound.play()
- if event.key == pygame.K_SPACE and game_active == False:
- game_active = True
- pipe_list.clear()
- bird_rect.center = (50, 170)
- bird_movement = 0
- score = 0
- # for spawning pipes
- if event.type == SPAWNPIPE:
- # extend function adds to a tuple
- pipe_list.extend(create_pipe())
- # for bird animation
- 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
- bird_movement += gravity
- bird_rect.centery += bird_movement
- rotated_bird = rotate_bird(bird_surface)
- screen.blit(rotated_bird, bird_rect)
- game_active = check_collision(pipe_list)
- # pipe movement
- pipe_list = move_pipes(pipe_list)
- draw_pipes(pipe_list)
- # show score
- score += 0.01
- score_display('main_game')
- #score_sound_countdown -= 1
- #if score_sound_countdown <= 0:
- # score_sound.play()
- # score_sound_countdown = 100
- else:
- screen.blit(game_over_surface, game_over_rect)
- high_score = update_score(score, high_score)
- score_display('game_over')
- # floor
- floor_x_pos -= 1
- draw_floor()
- if floor_x_pos <= -288:
- floor_x_pos = 0
- pygame.display.update()
- FPS.tick(120)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement