forked from pokitto/PokittoLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
495 lines (403 loc) · 15.8 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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import gc
import upygame as pygame
import urandom as random
import sprite
import marsattack_data as gamedata
import marsattack_classes as gameclass
import umachine as pok
# Init screen
pygame.display.init()
screen = pygame.display.set_mode() # full screen
screenRect = screen.get_rect()
screenW = screenRect.width
screenH = screenRect.height
# Copyright (C) 2018 Hannu Viitala
# This file is released under MIT license.
# Go to http://opensource.org/licenses/MIT for full license details.
# Create global objects
shipGob = None
bombGob = None
explosionGob = None
explosionGob2 = None
explosionGob3 = None
g_rockpileGobList = []
gameMode="play"
points = 0
g_rockPileSize = 1;
g_numPiles = 8;
g_ship_speed = 2
g_level = 0
test_num_piles = 8
g_debug_mode = 0
# Level definitions
g_levelParam = [
# palette, number of piles, max height, body bitmap,head bitmap, head collision rect
[[60683,32963,49572,65502], test_num_piles, 0, gamedata.rock1Surf, gamedata.rockHead1Surf, gamedata.rockHead1CollRect],
[[34490,4648,11215,63455], test_num_piles, 1, gamedata.rock2Surf, gamedata.rockHead2Surf, gamedata.rockHead2CollRect],
[[36590,6722,13283,61405], test_num_piles, 1, gamedata.rock3Surf, gamedata.rockHead3Surf, gamedata.rockHead3CollRect],
[[62916,16800,31456,65300], test_num_piles, 2, gamedata.rock6Surf, gamedata.rockHead6Surf, gamedata.rockHead6CollRect],
[[58427,30926,43283,65439], test_num_piles, 2, gamedata.rock5Surf, gamedata.rockHead5Surf, gamedata.rockHead5CollRect],
[[42260,10565,19049,61309], test_num_piles, 3, gamedata.rock7Surf, gamedata.rockHead7Surf, gamedata.rockHead7CollRect],
[[62480,43073,55393,65438], test_num_piles, 3, gamedata.rock4Surf, gamedata.rockHead4Surf, gamedata.rockHead4CollRect],
]
# Create sprite lists
all_sprites = sprite.Group()
all_rockpiles = gameclass.RockPileGroup()
all_gobs = gameclass.GobGroup()
# Set initial palette
pygame.display.set_palette_16bit([60683,32963,49572,65502]);
# Main function
def main():
# Create the whole level
RestartLevel()
while True:
# Draw points
DrawPoints()
# Choose mode
if gameMode=="start":
MainPlay()
elif gameMode=="level done":
MainLevelDone()
elif gameMode=="lost game":
MainLostGame()
else: # gameMode="play"
MainPlay()
# *** Playing main loop
def MainPlay():
global gameMode
global points
global g_level
# Init
vx = g_ship_speed;
vy = 0;
exitLoop = False
destroyedPilesCount = 0
# Main loop
while not exitLoop:
# read keys
eventtype = pygame.event.poll()
if eventtype != pygame.NOEVENT:
if eventtype.type == pygame.KEYDOWN:
if eventtype.key == pygame.BUT_A:
# Drop bomb if it is not visible already
if not bombGob.visible:
bombGob.rect.x = shipGob.rect.x + ((shipGob.rect.width- bombGob.rect.width)//2)
bombGob.rect.y = shipGob.rect.y + shipGob.rect.height
bombGob.setvel(0,2)
bombGob.visible = True
# Debug: Level up
if g_debug_mode>0 and eventtype.key == pygame.K_UP:
g_level+=1
if g_level > 6:
g_level = 6;
RestartLevel()
# Debug: Level down
if g_debug_mode>0 and eventtype.key == pygame.K_DOWN:
g_level-=1
if g_level < 0:
g_level = 0;
RestartLevel()
# Set speed
shipGob.setvel(vx,vy)
# Init collision rect for the bomb
bombGob.collRect2.x = bombGob.rect.x + bombGob.collRect.x
bombGob.collRect2.y = bombGob.rect.y + bombGob.collRect.y
bombGob.collRect2.width = bombGob.collRect.width
bombGob.collRect2.height = bombGob.collRect.height
# Check bomb collision to rockpile
hitSprite = sprite.spritecollideany(bombGob, all_rockpiles, IsSpriteCollided)
if hitSprite != None:
# Setup explosion
explosionGob.rect.x = hitSprite.rect.x
explosionGob.rect.y = hitSprite.rect.y
explosionGob.vx = 0
explosionGob.vy = 0
explosionGob.animDur = 1
explosionGob.visible = True
explosionGob.currentFrameNum = 0;
explosionGob.setSpriteFrame()
# Calc dirty rect of the rockpile
totalH = hitSprite.rect.height + hitSprite.frames[1].get_rect().height
dirtyRect = pygame.Rect(hitSprite.rect.x, hitSprite.rect.y, hitSprite.rect.width, totalH)
if hitSprite.numClones > 0:
# Reduce pile height by one rock
ChangeRockPile(hitSprite, hitSprite.numClones-1)
else:
# Hide pile as there is only one rock left in pile
hitSprite.rect.y = 999
hitSprite.collRect2.y = 999
destroyedPilesCount += 1
if destroyedPilesCount >= g_levelParam[g_level][1]:
gameMode="level done"
exitLoop = True
# Redraw background color in the dirty rect area only
screen.fill(0, dirtyRect) # TODO: can this be removed?
# Update pile state
hitSprite.update()
# Draw background image in the dirty rect area only
screen.set_clip(dirtyRect);
screen.blit(gamedata.backgroundSurf, 0, 0, 15) # no invisible color
screen.set_clip();
# Draw the rock pile
y = hitSprite.rect.y
screen.blit(hitSprite.image, hitSprite.rect.x, y) # blit head hwsprite
y += hitSprite.rect.height
rockSF = hitSprite.frames[1]
screen.blit(rockSF, hitSprite.rect.x, y) # blit body hwsprite
# Draw display to the screen hw
pygame.display.update(False, dirtyRect, True) # Draw now, draw only
# Draw points
points += 10
DrawPoints()
# Check the ship collision to rockpile
hitSprite = sprite.spritecollideany( shipGob, all_rockpiles )
if hitSprite != None:
# setup explosion 2
explosionGob2.rect.x = shipGob.rect.x
explosionGob2.rect.y = shipGob.rect.y
explosionGob2.animDur = 3
explosionGob2.visible = True
explosionGob2.currentFrameNum = 0;
explosionGob2.setSpriteFrame()
# setup explosion 3
explosionGob3.rect.x = shipGob.rect.x + explosionGob2.rect.width
explosionGob3.rect.y = shipGob.rect.y
explosionGob3.animDur = 3
explosionGob3.visible = True
explosionGob3.currentFrameNum = 0;
explosionGob3.setSpriteFrame()
# Hide ship
shipGob.vy = 999
shipGob.disableAfterOutOfScreen = True
# Exit and move to "lost game" state
gameMode = "lost game"
exitLoop = True
# update all gobs
all_sprites.update()
# Go round
if shipGob.rect.x > screenW:
shipGob.rect.x = -shipGob.rect.width
shipGob.rect.y += 14
# Draw gobs
all_gobs.draw(screen)
# Draw the screen. do not use buffered draw.
pygame.display.update(True)
# Level done, main loop
def MainLevelDone():
global gameMode
global g_level
global g_ship_speed
# Draw text
pok.draw_text(50, 30, " LEVEL COMPLETED!", 1)
pygame.display.update(False)
# Stop ship
shipGob.setvel(0,0);
# Main loop
exitLoop = False
while not exitLoop:
# read keys
eventtype = pygame.event.poll()
if eventtype != pygame.NOEVENT:
if eventtype.type == pygame.KEYDOWN:
if eventtype.key == pygame.BUT_A:
# Start the next level
g_level+=1
if g_level>6:
g_level = 0
g_ship_speed += 1
RestartLevel()
gameMode = "play"
exitLoop = True
# Update states of all gobs
all_sprites.update()
# Draw gobs
all_gobs.draw(screen)
# Draw screen
pygame.display.update(True)
# Game lost, main loop
def MainLostGame():
global gameMode
global points
global g_level
global g_ship_speed
global points
# Draw text
pok.draw_text(60, 30, " YOU LOST!", 1)
text = "YOUR SCORE: " + str(points)
pok.draw_text(60, 70, text, 1)
pygame.display.update(False)
# Stop ship
shipGob.setvel(0,0);
# Main loop
exitLoop = False
while not exitLoop:
# Read keys
eventtype = pygame.event.poll()
if eventtype != pygame.NOEVENT:
if eventtype.type == pygame.KEYDOWN:
if eventtype.key == pygame.BUT_A:
# Restart the game from the first level
g_level = 0
g_ship_speed = 2
points = 0
RestartLevel()
gameMode = "play"
exitLoop = True
points = 0
# Update states of all gobs
all_sprites.update()
# Draw gobs
all_gobs.draw(screen)
# Draw screen
pygame.display.update(True)
# Create level
def RestartLevel():
# Set palette
pygame.display.set_palette_16bit(g_levelParam[g_level][0])
# Create rock pile sprites
CreateRockPileSprites(g_levelParam[g_level][1], g_levelParam[g_level][2], g_levelParam[g_level][3], g_levelParam[g_level][4], g_levelParam[g_level][5])
# Init sprites
InitSprites()
# Update states of all piles
all_rockpiles.update()
# Draw background image
screen.blit(gamedata.backgroundSurf, 0, 0, 15) # no invisible color
# Draw piles
all_rockpiles.draw(screen)
# Draw screen
pygame.display.update(False)
# Callback which is called when the gob anim cycle is finished
def HideGobAfterAnimCycle(gob):
# Note: only move y so that the dirty rect for a sprite is optimum.
gob.vy = 999
# Callback which is called when a sprite collision must be checked
def IsSpriteCollided(sprite, s):
return( sprite.collRect2.colliderect( s.collRect2 ) )
# Change the pile height
def ChangeRockPile(gob, numClones):
# Reinit the pile
gob.frames[0] = gob.frames[1] # head rock is the same as body rock
gob.image = gob.frames[1]
gob.numClones = numClones
gob.rect.height = gob.image.get_rect().height
gob.rect.width = gob.image.get_rect().width
gob.rect.y = screenH - 5 - (gob.numClones*gob.frames[1].get_rect().height) - gob.frames[0].get_rect().height
# Collision rect
gob.collRect2.x = gob.rect.x + gob.collRect.x
gob.collRect2.y = gob.rect.y + gob.collRect.y
gob.collRect2.width = gob.collRect.width
gob.collRect2.height = gob.collRect.height
# Create the rock pile sprites
def CreateRockPileSprites(numPiles, rockPileSize, rockSF, rockHeadSF, rockHead1CollRect):
global g_rockpileGobList
for i in range(numPiles):
# Create or initialize gob
gob = None
if len(g_rockpileGobList) <= i:
# Create gob
gob = gameclass.GameObject([rockHeadSF, rockSF],[[0,0], [0,0]], screen, screenW, screenH)
g_rockpileGobList.append(gob)
all_sprites.add(g_rockpileGobList[i])
all_rockpiles.add(g_rockpileGobList[i])
else:
# Initialize gob
gob = g_rockpileGobList[i]
gob.frames = [rockHeadSF, rockSF]
gob.image = gob.frames[gob.currentFrameNum] # current image
gob.rect = gob.frames[0].get_rect()
# Initialize the rock pile
gob = g_rockpileGobList[i]
gob.rect.x = 30 + i*(gob.rect.width+4)
gob.collRect = rockHead1CollRect
gob.collRect2 = pygame.Rect(0,0,0,0)
gob.numClones = random.getrandbits(2) + rockPileSize # =2
gob.rect.y = screenH - 5 - (gob.numClones*gob.frames[1].get_rect().height) - gob.frames[0].get_rect().height
gob.visible = True
# Set the collision rect
gob.collRect2.x = gob.rect.x + gob.collRect.x
gob.collRect2.y = gob.rect.y + gob.collRect.y
gob.collRect2.width = gob.collRect.width
gob.collRect2.height = gob.collRect.height
# Create or initialize all sprites
def InitSprites():
global shipGob
global bombGob
global explosionGob
global explosionGob2
global explosionGob3
# Create or initialize ship
if shipGob == None:
shipGob = gameclass.GameObject([gamedata.shipF1Surf], [[0,0]], screen, screenW, screenH)
all_sprites.add(shipGob)
all_gobs.add(shipGob)
shipGob.setSprite(0, [[60683,32963,49572,65502]])
shipGob.rect.x = 50
shipGob.rect.y = 3
shipGob.animDur = 0;
shipGob.disableAfterOutOfScreen = False
shipGob.visible = True
# Create or initialize bomb
if bombGob == None:
bombGob = gameclass.GameObject([gamedata.bombSurf], [[0,0]], screen, screenW, screenH)
bombGob.collRect = gamedata.bombCollRect
bombGob.collRect2 = pygame.Rect(0,0,0,0)
all_sprites.add(bombGob)
all_gobs.add(bombGob)
bombGob.setSprite(1, [gamedata.bombColorMap])
bombGob.rect.x = 999
bombGob.rect.y = 999
# Create or initialize explosion
if explosionGob == None:
explosionGob = gameclass.GameObject(
[gamedata.explosion1f1Surf, gamedata.explosion1f2Surf, gamedata.explosion1f3Surf, gamedata.explosion1f4Surf],
[[0,0],[0,0],[0,0],[0,0]], screen, screenW, screenH
)
all_sprites.add(explosionGob)
all_gobs.add(explosionGob)
explosionGob.setSprite(2, [gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap])
explosionGob.rect.x = 999
explosionGob.rect.y = 999
explosionGob.shouldCycleAnim = False;
explosionGob.callAfterAnimCycle = HideGobAfterAnimCycle
explosionGob.setSpriteFrame()
explosionGob.visible = False
# Create or initialize explosion 2
if explosionGob2 == None:
explosionGob2 = gameclass.GameObject(
[gamedata.explosion1f1Surf, gamedata.explosion1f2Surf, gamedata.explosion1f3Surf, gamedata.explosion1f4Surf],
[[0,0],[0,0],[0,0],[0,0]], screen, screenW, screenH
)
all_sprites.add(explosionGob2)
all_gobs.add(explosionGob2)
explosionGob2.setSprite(3, [gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap])
explosionGob2.rect.x = 999
explosionGob2.rect.y = 999
explosionGob2.shouldCycleAnim = False;
explosionGob2.callAfterAnimCycle = HideGobAfterAnimCycle
explosionGob2.setSpriteFrame()
explosionGob2.visible = False
# Create or initialize explosion 3
if explosionGob3 == None:
explosionGob3 = gameclass.GameObject(
[gamedata.explosion1f1Surf, gamedata.explosion1f2Surf, gamedata.explosion1f3Surf, gamedata.explosion1f4Surf],
[[0,0],[0,0],[0,0],[0,0]], screen, screenW, screenH
)
all_sprites.add(explosionGob3)
all_gobs.add(explosionGob3)
explosionGob3.setSprite(4, [gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap, gamedata.explosion1ColorMap])
explosionGob3.rect.x = 999
explosionGob3.rect.y = 999
explosionGob3.shouldCycleAnim = False;
explosionGob3.callAfterAnimCycle = HideGobAfterAnimCycle
explosionGob3.setSpriteFrame()
explosionGob3.visible = False
# Draw points
def DrawPoints():
dirtyRect = pygame.Rect(10, 2, 8*4, 10)
screen.fill(0, dirtyRect)
# Draw points
pok.draw_text(10, 2, str(points), 1)
# Update dirty rect
pygame.display.update(False, dirtyRect, True)
# Main
main()