-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
190 lines (137 loc) · 4.98 KB
/
main.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
import sys
import random
from PIL import Image,ImageTk
from tkinter import Tk,Frame,Canvas,ALL,NW
class Cons:
BOARD_WIDTH = 300
BOARD_HEIGHT = 300
DELAY = 100
DOT_SIZE = 10
MAX_RAND_POS = 27
class Board(Canvas):
def __init__(self):
super().__init__(width=Cons.BOARD_WIDTH,height=Cons.BOARD_HEIGHT,
background="black", highlightthickness=0)
self.initGame()
self.pack()
def initGame(self):
self.inGame = True
self.dots = 3
self.score = 0
self.moveX = Cons.DOT_SIZE
self.moveY = 0
self.appleX = 100
self.appleY = 190
self.loadImages()
self.createObjects()
self.locateApple()
self.bind_all('<Key>', self.onKeyPressed)
self.after(Cons.DELAY, self.onTimer)
def loadImages(self):
try:
self.idot = Image.open('dot.png')
self.dot = ImageTk.PhotoImage(self.idot)
self.ihead = Image.open('head.png')
self.head = ImageTk.PhotoImage(self.ihead)
self.iapple = Image.open('apple.png')
self.apple = ImageTk.PhotoImage(self.iapple)
except IOError as e:
print(e)
sys.exit(1)
def createObjects(self):
self.create_text(30,10,text='Score: {0}'.format(self.score),
tag='score', fill='white')
self.create_image(self.appleX, self.appleY, image=self.apple,
anchor=NW, tag='apple')
self.create_image(50,50, image=self.head, anchor=NW, tag='head')
self.create_image(30,50, image=self.dot, anchor=NW, tag='dot')
self.create_image(40,50, image=self.dot, anchor=NW, tag='dot')
def checkAppleCollision(self):
apple = self.find_withtag('apple')
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 apple[0] == ovr:
self.score += 1
x,y = self.coords(apple)
self.create_image(x,y,image=self.dot, anchor=NW,tag='dot')
self.locateApple()
def moveSnake(self):
dots = self.find_withtag('dot')
head = self.find_withtag('head')
items = dots+head
z=0
while z < len(items)-1:
c1 = self.coords(items[z])
c2 = self.coords(items[z+1])
self.move(items[z], c2[0]-c1[0], c2[1]-c1[1])
z += 1
self.move(head, self.moveX, self.moveY)
def checkCollisions(self):
dots = 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 dot in dots:
for over in overlap:
if over == dot:
self.inGame = True
if x1 < 0 or x1 > Cons.BOARD_WIDTH - Cons.DOT_SIZE:
self.inGame = False
if y1 < 0 or y1 > Cons.BOARD_HEIGHT - Cons.DOT_SIZE:
self.inGame = False
def locateApple(self):
apple = self.find_withtag('apple')
self.delete(apple[0])
r = random.randint(0,Cons.MAX_RAND_POS)
self.appleX = r * Cons.DOT_SIZE
r = random.randint(0, Cons.MAX_RAND_POS)
self.appleY = r * Cons.DOT_SIZE
self.create_image(self.appleX, self.appleY, anchor=NW, imag=self.apple, tag = 'apple')
def onKeyPressed(self,e):
key = e.keysym
LEFT_CURSOR_KEY = 'Left'
if key == LEFT_CURSOR_KEY and self.moveX <= 0:
self.moveX = -Cons.DOT_SIZE
self.moveY = 0
RIGHT_CURSOR_KEY = 'Right'
if key == RIGHT_CURSOR_KEY and self.moveX >= 0:
self.moveX = Cons.DOT_SIZE
self.moveY = 0
UP_CURSOR_KEY = 'Up'
if key == UP_CURSOR_KEY and self.moveY <= 0:
self.moveX = 0
self.moveY = - Cons.DOT_SIZE
DOWN_CURSOR_KEY = 'Down'
if key == DOWN_CURSOR_KEY and self.moveY >= 0:
self.moveX = 0
self.moveY = Cons.DOT_SIZE
def onTimer(self):
self.drawScore()
self.checkCollisions()
if self.inGame:
self.checkAppleCollision()
self.moveSnake()
self.after(Cons.DELAY, self.onTimer)
else:
self.gameOver()
def drawScore(self):
score = self.find_withtag('score')
self.itemconfigure(score,text='Score: {0}'.format(self.score))
def gameOver(self):
self.delete(ALL)
self.create_text(self.winfo_width() / 2, self.winfo_height() / 2,
text='Game Over with score {0}'.format(self.score), fill='white')
class Snake(Frame):
def __init__(self):
super().__init__()
self.master.title('Snake')
self.board= Board()
self.pack()
def main():
root = Tk()
nib = Snake()
root.mainloop()
if __name__ == '__main__':
main()