-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdibrino.py
295 lines (224 loc) · 8.84 KB
/
dibrino.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
# -*- coding: utf-8 -*-
import sys
import pygame
import random
from scripts import pyganim
# define some functions
def loadImages():
imagesAndDurations = [('./images/vita/right_stand.%s.png' % (
str(num).rjust(3, '0')), 0.1) for num in range(4)]
animObjs['vita_right_stand'] = pyganim.PygAnimation(imagesAndDurations)
animObjs['vita_left_stand'] = animObjs['vita_right_stand'].getCopy()
animObjs['vita_left_stand'].flip(True, False)
animObjs['vita_left_stand'].makeTransformsPermanent()
imagesAndDurations = [('./images/vita/right_walk.%s.png' % (
str(num).rjust(3, '0')), 0.1) for num in range(6)]
animObjs['vita_right_walk'] = pyganim.PygAnimation(imagesAndDurations)
animObjs['vita_left_walk'] = animObjs['vita_right_walk'].getCopy()
animObjs['vita_left_walk'].flip(True, False)
animObjs['vita_left_walk'].makeTransformsPermanent()
imagesAndDurations = [('./images/vita/right_damage.%s.png' % (
str(num).rjust(3, '0')), 0.1) for num in range(4)]
animObjs['vita_right_damage'] = pyganim.PygAnimation(imagesAndDurations)
animObjs['vita_left_damage'] = animObjs['vita_right_damage'].getCopy()
animObjs['vita_left_damage'].flip(True, False)
animObjs['vita_left_damage'].makeTransformsPermanent()
imagesAndDurations = [('./images/vita/right_run.%s.png' % (
str(num).rjust(3, '0')), 0.1) for num in range(6)]
animObjs['vita_right_run'] = pyganim.PygAnimation(imagesAndDurations)
animObjs['vita_left_run'] = animObjs['vita_right_run'].getCopy()
animObjs['vita_left_run'].flip(True, False)
animObjs['vita_left_run'].makeTransformsPermanent()
imagesAndDurations = [('./images/fireball/down_fall.%s.png' % (
str(num).rjust(3, '0')), 0.15) for num in range(3)]
animObjs['fireball_down_fall'] = pyganim.PygAnimation(imagesAndDurations)
imagesAndDurations = [('./images/fireball/down_explosion.%s.png' % (
str(num).rjust(3, '0')), 0.15) for num in range(3)]
animObjs['fireball_down_explosion'] = pyganim.PygAnimation(
imagesAndDurations)
class Explosion:
def __init__(self):
self.x = -1
self.count = 0
self.exploding = False
def explodeBegin(self, x):
self.exploding = True
self.x = x
animObjs['fireball_down_explosion'].blit(windowSurface, (self.x, 400))
self.count = 1
def explode(self):
animObjs['fireball_down_explosion'].blit(windowSurface, (self.x, 400))
self.count += 1
if self.count == 4:
self.exploding = False
def getExploding(self):
return self.exploding
class Fireball():
def __init__(self):
if random.randrange(0, 75, 1) == 1:
self.x = random.randrange(0, 640, 32)
self.y = 0
self.spawned = True
else:
self.spawned = False
def fall(self, fall_rate):
if self.y > 415:
self.spawned = False
for explosion in explosions:
if not explosion.getExploding():
explosion.__init__()
explosion.explodeBegin(self.x)
break
return 1
self.y += fall_rate
return 0
def getX(self):
return self.x
def getY(self):
return self.y
def getSpawned(self):
return self.spawned
pygame.init()
background = pygame.image.load("./images/background.png")
gameover = pygame.image.load("./images/gameover.png")
# define some constants
LEFT = 'left'
RIGHT = 'right'
# set up the window
WINDOWWIDTH = 640
WINDOWHEIGHT = 480
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Dibrino Alpha')
# set player size
playerWidth = 76
playerHeight = 96
# creating the PygAnimation objects for player actions
animObjs = {}
# loading all game images, including PygAnimation files
loadImages()
moveConductor = pyganim.PygConductor(animObjs)
direction = LEFT # player starts off facing left
bg_color = (128, 255, 0)
mainClock = pygame.time.Clock()
x = 270 # x and y are the player's position
y = 370
x_fireball = 0
y_fireball = 416
WALK_RATE = 6
RUN_RATE = 10
FALL_RATE = 10
score = 0
sys_font = pygame.font.SysFont("None", 60)
running = moveLeft = moveRight = False
over = False
fireballs = [Fireball() for i in range(10)]
for fireball in fireballs:
fireball.__init__()
explosions = [Explosion() for i in range(10)]
for explosion in explosions:
explosion.__init__()
while True:
# main game loop
while not over:
windowSurface.blit(background, (0, 0))
for event in pygame.event.get(): # event handling loop
# handle ending the program
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
running = True
elif event.key == pygame.K_LEFT:
moveLeft = True
moveRight = False
direction = LEFT
elif event.key == pygame.K_RIGHT:
moveRight = True
moveLeft = False
direction = RIGHT
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LSHIFT, pygame.K_RSHIFT):
running = False
elif event.key == pygame.K_LEFT:
moveLeft = False
elif event.key == pygame.K_RIGHT:
moveRight = False
for explosion in explosions:
if explosion.getExploding():
explosion.explode()
if moveLeft or moveRight:
# draw the correct walking/running sprite from the animation object
if running:
rate = RUN_RATE
playerWidth = 96
if direction == LEFT:
animObjs['vita_left_run'].blit(windowSurface, (x, y))
elif direction == RIGHT:
animObjs['vita_right_run'].blit(windowSurface, (x, y))
else:
# walking
rate = WALK_RATE
playerWidth = 76
if direction == LEFT:
animObjs['vita_left_walk'].blit(windowSurface, (x, y))
elif direction == RIGHT:
animObjs['vita_right_walk'].blit(windowSurface, (x, y))
if moveLeft:
x -= rate
if moveRight:
x += rate
else:
# standing still
moveConductor.play()
if direction == LEFT:
animObjs['vita_left_stand'].blit(windowSurface, (x, y))
elif direction == RIGHT:
animObjs['vita_right_stand'].blit(windowSurface, (x, y))
# for fireball in fireballs:
for fireball in fireballs:
if not fireball.getSpawned():
fireball.__init__()
else:
score += fireball.fall(FALL_RATE)
animObjs['fireball_down_fall'].blit(windowSurface, (
fireball.getX(), fireball.getY()))
if fireball.getX() < (x + 62) and (fireball.getX() + 20) > x:
if fireball.getY() > 350:
over = True
# make sure the player does move off the screen
if x < 0 - (96 - playerWidth):
x = 0 - (96 - playerWidth)
if x > WINDOWWIDTH - playerWidth:
x = WINDOWWIDTH - playerWidth
if y < 0:
y = 0
if y > WINDOWHEIGHT - playerHeight:
y = WINDOWHEIGHT - playerHeight
FALL_RATE = 10 + score // 10
score_rendered = sys_font.render(str(score), 0, (0, 0, 0))
windowSurface.blit(score_rendered, (20, 20))
pygame.display.update()
mainClock.tick(24) # Feel free to experiment with any FPS setting
while over:
windowSurface.blit(gameover, (0, 0))
for event in pygame.event.get(): # event handling loop
# handle ending the program
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
over = False
score = 0
game_over = sys_font.render("GAME OVER", 0, (0, 0, 0))
windowSurface.blit(game_over, (200, 200))
final_score = sys_font.render(str(score), 0, (0, 0, 0))
windowSurface.blit(final_score, (310, 300))
for fireball in fireballs:
fireball.spawned = False
x = 270 # x and y are the player's position
y = 370
running = moveLeft = moveRight = False
pygame.display.update()
mainClock.tick(24) # Feel free to experiment with any FPS setting