Open Takkapi opened 4 weeks ago
`import pygame import random import math import sys
pygame.init()
WIDTH, HEIGHT = 800, 600 FPS = 60 WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) BLUE = (0, 0, 255) PLAYER_SIZE = 20 ENEMY_SIZE = 30 MAX_WAVES = 10
screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Wave Game")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 30)
class Player: def init(self): self.rect = pygame.Rect(WIDTH // 2, HEIGHT - 60, PLAYER_SIZE, PLAYER_SIZE) self.speed = 5 self.trail = []
def move(self, keys):
if keys[pygame.K_LEFT] and self.rect.left > 0:
self.rect.x -= self.speed
if keys[pygame.K_RIGHT] and self.rect.right < WIDTH:
self.rect.x += self.speed
def draw(self):
pygame.draw.rect(screen, BLUE, self.rect)
# Draw trails
for pos in self.trail:
pygame.draw.circle(screen, BLUE, pos, 3)
def update_trail(self):
self.trail.append((self.rect.centerx, self.rect.bottom))
if len(self.trail) > 15: # Limit trail length
self.trail.pop(0)
class Enemy: def init(self, x, y, speed): self.rect = pygame.Rect(x, y, ENEMY_SIZE, ENEMY_SIZE) self.speed = speed
def move(self):
self.rect.y += self.speed
def draw(self):
pygame.draw.rect(screen, RED, self.rect)
class WaveManager: def init(self): self.wave = 1 self.enemies = []
def spawn_enemies(self):
for _ in range(self.wave * 2): # Increase number of enemies with each wave
x = random.randint(0, WIDTH - ENEMY_SIZE)
y = random.randint(-500, -50)
speed = random.randint(3, 6) + self.wave // 2 # Faster enemies as wave increases
self.enemies.append(Enemy(x, y, speed))
def update(self):
for enemy in self.enemies:
enemy.move()
if enemy.rect.top > HEIGHT: # Remove enemies that move off-screen
self.enemies.remove(enemy)
def draw(self):
for enemy in self.enemies:
enemy.draw()
def next_wave(self):
self.wave += 1
self.enemies.clear()
self.spawn_enemies()
def check_collisions(player, enemies): for enemy in enemies: if player.rect.colliderect(enemy.rect): return True return False
def game_over_screen(): screen.fill(BLACK) text = font.render("Game Over! Press R to Restart", True, WHITE) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2)) pygame.display.flip()
# Wait for restart
waiting = True
while waiting:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
waiting = False
def main(): player = Player() wave_manager = WaveManager() wave_manager.spawn_enemies() running = True
while running:
clock.tick(FPS)
screen.fill(BLACK)
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Player movement
keys = pygame.key.get_pressed()
player.move(keys)
player.update_trail()
# Update wave manager
wave_manager.update()
# Draw everything
player.draw()
wave_manager.draw()
# Check for collisions
if check_collisions(player, wave_manager.enemies):
game_over_screen()
main() # Restart the game
# Check for next wave
if not wave_manager.enemies:
if wave_manager.wave < MAX_WAVES:
wave_manager.next_wave()
else:
game_over_screen()
main() # Restart the game
# Update the display
pygame.display.flip()
if name == "main": main() `
Recreate Wave in Python using pyGame library
Project criteria: