Untitled
unknown
plain_text
21 days ago
3.5 kB
4
Indexable
import pygame import random # Initialize Pygame pygame.init() # Set up the display SCREEN_WIDTH = 400 SCREEN_HEIGHT = 600 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Flappy Bird') # Set up colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) RED = (255, 0, 0) # Set up the clock clock = pygame.time.Clock() FPS = 60 # Set up fonts font = pygame.font.SysFont("Arial", 30) # Set up player and pipes player_width = 50 player_height = 50 player_x = 50 player_y = SCREEN_HEIGHT // 2 player_velocity = 0 gravity = 0.5 jump_strength = -10 pipe_width = 60 pipe_gap = 150 pipe_velocity = 4 # Set up game variables pipes = [] score = 0 game_over = False # Function to draw the player def draw_player(y): pygame.draw.rect(screen, BLUE, (player_x, y, player_width, player_height)) # Function to draw the pipes def draw_pipes(): for pipe in pipes: pygame.draw.rect(screen, GREEN, (pipe[0], pipe[1], pipe_width, pipe_gap)) pygame.draw.rect(screen, GREEN, (pipe[0], pipe[1] + pipe_gap + 150, pipe_width, SCREEN_HEIGHT)) # Function to handle pipe movement and generate new pipes def move_pipes(): global score for pipe in pipes: pipe[0] -= pipe_velocity if pipes and pipes[0][0] < -pipe_width: pipes.pop(0) score += 1 # Generate new pipes if len(pipes) == 0 or pipes[-1][0] < SCREEN_WIDTH - 200: height = random.randint(100, SCREEN_HEIGHT - pipe_gap - 100) pipes.append([SCREEN_WIDTH, height]) # Function to display the score def display_score(): score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) # Function to display game over text def display_game_over(): game_over_text = font.render("Game Over", True, RED) screen.blit(game_over_text, (SCREEN_WIDTH // 2 - 100, SCREEN_HEIGHT // 2 - 30)) restart_text = font.render("Press R to Restart", True, WHITE) screen.blit(restart_text, (SCREEN_WIDTH // 2 - 120, SCREEN_HEIGHT // 2 + 20)) # Main game loop while True: screen.fill(BLACK) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not game_over: player_velocity = jump_strength if event.key == pygame.K_r and game_over: # Reset the game player_y = SCREEN_HEIGHT // 2 player_velocity = 0 pipes = [] score = 0 game_over = False # Game mechanics if not game_over: player_velocity += gravity player_y += player_velocity # Move the pipes and check for collisions move_pipes() # Check for collisions with pipes for pipe in pipes: if pipe[0] < player_x + player_width and pipe[0] + pipe_width > player_x: if player_y < pipe[1] or player_y + player_height > pipe[1] + pipe_gap: game_over = True # Check if the player hits the ground or goes out of bounds if player_y + player_height >= SCREEN_HEIGHT or player_y < 0: game_over = True # Draw the player and pipes draw_player(player_y) draw_pipes() display_score() else: display_game_over() pygame.display.update() clock.tick(FPS)
Editor is loading...
Leave a Comment