Repository navigation
Expand file tree
/
Copy pathSnake.py
More file actions
154 lines (131 loc) · 4.03 KB
/
Copy pathSnake.py
File metadata and controls
154 lines (131 loc) · 4.03 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
import random
import time
from tkinter import *
master = Tk()
master.geometry("800x500")
master.resizable(width=0, height=0)
w = Canvas(master)
w.pack()
w.place(x=0, y=0, width=800, height=500)
for i in range(40):
w.create_line(i * 20, 0, i * 20, 500)
w.create_line(0, i * 20, 800, i * 20)
class Snake_Manager:
def __init__(self, x, y, w):
self.w = w
self.snake = []
self.snakecord = []
self.length_of_snake = 1
self.x = x
self.y = y
self.obj = None
self.dir = [0, 0]
self.cool = 3
self.gameloop = True
self.points = 0
def change_dir(self, x, y):
if self.gameloop and (abs(x) != abs(self.dir[0]) or abs(y) != abs(self.dir[1])):
self.dir = [x, y]
def move(self):
if self.w != None:
x = self.dir[0]
y = self.dir[1]
if (
self.x + x * 20 < 0
or self.x + x * 20 > 780
or self.y + y * 20 < 0
or self.y + y * 20 > 480
):
self.lose()
return
self.x += x * 20
self.y += y * 20
for tile in self.snakecord[:-1]:
if tile == (self.x, self.y):
self.lose()
self.remove_tile()
self.add_tile()
def add_tile(self):
if self.length_of_snake > len(self.snake):
self.obj = self.w.create_rectangle(
self.x, self.y, self.x + 20, self.y + 20, fill="green"
)
self.snake.append(self.obj)
self.snakecord.append((self.x, self.y))
def remove_tile(self):
if self.w != None:
self.w.delete(self.snake[0])
del self.snake[0]
del self.snakecord[0]
def update(self):
if self.w != None:
self.add_tile()
if self.cool <= 0:
self.cool = 3
self.move()
else:
self.cool -= 1
if self.x == Apple.x and self.y == Apple.y:
Apple.delete()
Apple.ini()
self.length_of_snake += 1
self.points += 10
def lose(self):
self.gameloop = False
if self.w != None:
self.w.destroy()
self.w = None
l = Label(master, text="LOST!", fg="red", font="Calibri 50", justify="center")
l.place(y=20, width=800)
p = Label(
master,
text="Points: " + str(self.points),
fg="green",
font="Calibri 50",
justify="center",
)
p.place(y=150, width=800)
def pause(self):
self.gameloop = not self.gameloop
class Apple:
x = 0
y = 0
obj = None
def ini():
Apple.x = random.randint(0, 784)
Apple.y = random.randint(0, 484)
Apple.x = round(Apple.x, -1)
Apple.y = round(Apple.y, -1)
if Apple.x % 20 != 0:
Apple.x += 10
if Apple.y % 20 != 0:
Apple.y += 10
Apple.obj = w.create_rectangle(
Apple.x, Apple.y, Apple.x + 20, Apple.y + 20, fill="red"
)
def delete():
w.delete(Apple.obj)
game = True
def on_closing():
global game
game = False
master.quit()
master.destroy()
s = Snake_Manager(100, 100, w)
Apple.ini()
master.protocol("WM_DELETE_WINDOW", on_closing)
master.bind("<space>", lambda ev: s.pause())
master.bind("w", lambda ev: s.change_dir(0, -1), s.update())
master.bind("a", lambda ev: s.change_dir(-1, 0), s.update())
master.bind("s", lambda ev: s.change_dir(0, 1), s.update())
master.bind("d", lambda ev: s.change_dir(1, 0), s.update())
master.bind("<Up>", lambda ev: s.change_dir(0, -1), s.update())
master.bind("<Left>", lambda ev: s.change_dir(-1, 0), s.update())
master.bind("<Down>", lambda ev: s.change_dir(0, 1), s.update())
master.bind("<Right>", lambda ev: s.change_dir(1, 0), s.update())
while game:
master.update()
master.update_idletasks()
if s.gameloop:
s.update()
time.sleep(0.04)