forked from mengguoru/TkInter-Snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
272 lines (218 loc) · 9.01 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python
# -*- encoding: utf-8 -*-
import random
try:
import tkinter
except ImportError:
import Tkinter as tkinter
import os.path
FOOD_COLOR = ('yellow', 'red','cyan','orange','blue')
class Application(object):
""" Application. """
#pylint: disable=too-many-instance-attributes
TITLE = 'Snake'
SIZE = 400, 400
BORDER = 10
MOVE = 10
SNAKE_SIZE = 7
SPEED_STEP = 3
SPEED_DEC = 5
# initialize parameters of this class && parameters of Tk() class
def __init__(self, master):
""" Init. """
self.master = master
self.head = None
self.head_position = None
# segment : Snake body
self.segments = []
self.segment_positions = []
self.food = None
self.food_position = None
self.direction = None
self.moved = True
# scores
self.scores = tkinter.StringVar()
self.highscore = 0
self.infos = tkinter.StringVar()
self.speed = 150
self.speed_flag = False
self.running = False
# below : initialize the specific parameter of Tk() class
self.init()
def init(self):
""" Init the game."""
self.master.title(self.TITLE)
self.canvas = tkinter.Canvas(self.master)
self.canvas.grid(sticky=tkinter.NSEW)
self.start_button = tkinter.Button(self.master, text='Start', command=self.on_start)
self.start_button.grid(row=1, sticky=tkinter.EW)
# scores
self.high_scores()
self.scores.set(('Score: %d High Score: %d' % (len(self.segments)*10, self.highscore)))
self.score_label = tkinter.Label(self.master, textvariable=self.scores)
self.score_label.grid(row=2, sticky=tkinter.EW)
self.infos.set('Speed: %d' % (100 - ((self.speed-50)*100/100)))
self.infos_label = tkinter.Label(self.master, textvariable=self.infos)
self.infos_label.grid(row=3, sticky=tkinter.EW)
self.master.bind('<Up>', self.on_up) #w
self.master.bind('<Left>', self.on_left) #a
self.master.bind('<Down>', self.on_down) #s
self.master.bind('<Right>', self.on_right) #d
self.master.bind('p', self.on_pause) #pause
self.master.columnconfigure(0, weight=1)
self.master.rowconfigure(0, weight=1)
self.master.resizable(width=False, height=False)
self.master.geometry('%dx%d' % self.SIZE)
def on_start(self):
""" Start Button Pressed."""
self.reset()
if self.running:
self.running = False
self.start_button.configure(text='Start')
else:
self.running = True
self.start_button.configure(text='Stop')
self.start()
def reset(self):
""" reset the game """
self.segments = []
self.segment_positions = []
self.canvas.delete(tkinter.ALL)
def start(self):
""" Start game. """
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
self.canvas.create_rectangle(self.BORDER, self.BORDER, width-self.BORDER, height-self.BORDER)
self.direction = random.choice('wasd')
# scores
self.high_scores()
self.scores.set(('Score: %d High Score: %d' % (len(self.segments)*10, self.highscore)))
self.speed = 150
self.speed_flag = False
pos_x = round(width // 2, -1)
pos_y = round(height // 2, -1)
head_position = [pos_x, pos_y, pos_x+self.SNAKE_SIZE, pos_y+self.SNAKE_SIZE]
self.head = self.canvas.create_oval(tuple(head_position), fill='green')
self.head_position = head_position
self.spawn_food()
self.tick()
def spawn_food(self):
"""Draw the food."""
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
positions = [tuple(self.head_position), self.food_position] + self.segment_positions
pos_x = round(random.randint(20, width-20), -1)
pos_y = round(random.randint(20, height-20), -1)
position = (pos_x, pos_y, pos_x+self.SNAKE_SIZE, pos_y+self.SNAKE_SIZE)
while position in positions:
pos_x = round(random.randint(20, width-20), -1)
pos_y = round(random.randint(20, height-20), -1)
position = (round(random.randint(20, width-20), -1), round(random.randint(20, height-20), -1))
color = random.choice(FOOD_COLOR)
self.food = self.canvas.create_rectangle(tuple(position), fill=color)
self.food_position = position
def tick(self):
"""Draw the snake."""
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
previous_head_position = tuple(self.head_position)
if self.direction == 'w':
self.head_position[1] -= self.MOVE
self.head_position[3] -= self.MOVE
elif self.direction == 'a':
self.head_position[0] -= self.MOVE
self.head_position[2] -= self.MOVE
elif self.direction == 's':
self.head_position[1] += self.MOVE
self.head_position[3] += self.MOVE
elif self.direction == 'd':
self.head_position[0] += self.MOVE
self.head_position[2] += self.MOVE
head_position = tuple(self.head_position)
if (self.head_position[0] < self.BORDER or self.head_position[0] >= width-self.BORDER or
self.head_position[1] < self.BORDER or self.head_position[1] >= height-self.BORDER or
any(segment_position == head_position for segment_position in self.segment_positions)):
self.game_over()
return
if head_position == self.food_position:
self.canvas.coords(self.food, previous_head_position)
self.segments.append(self.food)
self.segment_positions.append(previous_head_position)
self.spawn_food()
if self.segments:
previous_position = previous_head_position
for index, (segment, position) in enumerate(zip(self.segments, self.segment_positions)):
self.canvas.coords(segment, previous_position)
self.segment_positions[index] = previous_position
previous_position = position
self.canvas.coords(self.head, head_position)
if self.running and self.moved:
#speed
if len(self.segments) % self.SPEED_STEP == 0 and self.speed_flag and self.speed > 50:
self.speed -= self.SPEED_DEC
self.speed_flag = False
if len(self.segments) % self.SPEED_STEP > 0 and not self.speed_flag:
self.speed_flag = True
# scores
if (len(self.segments)*10) > self.highscore:
self.highscore = len(self.segments)*10
self.infos.set('Speed: %d' % (100 - ((self.speed-50)*100/100)))
self.scores.set(('Score: %d High Score: %d'% (len(self.segments)*10, self.highscore)))
self.canvas.after(self.speed, self.tick)
def game_over(self):
""" Game over."""
width = self.canvas.winfo_width()
height = self.canvas.winfo_height()
# scores
self.high_scores()
self.running = False
self.start_button.configure(text='Start')
score = len(self.segments) * 10
if score >= self.highscore:
self.canvas.create_text((round(width // 2, -1), round(height // 2, -1)), text='Game Over! Great, You have the High score : %d' % score)
else:
self.canvas.create_text((round(width // 2, -1), round(height // 2, -1)), text='Game Over! Your score is: %d' % score)
def high_scores(self):
""" Get and Save the High score to the file score.txt"""
filename = 'score.txt'
if os.path.isfile(filename):
ofile = open(filename, 'r')
score = ofile.read()
if len(score) != 0:
self.highscore = int(score)
ofile.close()
if (len(self.segments)*10) >= self.highscore:
wfile = open(filename, 'w')
score = ('%d' % (len(self.segments)*10))
wfile.write(score)
wfile.close()
def on_up(self, event):
""" Arrow key up pressed."""
if self.moved and not self.direction == 's':
self.direction = 'w'
def on_down(self, event):
""" Arrow key down pressed."""
if self.moved and not self.direction == 'w':
self.direction = 's'
def on_left(self, event):
""" Arrow key left pressed."""
if self.moved and not self.direction == 'd':
self.direction = 'a'
def on_right(self, event):
""" Arrow key right pressed."""
if self.moved and not self.direction == 'a':
self.direction = 'd'
def on_pause(self, event):
""" P (Pause) key pressed."""
if self.moved:
self.moved = False
else:
self.moved = True
self.tick()
def main():
""" start application."""
root = tkinter.Tk()
Application(root)
root.mainloop()
if __name__ == '__main__':
main()