-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snake.py
332 lines (324 loc) · 11.7 KB
/
Snake.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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import sys, random, time
from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import *
n = 10
p = 40
class Snake(QWidget):
def __init__(self, n, p):
super().__init__()
self.high_skore = 0
self.Setup(n, p)
def Setup(self, n, p):
self.snake_x = 4
self.snake_y = 4
self.level = 0
self.snake_length = self.level + 2
self.mapa_0()
self.pole[self.snake_y][self.snake_x] = 1
self.n = n
self.p = p
self.Jidlo()
self.current_state = "R"
self.next_state = "R"
self.rychlost = 200
self.timer1 = QTimer()
self.timer1.timeout.connect(self.HniSe)
self.timer1.start(self.rychlost)
self.skore = 0
self.bonus = 0
self.grid_x = 0
self.grid_y = 0
self.prohra = 0
self.koeficient_rychlosti = 0.1
self.pozastaveni = 0
def center_the_screen(self):
AG = QDesktopWidget().availableGeometry()
okno = self.geometry()
okno_x = (AG.width() - okno.width())/2
okno_y = (AG.height() - okno.height())/2
self.move(okno_x, okno_y)
def Jidlo(self):
self.food_x = random.randint(0, self.n - 1)
self.food_y = random.randint(0, self.n - 1)
while self.pole[self.food_y][self.food_x] != 0:
self.food_x = (random.randint(0, self.n - 1))
self.food_y = (random.randint(0, self.n - 1))
continue
self.pole[self.food_y][self.food_x] = -1
def paintEvent(self, event):
painter = QPainter(self)
if self.prohra == 0:
self.grid_y = 0
for i in self.pole:
self.grid_x = 0
for j in i:
if j == -2:
painter.setBrush(QColor(0, 0, 0))
painter.drawRect(self.p * self.grid_x, self.p * self.grid_y, self.p, self.p)
if j == -1:
painter.setBrush(QColor(255, 0, 0))
painter.drawRect(self.p * self.grid_x, self.p * self.grid_y, self.p, self.p)
if j == 1:
painter.setBrush(QColor(0, 0, 255))
painter.drawRect(self.p*self.grid_x, self.p*self.grid_y, self.p, self.p)
if j > 1:
painter.setBrush(QColor(0, 200, 255))
painter.drawRect(self.p*self.grid_x, self.p*self.grid_y, self.p, self.p)
self.grid_x += 1
self.grid_y += 1
painter.drawText(5, (self.n*self.p + 15), "SKÓRE: " + str(self.skore) + " ÚROVEŇ: " + str(self.level))
elif self.prohra == 1:
self.timer1.stop()
if self.skore > self.high_skore:
self.high_skore = self.skore
painter.setFont(QFont("Default", self.p*self.n/10))
painter.drawText(event.rect(), Qt.AlignCenter, "KONEC HRY!\nSKÓRE: "+str(self.skore))
painter.setFont(QFont("Default", self.p*self.n/20))
painter.drawText(event.rect(), Qt.AlignTop, "Nejvyšší skóre: " + str(self.high_skore))
painter.drawText(event.rect(), Qt.AlignBottom, "Hrát znovu? [Y]es/[N]o")
def keyPressEvent(self, m):
if self.prohra == 0:
if m.key() == Qt.Key_D or m.key() == Qt.Key_Right:
self.next_state = "R"
if m.key() == Qt.Key_A or m.key() == Qt.Key_Left:
self.next_state = "L"
if m.key() == Qt.Key_W or m.key() == Qt.Key_Up:
self.next_state = "U"
if m.key() == Qt.Key_S or m.key() == Qt.Key_Down:
self.next_state = "D"
if self.prohra == 1:
if m.key() == Qt.Key_Y:
self.Setup(n, p)
okno.resize((self.n * self.p), (self.n * self.p + 20))
okno.center_the_screen()
if m.key() == Qt.Key_N:
self.close()
app.close()
if m.key() == Qt.Key_Escape:
self.close()
app.close()
def HniSe(self):
if self.pozastaveni == 1:
time.sleep(1.5)
self.pozastaveni = 0
self.current_state = self.next_state
self.grid_y = 0
for i in self.pole:
self.grid_x = 0
for j in i:
if j >= 1:
self.pole[self.grid_y][self.grid_x] += 1
if j > self.snake_length:
self.pole[self.grid_y][self.grid_x] = 0
self.grid_x += 1
self.grid_y += 1
if self.current_state == "R":
if self.snake_x == self.n - 1 or self.pole[self.snake_y][self.snake_x + 1] > 1 or self.pole[self.snake_y][self.snake_x + 1] == -2:
self.prohra = 1
else:
self.snake_x += 1
self.pole[self.snake_y][self.snake_x] = 1
self.update()
if self.current_state == "L":
if self.snake_x == 0 or self.pole[self.snake_y][self.snake_x - 1] > 1 or self.pole[self.snake_y][self.snake_x - 1] == -2:
self.prohra = 1
else:
self.snake_x -= 1
self.pole[self.snake_y][self.snake_x] = 1
self.update()
if self.current_state == "U":
if self.snake_y == 0 or self.pole[self.snake_y - 1][self.snake_x] > 1 or self.pole[self.snake_y - 1][self.snake_x] == -2:
self.prohra = 1
else:
self.snake_y -= 1
self.pole[self.snake_y][self.snake_x] = 1
self.update()
if self.current_state == "D":
if self.snake_y == self.n-1 or self.pole[self.snake_y + 1][self.snake_x] > 1 or self.pole[self.snake_y + 1][self.snake_x] == -2:
self.prohra = 1
else:
self.snake_y += 1
self.pole[self.snake_y][self.snake_x] = 1
self.update()
if self.snake_x == self.food_x and self.snake_y == self.food_y:
self.timer1.stop()
self.level += 1
self.snake_length += 1
self.skore += 10 + self.bonus
self.rychlost -= (self.koeficient_rychlosti * self.rychlost)
if self.level == 5:
self.mapa_5()
self.bonus = self.level
if self.level == 10:
self.mapa_10()
self.bonus = self.level
if self.level == 15:
self.mapa_15()
self.bonus = self.level
if self.level == 20:
self.mapa_20()
self.bonus = self.level
if self.level == 25:
self.mapa_25()
self.bonus = self.level
if self.level == 30:
self.mapa_30()
self.bonus = self.level
if self.level == 35:
self.mapa_35()
self.bonus = self.level
okno.resize((self.n * self.p), (self.n * self.p + 20))
okno.center_the_screen()
if self.level % 5 == 0 and self.level < 36:
self.next_state = "R"
self.pozastaveni = 1
self.Jidlo()
self.timer1 = QTimer()
self.timer1.timeout.connect(self.HniSe)
self.timer1.start(self.rychlost)
def mapa_0(self):
self.pole = []
for i in range(0, n):
self.pole.append(n * [0])
self.snake_x = 4
self.snake_y = 4
def mapa_5(self):
self.n = 15
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in range(5, 10):
for j in range(5, 10):
self.pole[i][j] = -2
self.snake_x = 3
self.snake_y = 3
self.rychlost = 200
def mapa_10(self):
self.n = 15
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in range(4, 11):
self.pole[2][i] = -2
for i in range (4, 11):
self.pole[12][i] = -2
for i in range(4,11):
self.pole[i][2] = -2
for i in range(4,11):
self.pole[i][12] = -2
self.pole[7][7] = -2
self.snake_x = 6
self.snake_y = 6
self.rychlost = 220
def mapa_15(self):
self.n = 15
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in (2, 5):
for j in range(2, 6):
self.pole[i][j] = -2
for i in (9, 12):
for j in range(9, 13):
self.pole[i][j] = -2
for i in (2, 5):
for j in range(9, 13):
self.pole[j][i] = -2
for i in(9, 12):
for j in range(2, 6):
self.pole[j][i] = -2
self.snake_x = 7
self.snake_y = 7
self.rychlost = 220
def mapa_20(self):
self.n = 15
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in range (1, 14, 2):
for j in range(1, 14, 2):
self.pole[i][j] = -2
self.snake_x = 6
self.snake_y = 6
self.rychlost = 240
def mapa_25(self):
self.n = 20
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in (1, 2, 5, 6, 7, 8, 9):
self.pole[1][i] = -2
for i in (1, 2, 12, 13, 14, 15):
self.pole [2][i] = -2
for i in (4, 5, 9, 10):
self.pole [4][i] = -2
for i in (4, 10, 12):
self.pole[5][i] = -2
for i in (4, 10):
self.pole[9][i] = -2
for i in (4, 5, 9, 10):
self.pole[10][i] = -2
self.pole[12][5] = -2
for i in range(5, 10):
self.pole[i][1] = -2
for i in range(12, 16):
self.pole[i][2] = -2
for i in range (3, 6):
self.pole[15][i] = -2
for i in (12, 13, 16, 18):
self.pole[16][i] = -2
for i in range(0, 12):
self.pole[17][i] = -2
for i in range(8, 13):
self.pole[13][i] = -2
for i in range(8, 13):
self.pole[i][13] = -2
for i in range(3, 6):
self.pole[i][15] = -2
for i in range(0, 12):
self.pole[i][17] = -2
for i in (12, 13, 18):
self.pole[i][16] = -2
self.pole[18][18] = -2
self.pole[14][14] = -2
self.snake_x = 7
self.snake_y = 7
self.rychlost = 250
def mapa_30(self):
self.n = 19
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in (7, 8, 10, 11):
for j in (7, 8, 10, 11):
self.pole[j][i] = -2
for i in (2, 3, 4, 5, 6, 12, 13, 14, 15, 16):
for j in (2, 3, 4, 5, 6, 12, 13, 14, 15, 16):
self.pole[j][i] = -2
self.snake_x = 9
self.snake_y = 9
self.rychlost = 220
def mapa_35(self):
self.n = 23
self.pole = []
for i in range(0, self.n):
self.pole.append(self.n * [0])
for i in range(10, 13):
for j in range(10, 13):
self.pole[j][i] = -2
for i in (3, 4, 5, 6, 16, 17, 18, 19):
for j in (3, 4, 5, 6, 16, 17, 18, 19):
self.pole[j][i] = -2
self.snake_x = 8
self.snake_y = 8
self.koeficient_rychlosti = 0.05
self.rychlost = 230
if __name__ == "__main__":
app = QApplication(sys.argv)
okno = Snake(n, p)
okno.resize((n * p), (n * p + 20))
okno.center_the_screen()
okno.setWindowTitle("Snake")
okno.show()
sys.exit(app.exec_())