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("Krunker-like FPS Game") # Colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) # Player properties player_pos = [WIDTH // 2, HEIGHT // 2] player_size = 50 player_speed = 5 player_health = 100 # Bullet properties bullets = [] bullet_speed = 10 # Enemy properties enemies = [] enemy_size = 50 enemy_speed = 2 # Score score = 0 # Font for displaying score font = pygame.font.Font(None, 36) # Game loop control running = True def spawn_enemy(): x_pos = random.randint(0, WIDTH - enemy_size) return [x_pos, 0] def draw_player(): pygame.draw.rect(screen, WHITE, (player_pos[0], player_pos[1], player_size, player_size)) def draw_bullet(bullet): pygame.draw.rect(screen, WHITE, (bullet[0], bullet[1], 5, 10)) def draw_enemy(enemy): pygame.draw.rect(screen, RED, (enemy[0], enemy[1], enemy_size, enemy_size)) # Main game loop while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Movement keys = pygame.key.get_pressed() if keys[pygame.K_a] and player_pos[0] > 0: player_pos[0] -= player_speed if keys[pygame.K_d] and player_pos[0] < WIDTH - player_size: player_pos[0] += player_speed if keys[pygame.K_w] and player_pos[1] > 0: player_pos[1] -= player_speed if keys[pygame.K_s] and player_pos[1] < HEIGHT - player_size: player_pos[1] += player_speed # Shooting with mouse if pygame.mouse.get_pressed()[0]: # Left mouse button 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_size // 2, player_pos[1] + player_size // 2, bullet_dx, bullet_dy]) # Update bullets for bullet in bullets: bullet[0] += bullet[2] bullet[1] += bullet[3] bullets = [bullet for bullet in bullets if 0 < bullet[0] < WIDTH and 0 < bullet[1] < HEIGHT] # Spawn enemies if random.randint(1, 40) == 1: enemies.append(spawn_enemy()) # Update enemy positions for enemy in enemies: if enemy[1] < player_pos[1]: enemy[1] += enemy_speed else: # Basic enemy AI to follow player if enemy[0] < player_pos[0]: enemy[0] += enemy_speed elif enemy[0] > player_pos[0]: enemy[0] -= enemy_speed # Remove off-screen enemies enemies = [enemy for enemy in enemies if enemy[1] < HEIGHT] # Collision detection for bullet in bullets: for enemy in enemies: if (enemy[0] < bullet[0] < enemy[0] + enemy_size) and (enemy[1] < bullet[1] < enemy[1] + enemy_size): bullets.remove(bullet) enemies.remove(enemy) score += 1 break # Check for player health for enemy in enemies: if (enemy[0] < player_pos[0] < enemy[0] + enemy_size) and (enemy[1] < player_pos[1] < enemy[1] + enemy_size): player_health -= 1 if player_health <= 0: running = False # Game over # Drawing screen.fill(BLACK) draw_player() for bullet in bullets: draw_bullet(bullet) for enemy in enemies: draw_enemy(enemy) # Draw score and health score_text = font.render(f'Score: {score}', True, GREEN) health_text = font.render(f'Health: {player_health}', True, RED) screen.blit(score_text, (10, 10)) screen.blit(health_text, (10, 40)) pygame.display.flip() pygame.time.Clock().tick(30) pygame.quit()