-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
196 lines (163 loc) · 6.59 KB
/
snake.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import time
import random
import curses
from copy import deepcopy
from pyfiglet import figlet_format
UP = (0, -1)
DOWN = (0, 1)
LEFT = (-1, 0)
RIGHT = (1, 0)
INIT = "init"
RUNNING = "running"
GAMEOVER = "gameover"
PAUSED = "paused"
EXIT = "exit"
mapping = {
"w": UP,
"a": LEFT,
"s": DOWN,
"d": RIGHT
}
class Apple(object):
def __init__(self, width, height, forbidden):
self.coordinates = (0, 0)
self.width = width
self.height = height
self.respawn(forbidden)
def respawn(self, forbidden):
x, y = 0, 0
while (x, y) in forbidden or x == 0 or y == 0 or x == self.width + 1 or y == self.height + 1:
x, y = random.randint(1, self.width), random.randint(1, self.height)
self.coordinates = (x, y)
class Snake(object):
def __init__(self, body, direction):
# body is a list of tuples
# first in the list is the head
self.body = body
self.direction = direction
# add position to front of snake body and pop off the back
def take_step(self, apple):
new_position = (self.body[-1][0] + self.direction[0],
self.body[-1][1] + self.direction[1])
if new_position == apple:
self.body.append(new_position)
else:
self.body = self.body[1:] + [new_position]
return new_position
def set_direction(self, direction):
self.direction = direction
def valid_direction(self, direction):
return not (set([self.direction, direction]) == set([UP, DOWN]) or set([self.direction, direction]) == set([LEFT, RIGHT]))
def move(self, apple, direction=None):
if direction and self.valid_direction(direction):
self.set_direction(direction)
return self.take_step(apple)
def head(self):
return self.body[-1]
class Game(object):
def __init__(self, height, width, delay=0.5):
self.height = height
self.width = width
self.delay = delay
self.state = INIT
self.midpoint = (int(width/2), int(height/2))
self.apple = Apple(width, height, [self.midpoint])
self.snake = Snake([self.midpoint], None)
def render(self, stdscr):
k = 0
# Clear and refresh the screen for a blank canvas
stdscr.clear()
stdscr.refresh()
# Start colors in curses
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
stdscr.addstr(figlet_format('READY PLAYER ONE', font='starwars'))
stdscr.refresh()
time.sleep(self.delay * 5)
# Loop where k is the last character pressed
while self.state != EXIT: # (k != ord('q')):
# Initialization
stdscr.clear()
height, width = stdscr.getmaxyx()
stdscr.nodelay(1)
old_snake_body = deepcopy(self.snake.body)
status_str = ""
# Read and act on input
k = stdscr.getch()
if k != -1:
char = chr(k)
direction = None if char not in mapping else mapping[char]
if char == "q":
self.state = EXIT
elif self.state == INIT and direction:
self.state = RUNNING
x, y = self.snake.move(self.apple.coordinates, direction)
elif char == "p":
if self.state == RUNNING:
self.state = PAUSED
elif self.state == PAUSED:
self.state = RUNNING
elif char == "r" and self.state == GAMEOVER:
self.snake = Snake([self.midpoint], None)
self.apple.respawn([self.midpoint])
self.state = INIT
stdscr.addstr(figlet_format('READY PLAYER ONE', font='starwars'))
stdscr.refresh()
time.sleep(self.delay * 5)
elif self.state == RUNNING and direction:
x, y = self.snake.move(self.apple.coordinates, direction)
elif self.state == RUNNING:
x, y = self.snake.move(self.apple.coordinates)
if self.state == RUNNING:
if self.snake.body != old_snake_body and ((x, y) in old_snake_body or x == 0 or y == 0 or x == self.width + 1 or y == self.height + 1):
self.state = GAMEOVER
elif (x, y) == self.apple.coordinates:
self.apple.respawn(self.snake.body)
status_str += "PRESS P TO PAUSE"
elif self.state == INIT:
status_str += "TO BEGIN, USE THE WASD KEYS TO MOVE UP, LEFT, DOWN AND RIGHT RESPECTIVELY\n\n"
elif self.state == PAUSED:
status_str = "PAUSED: PRESS P TO RESUME"
elif self.state == GAMEOVER:
status_str = "GAME OVER: PRESS R TO START NEW GAME"
gameboard_str = ""
for y in range(self.height + 2):
for x in range(self.width + 2):
left = x == 0
right = x == self.width + 1
top = y == 0
bottom = y == self.height + 1
snake_body = (x, y) in self.snake.body
snake_head = (x, y) == self.snake.head()
apple = (x, y) == self.apple.coordinates
if ((top and left) or (top and right) or
(bottom and left) or (bottom and right)):
gameboard_str += "+"
elif right or left:
gameboard_str += "|"
elif top or bottom:
gameboard_str += "-"
elif snake_head:
gameboard_str += "x"
elif snake_body:
gameboard_str += "o"
elif apple:
gameboard_str += "*"
else:
gameboard_str += " "
gameboard_str += "\n"
status_str += "\nPRESS Q TO QUIT"
if self.state != EXIT:
printstr = gameboard_str + "\n\n" + status_str
stdscr.addstr(printstr)
stdscr.addstr("\n GAME STATE: {}".format(self.state))
# Refresh the screen
stdscr.refresh()
time.sleep(self.delay)
def main():
game = Game(30, 30, delay=0.1)
curses.wrapper(game.render)
if __name__ == '__main__':
main()