This repository was archived by the owner on Mar 14, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsnakegame.py
More file actions
149 lines (120 loc) · 3.82 KB
/
Copy pathsnakegame.py
File metadata and controls
149 lines (120 loc) · 3.82 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
from random import randint
class SnakeGame:
"""
Simple Snake game
"""
def __init__(self, board_dimensions):
self.board_dimensions = board_dimensions
self.restart()
def restart(self):
"""
Restores game to default state
"""
self.snake = Snake(positions=[[2, 2], [2, 1]])
self.putFood()
self.score = 0
def __str__(self):
"""
Draw the board
"""
# create empty board
# board_string = "+" + "-" * (self.board_dimensions[1]) + "+" + "\n"
board_string = ""
for i in range(self.board_dimensions[0]):
# row_string = "|"
row_string = ""
# draw snake
for j in range(self.board_dimensions[1]):
if [i, j] == self.snake.positions[0]:
row_string += ":python:" # head
elif [i, j] in self.snake.positions:
row_string += "🐍"
elif [i, j] == self.food:
row_string += "🍕"
else:
row_string += "◻️"
# row_string += "|\n"
board_string += row_string + "\n"
# board_string += "+" + "-" * (self.board_dimensions[1]) + "+\n"
return board_string
def move(self, direction):
"""
Executes one movement.
Returns information about the movement:
"ok", "forbidden", "lost", "food".
"""
direction_dict = {
"right": (0, 1),
"left": (0, -1),
"up": (-1, 0),
"down": (1, 0)
}
move_status = self.snake.move(direction_dict[direction])
if self.isLost():
move_status = "lost"
self.restart()
if self.isEating():
self.snake.grow()
move_status = "grow"
self.putFood()
self.score += 1
return move_status
def isLost(self):
head = self.snake.head
if (head[0] == -1 or
head[1] == -1 or
head[0] == self.board_dimensions[0] or
head[1] == self.board_dimensions[1] or
head in self.snake.positions[1:]):
return True
return False
def putFood(self):
valid = False
while not valid:
i = randint(0, self.board_dimensions[0] - 1)
j = randint(0, self.board_dimensions[1] - 1)
if [i, j] not in self.snake.positions:
valid = True
self.food = [i, j]
def isEating(self):
if self.snake.head == self.food:
return True
return False
class Snake:
"""
Actual snake in the game.
"""
def __init__(self, positions):
self.positions = positions
self.head = positions[0]
def move(self, velocity):
"""
Executes one movement.
Returns information about the movement:
"ok", "forbidden"
"""
if not self.isPossible(velocity):
print("Movement not allowed")
return "forbidden"
# delete tail but store it, as we might want the snake to grow
self.deletedTail = self.positions[-1]
self.positions = self.positions[:-1]
# move head
self.head = [self.head[0] + velocity[0],
self.head[1] + velocity[1]]
self.positions.insert(0, self.head)
return "ok"
def isPossible(self, velocity):
"""
Check Snake is trying to do an 180º turn.
"""
newHead = [self.head[0] + velocity[0],
self.head[1] + velocity[1]]
if newHead == self.positions[1]:
return False
return True
def grow(self):
"""
Makes the snake grow one square.
"""
self.positions.append(self.deletedTail)