import pygame import sys import random import math # Initialize Pygame pygame.init() # Constants SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 PLAYER_SIZE = 20 ENEMY_SIZE = 20 FOOD_SIZE = 10 PLAYER_COLOR = (0, 255, 0) ENEMY_COLOR = (255, 0, 0) FOOD_COLOR = (255, 255, 0) # Create the screen screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Agar.io AI") # Player class class Player: def __init__(self, x, y): self.x = x self.y = y self.size = PLAYER_SIZE def draw(self): pygame.draw.circle(screen, PLAYER_COLOR, (self.x, self.y), self.size) # Enemy class class Enemy: def __init__(self, x, y): self.x = x self.y = y self.size = ENEMY_SIZE self.speed = 2 def move(self): self.x += random.randint(-self.speed, self.speed) self.y += random.randint(-self.speed, self.speed) def draw(self): pygame.draw.circle(screen, ENEMY_COLOR, (self.x, self.y), self.size) # Food class class Food: def __init__(self, x, y): self.x = x self.y = y self.size = FOOD_SIZE def draw(self): pygame.draw.circle(screen, FOOD_COLOR, (self.x, self.y), self.size) # Function to calculate distance between two points def calculate_distance(x1, y1, x2, y2): return math.sqrt((x2 - x1)**2 + (y2 - y1)**2) # Function to spawn food at random location def spawn_food(): x = random.randint(0, SCREEN_WIDTH) y = random.randint(0, SCREEN_HEIGHT) return Food(x, y) # Create the player player = Player(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2) # Create initial enemies enemies = [Enemy(random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) for _ in range(5)] # Create initial food foods = [spawn_food() for _ in range(10)] # Game loop clock = pygame.time.Clock() running = True while running: screen.fill((255, 255, 255)) for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Player movement keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player.x -= 5 if keys[pygame.K_RIGHT]: player.x += 5 if keys[pygame.K_UP]: player.y -= 5 if keys[pygame.K_DOWN]: player.y += 5 # Draw player player.draw() # Move and draw enemies for enemy in enemies: enemy.move() enemy.draw() # Check collision with player if calculate_distance(player.x, player.y, enemy.x, enemy.y) < player.size - enemy.size: enemies.remove(enemy) player.size += 50 # Draw food for food in foods: food.draw() # Check collision with food for food in foods: if calculate_distance(player.x, player.y, food.x, food.y) < player.size + food.size: player.size += 1 foods.remove(food) foods.append(spawn_food()) pygame.display.flip() clock.tick(60) pygame.quit() sys.exit()