-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_exp.py
176 lines (147 loc) · 6.24 KB
/
game_exp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Copyright 2024 Sipho Zuma
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License."""
from collections import deque
from utils.search import Node, SnakeProblem
from utils.snake import Food, Snake
from utils.utils import PriorityQueue
from utils.constants import SCREEN_WIDTH, SCREEN_HEIGHT, CELL_SIZE
from game.game import Game
import pygame
from ui.controls import Controls
from game.agents import Environment
def move(head_cell, parent_snake):
"""Add head, remove tail, return child that moved"""
return list([head_cell] + parent_snake[:-1])
def eat(head_cell, parent_snake):
"""Add head, return child that grew an inch"""
return list([head_cell] + parent_snake)
def ocupiable_cells(nodes, parent_snake: list):
"""Return nodes that avoids the snake body"""
return [node for node in nodes if node.state not in parent_snake]
def search(snake: Snake, func, problem: SnakeProblem):
# The snake changes with each action performed
def _f(items):
node, _ = items
return func(node)
init_snake = list([snake.head.cell] + [sbo.cell for sbo in snake.tail])
node = Node(problem.init_pos)
frontier = PriorityQueue('min', _f)
frontier.append((node, init_snake))
reached = {node.state: node}
while frontier:
node, parent_snake = frontier.pop()
if problem.is_goal(node.state):
return node.solution()
# For now let us ignore eating
for child in ocupiable_cells(node.expand(problem), parent_snake):
state = child.state
if state not in reached:
child_snake = move(state, parent_snake)
frontier.append((child, child_snake))
reached[state] = node
class Park(Environment):
def percept(self, agent: Snake):
things = self.list_things_at(agent.location, Food)
return things
def execute_action(self, agent:Snake, action):
if action in ["UP", "DOWN", "LEFT", "RIGHT"]:
agent.set_direction(action)
agent.move()
elif action == "EAT":
agent.grow(size=1)
items = self.list_things_at(agent.location)
if len(items) != 0:
if agent.is_eating_food(items[0]): #Have the snake eat the first item
print('{} ate {} at location: {}'
.format(str(agent)[1:-1], str(items[0])[1:-1], agent.location))
self.delete_thing(items[0]) #Delete it from the Park after.
items[0].replace(agent)
self.add_thing(items[0], items[0].cell)
def is_done(self):
no_food = not any(isinstance(items, Food) for items in self.things)
dead_agents = not any(agent.is_alive() for agent in self.agents)
return no_food or dead_agents
class SnakeInThePark:
def __init__(self):
self.actions = deque()
def snake_program(precepts):
items = len(precepts)
for p in precepts:
if isinstance(p, Food):
return "EAT"
else:
if len(self.actions) != 0:
return self.actions.popleft()
else: return ""
self.snake = Snake((200, 200), snake_program) # Top left corner
self.food = Food(self.snake, (SCREEN_WIDTH, SCREEN_HEIGHT))
self.park = Park()
self.park.add_thing(self.snake, self.snake.location)
self.park.add_thing(self.food, self.food.cell)
self.problem = SnakeProblem(self.snake.location, self.food.location)
self.actions = deque(search(self.snake, lambda node: node.depth, self.problem))
print(self.actions)
self.background = self.create_background()
pygame.init()
self.clock = pygame.time.Clock()
self.window = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Snake Game")
self.controls = Controls()
self.running = True
def run(self):
while self.running:
self.handle_events()
self.update()
self.draw()
def update(self):
if len(self.actions) == 0:
self.problem = SnakeProblem(self.snake.location, self.food.cell)
solution = search(self.snake, lambda node: node.depth, self.problem)
if solution != None:
if len(solution) != 0:
self.actions = deque(solution)
print(self.actions)
else:
print("No solution")
def handle_events(self):
events = pygame.event.get()
self.controls.update(events)
for event in events:
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if self.controls.is_just_pressed(self.controls.ESCAPE):
self.park.run(10)
elif self.controls.is_pressed(self.controls.SPACE):
self.park.step()
def draw(self):
time = pygame.time.get_ticks() / 1000
self.window.blit(self.background, (0, 0))
self.snake.draw(self.window, time)
self.food.draw(self.window)
pygame.display.flip()
def create_background(self):
"""
Create and return the game background surface.
Returns:
pygame.Surface: The created background surface.
"""
background = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT))
for x in range(0, SCREEN_WIDTH, CELL_SIZE):
for y in range(0, SCREEN_HEIGHT, CELL_SIZE):
color = (60, 60, 60) if (x + y) // CELL_SIZE % 2 == 0 else (40, 40, 40)
pygame.draw.rect(background, color, (x, y, CELL_SIZE, CELL_SIZE))
return background
experiment = SnakeInThePark()
experiment.run()
pygame.quit()
import sys
sys.exit()