Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.9 kB
2
Indexable
Never
```python
import pygame
import random
import time

# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# Initialize pygame
pygame.init()

# Set up the screen
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Speedy Shapes Showdown")

# Define classes
class Shape(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.y = -height

class Outline(pygame.sprite.Sprite):
    def __init__(self, color, width, height, x_position):
        super().__init__()
        self.image = pygame.Surface([width, height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.x = x_position
        self.rect.y = SCREEN_HEIGHT - height

# Create sprite groups
shape_group = pygame.sprite.Group()
outline_group = pygame.sprite.Group()

# Create outlines
outline_width = 50
outline_height = 50
outline_gap = 100
for i in range(SCREEN_WIDTH // outline_gap):
    outline = Outline(BLACK, outline_width, outline_height, i * outline_gap)
    outline_group.add(outline)

# Set up game variables
shape_width = 50
shape_height = 50
shape_speed = 2
score = 0
clock = pygame.time.Clock()

# Main game loop
running = True
start_time = time.time()
while running:
    screen.fill(WHITE)

    # Create new shape
    if random.randint(0, 100) < 2:
        color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        shape = Shape(color, shape_width, shape_height)
        shape.rect.x = random.randint(0, SCREEN_WIDTH - shape_width)
        shape_group.add(shape)

    # Move shapes and check collisions
    for shape in shape_group:
        shape.rect.y += shape_speed
        if shape.rect.y > SCREEN_HEIGHT:
            shape_group.remove(shape)
            score -= 1

    # Check for collisions
    collisions = pygame.sprite.groupcollide(shape_group, outline_group, False, False)
    for shape in collisions:
        if collisions[shape]:
            shape_group.remove(shape)
            score += 1

    # Draw sprites
    shape_group.draw(screen)
    outline_group.draw(screen)

    # Display score
    font = pygame.font.Font(None, 36)
    score_text = font.render("Score: " + str(score), True, BLACK)
    screen.blit(score_text, (10, 10))

    # Check for game over
    if score < 0:
        running = False

    # Check for quit event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Update display
    pygame.display.flip()
    clock.tick(60)

# Game over screen
screen.fill(RED)
game_over_font = pygame.font.Font(None, 72)
game_over_text = game_over_font.render("Game Over", True, BLACK)
screen.blit(game_over_text, (SCREEN_WIDTH // 2 - game_over_text.get_width() // 2, SCREEN_HEIGHT // 2 - game_over_text.get_height() // 2))
pygame.display.flip()

# Display final score
final_score_font = pygame.font.Font(None, 36)
final_score_text = final_score_font.render("Final Score: " + str(score), True, BLACK)
screen.blit(final_score_text, (SCREEN_WIDTH // 2 - final_score_text.get_width() // 2, SCREEN_HEIGHT // 2 + 50))
pygame.display.flip()

# Wait for a few seconds before quitting
time.sleep(5)
pygame.quit()
```

This Python code creates a simple game using the Pygame library. The game is called "Speedy Shapes Showdown" where the player must match falling shapes with their corresponding outlines before they reach the bottom of the screen. The player earns points for each successful match and loses points if a shape reaches the bottom without being matched. The game ends if the player's score goes below zero.
Leave a Comment