Untitled
unknown
plain_text
a year ago
2.8 kB
3
Indexable
import pygame import sys import random # Initialize Pygame pygame.init() # Screen dimensions SCREEN_WIDTH, SCREEN_HEIGHT = 400, 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Colors WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = (255, 0, 0) # Game variables bird_pos = [100, SCREEN_HEIGHT // 2] ground_height = 100 gravity = 0.25 bird_movement = 0 game_active = True pipe_list = [] SPAWNPIPE = pygame.USEREVENT pygame.time.set_timer(SPAWNPIPE, 1200) pipe_height = [200, 300, 400] def draw_floor(): pygame.draw.rect(screen, GREEN, (0, SCREEN_HEIGHT - ground_height, SCREEN_WIDTH, ground_height)) def create_pipe(): random_pipe_pos = random.choice(pipe_height) bottom_pipe = pygame.Rect(SCREEN_WIDTH + 100, random_pipe_pos, 50, SCREEN_HEIGHT - random_pipe_pos - ground_height) top_pipe = pygame.Rect(SCREEN_WIDTH + 100, 0, 50, random_pipe_pos - 150) return bottom_pipe, top_pipe def move_pipes(pipes): for pipe in pipes: pipe.centerx -= 5 return pipes def draw_pipes(pipes): for pipe in pipes: if pipe.bottom >= SCREEN_HEIGHT - ground_height: pygame.draw.rect(screen, GREEN, pipe) else: flip_pipe = pygame.transform.flip(pygame.Surface((50, pipe.height)), False, True) screen.blit(flip_pipe, pipe.topleft) def check_collision(pipes): for pipe in pipes: if bird_rect.colliderect(pipe): return False if bird_rect.top <= -50 or bird_rect.bottom >= SCREEN_HEIGHT - ground_height: return False return True # Game loop clock = pygame.time.Clock() bird_rect = pygame.Rect(bird_pos[0], bird_pos[1], 30, 30) 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 -= 10 if event.key == pygame.K_SPACE and not game_active: game_active = True pipe_list.clear() bird_rect.center = (100, SCREEN_HEIGHT // 2) bird_movement = 0 if event.type == SPAWNPIPE: pipe_list.extend(create_pipe()) screen.fill(WHITE) if game_active: # Bird bird_movement += gravity bird_rect.centery += bird_movement pygame.draw.rect(screen, RED, bird_rect) game_active = check_collision(pipe_list) # Pipes pipe_list = move_pipes(pipe_list) draw_pipes(pipe_list) else: pipe_list.clear() bird_rect.center = (100, SCREEN_HEIGHT // 2) bird_movement = 0 # Floor draw_floor() pygame.display.update() clock.tick(120)
Editor is loading...
Leave a Comment