-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathutils.py
More file actions
216 lines (179 loc) · 7.08 KB
/
utils.py
File metadata and controls
216 lines (179 loc) · 7.08 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
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
import pygame
import engine
import globals
import gamecomponents
pygame.font.init()
font = pygame.font.Font(pygame.font.get_default_font(), 24)
# function from:
# https://nerdparadise.com/programming/pygameblitopacity
def blit_alpha(target, source, location, opacity):
x = location[0]
y = location[1]
temp = pygame.Surface((source.get_width(), source.get_height())).convert()
temp.blit(target, (-x, -y))
temp.blit(source, (0, 0))
temp.set_alpha(opacity)
target.blit(temp, location)
def drawText(screen, t, x, y, fg, alpha):
text = font.render(t, True, fg)
text_rectangle = text.get_rect()
text_rectangle.topleft = (x,y)
blit_alpha(screen, text, (x,y), alpha)
heart_image = pygame.image.load('images/heart.png')
def setHealth(entity):
if entity.battle:
entity.battle.lives = 3
def setInvisible(entity):
if entity.imageGroups:
entity.imageGroups.alpha = 50
def endInvisible(entity):
if entity.imageGroups:
entity.imageGroups.alpha = 255
powerups = ['health', 'invisible']
powerupImages = {
'health' : [pygame.image.load('images/powerup_health.png')],
'invisible' : [pygame.image.load('images/powerup_invisible.png')]
}
powerupSound = {
'health' : 'coin',
'invisible' : 'coin'
}
powerupApply = {
'health' : setHealth,
'invisible' : setInvisible
}
powerupEnd = {
'health' : None,
'invisible' : endInvisible
}
powerupEffectTimer = {
'health' : 0,
'invisible' : 1000
}
def makePowerup(type, x, y):
entity = engine.Entity()
entity.position = engine.Position(x,y,40,40)
entityAnimation = engine.ImageGroup(powerupImages[type])
entity.imageGroups.add('idle', entityAnimation)
entity.effect = gamecomponents.Effect(
powerupApply[type],
powerupEffectTimer[type],
powerupSound[type],
powerupEnd[type]
)
return entity
coin0 = pygame.image.load('images/coin_0.png')
coin1 = pygame.image.load('images/coin_1.png')
coin2 = pygame.image.load('images/coin_2.png')
coin3 = pygame.image.load('images/coin_3.png')
coin4 = pygame.image.load('images/coin_4.png')
coin5 = pygame.image.load('images/coin_5.png')
def makeCoin(x,y):
entity = engine.Entity()
entity.position = engine.Position(x,y,23,23)
entityAnimation = engine.ImageGroup([coin1, coin2, coin3, coin4, coin5], delay=12)
entity.imageGroups.add('idle', entityAnimation)
entity.type = 'collectable'
return entity
enemy0 = pygame.image.load('images/spike_monster.png')
def makeEnemy(x,y):
entity = engine.Entity()
entity.position = engine.Position(x,y,50,26)
entityAnimation = engine.ImageGroup([enemy0])
entity.imageGroups.add('idle', entityAnimation)
entity.type = 'dangerous'
return entity
playing = pygame.image.load('images/playing.png')
not_playing = pygame.image.load('images/not_playing.png')
idle0 = pygame.image.load('images/vita_00.png')
idle1 = pygame.image.load('images/vita_01.png')
idle2 = pygame.image.load('images/vita_02.png')
idle3 = pygame.image.load('images/vita_03.png')
walking0 = pygame.image.load('images/vita_04.png')
walking1 = pygame.image.load('images/vita_05.png')
walking2 = pygame.image.load('images/vita_06.png')
walking3 = pygame.image.load('images/vita_07.png')
walking4 = pygame.image.load('images/vita_08.png')
walking5 = pygame.image.load('images/vita_09.png')
jumping = pygame.image.load('images/vita_11.png')
def orderPlayers():
newPlayerOrder = []
for player in [globals.player1, globals.player2, globals.player3, globals.player4]:
if player in globals.players:
newPlayerOrder.append(player)
globals.players = newPlayerOrder
def setPlayerCameras():
screenWidth, screenHeight = pygame.display.get_surface().get_size()
# 1 player game
if len(globals.players) == 1:
cameraWidth = screenWidth - (2*10)
cameraHeight = screenHeight - (2*10)
p = globals.players[0]
p.camera = engine.Camera(10,10,cameraWidth, cameraHeight)
p.camera.setWorldPos(p.position.initial.x, p.position.initial.y)
p.camera.trackEntity(p)
# 2 player game
if len(globals.players) == 2:
cameraWidth = (screenWidth - (3*10)) / 2
cameraHeight = screenHeight - (2*10)
p1 = globals.players[0]
p1.camera = engine.Camera(10,10,cameraWidth, cameraHeight)
p1.camera.setWorldPos(p1.position.initial.x, p1.position.initial.y)
p1.camera.trackEntity(p1)
p2 = globals.players[1]
p2.camera = engine.Camera((2*10)+cameraWidth,10,cameraWidth, cameraHeight)
p2.camera.setWorldPos(p2.position.initial.x, p2.position.initial.y)
p2.camera.trackEntity(p2)
# 3 or 4 player game
if len(globals.players) >= 3:
cameraWidth = (screenWidth - (3*10)) / 2
cameraHeight = (screenHeight - (3*10)) / 2
p1 = globals.players[0]
p1.camera = engine.Camera(10,10,cameraWidth, cameraHeight)
p1.camera.setWorldPos(p1.position.initial.x, p1.position.initial.y)
p1.camera.trackEntity(p1)
p2 = globals.players[1]
p2.camera = engine.Camera((2*10)+cameraWidth,10,cameraWidth, cameraHeight)
p2.camera.setWorldPos(p2.position.initial.x, p2.position.initial.y)
p2.camera.trackEntity(p2)
p3 = globals.players[2]
p3.camera = engine.Camera(10,(2*10)+cameraHeight,cameraWidth, cameraHeight)
p3.camera.setWorldPos(p3.position.initial.x, p3.position.initial.y)
p3.camera.trackEntity(p3)
if len(globals.players) == 4:
p4 = globals.players[3]
p4.camera = engine.Camera((2*10)+cameraWidth,(2*10)+cameraHeight,cameraWidth, cameraHeight)
p4.camera.setWorldPos(p4.position.initial.x, p4.position.initial.y)
p4.camera.trackEntity(p4)
def resetPlayer(entity):
entity.score.score = 0
entity.battle.lives = 3
entity.position.rect.x = entity.position.initial.x
entity.position.rect.y = entity.position.initial.y
entity.speed = 0
entity.acceleration = entity.initialAcceleration
entity.camera.setWorldPos(entity.position.initial.x, entity.position.initial.y)
entity.direction = 'right'
entity.imageGroups.alpha = 255
entity.effect = None
entity.state = 'idle'
if entity.camera is not None:
entity.camera.zoomLevel = 1
def makePlayer(x,y):
entity = engine.Entity()
entity.position = engine.Position(x,y,45,51)
entityIdleAnimation = engine.ImageGroup([idle0, idle1, idle2, idle3])
entityWalkingAnimation = engine.ImageGroup([walking0, walking1, walking2, walking3, walking4, walking5], delay=6)
entityJumpingAnimation = engine.ImageGroup([jumping])
entity.imageGroups.add('idle', entityIdleAnimation)
entity.imageGroups.add('walking', entityWalkingAnimation)
entity.imageGroups.add('jumping', entityJumpingAnimation)
entity.score = gamecomponents.Score()
entity.battle = gamecomponents.Battle()
entity.intention = engine.Intention()
entity.acceleration = 0.3
entity.initialAcceleration = entity.acceleration
entity.type = 'player'
entity.reset = resetPlayer
entity.rigidBody = engine.RigidBody(10,1,25,50)
return entity