Skip to content

Commit 423fdb7

Browse files
bilhoxMyreMylar
andauthored
General example changes (#2581)
* scaletest.py fixes * example fixes * format fix * format fixes * liquid example fix * format fic * removed time import * fix 1 * update example * droid problem fixes * alien example update * chimp example * removed durk from buildconfig * Revert "removed durk from buildconfig" This reverts commit d5897be. * Revert "chimp example" This reverts commit 5ed783e. * chimp tutorial update * format fix * playmus fix * Further aliens.py cleanups * Further scaletest.py cleanups * Update examples/aliens.py * formatting * scaletest fix using irl key access * clock scaletest * revert changes resizing_new --------- Co-authored-by: Dan Lawrence <[email protected]>
1 parent 2de60f3 commit 423fdb7

File tree

10 files changed

+217
-279
lines changed

10 files changed

+217
-279
lines changed

docs/reST/tutorials/en/chimp-explanation.rst

+19-42
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ It also checks for the availability of some of the optional pygame modules. ::
6565
import os
6666
import pygame
6767

68-
if not pygame.font:
69-
print("Warning, fonts disabled")
70-
if not pygame.mixer:
71-
print("Warning, sound disabled")
72-
7368
main_dir = os.path.split(os.path.abspath(__file__))[0]
7469
data_dir = os.path.join(main_dir, "data")
7570

@@ -79,12 +74,6 @@ us to do things like create platform independent file paths.
7974

8075
In the next line, we import the pygame package.
8176

82-
Some pygame modules are optional, and if they aren't found,
83-
they evaluate to ``False``. Because of that, we decide to print
84-
a nice warning message if the :mod:`font<pygame.font>` or
85-
:mod:`mixer <pygame.mixer>` modules in pygame are not available.
86-
(Although they will only be unavailable in very uncommon situations).
87-
8877
Lastly, we prepare two paths for the rest of the code to use.
8978
``main_dir`` uses the `os.path` module and the `__file__` variable provided
9079
by Python to locate the game's python file, and extract the folder from
@@ -101,12 +90,10 @@ look at each function individually in this section. ::
10190
def load_image(name, colorkey=None, scale=1):
10291
fullname = os.path.join(data_dir, name)
10392
image = pygame.image.load(fullname)
93+
image = image.convert()
10494

105-
size = image.get_size()
106-
size = (size[0] * scale, size[1] * scale)
107-
image = pygame.transform.scale(image, size)
95+
image = pygame.transform.scale_by(image, scale)
10896

109-
image = image.convert()
11097
if colorkey is not None:
11198
if colorkey == -1:
11299
colorkey = image.get_at((0, 0))
@@ -130,9 +117,8 @@ call to the `convert()` function. This makes a new copy of a Surface and convert
130117
its color format and depth to match the display. This means blitting the
131118
image to the screen will happen as quickly as possible.
132119

133-
We then scale the image, using the :func:`pygame.transform.scale` function.
134-
This function takes a Surface and the size it should be scaled to. To scale
135-
by a scalar, we can get the size and scale the x and y by the scalar.
120+
We then scale the image, using the :func:`pygame.transform.scale_by` function.
121+
This function takes a Surface and the scale factor it should be scaled of.
136122

137123
Last, we set the colorkey for the image. If the user supplied an argument
138124
for the colorkey argument we use that value as the colorkey for the image.
@@ -146,7 +132,7 @@ that color for the colorkey. ::
146132
def play(self):
147133
pass
148134

149-
if not pygame.mixer or not pygame.mixer.get_init():
135+
if not pygame.mixer.get_init():
150136
return NoneSound()
151137

152138
fullname = os.path.join(data_dir, name)
@@ -156,7 +142,7 @@ that color for the colorkey. ::
156142

157143

158144
Next is the function to load a sound file. The first thing this function
159-
does is check to see if the :mod:`pygame.mixer` module was imported correctly.
145+
does is check to see if the :mod:`pygame.mixer` module was initialized.
160146
If not, it returns a small class instance that has a placeholder play method.
161147
This will act enough like a normal Sound object for this game to run without
162148
any extra error checking.
@@ -367,15 +353,12 @@ input formats. See the :mod:`pygame.Color` for all the color formats.
367353
Put Text On The Background, Centered
368354
------------------------------------
369355

370-
Now that we have a background surface, lets get the text rendered to it. We
371-
only do this if we see the :mod:`pygame.font` module has imported properly.
372-
If not, we just skip this section. ::
356+
Now that we have a background surface, lets get the text rendered to it. ::
373357

374-
if pygame.font:
375-
font = pygame.font.Font(None, 64)
376-
text = font.render("Pummel The Chimp, And Win $$$", True, (10, 10, 10))
377-
textpos = text.get_rect(centerx=background.get_width() / 2, y=10)
378-
background.blit(text, textpos)
358+
font = pygame.font.Font(None, 64)
359+
text = font.render("Pummel The Chimp, And Win $$$", True, (10, 10, 10))
360+
textpos = text.get_rect(centerx=background.get_width() / 2, y=10)
361+
background.blit(text, textpos)
379362

380363
As you see, there are a couple steps to getting this done. First we
381364
must create the font object and render it into a new surface. We then find
@@ -428,19 +411,15 @@ Here we create all the objects that the game is going to need.
428411
punch_sound = load_sound("punch.wav")
429412
chimp = Chimp()
430413
fist = Fist()
431-
allsprites = pygame.sprite.RenderPlain((chimp, fist))
432-
clock = pygame.time.Clock()
414+
all_sprites = pygame.sprite.Group((chimp, fist))
415+
clock = pygame.Clock()
433416

434417
First we load two sound effects using the `load_sound` function we defined
435418
above. Then we create an instance of each of our sprite classes. And lastly
436419
we create a sprite :class:`Group <pygame.sprite.Group>` which will contain all
437420
our sprites.
438421

439-
We actually use a special sprite group named :class:`RenderPlain
440-
<pygame.sprite.RenderPlain>`. This sprite group can draw all the sprites it
441-
contains to the screen. It is called `RenderPlain` because there are actually
442-
more advanced Render groups. But for our game, we just need simple drawing. We
443-
create the group named "allsprites" by passing a list with all the sprites that
422+
We create the group named "all_sprites" by passing a list with all the sprites that
444423
should belong in the group. We could later on add or remove sprites from this
445424
group, but in this game we won't need to.
446425

@@ -501,7 +480,7 @@ Update the Sprites
501480

502481
::
503482

504-
allsprites.update()
483+
all_sprites.update()
505484

506485
Sprite groups have an `update()` method, which simply calls the update method
507486
for all the sprites it contains. Each of the objects will move around, depending
@@ -515,16 +494,14 @@ Draw The Entire Scene
515494
Now that all the objects are in the right place, time to draw them. ::
516495

517496
screen.blit(background, (0, 0))
518-
allsprites.draw(screen)
497+
all_sprites.draw(screen)
519498
pygame.display.flip()
520499

521500
The first blit call will draw the background onto the entire screen. This
522501
erases everything we saw from the previous frame (slightly inefficient, but
523502
good enough for this game). Next we call the `draw()` method of the sprite
524-
container. Since this sprite container is really an instance of the "RenderPlain"
525-
sprite group, it knows how to draw our sprites. Lastly, we `flip()` the contents
526-
of pygame's software double buffer to the screen. This makes everything we've
527-
drawn visible all at once.
503+
container. Lastly, we `flip()` the contentsof pygame's software double buffer
504+
to the screen. This makes everything we've drawn visible all at once.
528505

529506

530507
Game Over
@@ -536,4 +513,4 @@ User has quit, time to clean up. ::
536513

537514
Cleaning up the running game in *pygame* is extremely simple.
538515
Since all variables are automatically destructed, we don't really have to do
539-
anything, but calling `pygame.quit()` explicitly cleans up pygame's internals.
516+
anything, but calling `pygame.quit()` explicitly cleans up pygame's internals.

examples/aacircle.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def main():
1010
pygame.init()
1111
screen = pygame.display.set_mode((500, 500))
1212
screen.fill((255, 0, 0))
13-
s = pygame.Surface(screen.get_size(), pygame.SRCALPHA, 32)
13+
s = pygame.Surface(screen.get_size(), pygame.SRCALPHA)
1414
pygame.draw.line(s, (0, 0, 0), (250, 250), (250 + 200, 250))
1515

1616
width = 1
@@ -23,18 +23,23 @@ def main():
2323
pygame.draw.circle(screen, "green", (50, 100), 10)
2424
pygame.draw.circle(screen, "black", (50, 100), 10, 1)
2525

26-
pygame.display.flip()
27-
try:
28-
while True:
29-
event = pygame.event.wait()
26+
running = True
27+
28+
clock = pygame.Clock()
29+
30+
while running:
31+
clock.tick(10)
32+
33+
for event in pygame.event.get():
3034
if event.type == pygame.QUIT:
31-
break
32-
if event.type == pygame.KEYDOWN:
33-
if event.key == pygame.K_ESCAPE or event.unicode == "q":
34-
break
35-
pygame.display.flip()
36-
finally:
37-
pygame.quit()
35+
running = False
36+
elif event.type == pygame.KEYDOWN:
37+
if event.key == pygame.K_q:
38+
running = False
39+
40+
pygame.display.flip()
41+
42+
pygame.quit()
3843

3944

4045
if __name__ == "__main__":

examples/aliens.py

+31-27
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,10 @@ class Player(pygame.sprite.Sprite):
8383
bounce = 24
8484
gun_offset = -11
8585
images = []
86+
containers = None
8687

8788
def __init__(self):
88-
pygame.sprite.Sprite.__init__(self, self.containers)
89+
super().__init__(self.containers)
8990
self.image = self.images[0]
9091
self.rect = self.image.get_rect(midbottom=SCREENRECT.midbottom)
9192
self.reloading = False
@@ -114,6 +115,7 @@ class Alien(pygame.sprite.Sprite):
114115
speed = 13
115116
animcycle = 12
116117
images = []
118+
containers = None
117119

118120
def __init__(self):
119121
pygame.sprite.Sprite.__init__(self, self.containers)
@@ -140,6 +142,7 @@ class Explosion(pygame.sprite.Sprite):
140142
defaultlife = 12
141143
animcycle = 3
142144
images = []
145+
containers = None
143146

144147
def __init__(self, actor):
145148
pygame.sprite.Sprite.__init__(self, self.containers)
@@ -166,6 +169,7 @@ class Shot(pygame.sprite.Sprite):
166169

167170
speed = -11
168171
images = []
172+
containers = None
169173

170174
def __init__(self, pos):
171175
pygame.sprite.Sprite.__init__(self, self.containers)
@@ -187,6 +191,7 @@ class Bomb(pygame.sprite.Sprite):
187191

188192
speed = 9
189193
images = []
194+
containers = None
190195

191196
def __init__(self, alien):
192197
pygame.sprite.Sprite.__init__(self, self.containers)
@@ -214,7 +219,7 @@ class Score(pygame.sprite.Sprite):
214219
def __init__(self):
215220
pygame.sprite.Sprite.__init__(self)
216221
self.font = pygame.Font(None, 20)
217-
self.font.set_italic(1)
222+
self.font.set_italic(True)
218223
self.color = "white"
219224
self.lastscore = -1
220225
self.update()
@@ -225,10 +230,10 @@ def update(self):
225230
if SCORE != self.lastscore:
226231
self.lastscore = SCORE
227232
msg = "Score: %d" % SCORE
228-
self.image = self.font.render(msg, 0, self.color)
233+
self.image = self.font.render(msg, False, self.color)
229234

230235

231-
def main(winstyle=0):
236+
def main():
232237
# Initialize pygame
233238
pygame.mixer.pre_init(44100, 32, 2, 1024)
234239
pygame.init()
@@ -244,9 +249,9 @@ def main(winstyle=0):
244249
# Load images, assign to sprite classes
245250
# (do this before the classes are used, after screen setup)
246251
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)]
248253
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)]
250255
Alien.images = [load_image(im) for im in ("alien1.gif", "alien2.gif", "alien3.gif")]
251256
Bomb.images = [load_image("bomb.gif")]
252257
Shot.images = [load_image("shot.gif")]
@@ -255,14 +260,13 @@ def main(winstyle=0):
255260
icon = pygame.transform.scale(Alien.images[0], (32, 32))
256261
pygame.display.set_icon(icon)
257262
pygame.display.set_caption("Pygame Aliens")
258-
pygame.mouse.set_visible(0)
263+
pygame.mouse.set_visible(False)
259264

