Implement a terminal based snake game in python with autoplay implementing BFS algo.
- User
Implement a terminal based snake game in python with autoplay implementing BFS algo.
Assistant
Implement a terminal based snake game in python with autoplay implementing BFS algo.
Here's a Python implementation of a terminal-based snake game with autoplay using the BFS (Breadth-First Search) algorithm. This implementation uses the curses library for terminal graphics and the collections library for the BFS algorithm.
import curses
import time
from collections import deque
from random import randint
# Game settings
WIDTH = 60
HEIGHT = 20
DELAY = 0.1 # seconds
# Directions
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
# Game objects
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __hash__(self):
return hash((self.x, self.y))
def __str__(self):
return f"({self.x}, {self.y})"
class Snake:
def __init__(self, start_point):
self.body = [start_point]
self.direction = RIGHT
def move(self, point):
self.body.insert(0, point)
if point != self.body[-1]:
self.body.pop()
def head(self):
return self.body[0]
def grow(self):
self.body.append(self.body[-1])
class Food:
def __init__(self):
self.point = self.generate_point()
def generate_point(self):
return Point(randint(1, WIDTH - 2), randint(1, HEIGHT - 2))
# BFS algorithm for autoplay
def bfs(snake, food):
queue = deque([(snake.head(), 0)])
visited = set()
while queue:
point, distance = queue.popleft()
visited.add(point)
if point == food.point:
return distance
for dx, dy in [(0, -1), (0, 1), (-1, 0), (1, 0)]:
new_point = Point(point.x + dx, point.y + dy)
if new_point not in visited and new_point not in snake.body:
queue.append((new_point, distance + 1))
return None
# Game loop
def main(screen):
curses.curs_set(0)
screen.nodelay(1)
snake = Snake(Point(WIDTH // 2, HEIGHT // 2))
food = Food()
while True:
screen.clear()
screen.border(0)
# Draw snake
for point in snake.body:
screen.addstr(point.y, point.x, "O")
# Draw food
screen.addstr(food.point.y, food.point.x, "@")
# Get user input or autoplay
key = screen.getch()
if key == curses.KEY_UP and snake.direction != DOWN:
snake.direction = UP
elif key == curses.KEY_DOWN and snake.direction != UP:
snake.direction = DOWN
elif key == curses.KEY_LEFT and snake.direction != RIGHT:
snake.direction = LEFT
elif key == curses.KEY_RIGHT and snake.direction != LEFT:
snake.direction = RIGHT
elif key == -1: # No user input, autoplay
distance = bfs(snake, food)
if distance is not None:
if snake.direction == UP or snake.direction == DOWN:
if distance % 2 == 0:
snake.direction = LEFT if snake.head().x < food.point.x else RIGHT
else:
snake.direction = RIGHT if snake.head().x < food.point.x else LEFT
else:
if distance % 2 == 0:
snake.direction = UP if snake.head().y < food.point.y else DOWN
else:
snake.direction = DOWN if snake.head().y < food.point.y else UP
# Move snake
new_point = Point(
snake.head().x + (1 if snake.direction == RIGHT else -1 if snake.direction == LEFT else 0),
snake.head().y + (1 if snake.direction == DOWN else -1 if snake.direction == UP else 0),
)
if new_point in snake.body or new_point.x < 0 or new_point.x >= WIDTH or new_point.y < 0 or new_point.y >= HEIGHT:
break
snake.move(new_point)
# Check if snake eats food
if new_point == food.point:
snake.grow()
food.point = food.generate_point()
screen.refresh()
time.sleep(DELAY)
curses.endwin()
if __name__ == "__main__":
curses.wrapper(main)
This code creates a terminal-based snake game with autoplay using the BFS algorithm. The autoplay feature moves the snake towards the food using the shortest path found by the BFS algorithm.