-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
381 lines (342 loc) · 15.6 KB
/
sprites.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
import pygame as pg
from settings import *
vec = pg.math.Vector2
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = PLAYER_LAYER
self.groups = game.allSprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
# self.width = self.height = 30
# self.image = pg.Surface((self.width, self.height))
# self.image.fill(YELLOW)
self.image = self.game.playerImage.copy()
self.width = self.image.get_size()[0]
self.height = self.image.get_size()[1]
self.rect = self.image.get_rect()
self.rect.center = (x,y)
self.pos = vec(x,y)
self.vel = vec(0,0)
self.acc = vec(0,0)
self.direction = "forward"
self.last = 0
self.braking = False
def jump(self):
self.vel.y = -20
def collideWalls(self, dir):
if dir == 'x':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
if hits:
if self.vel.x > 0:
self.x = hits[0].rect.left - self.rect.width
if self.vel.x < 0:
self.x = hits[0].rect.right
self.vel.x = 0
self.rect.x = self.x
if dir == 'y':
hits = pg.sprite.spritecollide(self, self.game.walls, False)
if hits:
if self.vel.y > 0:
self.y = hits[0].rect.top - self.rect.height
if self.vel.y < 0:
self.y = hits[0].rect.bottom
self.vel.y = 0
self.rect.y = self.y
def collideWithWalls(self, dir):
hitWall = pg.sprite.spritecollide(self.game.player, self.game.walls, False)
if hitWall:
if dir == "x":
if self.game.player.vel.x > 0:#going to the right [hit left of wall]
self.game.player.pos.x = hitWall[0].rect.left - self.game.player.width / 2
elif self.game.player.vel.x < 0:#going to the left [hit right of wall]
self.game.player.pos.x = hitWall[0].rect.right + self.game.player.width / 2
self.game.player.vel.x = 0
self.rect.centerx = self.pos.x
if dir == "y":
if self.game.player.vel.y > 0:#going down [hit top of wall]
self.game.player.pos.y = hitWall[0].rect.top - self.rect.height / 2
elif self.game.player.vel.y < 0:#going up [hit bottom of wall]
self.game.player.pos.y = hitWall[0].rect.bottom + self.game.player.height / 2
self.game.player.vel.y = 0
self.rect.centery = self.pos.y
def update(self):
if self.game.sceneMan.showPlayer == True:
# print("Moving Player")
self.acc = vec(0, PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_RIGHT] or keys[pg.K_d]:
if keys[pg.K_RIGHT]:
print("Pressed Right Arrow Key")
else:
print("Pressed D Key")
if self.direction == "forward" or self.direction == "stopped":
if self.braking == False:
self.acc.x = PLAYER_ACC
self.direction = "forward"
if keys[pg.K_LEFT] or keys[pg.K_a]:
if keys[pg.K_LEFT]:
print("Pressed Left Arrow Key")
else:
print("Pressed A Key")
if self.direction == "reverse" or self.direction == "stopped":
if self.braking == False:
self.acc.x = -PLAYER_ACC
self.direction = "reverse"
if abs(self.vel.x) < 0.5:
self.direction = "stopped"
if keys[pg.K_DOWN] or keys[pg.K_s]:
if keys[pg.K_DOWN]:
print("Pressed Down Arrow Key")
else:
print("Pressed S Key")
if self.direction != "stopped":
if self.vel.x > 0:
self.acc.x = -BRAKE_ACC
else:
self.acc.x = BRAKE_ACC
self.braking = True
else:
self.braking = False
# #Debug Motion
# now = pg.time.get_ticks()
# if now - self.last > 150:
# self.last = now
# print("{} direction Velocity X: {} Acceleration X: {}".format(self.direction, round(self.vel.x,0), round(self.acc.x, 0)))
# print(" Velocity Y: {} Acceleration Y: {}".format(round(self.vel.y,0), round(self.acc.y, 0)))
self.acc += self.vel * PLAYER_FRICTION
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
self.rect.centerx = self.pos.x
self.collideWithWalls('x')
self.rect.centery = self.pos.y
self.collideWithWalls('y')
# self.pos = self.rect.center
class Platform(pg.sprite.Sprite):
def __init__(self, game, x, y, width, height, colour, mode="platform", show=True):
self._layer = PLATFORM_LAYER
self.mode = mode
if show == True:
self.groups = game.allSprites, game.platforms
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
elif show == False:
self.groups = game.platforms
pg.sprite.Sprite.__init__(self, self.groups)
self.rect = pg.Rect(x, y, width, height)
if self.mode == "platform":
self.rect.x, self.rect.y = x * TILESIZE, y * TILESIZE
elif self.mode == "track":
self.rect.x, self.rect.y = x, y
class Wall(pg.sprite.Sprite):
def __init__(self, game, x, y, colour=None, altColour=None, mode=None, mode2=None, width=None, height=None):
self._layer = WALL_LAYER
if mode == "tiled":
if mode2 == "end":
self.groups = game.endWalls
else:
self.groups = game.walls
pg.sprite.Sprite.__init__(self, self.groups)
# self.image = pg.Surface((width, height))
# self.image.fill(RED)
# self.rect = self.image.get_rect()
self.rect = pg.Rect(x, y, width, height)
# pg.draw.rect(game.screen, RED, (x, y, width, height), 1)
self.rect.x, self.rect.y = x, y
else:
if mode == "end":
self.groups = game.allSprites, game.endWalls
else:
self.groups = game.allSprites, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((TILESIZE, TILESIZE))
if mode != "end":
self.image.fill(colour)
else:
self.image.fill(altColour)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x * TILESIZE, y * TILESIZE
class Rectangle(pg.sprite.Sprite):
def __init__(self, game, x, y, width, height, colour):
self._layer = RECTANGLE_LAYER
self.groups = game.allSprites, game.rectangles
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
# self.image = pg.draw.rect(game.screen, colour, (x, y, width, height))
# self.rect = self.image.get_rect()
class Track(pg.sprite.Sprite):
def __init__(self, game, x, y, width, height, colour):
self._layer = TRACK_LAYER
self.groups = game.allSprites, game.platforms
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x , y
class WallFile(pg.sprite.Sprite):#This is so the sizes are not multiped by TILESIZE as only one pixel is needed
def __init__(self, game, x, y, width, height, colour):
self.groups = game.allSprites, game.walls
pg.sprite.Sprite.__init__(self, self.groups)
self.image = pg.Surface((width, height))
self.image.fill(colour)
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
class Question(pg.sprite.Sprite):
def __init__(self, game, x, y, major=False):
self._layer = QUESTION_LAYER
self.groups = game.allSprites, game.questionItems
pg.sprite.Sprite.__init__(self, self.groups)
# self.image = pg.Surface((self.width, self.height))
# self.image.fill(LIGHT_BLUE)
self.image = game.questionImage.copy()
self.image = pg.transform.scale(self.image, (64, 64))
size = self.image.get_size()
self.width, self.height = size[0], size[1]
self.rect = self.image.get_rect()
self.x, self.y = x, y
self.rect.center = (self.x, self.y)
self.velY = 1
self.major = major
def update(self):
self.rect.y += self.velY
if self.rect.top > self.y - self.height/4:
self.velY = -1
if self.rect.bottom < self.y + self.height/4:
self.velY = 1
class Coin(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = COIN_LAYER
self.groups = game.allSprites, game.coins
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
# self.image = pg.Surface((30,30))
# self.image.fill(ORANGE)
# self.rect = self.image.get_rect()
# self.rect.center = (x, y)
self.x = x
self.y = y
self.currentFrame = 0
self.lastUpdated = 0
def update(self):
now = pg.time.get_ticks()
if now - self.lastUpdated > 200:
self.lastUpdated = now
self.currentFrame = (self.currentFrame + 1) % len(self.game.coinImages)
self.image = self.game.coinImages[self.currentFrame]
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
class InputBox(pg.sprite.Sprite):
def __init__(self, game, x, y, width, height, text='', textSize=32, fontName=None):
self._layer = INPUT_BOX_LAYER
self.groups = game.userInputBox
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.rect = pg.Rect(x, y, width, height)
self.colour = RED
self.text = text
if fontName == None:
self.font = pg.font.SysFont(None, textSize)
else:
self.font = pg.font.Font(fontName, textSize)
self.textSurf = self.font.render(text, True, self.colour)
self.boxActive = False
def inputKeys(self, event):
if event.type == pg.MOUSEBUTTONDOWN:
if self.rect.collidepoint(event.pos):
self.boxActive = not self.boxActive
else:
self.boxActive = False
if self.boxActive:
self.colour = GREEN
else:
self.colour = RED
if event.type == pg.KEYDOWN:
if self.boxActive:
if event.key == pg.K_RETURN:
print("This is the text: {}".format(self.text))
if self.game.sceneMan.currentScene == 'levelComplete':
self.game.sceneMan.updateHighscore(self.game.sceneMan.nextLevelTagNumber-1, self.game.score)
self.text = ''
elif event.key == pg.K_BACKSPACE:
self.text = self.text[:-1]
else:
self.text += event.unicode
print(self.text)
self.game.screen.fill(WHITE, (WIDTH*5/8, HEIGHT*7/11, self.rect.width+10, 32))
self.textSurf = self.font.render(self.text, True, self.colour)
def update(self):
newWidth = max(150, self.textSurf.get_width()+10)
self.rect.width = newWidth
def draw(self, surf):
surf.blit(self.textSurf, (self.rect.x+5, self.rect.y+5))
pg.draw.rect(surf, self.colour, self.rect, 2)
class Button(pg.sprite.Sprite):
def __init__(self, game, tag, x, y, width, height, solidColour, highlightColour, text, textSize=None, solidButtonImage=None, highlightButtonImage=None):
self._layer = BUTTON_LAYER
self.groups = game.buttons
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.tag = tag
self.width = width
self.height = height
self.solidColour = solidColour
self.highlightColour = highlightColour
self.text = text
if textSize == None:
self.textSize = int(width/(len(text)*0.45))
# print("Text Size {} for {}".format(self.textSize, self.tag))
if self.textSize > 24:
self.textSize = 24
else:
self.textSize = textSize
self.pos = vec(x,y)
self.rectDimensions = (self.pos[0]-self.width/2, self.pos[1]-self.height/2, self.width, self.height)
# self.image = pg.draw.rect(self.game.screen, self.solidColour, self.rectDimensions , 0)
if solidButtonImage == None:
self.solidImage = pg.transform.scale(self.game.menuButtonSolid,(int(self.width), int(self.height)))
self.highlightImage = pg.transform.scale(self.game.menuButtonHighlight,(int(self.width), int(self.height)))
else:
self.solidImage = pg.transform.scale(solidButtonImage,(int(self.width), int(self.height)))
self.highlightImage = pg.transform.scale(highlightButtonImage,(int(self.width), int(self.height)))
self.image = self.solidImage
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.centery = y
self.clicked = False
self.colchange = False
self.first = True
def highlight(self):
mousePos = pg.mouse.get_pos()
# prevcol = self.game.screen.get_at((int(self.pos[0]-self.width/2), int(self.pos[1]-self.height/2)))
if self.pos[0] - self.width/2 <= mousePos[0] <= self.pos[0] + self.width/2 and \
self.pos[1] - self.height/2 <= mousePos[1] <= self.pos[1] + self.height/2:
if self.image != self.highlightImage:
# self.image = pg.draw.rect(self.game.screen, self.highlightColour, self.rectDimensions , 0)
self.image = self.highlightImage
self.colchange = True
if pg.mouse.get_pressed()[0] == 1:
self.clicked = True
else:
if self.image != self.solidImage:
# self.image = pg.draw.rect(self.game.screen, self.solidColour, self.rectDimensions , 0)
self.image = self.solidImage
self.colchange = True
def update(self):
self.highlight()
if self.clicked:
for button in self.game.buttons:
if button != self:
button.kill()
def draw(self, surface):
if self.colchange == True or self.first == True:
surface.blit(self.image,self.rect)
print("drawn this button", self.tag)
if self.first:
# print(self.first, "self.first")
self.first = False
self.game.drawText(self.text, self.textSize, BLACK, self.pos[0], self.pos[1], surf=surface)
self.colchange = False