Takkapi / wave-in-every-language

0 stars 0 forks source link

Python #1

Open Takkapi opened 4 weeks ago

Takkapi commented 4 weeks ago

Recreate Wave in Python using pyGame library

Project criteria:

  1. Basic player movement
  2. Enemy (Basic and Fast) spawn in movement
  3. Trails
  4. Collisions
  5. Particles (Optional)
  6. Waves (max 10)
  7. Game Over screen
  8. Cross-platform
Naveed-baig commented 4 weeks ago

`import pygame import random import math import sys

Initialize Pygame

pygame.init()

Constants

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 Setup

screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Wave Game")

Clock for FPS

clock = pygame.time.Clock()

Fonts

font = pygame.font.SysFont("Arial", 30)

Player class

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)

Enemy class

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)

Wave Manager

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()

Check collision between player and enemies

def check_collisions(player, enemies): for enemy in enemies: if player.rect.colliderect(enemy.rect): return True return False

Game over screen

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

Main Game Loop

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()

Run the game

if name == "main": main() `