From ffc91b104fe8c4c174ff10d7e4c8dfebca74dff1 Mon Sep 17 00:00:00 2001 From: Kritika Singh Date: Sun, 3 Nov 2024 12:27:32 +0530 Subject: [PATCH 1/2] Implementation of Castle Defense Game --- Game_Development/Castle Defense/README.md | 32 +++++ Game_Development/Castle Defense/game.py | 146 ++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 Game_Development/Castle Defense/README.md create mode 100644 Game_Development/Castle Defense/game.py diff --git a/Game_Development/Castle Defense/README.md b/Game_Development/Castle Defense/README.md new file mode 100644 index 0000000000..ac5fcf6fc0 --- /dev/null +++ b/Game_Development/Castle Defense/README.md @@ -0,0 +1,32 @@ +# Castle Defense Game + +## Description + +Castle Defense is a tower defense game where players must protect their castle from waves of enemies. Players can strategically place traps to eliminate approaching foes and prevent damage to the castle. Earn points by defeating enemies to enhance your defenses and secure your kingdom! + +## Features + +- **Trap Placement**: Click anywhere on the screen to place traps, which help eliminate enemies. +- **Points System**: Earn points by defeating enemies to buy more traps or upgrade defenses. +- **Various Enemy Types**: Encounter different types of enemies with unique weaknesses. +- **Castle Health**: Monitor the health of your castle as enemies attempt to breach its defenses. + +## Gameplay Instructions + +1. **Starting the Game**: Run the game using Python and Pygame. +2. **Placing Traps**: + - Click anywhere on the screen to place a trap. Each trap costs 5 points. + - Earn points by defeating enemies that collide with your traps. +3. **Defeating Enemies**: + - Enemies will spawn at the top of the screen and move towards the castle. + - Use traps to eliminate them before they reach the castle. +4. **Castle Health**: + - The castle has a health of 100. If an enemy reaches the castle, its health decreases. + - If health reaches zero, the game is over. + +## Requirements + +- Python 3.x +- Pygame library + + diff --git a/Game_Development/Castle Defense/game.py b/Game_Development/Castle Defense/game.py new file mode 100644 index 0000000000..afd7de77fa --- /dev/null +++ b/Game_Development/Castle Defense/game.py @@ -0,0 +1,146 @@ +import pygame +import random +import sys + +# Initialize Pygame +pygame.init() + +# Game constants +WIDTH, HEIGHT = 800, 600 +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +RED = (255, 0, 0) +BLUE = (0, 0, 255) +CASTLE_COLOR = (139, 69, 19) # Dark brown for the castle walls +TOWER_COLOR = (105, 105, 105) # Gray for the towers +DOOR_COLOR = (139, 69, 19) # Brown for the castle door + +# Screen setup +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +pygame.display.set_caption("Castle Defense") + +# Player stats +player_points = 10 +castle_health = 100 + +# Trap and upgrade setup +traps = [] +trap_cost = 5 +upgrade_cost = 10 + +# Enemy setup +enemy_spawn_rate = 1000 # milliseconds +last_enemy_spawn_time = pygame.time.get_ticks() +enemies = [] +enemy_speed = 2 + +# Fonts +font = pygame.font.Font(None, 36) + +# Define castle_rect globally +castle_rect = pygame.Rect(WIDTH // 2 - 100, HEIGHT - 150, 200, 100) + +# Game functions +def spawn_enemy(): + x = random.randint(0, WIDTH) + y = -50 + enemy_rect = pygame.Rect(x, y, 40, 40) + enemies.append(enemy_rect) + +def draw_text(text, font, color, x, y): + text_surface = font.render(text, True, color) + screen.blit(text_surface, (x, y)) + +def check_traps(): + global player_points + for trap in traps: + for enemy in enemies: + if trap.colliderect(enemy): + enemies.remove(enemy) + player_points += 5 # Earn points for defeating an enemy + +def draw_castle(): + # Use the global castle_rect + global castle_rect + # Castle main wall + pygame.draw.rect(screen, CASTLE_COLOR, castle_rect) + + # Castle door + door_rect = pygame.Rect(WIDTH // 2 - 25, HEIGHT - 100, 50, 50) + pygame.draw.rect(screen, DOOR_COLOR, door_rect) + + # Left tower + left_tower = pygame.Rect(WIDTH // 2 - 130, HEIGHT - 180, 60, 130) + pygame.draw.rect(screen, TOWER_COLOR, left_tower) + pygame.draw.rect(screen, BLACK, (WIDTH // 2 - 130, HEIGHT - 180, 60, 30)) # Tower top + + # Right tower + right_tower = pygame.Rect(WIDTH // 2 + 70, HEIGHT - 180, 60, 130) + pygame.draw.rect(screen, TOWER_COLOR, right_tower) + pygame.draw.rect(screen, BLACK, (WIDTH // 2 + 70, HEIGHT - 180, 60, 30)) # Tower top + + # Add windows to the castle + pygame.draw.rect(screen, BLACK, (WIDTH // 2 - 75, HEIGHT - 120, 20, 20)) + pygame.draw.rect(screen, BLACK, (WIDTH // 2 + 55, HEIGHT - 120, 20, 20)) + +# Game loop +clock = pygame.time.Clock() +running = True + +while running: + screen.fill(WHITE) + + # Event handling + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + elif event.type == pygame.MOUSEBUTTONDOWN: + # Place trap on mouse click if player has enough points + if player_points >= trap_cost: + x, y = pygame.mouse.get_pos() + trap_rect = pygame.Rect(x, y, 20, 20) + traps.append(trap_rect) + player_points -= trap_cost + print(f"Trap placed at ({x}, {y}). Remaining points: {player_points}") + else: + print("Not enough points to place a trap.") + + # Enemy spawning + current_time = pygame.time.get_ticks() + if current_time - last_enemy_spawn_time > enemy_spawn_rate: + spawn_enemy() + last_enemy_spawn_time = current_time + + # Update enemies + for enemy in enemies: + enemy.y += enemy_speed + if enemy.colliderect(castle_rect): # Check collision with castle_rect + castle_health -= 10 # Reduce castle health if an enemy reaches it + enemies.remove(enemy) + + # Check traps + check_traps() + + # Draw game objects + draw_castle() # Draw the new castle + for enemy in enemies: + pygame.draw.rect(screen, RED, enemy) + for trap in traps: + pygame.draw.rect(screen, BLUE, trap) + + # Draw HUD + draw_text(f"Points: {player_points}", font, BLACK, 10, 10) + draw_text(f"Castle Health: {castle_health}", font, BLACK, 10, 50) + + # Check game over condition + if castle_health <= 0: + draw_text("Game Over", font, BLACK, WIDTH // 2 - 50, HEIGHT // 2) + pygame.display.flip() + pygame.time.delay(2000) + running = False + + pygame.display.flip() + clock.tick(60) + +pygame.quit() +sys.exit() From 50d32999d8e5aba538df500c5aa1f0a2fd2a0ae7 Mon Sep 17 00:00:00 2001 From: Kritika75 Date: Sun, 3 Nov 2024 06:58:25 +0000 Subject: [PATCH 2/2] updating Project-Structure.md --- Project-Structure.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project-Structure.md b/Project-Structure.md index 1d21bf4b54..199c7dda04 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -549,6 +549,8 @@ * [Main](Game_Development/Bubble%20Shoot/main.py) * Card Quest * [Cards](Game_Development/Card%20quest/Cards.py) + * Castle Defense + * [Game](Game_Development/Castle%20Defense/game.py) * Catch The Fruit * [Catch The Fruit](Game_Development/Catch_The_Fruit/Catch_the_fruit.py) * Crossword Game