-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_Game_Code
151 lines (117 loc) · 4.37 KB
/
Snake_Game_Code
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
import tkinter as tk
import random
# Global Variables
screen_width = 800
screen_height = 600
segment_size = 20
status = True
SCORE = 0
speed = 200
def setUpWindow():
global win
global my_canvas
win = tk.Tk()
win.title("Snake Game")
my_canvas = tk.Canvas(win, width=screen_width, height=screen_height, bg="#000000")
my_canvas.grid()
def restart():
global segment
global snake
global status
global speed
global SCORE
win.destroy()
setUpWindow()
segment = [Segment(segment_size, segment_size), Segment(segment_size * 2, segment_size),
Segment(segment_size * 3, segment_size)]
snake = Snake(segment)
my_canvas.bind("<KeyPress>", snake.changeDirection)
my_canvas.focus_set()
status=True
createApple()
main()
SCORE=0
speed=200
def createApple(): # Creates an apple to be made
global apple
x = segment_size * random.randint(1, (screen_width - segment_size) / segment_size)
y = segment_size * random.randint(1, (screen_height - segment_size) / segment_size)
apple = my_canvas.create_oval(x, y, x + segment_size, y + segment_size, fill="red")
class Segment:
def __init__(self, x, y):
self.instance = my_canvas.create_oval(x, y, x + segment_size, y + segment_size, fill="white")
class Snake:
def __init__(self, segment):
self.segment = segment
# Creating possible moves
self.mapping = {"Down": (0, 1), "Right": (1, 0), "Up": (0, -1),
"Left": (-1, 0)} # Use of dictionary to define the directions
# Begining
self.vector = self.mapping["Right"]
def move(self): # Move the snake
for i in range(len(self.segment) - 1):
segment = self.segment[i].instance
x1, y1, x2, y2 = my_canvas.coords(self.segment[i + 1].instance)
my_canvas.coords(segment, x1, y1, x2, y2)
x1, y1, x2, y2 = my_canvas.coords(self.segment[-2].instance)
my_canvas.coords(self.segment[-1].instance, x1 + self.vector[0] * segment_size,
y1 + self.vector[1] * segment_size, x2 + self.vector[0] * segment_size,
y2 + self.vector[1] * segment_size)
def addSegment(self): # Add segment to the snake
last_segment = my_canvas.coords(self.segment[0].instance)
x = last_segment[2] - segment_size
y = last_segment[3] - segment_size
self.segment.insert(0, Segment(x, y))
def changeDirection(self, event): # change the direction of snake
if event.keysym in self.mapping:
self.vector = self.mapping[event.keysym]
# Main--Handling the game process
def main():
global status
global SCORE
global speed
if status:
snake.move()
head = my_canvas.coords(snake.segment[-1].instance)
x1, y1, x2, y2 = head
# Collision check
if x2 > screen_width or x1 < 0 or y1 < 0 or y2 > screen_height:
status = False
elif SCORE == 20:
status = False
# Eating Apples
elif head == my_canvas.coords(apple):
snake.addSegment()
SCORE = SCORE + 1
speed = speed - int(50 / SCORE)
my_canvas.delete(apple)
createApple()
# Eating Itself
else:
for j in range(len(snake.segment) - 1):
if head == my_canvas.coords(snake.segment[j].instance):
status = False
win.after(speed, main)
else:
if SCORE == 20:
my_canvas.create_text(screen_width / 2, screen_height / 2, text="You Win! \n Score: " + str(SCORE),
font="Impact 30", fill="green")
else:
my_canvas.create_text(screen_width / 2, screen_height / 2, text="Game Over! \n Score: " + str(SCORE),
font="Impact 30", fill="red")
restart_button = tk.Button(win, text="Restart")
restart_button['command'] = restart
restart_button.grid(row=20, columnspan=20)
# Setting up window
setUpWindow()
# creating segment and the snake
segment = [Segment(segment_size, segment_size), Segment(segment_size * 2, segment_size),
Segment(segment_size * 3, segment_size)]
snake = Snake(segment)
# key bindings
my_canvas.bind("<KeyPress>", snake.changeDirection)
# catching keypress
my_canvas.focus_set()
createApple()
main()
win.mainloop()