-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
347 lines (284 loc) · 12.6 KB
/
main.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
import pygame
pygame.init()
window_width, window_height = 1280, 720
window = pygame.display.set_caption('Gra')
window = pygame.display.set_mode((window_width, window_height),pygame.FULLSCREEN)
# Tekstury
icon = pygame.image.load('Textures/icon.ico').convert_alpha()
bgmenu = pygame.image.load('Textures/menubg.png').convert_alpha()
playbutton = pygame.image.load('Textures/playbutton.png').convert_alpha()
playbuttonselect = pygame.image.load('Textures/playbuttonselect.png').convert_alpha()
settingsbutton = pygame.image.load('Textures/settingsbutton.png').convert_alpha()
settingsbuttonselect = pygame.image.load('Textures/settingsbuttonselect.png').convert_alpha()
menubutton = pygame.image.load('Textures/menubutton.png').convert_alpha()
menubuttonselect = pygame.image.load('Textures/menubuttonselect.png').convert_alpha()
GameName = pygame.image.load('Textures/GameName.png').convert_alpha()
platform_texture = pygame.image.load('Textures/platform_texture.png').convert_alpha()
lava_texture = pygame.image.load('Textures/lava_texture.png').convert_alpha()
pygame.display.set_icon(icon)
level = 1
pygame.mixer.init()
pygame.mixer.music.load("Audio/loopmusic.mp3")
pygame.mixer.music.play(-1, 0.0)
class Physic:
def __init__(self, x, y, szerokosc, wysokosc, przyspieszenie, MAX_SPEED):
self.level = level
self.x_cord = x
self.y_cord = y
self.predkosc_poziomu = 0
self.predkosc_pionu = 0
self.przyspieszenie = przyspieszenie
self.MAX_SPEED = MAX_SPEED
self.szerokosc = szerokosc
self.wysokosc = wysokosc
self.poprzednie_x = x
self.poprzednie_y = y
self.jumping = False
self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.szerokosc, self.wysokosc)
def physic_tick(self, sciany, lawy):
self.predkosc_pionu += 0.4
self.x_cord += self.predkosc_poziomu
self.y_cord += self.predkosc_pionu
self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.szerokosc, self.wysokosc)
for sciana in sciany:
if sciana.hitbox.colliderect(self.hitbox):
if self.x_cord + self.szerokosc >= sciana.x_cord + 1 > self.poprzednie_x + self.szerokosc: # z lewej
self.x_cord = self.poprzednie_x
self.predkosc_poziomu = 0
if self.x_cord <= sciana.x_cord + sciana.szerokosc - 1 < self.poprzednie_x: # z prawej
self.x_cord = self.poprzednie_x
self.predkosc_poziomu = 0
if self.y_cord + self.wysokosc >= sciana.y_cord + 1 > self.poprzednie_y + self.wysokosc: # z góry
self.y_cord = self.poprzednie_y
self.predkosc_pionu = 0
self.jumping = False
if self.y_cord >= sciana.y_cord < self.poprzednie_y: # z dołu
self.y_cord = self.poprzednie_y
self.predkosc_pionu = 0
for lawa in lawy:
if lawa.hitbox.colliderect(self.hitbox):
pygame.mixer.Channel(1).play(pygame.mixer.Sound("Audio/dead.mp3"))
self.x_cord = 1
self.y_cord = 640
self.poprzednie_x = 1
self.poprzednie_y = 655
if self.y_cord > 1000:
self.x_cord = 1
self.y_cord = 655
self.poprzednie_x = 1
self.poprzednie_y = 655
pygame.mixer.Channel(1).play(pygame.mixer.Sound("Audio/dead.mp3"))
if self.x_cord >= 1275:
self.level += 1
self.x_cord = 1
self.y_cord = 655
self.poprzednie_x = 1
self.poprzednie_y = 655
if self.x_cord < 0:
self.x_cord = 1
self.poprzednie_x = self.x_cord
self.poprzednie_y = self.y_cord
class Player(Physic):
def __init__(self):
self.image = pygame.image.load("Textures/postac1.png").convert_alpha()
self.Sterowanie = 'wsad'
pygame.mixer.init()
szerokosc = self.image.get_width()
wysokosc = self.image.get_height()
super().__init__(1, 655, szerokosc, wysokosc, 0.125, 3)
def tick(self, keys, sciany, lawy):
self.physic_tick(sciany, lawy)
if keys[pygame.K_w] or keys[pygame.K_a] or keys[pygame.K_d]:
self.Sterowanie = 0
if keys[pygame.K_UP] or keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]:
self.Sterowanie = 1
if self.Sterowanie == 0:
if keys[pygame.K_a] and self.predkosc_poziomu > self.MAX_SPEED * -1:
self.image = pygame.image.load("Textures/postac2.png").convert_alpha()
self.predkosc_poziomu -= self.przyspieszenie
if keys[pygame.K_d] and self.predkosc_poziomu < self.MAX_SPEED:
self.image = pygame.image.load("Textures/postac1.png").convert_alpha()
self.predkosc_poziomu += self.przyspieszenie
if keys[pygame.K_w] and self.jumping is False:
self.predkosc_pionu -= 10
self.jumping = True
if not (keys[pygame.K_d] or keys[pygame.K_a]):
if self.predkosc_poziomu > 0:
self.predkosc_poziomu -= self.przyspieszenie
elif self.predkosc_poziomu < 0:
self.predkosc_poziomu += self.przyspieszenie
if self.Sterowanie == 1:
if keys[pygame.K_LEFT] and self.predkosc_poziomu > self.MAX_SPEED * -1:
self.image = pygame.image.load("Textures/postac2.png").convert_alpha()
self.predkosc_poziomu -= self.przyspieszenie
if keys[pygame.K_RIGHT] and self.predkosc_poziomu < self.MAX_SPEED:
self.image = pygame.image.load("Textures/postac1.png").convert_alpha()
self.predkosc_poziomu += self.przyspieszenie
if keys[pygame.K_UP] and self.jumping is False:
self.predkosc_pionu -= 10
self.jumping = True
if not (keys[pygame.K_RIGHT] or keys[pygame.K_LEFT]):
if self.predkosc_poziomu > 0:
self.predkosc_poziomu -= self.przyspieszenie
elif self.predkosc_poziomu < 0:
self.predkosc_poziomu += self.przyspieszenie
def draw(self):
window.blit(self.image, (self.x_cord, self.y_cord))
pygame.mixer.init()
class Sciana:
def __init__(self, x, y, szerokosc, wysokosc):
self.x_cord = x
self.y_cord = y
self.szerokosc = szerokosc
self.wysokosc = wysokosc
self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.szerokosc, self.wysokosc)
def draw(self, win):
for x in range(self.x_cord, self.x_cord + self.szerokosc, 25):
for y in range(self.y_cord, self.y_cord + self.wysokosc, 25):
win.blit(platform_texture, (x, y))
class Lawa:
def __init__(self, x, y, szerokosc, wysokosc):
self.x_cord = x
self.y_cord = y
self.szerokosc = szerokosc
self.wysokosc = wysokosc
self.hitbox = pygame.Rect(self.x_cord, self.y_cord, self.szerokosc, self.wysokosc)
def draw(self, win):
for x in range(self.x_cord, self.x_cord + self.szerokosc, 50):
for y in range(self.y_cord, self.y_cord + self.wysokosc, 50):
win.blit(lava_texture, (x, y))
def main():
gamestart = False
run = True
player = Player()
clock = 0
czcionka = pygame.font.SysFont('Death Spirit', 40)
background = pygame.image.load("Textures/Tlo.png").convert_alpha()
sciany = [
Sciana(0, 695, 620, 25),
Sciana(600, 620, 25, 100),
Sciana(800, 695, 620, 25),
]
lawy = [
Lawa(270, 695, 70, 25),
]
while run:
leveltext = czcionka.render(' Level: ' + (str(player.level)), False, (255, 255, 255))
clock += pygame.time.Clock().tick(130) * 100
window.blit(background, (0, 0))
window.blit(leveltext, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
mouseposx = pygame.mouse.get_pos()[0]
mouseposy = pygame.mouse.get_pos()[1]
# MENU
if gamestart == False:
window.blit(bgmenu, (0, 0))
window.blit(GameName, (240, 25))
# PLAY BUTTON
if mouseposx >= 530 and mouseposx <= 730 and mouseposy >= 270 and mouseposy <= 370:
window.blit(playbuttonselect, (530, 265))
if pygame.mouse.get_pressed()[0]:
gamestart = True
pygame.mixer.Channel(1).play(pygame.mixer.Sound("Audio/clickbutton.wav"))
else:
window.blit(playbutton, (530, 265))
# SETTINGS BUTTON
if mouseposx >= 530 and mouseposx <= 730 and mouseposy >= 475 and mouseposy <= 675:
window.blit(settingsbuttonselect, (530, 475))
if pygame.mouse.get_pressed()[0]:
pygame.mixer.Channel(1).play(pygame.mixer.Sound("Audio/clickbutton.wav"))
else:
window.blit(settingsbutton, (530, 475))
# GAME
if gamestart == True:
keys = pygame.key.get_pressed()
player.tick(keys, sciany, lawy)
player.draw()
for sciana in sciany:
sciana.draw(window)
for lawa in lawy:
lawa.draw(window)
# MENU BUTTON
if mouseposx >= 1200 and mouseposx <= 1280 and mouseposy >= 0 and mouseposy <= 70:
window.blit(menubuttonselect, (1200, 5))
if pygame.mouse.get_pressed()[0]:
pygame.mixer.Channel(1).play(pygame.mixer.Sound("Audio/clickbutton.wav"))
gamestart = False
else:
window.blit(menubutton, (1200, 5))
# poziomy:
if player.level == 2:
sciany = [
Sciana(0, 695, 620, 25),
Sciana(50, 490, 25, 100),
Sciana(600, 600, 25, 200),
Sciana(50, 570, 470, 25),
Sciana(200, 410, 470, 25),
Sciana(800, 410, 670, 25),
]
lawy = [
Lawa(625, 695, 800, 25),
]
if player.level == 3:
sciany = [
Sciana(0, 695, 620, 25),
Sciana(0, 420, 870, 25),
Sciana(0, 320, 270, 25),
Sciana(270, 220, 1270, 25),
Sciana(600, 620, 25, 100),
Sciana(750, 620, 25, 100),
Sciana(900, 620, 25, 100),
Sciana(1050, 620, 320, 25),
Sciana(1255, 245, 25, 375),
Sciana(1150, 520, 220, 25),
Sciana(1000, 420, 150, 25),
]
lawy = [
]
if player.level == 4:
sciany = [
Sciana(0, 695, 620, 25),
Sciana(600, 620, 25, 100),
Sciana(800, 220, 25, 500),
Sciana(0, 520, 500, 25),
Sciana(400, 410, 200, 25),
Sciana(0, 445, 25, 75),
Sciana(0, 300, 500, 25),
Sciana(400, 200, 425, 25),
Sciana(950, 195, 25, 525),
Sciana(1100, 195, 25, 525),
Sciana(1100, 195, 500, 25),
Sciana(600, 205, 25, 230),
]
lawy = [
Lawa(625, 695, 175, 25),
Lawa(825, 695, 125, 25),
Lawa(975, 695, 125, 25),
]
if player.level == 5:
sciany = [
Sciana(0, 700, 225, 25),
Sciana(225, 625, 25, 100),
Sciana(0, 575, 125, 25),
Sciana(0, 500, 25, 100),
Sciana(100, 400, 25, 75),
Sciana(100, 400, 75, 25),
Sciana(250, 475, 75, 25),
Sciana(300, 425, 25, 75),
Sciana(400, 350, 25, 75),
Sciana(400, 350, 75, 25),
Sciana(550, 350, 75, 25),
Sciana(700, 350, 75, 25),
Sciana(750, 300, 25, 75),
Sciana(775, 700, 125, 25),
Sciana(875, 625, 25, 125),
Sciana(1025, 625, 150, 25),
Sciana(1150, 550, 25, 100),
Sciana(1150, 550, 150, 25),
]
lawy = []
pygame.display.update()
main()
pygame.quit()