-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.pyw
More file actions
148 lines (129 loc) · 3.94 KB
/
Copy pathsnake.pyw
File metadata and controls
148 lines (129 loc) · 3.94 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
from tkinter import *
from random import (choice)
SIZE = 20
KEY = str("")
EX, EY = 0, 0
DELAY = 100
SNAKE = []
COORD = []
class Jogo:
def __init__(self):
self.window = Tk()
self.window.geometry("500x500+400+100")
self.window.bind("<KeyPress>", self.keypress)
self.map()
def map(self):
self.mapa = Frame(
self.window, width=500, height=500, bg="teal"
)
self.mapa.pack(expand=True)
self.snake()
self.place()
self.food()
self.loop()
def keypress(self, key):
global KEY
KEY = key.keysym
def move(self):
global EX, EY
if KEY == "Right":
EX = EX + SIZE
elif KEY == "Left":
EX = EX - SIZE
elif KEY == "Up":
EY = EY - SIZE
elif KEY == "Down":
EY = EY + SIZE
self.cobra.place(x=EX, y=EY)
def snake(self):
self.cobra = Frame(
self.mapa, width=SIZE, height=SIZE, bg="blue"
)
self.cobra.place(x=0, y=0)
def place(self):
lis = []
for k in range(0, 500 - SIZE, SIZE):
lis.append(k)
self.x = choice(lis)
self.y = choice(lis)
def food(self):
cor = [
"white", "gray", "black",
"pink", "blue", "purple",
"red", "violet", "yellow",
"yellow", "orange", "pink",
"#9900ff", "#cccc00", "#33bbff",
"#4d4d00", "#ff80b3", "#77b300"
]
self.fruit = Frame(
self.mapa, width=SIZE, height=SIZE, bg=choice(cor)
)
self.fruit.place(x=self.x, y=self.y)
def collision(self):
if EX < 0 or EX > 500 - SIZE:
return "saiu"
if EY < 0 or EY > 500 - SIZE:
return "saiu"
if EX == self.x and EY == self.y:
return "comeu"
tup = tuple((EX, EY))
if tup in COORD:
return "morreu"
def game_over(self):
self.texto = Label(
self.mapa, fg="red", text="Fim de Jogo",
font=("Arial", 50, "bold"), bg="teal"
)
self.pontos = Label(
self.mapa, fg="blue", text="Pontos: " +
str(len(SNAKE) * 5) + "\nTamanho: " + str(len(SNAKE)),
font=("Arial", 30, "bold"), bg="teal",
)
self.bt = Button(
self.mapa, text="Jogar Novamente", relief="flat",
cursor="exchange", font=("Arial", 20, "bold"),
fg="blue", bg="green", command=self.clear
)
self.texto.place(relx=0.1, rely=0.1)
self.pontos.place(relx=0.3, rely=0.3)
self.bt.place(relx=0.25, rely=0.5)
def clear(self):
global EX, EY, SNAKE, COORD, KEY
EX, EY, KEY = 0, 0, ""
SNAKE, COORD = [], []
for k in [
self.mapa, self.fruit,
self.texto, self.pontos,
self.cobra
]:
k.destroy()
self.map()
return
def loop(self):
global SNAKE, COORD
colisao = self.collision()
if colisao == "saiu":
self.game_over()
return
if colisao == "comeu":
self.fruit.destroy()
self.place()
self.food()
SNAKE.append(Frame(self.mapa, width=SIZE, height=SIZE, bg=str(self.fruit['bg'])))
if colisao == "morreu":
self.game_over()
return
COORD.append((EX, EY))
if len(COORD) > len(SNAKE):
del(COORD[0])
diminu = len(SNAKE)
for self.k in SNAKE:
self.k.place(x=COORD[-diminu][0], y=COORD[-diminu][1])
diminu = diminu - 1
self.move()
self.window.after(DELAY, self.loop)
def run(self):
self.window.mainloop()
if __name__ == "__main__":
snake = Jogo()
snake.run()