Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.9 kB
1
Indexable
import pygame
import sys

# Initialize Pygame
pygame.init()

# Set up some constants
WIDTH = 400
HEIGHT = 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

# Set up the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# Set up the player and pipes
player = pygame.Rect(WIDTH / 2, HEIGHT / 2, 50, 50)
pipes = []
pipe_width = 50

# Set up the score
score = 0

# Set up the game loop
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:
                player.y -= 50

    # Move the player
    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE]:
        player.y -= 5

    # Move the pipes
    for pipe in pipes:
        pipe.x -= 2
        if pipe.x < -pipe_width:
            pipes.remove(pipe)
    new_pipe = pygame.Rect(WIDTH, HEIGHT / 2 + pipe_width / 2, pipe_width, HEIGHT / 2)
    pipes.append(new_pipe)

    # Check for collisions
    for pipe in pipes:
        if player.colliderect(pipe):
            print("Game Over")
            pygame.quit()
            sys.exit()

    # Draw everything
    screen.fill(WHITE)
    pygame.draw.rect(screen, RED, player)
    for pipe in pipes:
        pygame.draw.rect(screen, GREEN, pipe)
        if pipe.x < WIDTH - pipe_width:
            pygame.draw.rect(screen, GREEN, (pipe.x + pipe_width, pipe.y, pipe_width, HEIGHT / 2))
    pygame.display.flip()

    # Check for score
    for pipe in pipes:
        if pipe.y < player.y < pipe.y + pipe_height / 2:
            score += 1

    # Draw the score
    font = pygame.font.Font(None, 36)
    text = font.render("Score: " + str(score), True, RED)
    screen.blit(text, (10, 10))

    # Cap the frame rate
    pygame.time.Clock().tick(60)
Leave a Comment