autonomous rpg snage game
- User
autonomous rpg snage game
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
autonomous rpg snage game
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I can help you create an autonomous RPG snake game! This would be a snake game where the snake moves automatically and makes decisions on its own, combined with RPG elements like leveling up, stats, and progression.
Here's a Python implementation using pygame:
import pygame
import random
import math
from enum import Enum
from dataclasses import dataclass
from typing import List, Tuple
# Initialize Pygame
pygame.init()
# Constants
WINDOW_WIDTH = 1000
WINDOW_HEIGHT = 700
GRID_SIZE = 20
GRID_WIDTH = WINDOW_WIDTH // GRID_SIZE
GRID_HEIGHT = WINDOW_HEIGHT // GRID_SIZE
# Colors
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
WHITE = (255, 255, 255)
PURPLE = (128, 0, 128)
ORANGE = (255, 165, 0)
class Direction(Enum):
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
@dataclass
class Position:
x: int
y: int
def __add__(self, other):
return Position(self.x + other[0], self.y + other[1])
class Food:
def __init__(self, pos: Position, food_type: str = "normal"):
self.pos = pos
self.type = food_type
self.value = {"normal": 1, "super": 3, "mega": 5}[food_type]
self.color = {"normal": RED, "super": YELLOW, "mega": PURPLE}[food_type]
class Snake:
def __init__(self):
self.body = [Position(GRID_WIDTH // 2, GRID_HEIGHT // 2)]
self.direction = Direction.RIGHT
self.grow_pending = 0
# RPG Stats
self.level = 1
self.experience = 0
self.exp_to_next_level = 10
self.health = 100
self.max_health = 100
self.speed = 5 # moves per second
self.intelligence = 1 # affects pathfinding
self.luck = 1 # affects special food spawns
def move(self):
head = self.body[0] + self.direction.value
# Wrap around screen
head.x = head.x % GRID_WIDTH
head.y = head.y % GRID_HEIGHT
self.body.insert(0, head)
if self.grow_pending > 0:
self.grow_pending -= 1
else:
self.body.pop()
def grow(self, amount: int = 1):
self.grow_pending += amount
def gain_experience(self, amount: int):
self.experience += amount
while self.experience >= self.exp_to_next_level:
self.level_up()
def level_up(self):
self.experience -= self.exp_to_next_level
self.level += 1
self.exp_to_next_level = int(self.exp_to_next_level * 1.5)
# Stat increases
self.max_health += 20
self.health = self.max_health
self.intelligence += 1
self.luck += 1
if self.level % 3 == 0:
self.speed += 1
def check_self_collision(self) -> bool:
head = self.body[0]
return head in self.body[1:]
def get_head(self) -> Position:
return self.body[0]
class AutonomousAI:
def __init__(self, snake: Snake):
self.snake = snake
self.target_food = None
def find_nearest_food(self, foods: List[Food]) -> Food:
if not foods:
return None
head = self.snake.get_head()
nearest = min(foods, key=lambda f: self.manhattan_distance(head, f.pos))
return nearest
def manhattan_distance(self, pos1: Position, pos2: Position) -> int:
return abs(pos1.x - pos2.x) + abs(pos1.y - pos2.y)
def get_safe_directions(self) -> List[Direction]:
head = self.snake.get_head()
safe_directions = []
for direction in Direction:
new_pos = head + direction.value
new_pos.x = new_pos.x % GRID_WIDTH
new_pos.y = new_pos.y % GRID_HEIGHT
# Check if this position would cause self-collision
if new_pos not in self.snake.body[:-1]: # Exclude tail as it will move
safe_directions.append(direction)
return safe_directions
def choose_direction(self, foods: List[Food]) -> Direction:
safe_directions = self.get_safe_directions()
if not safe_directions:
# No safe moves, just continue current direction
return self.snake.direction
# Find target food
target = self.find_nearest_food(foods)
if not target:
# No food available, move randomly but safely
return random.choice(safe_directions)
head = self.snake.get_head()
# Simple pathfinding based on intelligence
best_direction = self.snake.direction
best_distance = float('inf')
for direction in safe_directions:
new_pos = head + direction.value
new_pos.x = new_pos.x % GRID_WIDTH
new_pos.y = new_pos.y % GRID_HEIGHT
distance = self.manhattan_distance(new_pos, target.pos)
# Add some intelligence-based lookahead
if self.snake.intelligence > 1:
# Look ahead one more step
for next_dir in Direction:
future_pos = new_pos + next_dir.value
future_pos.x = future_pos.x % GRID_WIDTH
future_pos.y = future_pos.y % GRID_HEIGHT
if future_pos not in self.snake.body:
future_distance = self.manhattan_distance(future_pos, target.pos)
distance = min(distance, future_distance * 0.8)
if distance < best_distance:
best_distance = distance
best_direction = direction
return best_direction
class Game:
def __init__(self):
self.screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Autonomous RPG Snake")
self.clock = pygame.time.Clock()
self.font = pygame.font.Font(None, 36)
self.small_font = pygame.font.Font(None, 24)
self.snake = Snake()
self.ai = AutonomousAI(self.snake)
self.foods = []
self.score = 0
self.game_time = 0
# Spawn initial food
self.spawn_food()
def spawn_food(self):
while len(self.foods) < 3: # Keep 3 foods on screen
pos = Position(random.randint(0, GRID_WIDTH-1), random.randint(0, GRID_HEIGHT-1))
# Don't spawn on snake
if pos not in [segment for segment in self.snake.body]:
# Determine food type based on luck
rand = random.randint(1, 100)
if rand <= self.snake.luck * 2:
food_type = "mega"
elif rand <= self.snake.luck * 10:
food_type = "super"
else:
food_type = "normal"
self.foods.append(Food(pos, food_type))
def handle_food_collision(self):
head = self.snake.get_head()
for food in self.foods[:]: # Copy list to avoid modification during iteration
if head.x == food.pos.x and head.y == food.pos.y:
self.snake.grow(food.value)
self.snake.gain_experience(food.value * 2)
self.score += food.value * 10
self.foods.remove(food)
break
def draw(self):
self.screen.fill(BLACK)
# Draw snake
for i, segment in enumerate(self.snake.body):
color = GREEN if i == 0 else (0, 200, 0) # Head is brighter
rect = pygame.Rect(segment.x * GRID_SIZE, segment.y * GRID_SIZE,
GRID_SIZE, GRID_SIZE)
pygame.draw.rect(self.screen, color, rect)
pygame.draw.rect(self.screen, WHITE, rect, 1)
# Draw food
for food in self.foods:
rect = pygame.Rect(food.pos.x * GRID_SIZE, food.pos.y * GRID_SIZE,
GRID_SIZE, GRID_SIZE)
pygame.draw.rect(self.screen, food.color, rect)
pygame.draw.rect(self.screen, WHITE, rect, 1)
# Draw UI
self.draw_ui()
pygame.display.flip()
def draw_ui(self):
# Stats panel
stats = [
f"Score: {self.score}",
f"Level: {self.snake.level}",
f"Length: {len(self.snake.body)}",
f"HP: {self.snake.health}/{self.snake.max_health}",
f"Speed: {self.snake.speed}",
f"Intelligence: {self.snake.intelligence}",
f"Luck: {self.snake.luck}",
f"EXP: {self.snake.experience}/{self.snake.exp_to_next_level}"
]
y_offset = 10
for stat in stats:
text = self.small_font.render(stat, True, WHITE)
self.screen.blit(text, (10, y_offset))
y_offset += 25
# Experience bar
bar_width = 200
bar_height = 10
bar_x = 10
bar_y = y_offset + 10
# Background
pygame.draw.rect(self.screen, WHITE, (bar_x, bar_y, bar_width, bar_height))
# Fill
fill_width = int((self.snake.experience / self.snake.exp_to_next_level) * bar_width)
pygame.draw.rect(self.screen, BLUE, (bar_x, bar_y, fill_width, bar_height))
def run(self):
running = True
move_timer = 0
while running:
dt = self.clock.tick(60) / 1000.0 # Delta time in seconds
self.game_time += dt
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# Pause/unpause (for debugging)
pygame.time.wait(100)
# Move snake based on its speed
move_timer += dt
move_interval = 1.0 / self.snake.speed
if move_timer >= move_interval:
move_timer = 0
# AI chooses direction
new_direction = self.ai.choose_direction(self.foods)
self.snake.direction = new_direction
# Move snake
self.snake.move()
# Check collisions
if self.snake.check_self_collision():
self.snake.health -= 10
if self.snake.health <= 0:
print(f"Game Over! Final Score: {self.score}, Level: {self.snake.level}")
running = False
# Handle food
self.handle_food_collision()
self.spawn_food()
self.draw()
pygame.quit()
if __name__ == "__main__":
game = Game()
game.run()
This autonomous RPG snake game features:
Autonomous Behavior:
RPG Elements:
Game Features:
The snake will automatically navigate toward food while avoiding collisions with itself. As it levels up, it becomes faster and smarter, making better pathfinding decisions. You can watch it play or pause with the spacebar for debugging.
Would you like me to add any specific features like special abilities, different enemy types, or more complex AI behaviors?