-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
181 lines (136 loc) · 4.68 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
import sys
import random
from PIL import Image, ImageTk
from tkinter import Tk, Frame, Canvas, ALL, NW
WIDTH = 300
HEIGHT = 300
DOT_SIZE = 10
RAND_POS = 27
DELAY = 100
class Board(Canvas):
def __init__(self,parent):
super().__init__(width=WIDTH, height=HEIGHT,background='black',highlightthickness=0)
self.parent = parent
self.ini_game()
self.pack()
def ini_game(self):
self.left = False
self.right = True
self.up = False
self.down = False
self.in_game = True
self.dot_x = 100
self.dot_y = 190
try:
self.ibody = Image.open('dot.png')
self.body = ImageTk.PhotoImage(self.ibody)
self.ihead = Image.open('head.png')
self.head = ImageTk.PhotoImage(self.ihead)
self.idot = Image.open('dot.png')
self.dot = ImageTk.PhotoImage(self.idot)
except IOError as e:
print(str(e))
sys.exit(1)
self.focus_get()
self.create_objects()
self.bind_all('<Key>',self.on_key_pressed)
self.after(DELAY,self.on_time)
def create_objects(self):
self.create_image(self.dot_x, self.dot_y, image=self.dot, anchor=NW, tag='dot')
self.create_image(50, 50, image=self.head, anchor=NW, tag='head')
self.create_image(40, 50, image=self.body, anchor=NW, tag='body')
self.create_image(30, 50, image=self.body, anchor=NW, tag='body')
def check_dot(self):
dot = self.find_withtag('dot')
head = self.find_withtag('head')
x1, y1, x2 , y2 = self.bbox(head)
overlap = self.find_overlapping(x1, y1 , x2, y2)
for ovr in overlap:
if dot[0] == ovr:
x , y = self.coords(dot)
self.create_image(x, y, image = self.body, anchor=NW, tag ='body')
self.locate_dot()
def locate_dot(self):
dot = self.find_withtag('dot')
self.delete(dot[0])
r = random.randint(0, RAND_POS)
self.dot_x = r * DOT_SIZE
r = random.randint(0, RAND_POS)
self.dot_y = r * DOT_SIZE
self.create_image(self.dot_x, self.dot_y, image = self.body, anchor=NW, tag ='dot')
def do_move(self):
bodys = self.find_withtag('body')
head = self.find_withtag('head')
items = bodys + head
k=0
while(k < len(items) - 1):
c1 = self.coords(items[k])
c2 = self.coords(items[k + 1])
self.move(items[k], c2[0] -c1[0], c2[1] - c1[1])
k += 1
if self.left:
self.move(head, -DOT_SIZE,0)
if self.right:
self.move(head, DOT_SIZE,0)
if self.up:
self.move(head, 0, -DOT_SIZE)
if self.down:
self.move(head, 0, DOT_SIZE)
def check_collisions(self):
bodys = self.find_withtag('body')
head = self.find_withtag('head')
x1, y1, x2, y2 = self.bbox(head)
overlap = self.find_overlapping(x1, y1, x2, y2)
for body in bodys:
for ovr in overlap:
if body == ovr:
self.in_game = False
if x1 < 0:
self.in_game = False
if x1 > WIDTH - DOT_SIZE:
self.in_game = False
if y1 < 0:
self.in_game = False
if y1 > HEIGHT - DOT_SIZE:
self.in_game = False
def on_key_pressed(self, e):
key = e.keysym
if key == 'Left' and not self.right:
self.left = True
self.up = False
self.down = False
if key == 'Right' and not self.left:
self.right = True
self.up = False
self.down = False
if key == 'Up' and not self.down:
self.left = False
self.up = True
self.right = False
if key == 'Down' and not self.up:
self.down = True
self.left = False
self.right = False
def on_time(self):
if self.in_game:
self.check_collisions()
self.check_dot()
self.do_move()
self.after(DELAY,self.on_time)
else:
self.game_over()
def game_over(self):
self.delete(ALL)
self.create_text(self.winfo_width()/2, self.winfo_height()/2, text='Game Over',fill='White')
class Snake(Frame):
def __init__(self,parent):
super().__init__(parent)
parent.title('Snake')
self.board = Board(parent)
self.pack()
def main():
root = Tk()
snake = Snake(root)
root.mainloop()
if __name__ == "__main__":
main()