260265
# create the background, tile the bgd image
261266
bgdtile = load_image("background.gif")
262267
background = pygame.Surface(SCREENRECT.size)
263268
for x in range(0, SCREENRECT.width, bgdtile.get_width()):
264269
background.blit(bgdtile, (x, 0))
265-
screen.blit(background, (0, 0))
266270
pygame.display.flip()
267271

268272
# load the sound effects
@@ -277,19 +281,18 @@ def main(winstyle=0):
277281
aliens = pygame.sprite.Group()
278282
shots = pygame.sprite.Group()
279283
bombs = pygame.sprite.Group()
280-
all = pygame.sprite.RenderUpdates()
284+
all_sprites = pygame.sprite.Group()
281285
lastalien = pygame.sprite.GroupSingle()
282286

283287
# 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
290294

291295
# Create Some Starting Values
292-
global score
293296
alienreload = ALIEN_RELOAD
294297
clock = pygame.Clock()
295298

@@ -298,10 +301,13 @@ def main(winstyle=0):
298301
player = Player()
299302
Alien() # note, this 'lives' because it goes into a sprite group
300303
if pygame.font:
301-
all.add(Score())
304+
all_sprites.add(Score())
302305

303306
# Run our main loop whilst the player is alive.
304307
while player.alive():
308+
# Erase last frame by blitting the background to the screen
309+
screen.blit(background, (0, 0))
310+
305311
# get input
306312
for event in pygame.event.get():
307313
if event.type == pygame.QUIT:
@@ -314,7 +320,8 @@ def main(winstyle=0):
314320
print("Changing to FULLSCREEN")
315321
screen_backup = screen.copy()
316322
screen = pygame.display.set_mode(
317-
SCREENRECT.size, winstyle | pygame.FULLSCREEN
323+
SCREENRECT.size,
324+
winstyle | pygame.FULLSCREEN | pygame.SCALED,
318325
)
319326
screen.blit(screen_backup, (0, 0))
320327
else:
@@ -327,11 +334,8 @@ def main(winstyle=0):
327334

