Make a simple Python game with a GUI and actual visual (sprite) elements.
- User
Make a simple Python game with a GUI and actual visual (sprite) elements.
Assistant
Of course! Here is a simple but complete game using the popular
Pygamelibrary. The game is a classic "dodge the falling objects" style game.Game: "Dodger"
Objective: Move the player (a blue square) left and right to avoid the falling red enemies. Your score is how long you survive.
Prerequisites
You need to install the
pygamelibrary first. Open your terminal or command prompt and run:pip install pygameThe Python Script
Copy and paste the code below into a file named
dodger_game.pyand run it. The script is self-contained and creates its own visual elements, so you don't need any external image files.import pygame import random import sys # --- Constants --- SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 PLAYER_SIZE = 50 ENEMY_SIZE = 50 PLAYER_SPEED = 10 ENEMY_SPEED_MIN = 3 ENEMY_SPEED_MAX = 8 # --- Colors --- WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) # --- Player Class --- class Player(pygame.sprite.Sprite): """Represents the player sprite.""" def __init__(self): super().__init__() # Create a blue square for the player self.image = pygame.Surface([PLAYER_SIZE, PLAYER_SIZE]) self.image.fill(BLUE) self.rect = self.image.get_rect() # Position the player at the bottom center self.rect.x = (SCREEN_WIDTH - PLAYER_SIZE) // 2 self.rect.y = SCREEN_HEIGHT - PLAYER_SIZE - 10 def update(self): """Update the player's position based on key presses.""" keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and self.rect.left > 0: self.rect.x -= PLAYER_SPEED if keys[pygame.K_RIGHT] and self.rect.right < SCREEN_WIDTH: self.rect.x += PLAYER_SPEED # --- Enemy Class --- class Enemy(pygame.sprite.Sprite): """Represents a falling enemy sprite.""" def __init__(self): super().__init__() # Create a red square for the enemy self.image = pygame.Surface([ENEMY_SIZE, ENEMY_SIZE]) self.image.fill(RED) self.rect = self.image.get_rect() # Set a random starting position at the top self.rect.x = random.randint(0, SCREEN_WIDTH - ENEMY_SIZE) self.rect.y = random.randint(-100, -ENEMY_SIZE) # Set a random falling speed self.speed = random.randint(ENEMY_SPEED_MIN, ENEMY_SPEED_MAX) def update(self): """Move the enemy down the screen.""" self.rect.y += self.speed # If the enemy is off-screen, reset it to the top if self.rect.top > SCREEN_HEIGHT: self.rect.x = random.randint(0, SCREEN_WIDTH - ENEMY_SIZE) self.rect.y = random.randint(-100, -ENEMY_SIZE) self.speed = random.randint(ENEMY_SPEED_MIN, ENEMY_SPEED_MAX) # --- Helper function for drawing text --- def draw_text(surface, text, size, x, y, color): """Draws text on the screen.""" font = pygame.font.Font(None, size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() text_rect.midtop = (x, y) surface.blit(text_surface, text_rect) # --- Main Game Function --- def main(): """Main game logic.""" pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Dodger") clock = pygame.time.Clock() # Create sprite groups all_sprites = pygame.sprite.Group() enemies = pygame.sprite.Group() # Create player and add to sprite group player = Player() all_sprites.add(player) # Create enemies and add them to groups for _ in range(8): enemy = Enemy() all_sprites.add(enemy) enemies.add(enemy) start_time = pygame.time.get_ticks() game_over = False # --- Game Loop --- running = True while running: # Keep loop running at the right speed clock.tick(60) # Process input (events) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN and game_over: # Restart the game on key press after game over main() if not game_over: # --- Update --- all_sprites.update() # Check for collisions between player and enemies if pygame.sprite.spritecollide(player, enemies, False): game_over = True # Calculate score (time survived) score = (pygame.time.get_ticks() - start_time) // 1000 # --- Draw / Render --- screen.fill(BLACK) all_sprites.draw(screen) draw_text(screen, f"Score: {score}", 30, SCREEN_WIDTH / 2, 10, WHITE) else: # --- Game Over Screen --- draw_text(screen, "GAME OVER", 64, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4, WHITE) draw_text(screen, f"Final Score: {score}", 40, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, WHITE) draw_text(screen, "Press any key to play again", 22, SCREEN_WIDTH / 2, SCREEN_HEIGHT * 3 / 4, WHITE) # *After* drawing everything, flip the display pygame.display.flip() pygame.quit() sys.exit() if __name__ == "__main__": main()How to Run and Play
- Make sure you have
pygameinstalled. - Save the code as
dodger_game.py. - Run it from your terminal:
python dodger_game.py - A window will appear. Use the Left and Right arrow keys to move your blue square.
- Dodge the falling red squares for as long as you can.
- When you get hit, a "GAME OVER" screen will appear. Press any key to start over.
- Make sure you have
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.