@@ -83,9 +83,10 @@ class Player(pygame.sprite.Sprite):
83
83
bounce = 24
84
84
gun_offset = - 11
85
85
images = []
86
+ containers = None
86
87
87
88
def __init__ (self ):
88
- pygame . sprite . Sprite . __init__ (self , self .containers )
89
+ super (). __init__ (self .containers )
89
90
self .image = self .images [0 ]
90
91
self .rect = self .image .get_rect (midbottom = SCREENRECT .midbottom )
91
92
self .reloading = False
@@ -114,6 +115,7 @@ class Alien(pygame.sprite.Sprite):
114
115
speed = 13
115
116
animcycle = 12
116
117
images = []
118
+ containers = None
117
119
118
120
def __init__ (self ):
119
121
pygame .sprite .Sprite .__init__ (self , self .containers )
@@ -140,6 +142,7 @@ class Explosion(pygame.sprite.Sprite):
140
142
defaultlife = 12
141
143
animcycle = 3
142
144
images = []
145
+ containers = None
143
146
144
147
def __init__ (self , actor ):
145
148
pygame .sprite .Sprite .__init__ (self , self .containers )
@@ -166,6 +169,7 @@ class Shot(pygame.sprite.Sprite):
166
169
167
170
speed = - 11
168
171
images = []
172
+ containers = None
169
173
170
174
def __init__ (self , pos ):
171
175
pygame .sprite .Sprite .__init__ (self , self .containers )
@@ -187,6 +191,7 @@ class Bomb(pygame.sprite.Sprite):
187
191
188
192
speed = 9
189
193
images = []
194
+ containers = None
190
195
191
196
def __init__ (self , alien ):
192
197
pygame .sprite .Sprite .__init__ (self , self .containers )
@@ -214,7 +219,7 @@ class Score(pygame.sprite.Sprite):
214
219
def __init__ (self ):
215
220
pygame .sprite .Sprite .__init__ (self )
216
221
self .font = pygame .Font (None , 20 )
217
- self .font .set_italic (1 )
222
+ self .font .set_italic (True )
218
223
self .color = "white"
219
224
self .lastscore = - 1
220
225
self .update ()
@@ -225,10 +230,10 @@ def update(self):
225
230
if SCORE != self .lastscore :
226
231
self .lastscore = SCORE
227
232
msg = "Score: %d" % SCORE
228
- self .image = self .font .render (msg , 0 , self .color )
233
+ self .image = self .font .render (msg , False , self .color )
229
234
230
235
231
- def main (winstyle = 0 ):
236
+ def main ():
232
237
# Initialize pygame
233
238
pygame .mixer .pre_init (44100 , 32 , 2 , 1024 )
234
239
pygame .init ()
@@ -244,9 +249,9 @@ def main(winstyle=0):
244
249
# Load images, assign to sprite classes
245
250
# (do this before the classes are used, after screen setup)
246
251
img = load_image ("player1.gif" )
247
- Player .images = [img , pygame .transform .flip (img , 1 , 0 )]
252
+ Player .images = [img , pygame .transform .flip (img , True , False )]
248
253
img = load_image ("explosion1.gif" )
249
- Explosion .images = [img , pygame .transform .flip (img , 1 , 1 )]
254
+ Explosion .images = [img , pygame .transform .flip (img , True , True )]
250
255
Alien .images = [load_image (im ) for im in ("alien1.gif" , "alien2.gif" , "alien3.gif" )]
251
256
Bomb .images = [load_image ("bomb.gif" )]
252
257
Shot .images = [load_image ("shot.gif" )]
@@ -255,14 +260,13 @@ def main(winstyle=0):
255
260
icon = pygame .transform .scale (Alien .images [0 ], (32 , 32 ))
256
261
pygame .display .set_icon (icon )
257
262
pygame .display .set_caption ("Pygame Aliens" )
258
- pygame .mouse .set_visible (0 )
263
+ pygame .mouse .set_visible (False )
259
264
260
265
# create the background, tile the bgd image
261
266
bgdtile = load_image ("background.gif" )
262
267
background = pygame .Surface (SCREENRECT .size )
263
268
for x in range (0 , SCREENRECT .width , bgdtile .get_width ()):
264
269
background .blit (bgdtile , (x , 0 ))
265
- screen .blit (background , (0 , 0 ))
266
270
pygame .display .flip ()
267
271
268
272
# load the sound effects
@@ -277,19 +281,18 @@ def main(winstyle=0):
277
281
aliens = pygame .sprite .Group ()
278
282
shots = pygame .sprite .Group ()
279
283
bombs = pygame .sprite .Group ()
280
- all = pygame .sprite .RenderUpdates ()
284
+ all_sprites = pygame .sprite .Group ()
281
285
lastalien = pygame .sprite .GroupSingle ()
282
286
283
287
# assign default groups to each sprite class
284
- Player .containers = all
285
- Alien .containers = aliens , all , lastalien
286
- Shot .containers = shots , all
287
- Bomb .containers = bombs , all
288
- Explosion .containers = all
289
- Score .containers = all
288
+ Player .containers = all_sprites
289
+ Alien .containers = aliens , all_sprites , lastalien
290
+ Shot .containers = shots , all_sprites
291
+ Bomb .containers = bombs , all_sprites
292
+ Explosion .containers = all_sprites
293
+ Score .containers = all_sprites
290
294
291
295
# Create Some Starting Values
292
- global score
293
296
alienreload = ALIEN_RELOAD
294
297
clock = pygame .Clock ()
295
298
@@ -298,10 +301,13 @@ def main(winstyle=0):
298
301
player = Player ()
299
302
Alien () # note, this 'lives' because it goes into a sprite group
300
303
if pygame .font :
301
- all .add (Score ())
304
+ all_sprites .add (Score ())
302
305
303
306
# Run our main loop whilst the player is alive.
304
307
while player .alive ():
308
+ # Erase last frame by blitting the background to the screen
309
+ screen .blit (background , (0 , 0 ))
310
+
305
311
# get input
306
312
for event in pygame .event .get ():
307
313
if event .type == pygame .QUIT :
@@ -314,7 +320,8 @@ def main(winstyle=0):
314
320
print ("Changing to FULLSCREEN" )
315
321
screen_backup = screen .copy ()
316
322
screen = pygame .display .set_mode (
317
- SCREENRECT .size , winstyle | pygame .FULLSCREEN
323
+ SCREENRECT .size ,
324
+ winstyle | pygame .FULLSCREEN | pygame .SCALED ,
318
325
)
319
326
screen .blit (screen_backup , (0 , 0 ))
320
327
else :
@@ -327,11 +334,8 @@ def main(winstyle=0):
327
334
328
335
keystate = pygame .key .get_pressed ()
329
336
330
- # clear/erase the last drawn sprites
331
- all .clear (screen , background )
332
-
333
337
# update all the sprites
334
- all .update ()
338
+ all_sprites .update ()
335
339
336
340
# handle player input
337
341
direction = keystate [pygame .K_RIGHT ] - keystate [pygame .K_LEFT ]
@@ -355,7 +359,7 @@ def main(winstyle=0):
355
359
Bomb (lastalien .sprite )
356
360
357
361
# Detect collisions between aliens and players.
358
- for alien in pygame .sprite .spritecollide (player , aliens , 1 ):
362
+ for alien in pygame .sprite .spritecollide (player , aliens , True ):
359
363
if pygame .mixer :
360
364
boom_sound .play ()
361
365
Explosion (alien )
@@ -364,23 +368,23 @@ def main(winstyle=0):
364
368
player .kill ()
365
369
366
370
# See if shots hit the aliens.
367
- for alien in pygame .sprite .groupcollide (aliens , shots , 1 , 1 ).keys ():
371
+ for alien in pygame .sprite .groupcollide (aliens , shots , True , True ).keys ():
368
372
if pygame .mixer :
369
373
boom_sound .play ()
370
374
Explosion (alien )
371
375
SCORE = SCORE + 1
372
376
373
377
# See if alien bombs hit the player.
374
- for bomb in pygame .sprite .spritecollide (player , bombs , 1 ):
378
+ for bomb in pygame .sprite .spritecollide (player , bombs , True ):
375
379
if pygame .mixer :
376
380
boom_sound .play ()
377
381
Explosion (player )
378
382
Explosion (bomb )
379
383
player .kill ()
380
384
381
385
# draw the scene
382
- dirty = all .draw (screen )
383
- pygame .display .update ( dirty )
386
+ all_sprites .draw (screen )
387
+ pygame .display .flip ( )
384
388
385
389
# cap the framerate at 40fps. Also called 40HZ or 40 times per second.
386
390
clock .tick (40 )
0 commit comments