328335
keystate = pygame.key.get_pressed()
329336

330-
# clear/erase the last drawn sprites
331-
all.clear(screen, background)
332-
333337
# update all the sprites
334-
all.update()
338+
all_sprites.update()
335339

336340
# handle player input
337341
direction = keystate[pygame.K_RIGHT] - keystate[pygame.K_LEFT]
@@ -355,7 +359,7 @@ def main(winstyle=0):
355359
Bomb(lastalien.sprite)
356360

357361
# 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):
359363
if pygame.mixer:
360364
boom_sound.play()
361365
Explosion(alien)
@@ -364,23 +368,23 @@ def main(winstyle=0):
364368
player.kill()
365369

366370
# 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():
368372
if pygame.mixer:
369373
boom_sound.play()
370374
Explosion(alien)
371375
SCORE = SCORE + 1
372376

373377
# 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):
375379
if pygame.mixer:
376380
boom_sound.play()
377381
Explosion(player)
378382
Explosion(bomb)
379383
player.kill()
380384

381385
# draw the scene
382-
dirty = all.draw(screen)
383-
pygame.display.update(dirty)
386+
all_sprites.draw(screen)
387+
pygame.display.flip()
384388

385389
# cap the framerate at 40fps. Also called 40HZ or 40 times per second.
386390
clock.tick(40)

0 commit comments

Comments
 (0)