Skip to content

Commit 87aeb2d

Browse files
authored
Merge pull request #3181 from damusss/__main__-guard-examples
Add main guard to examples (+ incredibly small fixes)
2 parents 8b09b1f + 8c377c0 commit 87aeb2d

13 files changed

+682
-620
lines changed

examples/README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ music_drop_fade.py
7878
several events. Uses fade_ms added in pygame2, as well as set_endevent,
7979
set_volume, drag and drop events, and the scrap module.
8080

81+
ninepatch.py
82+
Demonstrate the purpose of the 9-patch scale method and a way to implement it.
83+
8184
pixelarray.py
8285
Process whole arrays of pixels at a time.
8386
Like numpy, but for pixels, and also built into pygame.

examples/audiocapture.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
)
1919
from pygame._sdl2.mixer import set_post_mix
2020

21-
22-
pygame.mixer.pre_init(44100, 32, 2, 512)
23-
pygame.init()
24-
25-
# init_subsystem(INIT_AUDIO)
26-
names = get_audio_device_names(True)
27-
print(names)
28-
2921
sounds = []
3022
sound_chunks = []
3123

@@ -49,31 +41,42 @@ def postmix_callback(postmix, audiomemoryview):
4941
print(postmix)
5042

5143

52-
set_post_mix(postmix_callback)
44+
def main():
45+
pygame.mixer.pre_init(44100, 32, 2, 512)
46+
pygame.init()
5347

54-
audio = AudioDevice(
55-
devicename=names[0],
56-
iscapture=True,
57-
frequency=44100,
58-
audioformat=AUDIO_F32,
59-
numchannels=2,
60-
chunksize=512,
61-
allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE,
62-
callback=callback,
63-
)
64-
# start recording.
65-
audio.pause(0)
48+
# init_subsystem(INIT_AUDIO)
49+
names = get_audio_device_names(True)
50+
print(names)
51+
52+
set_post_mix(postmix_callback)
53+
54+
audio = AudioDevice(
55+
devicename=names[0],
56+
iscapture=True,
57+
frequency=44100,
58+
audioformat=AUDIO_F32,
59+
numchannels=2,
60+
chunksize=512,
61+
allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE,
62+
callback=callback,
63+
)
64+
# start recording.
65+
audio.pause(0)
66+
67+
print(audio)
6668

67-
print(audio)
69+
print(f"recording with '{names[0]}'")
70+
time.sleep(5)
6871

69-
print(f"recording with '{names[0]}'")
70-
time.sleep(5)
72+
print("Turning data into a pygame.mixer.Sound")
73+
sound = pygame.mixer.Sound(buffer=b"".join(sound_chunks))
7174

75+
print("playing back recorded sound")
76+
sound.play()
77+
time.sleep(5)
78+
pygame.quit()
7279

73-
print("Turning data into a pygame.mixer.Sound")
74-
sound = pygame.mixer.Sound(buffer=b"".join(sound_chunks))
7580

76-
print("playing back recorded sound")
77-
sound.play()
78-
time.sleep(5)
79-
pygame.quit()
81+
if __name__ == "__main__":
82+
main()

examples/font_viewer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def render_fonts(self, text="A display of font &N"):
122122
line = text.replace("&N", name)
123123
try:
124124
surf = font.render(
125-
line, 1, color, self.back_color, self.screen_size[0] - 20
125+
line, True, color, self.back_color, self.screen_size[0] - 20
126126
)
127127
except pygame.error as e:
128128
print(e)
@@ -281,5 +281,6 @@ def handle_events(self):
281281
return True
282282

283283

284-
viewer = FontViewer()
285-
pygame.quit()
284+
if __name__ == "__main__":
285+
viewer = FontViewer()
286+
pygame.quit()

examples/go_over_there.py

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
SCREEN_SIZE = pygame.Vector2(1000, 600)
2020
CIRCLE_RADIUS = 5
2121

22-
pygame.init()
23-
screen = pygame.display.set_mode(SCREEN_SIZE)
24-
clock = pygame.Clock()
25-
2622
target_position = None
2723
balls = []
2824

@@ -49,35 +45,47 @@ def reset():
4945
balls.append(b)
5046

5147

52-
reset()
53-
delta_time = 0
54-
running = True
55-
while running:
56-
for event in pygame.event.get():
57-
if event.type == pygame.QUIT:
58-
running = False
48+
def main():
49+
global target_position
50+
global balls
5951

60-
if event.type == pygame.MOUSEBUTTONUP:
61-
target_position = pygame.mouse.get_pos()
52+
pygame.init()
53+
screen = pygame.display.set_mode(SCREEN_SIZE)
54+
clock = pygame.Clock()
6255

63-
if event.type == pygame.KEYUP:
64-
if event.key == pygame.K_ESCAPE:
56+
reset()
57+
delta_time = 0
58+
running = True
59+
while running:
60+
for event in pygame.event.get():
61+
if event.type == pygame.QUIT:
6562
running = False
6663

67-
if event.key == pygame.K_r:
68-
reset()
64+
if event.type == pygame.MOUSEBUTTONUP:
65+
target_position = pygame.mouse.get_pos()
66+
67+
if event.type == pygame.KEYUP:
68+
if event.key == pygame.K_ESCAPE:
69+
running = False
6970

70-
screen.fill((31, 143, 65))
71+
if event.key == pygame.K_r:
72+
reset()
73+
74+
screen.fill((31, 143, 65))
75+
76+
for o in balls:
77+
if target_position is not None:
78+
o.position.move_towards_ip(target_position, o.speed * delta_time)
79+
pygame.draw.circle(screen, (118, 207, 145), o.position, CIRCLE_RADIUS)
80+
81+
pygame.display.flip()
82+
delta_time = clock.tick(60)
83+
pygame.display.set_caption(
84+
f"fps: {round(clock.get_fps(), 2)}, ball count: {len(balls)}"
85+
)
7186

72-
for o in balls:
73-
if target_position is not None:
74-
o.position.move_towards_ip(target_position, o.speed * delta_time)
75-
pygame.draw.circle(screen, (118, 207, 145), o.position, CIRCLE_RADIUS)
87+
pygame.quit()
7688

77-
pygame.display.flip()
78-
delta_time = clock.tick(60)
79-
pygame.display.set_caption(
80-
f"fps: {round(clock.get_fps(), 2)}, ball count: {len(balls)}"
81-
)
8289

83-
pygame.quit()
90+
if __name__ == "__main__":
91+
main()

0 commit comments

Comments
 (0)