-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
312 lines (261 loc) · 7.88 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
import pygame
from pygame.locals import *
import sys
import random
import math
pygame.init()
vec = pygame.math.Vector2 # 2 for two dimensional
HEIGHT = 380
WIDTH = 500
FPS = 60
GRAV = 0.7
SIDE = 32
FramePerSec = pygame.time.Clock()
displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.sprite = "stand"
self.state = "small"
self.surf = pygame.image.load("Sprites/" + self.sprite + "_" + self.state + ".png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect()
self.pos = vec((10, HEIGHT - 104))
self.vel = vec(0,0)
self.acc = vec(0,0)
self.facing = 1
self.scroll = False
self.running = False
self.moving = False
self.collisions = []
self.jumpable = False
self.movingFrames = 0
self.inAir = True
self.dying = False
self.crouching = False
def walk(self):
self.acc.x = self.facing * 0.4
if self.facing == -1 and self.vel.x < 0:
self.scroll = False
self.moving = True
def run(self):
self.acc.x = self.facing * 0.4
if self.facing == -1 and self.vel.x < 0:
self.scroll = False
self.running = True
self.moving = True
def checkCollision(self):
self.collisions = []
hits = pygame.sprite.spritecollide(P1 , platforms, False)
for platform in hits:
if self.rect.bottom >= platform.rect.top and self.rect.bottom <= platform.rect.bottom:
self.collisions.append((platform, 'bottom'))
elif self.rect.top <= platform.rect.bottom and self.rect.top >= platform.rect.top:
self.collisions.append((platform, 'top'))
elif self.rect.left <= platform.rect.right and self.rect.left >= platform.rect.left:
self.collisions.append((platform, 'right'))
elif self.rect.right >= platform.rect.left and self.rect.right <= platform.rect.right:
self.collisions.append((platform, 'left'))
def update(self):
if not self.dying:
self.acc = vec(0, GRAV)
self.running = False
self.moving = False
self.jumpable = False
self.inAir = True
self.checkCollision()
self.checkDamage()
# Get key presses
pressed_keys = pygame.key.get_pressed()
# Trigger movement with key presses
if not self.dying:
if pressed_keys[K_LSHIFT] and not self.crouching:
if pressed_keys[K_RIGHT]:
self.facing = 1
self.run()
elif pressed_keys[K_LEFT]:
self.facing = -1
self.run()
elif not pressed_keys[K_LSHIFT] and not self.crouching:
if pressed_keys[K_RIGHT]:
self.facing = 1
self.walk()
elif pressed_keys[K_LEFT]:
self.facing = -1
self.walk()
# Collision detection
for collision in self.collisions:
if collision[1] == 'bottom' and self.vel.y >= 0:
self.pos.y = collision[0].rect.top + 1
self.vel.y = 0
self.jumpable = True
self.inAir = False
if collision[1] == 'top' and self.vel.y <= 0:
self.pos.y = collision[0].rect.bottom + 30
self.vel.y = 0
if collision[0].sprite == "question" or collision[0].sprite == "brick":
collision[0].opening = True
if collision[1] == 'right':
self.pos.x = collision[0].rect.left - 30
self.vel.x = 0
if collision[1] == 'left':
self.pos.x = collision[0].rect.right + 1
self.vel.x = 0
# Deceleration
if not self.moving and not self.inAir:
if self.facing * self.vel.x > 0:
self.acc.x = self.facing * -0.6
elif self.facing * self.vel.x < 0:
self.vel.x = 0
self.acc.x = 0
# Update the velocity and position using kinematics
self.vel += self.acc
self.pos.y += self.vel.y + 0.5 * self.acc.y
if not self.scroll:
self.pos.x += self.vel.x + 0.5 * self.acc.x
# Left border
if self.pos.x < 0:
self.pos.x = 0
self.vel.x = 0
# Speed cap
if self.running or (self.inAir and self.running):
if self.vel.x >= 10:
self.vel.x = 10
if self.vel.x <= -10:
self.vel.x = -10
else:
if self.vel.x >= 4:
self.vel.x = 4
if self.vel.x <= -4:
self.vel.x = -4
# Scrolling
if self.rect.right >= WIDTH / 2.5 and self.vel.x >= 0:
self.scroll = True
for plat in platforms:
plat.rect.x -= abs(self.vel.x)
if plat.rect.right <= 0:
plat.kill()
if self.moving and not self.inAir:
self.movingFrames += 1
else:
self.movingFrames = 0
frameDiff = 5
if not self.inAir:
if self.movingFrames == 0:
self.sprite = "stand"
elif self.movingFrames > 0 and self.movingFrames <= frameDiff:
self.sprite = "walk0"
elif self.movingFrames > frameDiff and self.movingFrames <= 2 * frameDiff:
self.sprite = "walk1"
elif self.movingFrames > 2 * frameDiff and self.movingFrames <= 3 * frameDiff:
self.sprite = "walk2"
elif self.movingFrames == 3 * frameDiff + 1:
self.movingFrames = 0
elif self.inAir:
self.sprite = "jump"
if ((self.vel.x < 0 and self.acc.x > 0) or (self.vel.x > 0 and self.acc.x < 0)) and not self.inAir:
self.sprite = "skid"
if not self.inAir and pressed_keys[K_DOWN] and self.state == "big":
self.sprite = "crouch"
self.crouching = True
else:
self.crouching = False
if self.dying == True:
self.sprite = "die"
if self.facing == 1:
self.surf = pygame.image.load("Sprites/" + self.sprite + "_" + self.state + ".png").convert()
else:
self.surf = pygame.transform.flip(pygame.image.load("Sprites/" + self.sprite + "_" + self.state + ".png").convert(), True, False)
self.rect = self.surf.get_rect()
# Update actual position of the sprite
self.rect.bottomleft = self.pos
def jump(self):
if self.jumpable:
self.vel.y = -14 - abs(self.vel.x + 0.01)/3
def checkDamage(self):
if self.rect.top > HEIGHT and not self.dying:
self.state = "small"
self.deathAnim()
def deathAnim(self):
self.vel = vec(0, -16)
self.acc = vec(0, 0.6)
self.dying = True
class platform(pygame.sprite.Sprite):
def __init__(self, x, y, sprite):
super().__init__()
self.sprite = sprite
self.x = SIDE/2 + SIDE * x
self.y = HEIGHT - SIDE/2 - SIDE * y
self.opening = False
self.surf = pygame.image.load("Sprites/" + self.sprite + ".png").convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect(center = (self.x, self.y))
self.pos = vec(0, 0)
self.pos.x = self.x
self.pos.y = self.y
self.vel = vec(0,0)
def open(self):
if self.sprite == "question":
if self.vel.y <= 0:
self.vel.y = -5
self.pos.y += self.vel.y
if self.pos.y <= self.y - 25 or self.vel.y > 0:
self.vel.y = 5
self.pos.y += self.vel.y
if self.pos.y >= self.y:
self.vel.y = 0
self.pos.y = self.y
self.opening = False
self.sprite = "empty-question"
if self.sprite == "brick":
self.kill()
def update(self):
if self.opening:
self.open()
self.rect.bottom = self.pos.y + SIDE/2
self.surf = pygame.image.load("Sprites/" + self.sprite + ".png").convert()
P1 = Player()
all_sprites = pygame.sprite.Group()
all_sprites.add(P1)
platforms = pygame.sprite.Group()
for i in range(0, 150):
for k in range(0, 2):
floor = platform(i, k, "ground")
platforms.add(floor)
all_sprites.add(floor)
blocks = [
platform(10, 4, "question"),
platform(13, 4, "brick"),
platform(14, 4, "question"),
platform(15, 4, "brick"),
platform(16, 4, "question"),
platform(17, 4, "brick"),
platform(15, 7, "question"),
]
for block in blocks:
platforms.add(block)
all_sprites.add(block)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
P1.jump()
displaysurface.fill((41, 200, 214))
P1.update()
for platform in platforms:
if platform.opening:
platform.update()
for entity in all_sprites:
displaysurface.blit(entity.surf, entity.rect)
pygame.display.update()
FramePerSec.tick(FPS)
'''
--BUGS--
- Left and right collision doesn't function
- Platforms going offscreen during scrolling go slower at the border for some reason
- Clipping into platforms for 1 frame every time theres collision
'''