import pygame import sys import math import random # Initialize Pygame pygame.init() # Screen dimensions SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FPS = 60 # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Player properties PLAYER_SIZE = 50 PLAYER_COLOR = GREEN PLAYER_SPEED = 5 PLAYER_HEALTH = 100 # Bullet properties BULLET_RADIUS = 5 BULLET_COLOR = RED BULLET_SPEED = 10 # Enemy properties ENEMY_SIZE = 40 ENEMY_COLOR = BLUE ENEMY_SPEED = 2 INITIAL_ENEMY_DAMAGE = 10 ENEMY_DAMAGE_INCREASE = 0.1 # Damage increase per second ENEMY_RESPAWN_TIME = 5000 # Time in milliseconds # Wall properties WALL_COLOR = BLACK WALL_THICKNESS = 20 # Create the screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("2D Shooter with Enemies and Walls") # Clock object to control the frame rate clock = pygame.time.Clock() # Define the player object player = pygame.Rect(SCREEN_WIDTH // 2 - PLAYER_SIZE // 2, SCREEN_HEIGHT // 2 - PLAYER_SIZE // 2, PLAYER_SIZE, PLAYER_SIZE) player_health = PLAYER_HEALTH # Define multiple walls walls = [ pygame.Rect(100, 100, 600, WALL_THICKNESS), # Top wall pygame.Rect(100, 100, WALL_THICKNESS, 400), # Left wall pygame.Rect(700, 100, WALL_THICKNESS, 400), # Right wall pygame.Rect(100, 500, 600, WALL_THICKNESS) # Bottom wall ] # Define multiple enemies enemies = [] def create_enemy(): x = random.randint(100, SCREEN_WIDTH - ENEMY_SIZE - 100) y = random.randint(100, SCREEN_HEIGHT - ENEMY_SIZE - 100) return pygame.Rect(x, y, ENEMY_SIZE, ENEMY_SIZE) for _ in range(3): # Initial number of enemies enemies.append(create_enemy()) # List to store bullets bullets = [] # Timing variables last_enemy_respawn = pygame.time.get_ticks() damage_start_time = pygame.time.get_ticks() def handle_movement(): keys = pygame.key.get_pressed() dx, dy = 0, 0 if keys[pygame.K_LEFT]: dx -= PLAYER_SPEED if keys[pygame.K_RIGHT]: dx += PLAYER_SPEED if keys[pygame.K_UP]: dy -= PLAYER_SPEED if keys[pygame.K_DOWN]: dy += PLAYER_SPEED new_player_rect = player.move(dx, dy) # Check collision with walls if not any(new_player_rect.colliderect(wall) for wall in walls): player.x += dx player.y += dy def shoot_bullet(): global player_angle mouse_x, mouse_y = pygame.mouse.get_pos() player_center_x, player_center_y = player.center angle = math.atan2(mouse_y - player_center_y, mouse_x - player_center_x) bullet_dx = BULLET_SPEED * math.cos(angle) bullet_dy = BULLET_SPEED * math.sin(angle) bullet = {'pos': [player_center_x, player_center_y], 'dx': bullet_dx, 'dy': bullet_dy} bullets.append(bullet) def update_bullets(): global player_health for bullet in bullets[:]: bullet['pos'][0] += bullet['dx'] bullet['pos'][1] += bullet['dy'] bullet_rect = pygame.Rect(bullet['pos'][0] - BULLET_RADIUS, bullet['pos'][1] - BULLET_RADIUS, BULLET_RADIUS * 2, BULLET_RADIUS * 2) # Remove bullets that are off screen or hit walls if (bullet_rect.left < 0 or bullet_rect.right > SCREEN_WIDTH or bullet_rect.top < 0 or bullet_rect.bottom > SCREEN_HEIGHT): bullets.remove(bullet) else: if any(bullet_rect.colliderect(wall) for wall in walls): bullets.remove(bullet) # Check collision with enemies for enemy in enemies[:]: if bullet_rect.colliderect(enemy): enemies.remove(enemy) bullets.remove(bullet) break def update_enemies(): global player_health for enemy in enemies[:]: # Move enemies towards player dx = player.centerx - enemy.centerx dy = player.centery - enemy.centery distance = math.hypot(dx, dy) if distance != 0: dx, dy = dx / distance * ENEMY_SPEED, dy / distance * ENEMY_SPEED else: dx, dy = 0, 0 enemy.x += dx enemy.y += dy # Check collision with walls new_enemy_rect = enemy.move(dx, dy) if any(new_enemy_rect.colliderect(wall) for wall in walls): enemy.x -= dx enemy.y -= dy # Check collision with player if enemy.colliderect(player): global player_health player_health -= ENEMY_DAMAGE enemies.remove(enemy) if player_health <= 0: print("Game Over!") pygame.quit() sys.exit() def respawn_enemies(): global last_enemy_respawn current_time = pygame.time.get_ticks() if current_time - last_enemy_respawn > ENEMY_RESPAWN_TIME: if len(enemies) < 3: # Ensure there are always 3 enemies enemies.append(create_enemy()) last_enemy_respawn = current_time def increase_enemy_damage(): global ENEMY_DAMAGE current_time = pygame.time.get_ticks() elapsed_time = (current_time - damage_start_time) / 1000 # Convert to seconds ENEMY_DAMAGE = INITIAL_ENEMY_DAMAGE + ENEMY_DAMAGE_INCREASE * elapsed_time def draw(): screen.fill(WHITE) pygame.draw.rect(screen, PLAYER_COLOR, player) # Draw walls for wall in walls: pygame.draw.rect(screen, WALL_COLOR, wall) # Draw enemies for enemy in enemies: pygame.draw.rect(screen, ENEMY_COLOR, enemy) # Draw bullets for bullet in bullets: pygame.draw.circle(screen, BULLET_COLOR, (int(bullet['pos'][0]), int(bullet['pos'][1])), BULLET_RADIUS) # Draw player health font = pygame.font.SysFont(None, 36) health_text = font.render(f'Health: {player_health}', True, BLACK) screen.blit(health_text, (10, 10)) pygame.display.flip() # Main game loop while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: # Left mouse button shoot_bullet() handle_movement() update_bullets() update_enemies() respawn_enemies() increase_enemy_damage() draw() clock.tick(FPS)