-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnake.py
More file actions
173 lines (136 loc) · 5.12 KB
/
snake.py
File metadata and controls
173 lines (136 loc) · 5.12 KB
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
import pygame
import numpy as np
from enum import Enum
from collections import namedtuple
from typing import Optional, Tuple
Point = namedtuple('Point', 'x, y')
class Direction(Enum):
RIGHT = (1, 0) # (dx, dy)
LEFT = (-1, 0)
UP = (0, -1)
DOWN = (0, 1)
@property
def dx(self) -> int:
return self.value[0] * BLOCK_SIZE
@property
def dy(self) -> int:
return self.value[1] * BLOCK_SIZE
def turn_right(self) -> 'Direction':
"""Return the direction after turning right."""
rotations = {
Direction.RIGHT: Direction.DOWN,
Direction.DOWN: Direction.LEFT,
Direction.LEFT: Direction.UP,
Direction.UP: Direction.RIGHT
}
return rotations[self]
def turn_left(self) -> 'Direction':
"""Return the direction after turning left."""
rotations = {
Direction.RIGHT: Direction.UP,
Direction.UP: Direction.LEFT,
Direction.LEFT: Direction.DOWN,
Direction.DOWN: Direction.RIGHT
}
return rotations[self]
BLOCK_SIZE = 20
SPEED = 40
WHITE = (255, 255, 255)
RED = (200, 0, 0)
GREEN1 = (0, 200, 0)
GREEN2 = (0, 150, 0)
BLACK = (0, 0, 0)
class SnakeGame:
def __init__(self, w: int = 640, h: int = 480, speed: int = SPEED, headless: bool = False) -> None:
self.w = w
self.h = h
self.speed = speed
self.headless = headless
if not headless:
pygame.init()
self.display = pygame.display.set_mode((self.w, self.h))
pygame.display.set_caption('Snake')
self.clock = pygame.time.Clock()
else:
self.display = None
self.clock = None
self.reset()
def reset(self) -> np.ndarray:
self.direction = Direction.RIGHT
self.head = Point(self.w // 2, self.h // 2)
self.snake = [self.head,
Point(self.head.x - BLOCK_SIZE, self.head.y),
Point(self.head.x - (2 * BLOCK_SIZE), self.head.y)]
self.score = 0
self.food = None
self._place_food()
self.frame_iteration = 0
return self.get_state()
def cleanup(self) -> None:
"""Clean up pygame resources."""
if not self.headless:
pygame.quit()
def _place_food(self) -> None:
x = np.random.randint(0, (self.w - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
y = np.random.randint(0, (self.h - BLOCK_SIZE) // BLOCK_SIZE) * BLOCK_SIZE
self.food = Point(x, y)
if self.food in self.snake:
self._place_food()
def play_step(self, action: int) -> Tuple[float, bool, int]:
self.frame_iteration += 1
if not self.headless:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Store old head position for distance calculation
old_head = self.head
self._move(action)
self.snake.insert(0, self.head)
# Check game over
if self.is_collision() or self.frame_iteration > 100 * len(self.snake):
return -10, True, self.score
# Check food
reward = 0
if self.head == self.food:
self.score += 1
reward = 10
self._place_food()
else:
self.snake.pop()
# TODO: can we improve our reward function?
if not self.headless:
self._update_ui()
self.clock.tick(self.speed)
return reward, False, self.score
def is_collision(self, pt: Optional[Point] = None) -> bool:
pt = pt or self.head
return (pt.x >= self.w or pt.x < 0 or pt.y >= self.h or pt.y < 0 or
pt in self.snake[1:])
def _update_ui(self) -> None:
self.display.fill(BLACK)
for i, pt in enumerate(self.snake):
color = GREEN1 if i % 2 == 0 else GREEN2
pygame.draw.rect(self.display, color, pygame.Rect(pt.x, pt.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(self.display, RED, pygame.Rect(self.food.x, self.food.y, BLOCK_SIZE, BLOCK_SIZE))
font = pygame.font.Font(None, 36)
text = font.render(f"Score: {self.score}", True, WHITE)
self.display.blit(text, [0, 0])
pygame.display.flip()
def _move(self, action: int) -> None:
# Update direction based on action: 0=straight, 1=right, 2=left
if action == 1:
self.direction = self.direction.turn_right()
elif action == 2:
self.direction = self.direction.turn_left()
self.head = Point(self.head.x + self.direction.dx,
self.head.y + self.direction.dy)
def get_state(self) -> np.ndarray:
# Danger in relative directions (straight, right, left)
dir_right = self.direction.turn_right()
dir_left = self.direction.turn_left()
# Taxicab distance to food
taxicab_distance = abs(self.food.x - self.head.x) + abs(self.food.y - self.head.y)
return np.array([
# TODO: implement state variables... 12 of them ideally :0
], dtype=float)