design a simple python game of shooting of astrodes by spaceship
- User
design a simple python game of shooting of astrodes by spaceship
Assistant
I'll help you create a simple asteroid shooting game using Pygame. Here's a basic implementation:
import pygame import random import math # Initialize Pygame pygame.init() # Set up the display WIDTH = 800 HEIGHT = 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Asteroid Shooter") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) # Player spaceship class Player: def __init__(self): self.x = WIDTH // 2 self.y = HEIGHT - 50 self.speed = 5 self.size = 30 def draw(self): pygame.draw.polygon(screen, WHITE, [ (self.x, self.y - self.size), (self.x - self.size, self.y + self.size), (self.x + self.size, self.y + self.size) ]) def move(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and self.x > self.size: self.x -= self.speed if keys[pygame.K_RIGHT] and self.x < WIDTH - self.size: self.x += self.speed # Bullet class class Bullet: def __init__(self, x, y): self.x = x self.y = y self.speed = 7 self.size = 5 def draw(self): pygame.draw.circle(screen, WHITE, (self.x, self.y), self.size) def move(self): self.y -= self.speed # Asteroid class class Asteroid: def __init__(self): self.size = random.randint(20, 50) self.x = random.randint(self.size, WIDTH - self.size) self.y = -self.size self.speed = random.randint(2, 5) def draw(self): pygame.draw.circle(screen, WHITE, (self.x, self.y), self.size) def move(self): self.y += self.speed # Game objects player = Player() bullets = [] asteroids = [] score = 0 game_over = False # Game loop clock = pygame.time.Clock() running = True while running: # Event handling for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE and not game_over: bullets.append(Bullet(player.x, player.y - player.size)) if not game_over: # Update game objects player.move() # Update bullets for bullet in bullets[:]: bullet.move() if bullet.y < 0: bullets.remove(bullet) # Create new asteroids if random.random() < 0.02: asteroids.append(Asteroid()) # Update asteroids for asteroid in asteroids[:]: asteroid.move() if asteroid.y > HEIGHT: asteroids.remove(asteroid) # Check collision with player distance = math.sqrt((player.x - asteroid.x)**2 + (player.y - asteroid.y)**2) if distance < asteroid.size + player.size: game_over = True # Check collision with bullets for bullet in bullets[:]: distance = math.sqrt((bullet.x - asteroid.x)**2 + (bullet.y - asteroid.y)**2) if distance < asteroid.size + bullet.size: if asteroid in asteroids: asteroids.remove(asteroid) if bullet in bullets: bullets.remove(bullet) score += 1 # Draw everything screen.fill(BLACK) player.draw() for bullet in bullets: bullet.draw() for asteroid in asteroids: asteroid.draw() # Draw score font = pygame.font.Font(None, 36) score_text = font.render(f"Score: {score}", True, WHITE) screen.blit(score_text, (10, 10)) if game_over: game_over_text = font.render("Game Over!", True, WHITE) screen.blit(game_over_text, (WIDTH//2 - 100, HEIGHT//2)) pygame.display.flip() clock.tick(60) pygame.quit()This game includes:
- A triangular spaceship that moves left and right with arrow keys
- Space bar to shoot bullets
- Randomly generated asteroids falling from the top
- Collision detection between bullets/asteroids and player/asteroids
- Score tracking
- Game over when an asteroid hits the player
To play:
- Make sure you have Pygame installed (
pip install pygame) - Copy and run the code
- Use left/right arrow keys to move
- Press spacebar to shoot
- Avoid asteroids while trying to shoot them for points
Would you like me to explain any part of the code in more detail or suggest ways to enhance the game?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.