-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
117 lines (98 loc) · 3.26 KB
/
Game.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
import pygame
import sys
import numpy as np
partikel = 25
schlange = [[13,13],[13,14]]
apfelCoords = []
richtung = 0 # 0 up 1 right 2 left 3 down
pygame.init()
clock = pygame.time.Clock()
font = pygame.font.SysFont('arialblack',35)
screen = pygame.display.set_mode([700,700])
def textObjekt(text, font):
textFlaeche = font.render(text, True, (255,255,255))
return textFlaeche, textFlaeche.get_rect()
def zeichner():
screen.fill((0,102,0))
for a in apfelCoords:
Coords = [a[0]*partikel,a[1]*partikel]
pygame.draw.rect(screen, (255,0,0), (Coords[0],Coords[1],partikel,partikel) ,0)
kopf = True
for x in schlange:
Coords = [x[0] * partikel, x[1] * partikel]
if kopf:
pygame.draw.rect(screen, (0,0,0), (Coords[0],Coords[1],partikel,partikel), 0)
kopf = False
else:pygame.draw.rect(screen, (47,79,79), (Coords[0],Coords[1],partikel,partikel), 0)
def apfelCoordGen():
notOK = True
while notOK:
Coord = [np.random.randint(0,28),np.random.randint(0,28)]
change = False
for x in schlange:
if Coord == x:
change = True
for x in apfelCoords:
if Coord == x:
change = True
if change == False:
return Coord
apfelCoords.append(apfelCoordGen())
go = True
anhang = None
apfelInd = -1
ende = False
score = 0
while go:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and richtung != 2:
richtung = 0
if event.key == pygame.K_RIGHT and richtung != 3:
richtung = 1
if event.key == pygame.K_DOWN and richtung != 0:
richtung = 2
if event.key == pygame.K_LEFT and richtung != 1:
richtung = 3
for x in range(1,len(schlange)):
if schlange[0] == schlange [x]:
ende = True
if schlange[0][0] < 0 or schlange [0][0] > 27:
ende=True
if schlange [0][1] < 0 or schlange [0][1] > 27:
ende = True
if anhang != None:
schlange.append(anhang.copy())
anhang = None
apfelCoords.pop(apfelInd)
zahl = len(schlange)-1
for i in range(1,len(schlange)):
schlange[zahl] = schlange[zahl-1].copy()
zahl -= 1
if richtung == 0:
schlange[0][1] -= 1
if richtung == 1:
schlange[0][0] += 1
if richtung == 2:
schlange[0][1] += 1
if richtung == 3:
schlange[0][0] -= 1
for x in range(0,len(apfelCoords)):
if apfelCoords[x] == schlange[0]:
anhang = schlange[-1].copy()
apfelInd = x
score += 10
zufall = np.random.randint(0,100)
if zufall <= 1 and len(apfelCoords) < 4 or len(apfelCoords) == 0:
apfelCoords.append(apfelCoordGen())
if ende == False:
zeichner()
textGrund,textKasten = textObjekt("Score: " + str(score),font)
textKasten.center = ((350,40))
screen.blit(textGrund,textKasten)
pygame.display.update()
else:
print("Du hast " + str(score) + " Punkte ereicht")
sys.exit()
clock.tick(10) #Zeit ( Bewegungs Tempo)