Here is a simple implementation of the snake game using Pygame and A* algorithm to make the snake auto-played:
Python
import pygame
import sys
import heapq
# Directions
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
class Node:
def __init__(self, parent=None, position=None):
self.parent = parent
self.position = position
self.g = 0
self.h = 0
self.f = 0
def __eq__(self, other):
return self.position == other.position
def astar(maze, start, end):
start_node = Node(None, start)
start_node.g = start_node.h = start_node.f = 0
end_node = Node(None, end)
end_node.g = end_node.h = end_node.f = 0
open_list = []
closed_list = []
open_list.append(start_node)
while len(open_list) > 0:
current_node = open_list[0]
current_index = 0
for index, item in enumerate(open_list):
if item.f < current_node.f:
current_node = item
current_index = index
open_list.pop(current_index)
closed_list.append(current_node)
if current_node == end_node:
path = []
current = current_node
while current is not None:
path.append(current.position)
current = current.parent
return path[::-1]
children = []
for new_position in [(0, -1), (0, 1), (-1, 0), (1, 0), (-1, -1), (-1, 1), (1, -1), (1, 1)]:
node_position = (current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])
if node_position[0] > (len(maze) - 1) or node_position[0] < 0 or node_position[1] > (len(maze[len(maze)-1]) -1) or node_position[1] < 0:
continue
if maze[node_position[0]][node_position[1]] != 0:
continue
new_node = Node(current_node, node_position)
children.append(new_node)
for child in children:
for closed_child in closed_list:
if child == closed_child:
continue
child.g = current_node.g + 1
child.h = ((child.position[0] - end_node.position[0]) ** 2) + ((child.position[1] - end_node.position[1]) ** 2)
child.f = child.g + child.h
for open_node in open_list:
if child == open_node and child.g > open_node.g:
continue
open_list.append(child)
return None
def draw_grid(maze, rows, cols, window):
window.fill((255, 255, 255))
for i in range(rows):
for j in range(cols):
pygame.draw.rect(window, (0, 0, 0), (j * 20, i * 20, 20, 20), 1)
for i in range(len(maze)):
for j in range(len(maze[i])):
if maze[i][j] == 1:
pygame.draw.rect(window, (255, 0, 0), (j * 20, i * 20, 20, 20))
pygame.display.update()
def main():
pygame.init()
rows = 20
cols = 20
maze = [[0 for _ in range(cols)] for _ in range(rows)]
start = (1, 1)
end = (rows - 2, cols - 2)
maze[start[0]][start[1]] = 1
maze[end[0]][end[1]] = 1
window = pygame.display.set_mode((cols * 20, rows * 20))
pygame.display.set_caption('Snake Game')
snake_pos = [start]
direction = RIGHT
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
new_pos = (snake_pos[-1][0] + direction[0], snake_pos[-1][1] + direction[1])
path = astar(maze, snake_pos[-1], new_pos)
if path is None:
print("No path found")
break
for pos in path:
maze[pos[0]][pos[1]] = 1
snake_pos.append(new_pos)
if new_pos in snake_pos[:-1] or new_pos[0] < 0 or new_pos[0] >= rows or new_pos[1] < 0 or new_pos[1] >= cols:
break
draw_grid(maze, rows, cols, window)
clock.tick(10)
pygame.quit()
if __name__ == '__main__':
main()
This code creates a simple grid-based maze and uses A* algorithm to find the shortest path for the snake. The snake moves in the direction of the shortest path. The game ends when the snake runs into itself or the edge of the maze.
Please note that this code does not handle the food in the game, it just moves the snake in the shortest path. You would need to add more code to handle the food and score.