-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.py
executable file
·544 lines (439 loc) · 20.1 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
##################
## Kapitel 6.4 ##
##################
import random
from time import sleep, time
import pygame
def load_image(filename, colorkey=None):
image = pygame.image.load(filename)
if image.get_alpha() is None:
image = image.convert()
else:
image = image.convert_alpha()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image
class Button(pygame.sprite.Sprite):
"""Class used to create a button, use setCords to set
position of topleft corner. Method pressed() returns
a boolean and should be called inside the input loop."""
def __init__(self, image_path, text_str, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = load_image(image_path)
self.rect = self.image.get_rect()
font = pygame.font.Font("fonts/font.ttf", 20)
text = font.render(str(text_str), True, (0, 0, 0))
self.image.blit(text, (int(self.image.get_width() / 2 - text.get_width() / 2),
int(self.image.get_height() / 2 - text.get_height() / 2)))
self.rect.topleft = int(
x - self.image.get_width() / 2), int(y - self.image.get_height() / 2)
def setCords(self, x, y):
self.rect.topleft = int(
x - self.image.get_width() / 2), int(y - self.image.get_height() / 2)
def pressed(self, mouse):
if mouse[0] > self.rect.topleft[0] and mouse[1] > self.rect.topleft[1] and mouse[0] < self.rect.bottomright[0] and mouse[1] < self.rect.bottomright[1]:
return True
else:
return False
def title_screen(screen):
buttons = pygame.sprite.Group()
clock = pygame.time.Clock()
title_image = load_image("gfx/title_screen.png")
classic_snake_button = Button(
"gfx/button1.png", "Classic Snake Game", 400, 300)
two_player_snake_button = Button(
"gfx/button2.png", "Classic Snake with two players", 400, 350)
tron_button = Button("gfx/button1.png", "Tron (two players)", 400, 400)
quit_button = Button("gfx/button2.png", "Quit", 400, 450)
buttons.add(classic_snake_button, two_player_snake_button,
tron_button, quit_button)
running = True
while running:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return "Quit"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return "Quit"
if event.type == pygame.MOUSEBUTTONDOWN:
mouse = pygame.mouse.get_pos()
# Button's pressed method is called
if classic_snake_button.pressed(mouse):
return [3, 5, 1]
if two_player_snake_button.pressed(mouse):
return [3, 5, 2]
if tron_button.pressed(mouse):
return [2, 1000, 2]
if quit_button.pressed(mouse):
return "Quit"
screen.blit(title_image, (0, 0))
buttons.draw(screen)
pygame.display.flip()
def snake(screen, field_width, field_height, tile_size, player_number, items_on, open_border, snake_start_length, speed):
font_gr = pygame.font.Font("fonts/font.ttf", 20)
font_sm = pygame.font.Font("fonts/font.ttf", 16)
font_hu = pygame.font.Font("fonts/font.ttf", 40)
clock = pygame.time.Clock()
field = []
died_1 = 0
died_2 = 0
snake_length_1 = snake_start_length
if player_number > 1:
snake_length_2 = snake_start_length
else:
snake_length_2 = 0
for i in range(0, field_width):
field.append(field_height*[0])
if player_number > 1:
snake_body_1_image = load_image(
'gfx/tron_'+str(tile_size)+'x'+str(tile_size)+'_1.png')
snake_body_2_image = load_image(
'gfx/tron_'+str(tile_size)+'x'+str(tile_size)+'_2.png')
else:
snake_body_1_image = load_image(
'gfx/snake_'+str(tile_size)+'x'+str(tile_size)+'_1.png')
for i in range(0, 40):
field.append(30*[0])
pos_x_1 = random.randint(5, field_width-1-5)
pos_y_1 = random.randint(5, field_height-1-5)
field[pos_x_1][pos_y_1] = snake_length_1
if player_number > 1:
pos_x_2 = random.randint(5, field_width-1-5)
pos_y_2 = random.randint(5, field_height-1-5)
field[pos_x_2][pos_y_2] = 2000 + snake_length_2
direction_1 = 0
direction_2 = 4
temp_direction_1 = 2
temp_direction_2 = 4
highscore_1 = 0
highscore_2 = 0
if items_on:
apple_image = load_image(
'gfx/apple_'+str(tile_size)+'x'+str(tile_size)+'.png')
coin_image = load_image(
'gfx/coin_'+str(tile_size)+'x'+str(tile_size)+'.png')
virus_image = load_image(
'gfx/virus_'+str(tile_size)+'x'+str(tile_size)+'.png')
for i in range(0, 10):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -1
for i in range(0, 20):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -2
for i in range(0, 10):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -3
corona_1 = 0
corona_2 = 0
timer = 0
running = True
while running:
clock.tick(30)
timer = timer + 1
if corona_1 > 0:
corona_1 -= 1
if corona_2 > 0:
corona_2 -= 1
if timer == speed:
timer = 0
if direction_1 == 1:
# Wenn ich am Bildrand bin ...
if pos_y_1 == 0:
# Wenn die Grenze offen ist ...
if open_border:
pos_y_1 = field_height-1
# Wenn die Grenze geschlossen ist ...
else:
running = False
# Wenn ich nicht am Bildrand bin ...
else:
pos_y_1 = pos_y_1 - 1
# Anpassung an die Speicherung der Schlangenlänge
# Die Schlange bewegt sich nicht mehr hier auf das neue Feld, sondern später.
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 2
if direction_1 == 0:
if pos_x_1 == field_width-1:
if open_border:
pos_x_1 = 0
else:
running = False
else:
pos_x_1 = pos_x_1 + 1
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 3
if direction_1 == 3:
if pos_y_1 == field_height-1:
if open_border:
pos_y_1 = 0
else:
running = False
else:
pos_y_1 = pos_y_1 + 1
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 4
if direction_1 == 4:
if pos_x_1 == 0:
if open_border:
pos_x_1 = field_width-1
else:
running = False
else:
pos_x_1 = pos_x_1 - 1
if field[pos_x_1][pos_y_1] in range(2, 1999) or field[pos_x_1][pos_y_1] in range(2002, 3999):
running = False
# Falls mehr als ein Spieler spielen möchte, wird die Bewegung von Schlange 2 berechnet und überwacht.
if running == False:
died_1 = 1
if player_number > 1:
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 4
# für Spieler 2
if direction_2 == 1:
if pos_y_2 == 0:
if open_border:
pos_y_2 = field_height-1
else:
running = False
else:
pos_y_2 = pos_y_2 - 1
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 2
# für Spieler 2
if direction_2 == 0:
if pos_x_2 == field_width-1:
if open_border:
pos_x_2 = 0
else:
running = False
else:
pos_x_2 = pos_x_2 + 1
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 3
# für Spieler 2
if direction_2 == 3:
if pos_y_2 == field_height-1:
if open_border:
pos_y_2 = 0
else:
running = False
else:
pos_y_2 = pos_y_2 + 1
# Code analog zu Spieler 1 und Richtung 1 mit Anpassung an Richtung 4
# für Spieler 2
if direction_2 == 4:
if pos_x_2 == 0:
if open_border:
pos_x_2 = field_width-1
else:
running = False
else:
pos_x_2 = pos_x_2 - 1
if field[pos_x_2][pos_y_2] in range(2, 1999) or field[pos_x_2][pos_y_2] in range(2002, 3999):
running = False
if running == False and died_1 == 0:
died_2 = 1
# Die Variable running wäre False, wenn die Schlange sich selbst oder die andere gefressen
# oder mit einer geschlossenen Wand kollidiert wäre. Wenn running True ist, bewegt sich die
# Schlange nun auf das neue Schlangenkopffeld vor. Außerdem finden hier Anpassungen an die
# nun variable Schlangenlänge statt.
if running:
if field[pos_x_1][pos_y_1] == -1:
highscore_1 += 500
snake_length_1 += 5
for x in range(0, field_width):
for y in range(0, field_height):
if field[x][y]in range(1, 1990):
field[x][y] += 5
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -1
if field[pos_x_1][pos_y_1] == -2:
highscore_1 += 1000
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -2
if field[pos_x_1][pos_y_1] == -3:
corona_1 += 30
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -3
field[pos_x_1][pos_y_1] = snake_length_1 + 1
# Wenn es mehr als einen Spieler gibt, bewegt sich natürlich auch Schlange zwei.
if player_number > 1:
if field[pos_x_2][pos_y_2] == -1:
highscore_2 += 500
snake_length_2 += 5
for x in range(0, field_width):
for y in range(0, field_height):
if field[x][y]in range(2001, 3990):
field[x][y] += 5
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -1
if field[pos_x_2][pos_y_2] == -2:
highscore_2 += 1000
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
i += 1
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = - 2
if field[pos_x_2][pos_y_2] == -3:
corona_2 += 30
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
while (field[pos_x][pos_y] != 0):
pos_x = random.randint(0, field_width-1)
pos_y = random.randint(0, field_height-1)
field[pos_x][pos_y] = -3
field[pos_x_2][pos_y_2] = 2000 + snake_length_2 + 1
elif running != True:
print("Died.")
for x in range(0, field_width):
for y in range(0, field_height):
if field[x][y] in range(1, 1999) or field[x][y] in range(2001, 3999):
field[x][y] = field[x][y] - 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return
if corona_1 == 0:
if event.key == pygame.K_UP:
temp_direction_1 = 1
if event.key == pygame.K_DOWN:
temp_direction_1 = 3
if event.key == pygame.K_LEFT:
temp_direction_1 = 4
if event.key == pygame.K_RIGHT:
temp_direction_1 = 0
if 4-direction_1 != temp_direction_1:
direction_1 = temp_direction_1
if corona_2 == 0:
if event.key == pygame.K_w:
temp_direction_2 = 1
if event.key == pygame.K_s:
temp_direction_2 = 3
if event.key == pygame.K_a:
temp_direction_2 = 4
if event.key == pygame.K_d:
temp_direction_2 = 0
if 4-direction_2 != temp_direction_2:
direction_2 = temp_direction_2
screen.fill((153, 204, 255))
for x in range(0, field_width):
for y in range(0, field_height):
if field[x][y] in range(1, 1999):
screen.blit(snake_body_1_image, (tile_size*x, tile_size*y))
if field[x][y] in range(2001, 3999):
screen.blit(snake_body_2_image, (tile_size*x, tile_size*y))
if field[x][y] == -1:
screen.blit(apple_image, (tile_size*x, tile_size*y))
if field[x][y] == -2:
screen.blit(coin_image, (tile_size * x, tile_size * y))
if field[x][y] == -3:
screen.blit(virus_image, (tile_size * x, tile_size * y))
pygame.draw.rect(screen, (127, 127, 127), (0, tile_size *
field_height, tile_size * field_width, 2))
text_str = 'Score: ' + str(highscore_1)
text = font_gr.render(text_str, True, (255, 10, 10))
screen.blit(text, (10, tile_size * field_height + 2))
text_str = 'Länge: ' + str(snake_length_1)
text = font_sm.render(text_str, True, (255, 10, 10))
screen.blit(text, (int((tile_size * field_width) / 2) -
10 - text.get_width(), tile_size * field_height + 2))
if player_number > 1:
text_str = 'Score: ' + str(highscore_2)
text = font_gr.render(text_str, True, (10, 10, 255))
screen.blit(text, (tile_size * field_width - 10 -
text.get_width(), tile_size * field_height + 2))
text_str = 'Länge: ' + str(snake_length_2)
text = font_sm.render(text_str, True, (10, 10, 255))
screen.blit(text, (int((tile_size * field_width) / 2) +
10, tile_size * field_height + 2))
pygame.display.flip()
if player_number > 1:
if died_1 == 1:
text_str = "Snake yellow died, Snake blue Wins!"
if died_2 == 1:
text_str = "Snake blue died, Snake yellow wins!"
text_str_points_1 = f"Snake blue: {highscore_2} points"
text_str_points_2 = f"Snake yellow: {highscore_1} points"
else:
text_str = "You died!"
text_str_points_1 = f"You scored {highscore_1} points!"
running = True
i = 1
while running:
if i == 1:
screen.fill((0, 0, 0))
text = font_hu.render(text_str, True, (255, 255, 255))
text_2 = font_hu.render(text_str_points_1, True, (255, 255, 255))
if player_number > 1:
text_3 = font_hu.render(
text_str_points_2, True, (255, 255, 255))
if i == 5:
screen.fill((255, 255, 255))
text = font_hu.render(text_str, True, (0, 0, 0))
text_2 = font_hu.render(text_str_points_1, True, (0, 0, 0))
if player_number > 1:
text_3 = font_hu.render(text_str_points_2, True, (0, 0, 0))
screen.blit(text, (int((tile_size * field_width) / 2) - int(text.get_width() / 2),
(int((tile_size * field_height) / 2) - int(text.get_height() / 2))))
screen.blit(text_2, (int((tile_size * field_width) / 2) - int(text_2.get_width() / 2),
(int((tile_size * field_height) / 2) - int(text_2.get_height() / 2) + 50)))
if player_number > 1:
screen.blit(text_3, (int((tile_size * field_width) / 2) - int(text_3.get_width() / 2),
(int((tile_size * field_height) / 2) - int(text_3.get_height() / 2) + 100)))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
running = False
if i <= 7:
i += 1
else:
i = 1
sleep(0.2)
pygame.quit()
def main():
screen_width = 800
screen_height = 600
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_icon(pygame.image.load("gfx/python_logo_20x20.png"))
pygame.display.set_caption("Snäik bai Lili mit einem 'l' und Emil")
pygame.mouse.set_visible(1)
pygame.key.set_repeat(0, 30)
args = title_screen(screen)
pygame.mouse.set_visible(0)
if not args == "Quit":
snake(screen, 40, 28, 20, int(args[2]),
True, True, int(args[1]), int(args[0]))
if __name__ == '__main__':
main()