import pygame import random import math # Initialize Pygame pygame.init() # Screen dimensions WIDTH, HEIGHT = 800, 600 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Infinite Enemies Game") # Colors WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # Player settings player_pos = [WIDTH // 2, HEIGHT // 2] player_radius = 20 player_speed = 5 player_health = 100 # Enemy settings enemy_radius = 15 enemy_speed = 2 enemies = [] # Bullet settings bullet_radius = 5 bullet_speed = 10 bullets = [] # Game clock clock = pygame.time.Clock() def spawn_enemy(): x = random.randint(0, WIDTH) y = random.randint(0, HEIGHT) enemies.append([x, y]) def move_enemies(): for enemy in enemies: angle = math.atan2(player_pos[1] - enemy[1], player_pos[0] - enemy[0]) enemy[0] += enemy_speed * math.cos(angle) enemy[1] += enemy_speed * math.sin(angle) def shoot_bullet(): mouse_x, mouse_y = pygame.mouse.get_pos() angle = math.atan2(mouse_y - player_pos[1], mouse_x - player_pos[0]) bullet_dx = bullet_speed * math.cos(angle) bullet_dy = bullet_speed * math.sin(angle) bullets.append([player_pos[0], player_pos[1], bullet_dx, bullet_dy]) def move_bullets(): for bullet in bullets: bullet[0] += bullet[2] bullet[1] += bullet[3] # Remove bullets that go off screen bullets[:] = [bullet for bullet in bullets if 0 < bullet[0] < WIDTH and 0 < bullet[1] < HEIGHT] def check_collisions(): global player_health for bullet in bullets: for enemy in enemies: if math.hypot(bullet[0] - enemy[0], bullet[1] - enemy[1]) < bullet_radius + enemy_radius: bullets.remove(bullet) enemies.remove(enemy) break for enemy in enemies: if math.hypot(player_pos[0] - enemy[0], player_pos[1] - enemy[1]) < player_radius + enemy_radius: enemies.remove(enemy) player_health -= 10 # Decrease health on collision if player_health <= 0: print("Game Over!") pygame.quit() exit() def draw(): screen.fill(WHITE) pygame.draw.circle(screen, BLUE, (int(player_pos[0]), int(player_pos[1])), player_radius) for enemy in enemies: pygame.draw.circle(screen, RED, (int(enemy[0]), int(enemy[1])), enemy_radius) for bullet in bullets: pygame.draw.circle(screen, GREEN, (int(bullet[0]), int(bullet[1])), bullet_radius) # Draw health bar health_bar_length = 200 health_bar_height = 20 pygame.draw.rect(screen, RED, (10, 10, health_bar_length, health_bar_height)) pygame.draw.rect(screen, GREEN, (10, 10, health_bar_length * (player_health / 100), health_bar_height)) pygame.display.flip() def main(): running = True spawn_timer = 0 while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: shoot_bullet() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_pos[0] > player_radius: player_pos[0] -= player_speed if keys[pygame.K_RIGHT] and player_pos[0] < WIDTH - player_radius: player_pos[0] += player_speed if keys[pygame.K_UP] and player_pos[1] > player_radius: player_pos[1] -= player_speed if keys[pygame.K_DOWN] and player_pos[1] < HEIGHT - player_radius: player_pos[1] += player_speed spawn_timer += 1 if spawn_timer > 5: spawn_enemy() spawn_timer = 0 move_enemies() move_bullets() check_collisions() draw() clock.tick(60) pygame.quit() if __name__ == "__main__": main()