-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgamesystems.py
More file actions
91 lines (80 loc) · 3.82 KB
/
gamesystems.py
File metadata and controls
91 lines (80 loc) · 3.82 KB
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
import engine
import globals
import random
import utils
class PowerupSystem(engine.System):
def __init__(self):
self.timer = 0
def check(self, entity):
return entity.effect is not None
def update(self, screen=None, inputStream=None):
super().update(screen, inputStream)
# count the number of powerups in the world
count = 0
for entity in globals.world.entities:
if entity.type != 'player':
if entity.effect:
count += 1
# if no powerups -- start a timer to create new
if count == 0 and self.timer == 0:
self.timer = 500
# create new powerup if it's time
if self.timer > 0:
# decrement timer
self.timer -= 1
if self.timer <= 0:
# spawn a powerup
if globals.world.powerupSpawnPoints is not None:
if len(globals.world.powerupSpawnPoints) > 0:
spawnPos = random.choice(globals.world.powerupSpawnPoints)
globals.world.entities.append(
utils.makePowerup(random.choice(utils.powerups), spawnPos[0], spawnPos[1])
)
def updateEntity(self, screen, inputStream, entity):
# player collection of powerups
for otherEntity in globals.world.entities:
if otherEntity is not entity and otherEntity.type == 'player' and entity.type != 'player':
if entity.position.rect.colliderect(otherEntity.position.rect):
# give the effect component to the player
otherEntity.effect = entity.effect
engine.soundManager.playSound(entity.effect.sound)
# remove the collected powerup from the world
globals.world.entities.remove(entity)
# apply powerup effects for players
if entity.type == 'player':
entity.effect.apply(entity)
entity.effect.timer -= 1
# if the effect has run out
if entity.effect.timer < 0:
# reset entity if appropriate
if entity.effect.end:
entity.effect.end(entity)
# destroy the effect
entity.effect = None
class CollectionSystem(engine.System):
def check(self, entity):
return entity.type == 'player' and entity.score is not None
def updateEntity(self, screen, inputStream, entity):
for otherEntity in globals.world.entities:
if otherEntity is not entity and otherEntity.type == 'collectable':
if entity.position.rect.colliderect(otherEntity.position.rect):
# entity.collectable.onCollide(entity, otherEntity)
engine.soundManager.playSound('coin')
globals.world.entities.remove(otherEntity)
entity.score.score += 1
class BattleSystem(engine.System):
def check(self, entity):
return entity.type == 'player' and entity.battle is not None
def updateEntity(self, screen, inputStream, entity):
for otherEntity in globals.world.entities:
if otherEntity is not entity and otherEntity.type == 'dangerous':
if entity.position.rect.colliderect(otherEntity.position.rect):
# entity.battle.onCollide(entity, otherEntity)
entity.battle.lives -= 1
# reset player position
entity.position.rect.x = entity.position.initial.x
entity.position.rect.y = entity.position.initial.y
entity.speed = 0
# remove player if no lives left
if entity.battle.lives <= 0:
globals.world.entities.remove(entity)