-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_official.py
217 lines (199 loc) · 7.41 KB
/
snake_official.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# snake8.py
import random
from Tkinter import *
def mousePressed(event):
canvas = event.widget.canvas
redrawAll(canvas)
def keyPressed(event):
canvas = event.widget.canvas
canvas.data["ignoreNextTimerEvent"] = True # for better timing
# first process keys that work even if the game is over
if (event.char == "q"):
gameOver(canvas)
elif (event.char == "r"):
init(canvas)
elif (event.char == "d"):
canvas.data["inDebugMode"] = not canvas.data["inDebugMode"]
# now process keys that only work if the game is not over
if (canvas.data["isGameOver"] == False):
if (event.keysym == "Up"):
moveSnake(canvas, -1, 0)
elif (event.keysym == "Down"):
moveSnake(canvas, +1, 0)
elif (event.keysym == "Left"):
moveSnake(canvas, 0,-1)
elif (event.keysym == "Right"):
moveSnake(canvas, 0,+1)
redrawAll(canvas)
def moveSnake(canvas, drow, dcol):
# move the snake one step forward in the given direction.
canvas.data["snakeDrow"] = drow # store direction for next timer event
canvas.data["snakeDcol"] = dcol
snakeBoard = canvas.data["snakeBoard"]
rows = len(snakeBoard)
cols = len(snakeBoard[0])
headRow = canvas.data["headRow"]
headCol = canvas.data["headCol"]
newHeadRow = headRow + drow
newHeadCol = headCol + dcol
if ((newHeadRow < 0) or (newHeadRow >= rows) or
(newHeadCol < 0) or (newHeadCol >= cols)):
# snake ran off the board
gameOver(canvas)
elif (snakeBoard[newHeadRow][newHeadCol] > 0):
# snake ran into itself!
gameOver(canvas)
elif (snakeBoard[newHeadRow][newHeadCol] < 0):
# eating food! Yum!
snakeBoard[newHeadRow][newHeadCol] = 1 + snakeBoard[headRow][headCol];
canvas.data["headRow"] = newHeadRow
canvas.data["headCol"] = newHeadCol
placeFood(canvas)
else:
# normal move forward (not eating food)
snakeBoard[newHeadRow][newHeadCol] = 1 + snakeBoard[headRow][headCol];
canvas.data["headRow"] = newHeadRow
canvas.data["headCol"] = newHeadCol
removeTail(canvas)
def removeTail(canvas):
# find every snake cell and subtract 1 from it. When we're done,
# the old tail (which was 1) will become 0, so will not be part of the snake.
# So the snake shrinks by 1 value, the tail.
snakeBoard = canvas.data["snakeBoard"]
rows = len(snakeBoard)
cols = len(snakeBoard[0])
for row in range(rows):
for col in range(cols):
if (snakeBoard[row][col] > 0):
snakeBoard[row][col] -= 1
def gameOver(canvas):
canvas.data["isGameOver"] = True
def timerFired(canvas):
ignoreThisTimerEvent = canvas.data["ignoreNextTimerEvent"]
canvas.data["ignoreNextTimerEvent"] = False
if ((canvas.data["isGameOver"] == False) and
(ignoreThisTimerEvent == False)):
# only process timerFired if game is not over
drow = canvas.data["snakeDrow"]
dcol = canvas.data["snakeDcol"]
moveSnake(canvas, drow, dcol)
redrawAll(canvas)
# whether or not game is over, call next timerFired
# (or we'll never call timerFired again!)
delay = 150 # milliseconds
canvas.after(delay, timerFired, canvas) # pause, then call timerFired again
def redrawAll(canvas):
canvas.delete(ALL)
drawSnakeBoard(canvas)
if (canvas.data["isGameOver"] == True):
cx = canvas.data["canvasWidth"]/2
cy = canvas.data["canvasHeight"]/2
canvas.create_text(cx, cy, text="Game Over!", font=("Helvetica", 32, "bold"))
def drawSnakeBoard(canvas):
snakeBoard = canvas.data["snakeBoard"]
rows = len(snakeBoard)
cols = len(snakeBoard[0])
for row in range(rows):
for col in range(cols):
drawSnakeCell(canvas, snakeBoard, row, col)
def drawSnakeCell(canvas, snakeBoard, row, col):
margin = canvas.data["margin"]
cellSize = canvas.data["cellSize"]
left = margin + col * cellSize
right = left + cellSize
top = margin + row * cellSize
bottom = top + cellSize
canvas.create_rectangle(left, top, right, bottom, fill="white")
if (snakeBoard[row][col] > 0):
# draw part of the snake body
canvas.create_oval(left, top, right, bottom, fill="blue")
elif (snakeBoard[row][col] < 0):
# draw food
canvas.create_oval(left, top, right, bottom, fill="green")
# for debugging, draw the number in the cell
if (canvas.data["inDebugMode"] == True):
canvas.create_text(left+cellSize/2,top+cellSize/2,
text=str(snakeBoard[row][col]),font=("Helvatica", 14, "bold"))
def loadSnakeBoard(canvas):
rows = canvas.data["rows"]
cols = canvas.data["cols"]
snakeBoard = [ ]
for row in range(rows): snakeBoard += [[0] * cols]
snakeBoard[rows/2][cols/2] = 1
canvas.data["snakeBoard"] = snakeBoard
findSnakeHead(canvas)
placeFood(canvas)
def placeFood(canvas):
# place food (-1) in a random location on the snakeBoard, but
# keep picking random locations until we find one that is not
# part of the snake!
snakeBoard = canvas.data["snakeBoard"]
rows = len(snakeBoard)
cols = len(snakeBoard[0])
while True:
row = random.randint(0,rows-1)
col = random.randint(0,cols-1)
if (snakeBoard[row][col] == 0):
break
snakeBoard[row][col] = -1
def findSnakeHead(canvas):
# find where snakeBoard[row][col] is largest, and
# store this location in headRow, headCol
snakeBoard = canvas.data["snakeBoard"]
rows = len(snakeBoard)
cols = len(snakeBoard[0])
headRow = 0
headCol = 0
for row in range(rows):
for col in range(cols):
if (snakeBoard[row][col] > snakeBoard[headRow][headCol]):
headRow = row
headCol = col
canvas.data["headRow"] = headRow
canvas.data["headCol"] = headCol
def printInstructions():
print "Snake!"
print "Use the arrow keys to move the snake."
print "Eat food to grow."
print "Stay on the board!"
print "And don't crash into yourself!"
print "Press 'd' for debug mode."
print "Press 'r' to restart."
def init(canvas):
printInstructions()
loadSnakeBoard(canvas)
canvas.data["inDebugMode"] = False
canvas.data["isGameOver"] = False
canvas.data["snakeDrow"] = 0
canvas.data["snakeDcol"] = -1 # start moving left
canvas.data["ignoreNextTimerEvent"] = False
redrawAll(canvas)
########### copy-paste below here ###########
def run(rows, cols):
# create the root and the canvas
root = Tk()
margin = 5
cellSize = 30
canvasWidth = 2*margin + cols*cellSize
canvasHeight = 2*margin + rows*cellSize
canvas = Canvas(root, width=canvasWidth, height=canvasHeight)
canvas.pack()
root.resizable(width=0, height=0)
# Store canvas in root and in canvas itself for callbacks
root.canvas = canvas.canvas = canvas
# Set up canvas data and call init
canvas.data = { }
canvas.data["margin"] = margin
canvas.data["cellSize"] = cellSize
canvas.data["canvasWidth"] = canvasWidth
canvas.data["canvasHeight"] = canvasHeight
canvas.data["rows"] = rows
canvas.data["cols"] = cols
init(canvas)
# set up events
root.bind("<Button-1>", mousePressed)
root.bind("<Key>", keyPressed)
timerFired(canvas)
# and launch the app
root.mainloop() # This call BLOCKS (so your program waits until you close the window!)
run(8,16)