-
Notifications
You must be signed in to change notification settings - Fork 0
/
labirint.py
233 lines (212 loc) · 7.01 KB
/
labirint.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
from pygame import *
from time import sleep
#Main
windowwidth = 1920
windowheight = 1030
red = (255, 0 , 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
windows = display.set_mode((windowwidth, windowheight))
clock = time.Clock()
bg = transform.scale(image.load('bg.jpg'), (windowwidth, windowheight))
display.set_caption('MazeGame')
class GamesSprite(sprite.Sprite):
def __init__(self, x, y, width, height, pic):
sprite.Sprite.__init__(self)
self.image = transform.scale(image.load(pic), (width, height))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def reset(self):
windows.blit(self.image, (self.rect.x, self.rect.y))
class Bullet(GamesSprite):
def __init__(self, x, y, width, height, pic, xspd):
super().__init__(x, y, width, height, pic)
self.xspd = xspd
def update(self):
self.rect.x += self.xspd
if self.rect.x >= windowwidth:
self.kill()
class Player(GamesSprite):
def __init__(self, x, y, width, height, pic, xspd, yspd):
super().__init__(x, y, width, height, pic)
self.xspd = xspd
self.yspd = yspd
def movement(self):
if char.rect.x < windowwidth-75 and char.xspd > 0 or char.rect.x > 0 and char.xspd < 0:
self.rect.x += self.xspd
platformpatch = sprite.spritecollide(self, wallgroup, False)
if self.xspd > 0:
for p in platformpatch:
self.rect.right = min(self.rect.right, p.rect.left)
else:
for p in platformpatch:
self.rect.left = max(self.rect.left, p.rect.right)
if char.rect.y < windowheight-250 and char.yspd > 0 or char.rect.y > 0 and char.yspd < 0:
self.rect.y += self.yspd
platformpatch = sprite.spritecollide(self, wallgroup, False)
if self.yspd < 0:
for p in platformpatch:
self.rect.top = max(self.rect.top, p.rect.bottom)
else:
for p in platformpatch:
self.rect.bottom = min(self.rect.bottom, p.rect.top)
def fire(self):
bullet = Bullet(self.rect.right, self.rect.centery, 45, 15, 'arrow.png', 20)
bullets.add(bullet)
class Enemy(GamesSprite):
direction = 'down'
def __init__(self, x, y, width, height, pic, yspd):
super().__init__(x, y, width, height, pic)
self.yspd = yspd
self.cooldown = 20
def movement(self):
if self.rect.y >= 800:
self.direction = 'up'
elif self.rect.y <= 215:
self.direction = 'down'
if self.direction == 'up':
self.rect.y -= self.yspd
else:
self.rect.y += self.yspd
def fire(self):
bullet = Bullet(self.rect.right, self.rect.centery, 45, 15, 'arrow.png', 10)
enemybullets.add(bullet)
#Characters
char = Player(100, 515, 75, 250, 'steve right.png', 0, 0)
bullets = sprite.Group()
enemybullets = sprite.Group()
enemy1 = Enemy(800, 515, 225, 175, 'Enemy1.png', 0)
enemy2 = Enemy(1200, 215, 225, 175, 'Enemy1.png', 5)
wallgroup = sprite.Group()
enemygroup = sprite.Group()
enemygroup.add(enemy1)
enemygroup.add(enemy2)
#Walls
walls1 = []
walls1a = 3
walls2 = []
walls2a = 3
walls3 = []
walls3a = 3
walls4 = []
walls4a = 3
walls5 = []
walls5a = 3
walls6 = []
walls6a = 3
wallsx = 500
wallsx2 = 700
wallsx3 = 700
wallsx4 = 700
wallsx5 = 1100
wallsx6 = 1000
wallsy = 200
wallsy2 = 600
wallsy3 = 30
wallsy4 = 400
wallsy5 = 100
wallsy6 = 700
#Finish
finishsprite = GamesSprite(1500, 500, 100, 100, 'Diamond.png')
for i in range(walls1a):
wall = GamesSprite(wallsx, wallsy, 100, 100, 'Wall.png')
walls1.append(wall)
wallsy += 100
wallgroup.add(wall)
for i in range(walls2a):
wall = GamesSprite(wallsx2, wallsy2, 100, 100, 'Wall.png')
walls2.append(wall)
wallsy2 -= 100
wallgroup.add(wall)
for i in range(walls3a):
wall = GamesSprite(wallsx3, wallsy3, 100, 100, 'Wall.png')
walls3.append(wall)
wallsx3 += 100
wallgroup.add(wall)
for i in range(walls4a):
wall = GamesSprite(wallsx4, wallsy4, 100, 100, 'Wall.png')
walls4.append(wall)
wallsx4 += 100
wallgroup.add(wall)
for i in range(walls5a):
wall = GamesSprite(wallsx5, wallsy5, 100, 100, 'Wall.png')
walls5.append(wall)
wallsy5 += 100
wallgroup.add(wall)
for i in range(walls6a):
wall = GamesSprite(wallsx6, wallsy6, 100, 100, 'Wall.png')
walls6.append(wall)
wallsy6 += 100
wallgroup.add(wall)
run = True
finish = False
while run:
for e in event.get():
if e.type == QUIT:
run = False
elif e.type == KEYDOWN:
if e.key == K_w:
char.yspd = -5
if e.key == K_s:
char.yspd = 5
if e.key == K_a:
char.xspd = -5
if e.key == K_d:
char.xspd = 5
elif e.type == KEYUP:
if e.key == K_w:
char.yspd = 0
if e.key == K_s:
char.yspd = 0
if e.key == K_a:
char.xspd = 0
if e.key == K_d:
char.xspd = 0
if e.key == K_SPACE:
char.fire()
if finish != True:
windows.blit(bg, (0,0))
# for wall in walls1:
# wall.reset()
# for wall in walls2:
# wall.reset()
# for wall in walls3:
# wall.reset()
# for wall in walls4:
# wall.reset()
# for wall in walls5:
# wall.reset()
# for wall in walls6:
# wall.reset()
enemy1.cooldown -= 1
if enemy1.cooldown < 0:
enemy1.cooldown = 100
enemy1.fire()
wallgroup.draw(windows)
finishsprite.reset()
enemy2.movement()
enemygroup.draw(windows)
char.reset()
char.movement()
bullets.update()
bullets.draw(windows)
enemybullets.update()
enemybullets.draw(windows)
sprite.groupcollide(bullets, wallgroup, True, False)
sprite.groupcollide(bullets, enemygroup, True, True)
if sprite.collide_rect(char, finishsprite):
finish = True
endingscreen = transform.scale(image.load('The End.webp'), (800, 160))
windows.blit(endingscreen, (530, 450))
if sprite.spritecollide(char, enemygroup, False):
finish = True
endingscreen = transform.scale(image.load('You Died.webp'), (windowwidth, windowheight))
windows.blit(endingscreen, (0, 0))
if sprite.spritecollide(char, enemybullets, True):
finish = True
endingscreen = transform.scale(image.load('You Died.webp'), (windowwidth, windowheight))
windows.blit(endingscreen, (0, 0))
clock.tick(60)
display.update()