-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.py
More file actions
219 lines (185 loc) · 7.64 KB
/
Snake.py
File metadata and controls
219 lines (185 loc) · 7.64 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
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
'''
Snake Game Using Python tkinter
Hello World ! I am Pratik Rathod
A snake game experiment in just 4 hours
'''
from tkinter import *
import random
import tkinter.messagebox as tkbox
class Snake:
def __init__(self, root=None):
'''Globles Variables'''
self.root = root
#Height & Width
self.windowWidth = 895
self.windowHeight = 495
self.score = 0
# gameBoundary
self.gameBoundaryTop = 20
self.gameBoundaryBottom = self.windowHeight-20
self.gameBoundaryRight = self.windowWidth-20
self.gameBoundaryLeft = 20
#direction
self.direction = "Right"
# snakeIntialCordinates
self.steps = 20
self.speed = 120 # ms
self.x = 0
self.y = self.steps
# virtual coordnates
self.xpos_snake = 6
self.ypos_snake = 5
self.x0 = self.steps * (self.xpos_snake+1)
self.y0 = self.steps * (self.ypos_snake+1)
# food
self.food = None
#snake
snake = [[self.xpos_snake,self.ypos_snake] for x in range(1,4)]
# canvas variable
self.c = Canvas(self.root, bg="black", height=int(
self.root.winfo_screenheight()-200), width=self.root.winfo_screenwidth())
self.c.pack()
# calling Window function
player = self.snakePlayer(snake)
self.snakeWindow()
self.design()
self.scoreLable = self.c.create_text(770,450,fill="gray",font="Times 18",text="Score : 0")
self.movePlayer(snake,player)
def snakeWindow(self):
# Gets both half the screen width/height and window width/height
positionRight = int(
self.root.winfo_screenwidth()/2 - self.windowWidth/2)
positionDown = int(
self.root.winfo_screenheight()/2 - self.windowHeight/2)
# Positions the window in the center of the page.
self.root.geometry("{}x{}+{}+{}".format(self.windowWidth,
self.windowHeight, positionRight, positionDown))
self.c.create_text(150,40,fill="darkblue",font="Castellar 18 italic bold",text=" A snake Game")
def design(self):
# Top Boudary
self.c.create_rectangle(
0, 0, self.windowWidth, self.gameBoundaryTop, fill="gray", outline="gray")
self.c.create_text(100,10,fill="lightgray",font="Times 12 ",text="Creator : Pratik Rathod")
# Bottom Boundary
self.c.create_rectangle(0, self.windowHeight, self.windowWidth,
self.gameBoundaryBottom, fill="gray", outline="gray")
# Left Boundary
self.c.create_rectangle(0, 0, self.gameBoundaryLeft,
self.windowHeight, fill="gray", outline="gray")
# Right Boundary
self.c.create_rectangle(self.windowWidth, 0, self.gameBoundaryRight,
self.windowHeight, fill="gray", outline="gray")
def snakePlayer(self,snake):
player = []
for x,y in snake:
self.x0 = self.steps * x
self.y0 = self.steps * y
ract = self.c.create_rectangle(
self.x0, self.y0, self.x0+15, self.y0+15, fill="brown", outline="red")
player.append(ract)
return player
def createFood(self):
self.c.itemconfigure(self.scoreLable,text="Score : "+str(self.score))
self.xpos = random.randrange(20, self.gameBoundaryRight-90, 20)
self.ypos = random.randrange(20, self.gameBoundaryBottom, 20)
self.food = self.c.create_rectangle(
self.xpos, self.ypos, self.xpos+15, self.ypos+15, fill="yellow", outline="yellow")
def movePlayer(self,snake,player,delete_tail=True):
self.c.move(player, self.x, self.y)
# get head
x, y = snake[0]
direction = self.direction
if direction == 'Up':
y = y-1
elif direction == 'Down':
y = y+1
elif direction == 'Left':
x = x-1
elif direction == 'Right':
x = x+1
snake.insert(0, [x, y])
self.x0 = self.steps * x
self.y0 = self.steps * y
ract = self.c.create_rectangle(
self.x0, self.y0, self.x0+15, self.y0+15, fill="brown", outline="brown")
player.insert(0,ract)
if delete_tail :
del snake[-1]
self.c.delete(player[-1])
del player[-1]
if(self.food == None):
self.createFood()
if(self.c.coords(player[0]) == self.c.coords(self.food)):
self.c.delete(self.food)
self.score = self.score + 50
self.food = None
delete_tail = False
else:
delete_tail = True
try:
self.checkGameStatus(self.c.coords(player[0]))
self.checkGameCollisions(player)
self.c.after(self.speed, self.movePlayer,snake,player,delete_tail)
except:
print("Bye");
def checkGameStatus(self, snakeCordinates):
if snakeCordinates[0] < self.gameBoundaryLeft:
tkbox.showinfo("Shanke Says","Game Over \n Final Score :"+str(self.score))
self.root.destroy()
elif snakeCordinates[1] < self.gameBoundaryTop:
tkbox.showinfo("Shanke Says","Game Over \n Final Score :"+str(self.score))
self.root.destroy()
elif snakeCordinates[2] > self.gameBoundaryRight:
tkbox.showinfo("Shanke Says","Game Over \n Final Score :"+str(self.score))
self.root.destroy()
elif snakeCordinates[3] > self.gameBoundaryBottom:
tkbox.showinfo("Shanke Says","Game Over \n Final Score :"+str(self.score))
self.root.destroy()
def checkGameCollisions(self,player):
head = player[0]
self.c.coords(head)
for i in range(1,len(player)):
if(self.c.coords(head) == self.c.coords(player[i])):
tkbox.showinfo("Shanke Says","Game Over \n Final Score :"+str(self.score))
self.root.destroy()
def control(self, event):
if event.keysym == 'Escape':
print("Bye")
self.root.destroy()
if event.keysym == 'Up':
if self.direction !='Down':
self.direction = event.keysym
if event.keysym == 'Down':
if self.direction !='Up':
self.direction = event.keysym
if event.keysym == 'Left':
if self.direction !='Right':
self.direction = event.keysym
if event.keysym == 'Right':
if self.direction !='Left':
self.direction = event.keysym
if event.keysym == 'space':
if self.steps < 20:
self.steps = self.steps+1
print(self.steps)
elif event.keysym == 'Shift_L':
if self.steps > 5:
self.steps = self.steps-1
print(self.steps)
if event.keysym == 'comma':
if(self.speed > 10):
self.speed = self.speed-10
print(self.speed)
elif event.keysym == 'period':
if(self.speed < 1000):
self.speed = self.speed+10
print(self.speed)
# Function
if __name__ == "__main__":
# root variable
root = Tk()
root.overrideredirect(1)
snake = Snake(root)
# Keys Events
root.bind_all('<Key>', snake.control)
mainloop()