Skip to content

Commit

Permalink
Merge pull request #3181 from damusss/__main__-guard-examples
Browse files Browse the repository at this point in the history
Add main guard to examples (+ incredibly small fixes)
  • Loading branch information
ankith26 authored Oct 21, 2024
2 parents 8b09b1f + 8c377c0 commit 87aeb2d
Show file tree
Hide file tree
Showing 13 changed files with 682 additions and 620 deletions.
3 changes: 3 additions & 0 deletions examples/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ music_drop_fade.py
several events. Uses fade_ms added in pygame2, as well as set_endevent,
set_volume, drag and drop events, and the scrap module.

ninepatch.py
Demonstrate the purpose of the 9-patch scale method and a way to implement it.

pixelarray.py
Process whole arrays of pixels at a time.
Like numpy, but for pixels, and also built into pygame.
Expand Down
63 changes: 33 additions & 30 deletions examples/audiocapture.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
)
from pygame._sdl2.mixer import set_post_mix


pygame.mixer.pre_init(44100, 32, 2, 512)
pygame.init()

# init_subsystem(INIT_AUDIO)
names = get_audio_device_names(True)
print(names)

sounds = []
sound_chunks = []

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


set_post_mix(postmix_callback)
def main():
pygame.mixer.pre_init(44100, 32, 2, 512)
pygame.init()

audio = AudioDevice(
devicename=names[0],
iscapture=True,
frequency=44100,
audioformat=AUDIO_F32,
numchannels=2,
chunksize=512,
allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE,
callback=callback,
)
# start recording.
audio.pause(0)
# init_subsystem(INIT_AUDIO)
names = get_audio_device_names(True)
print(names)

set_post_mix(postmix_callback)

audio = AudioDevice(
devicename=names[0],
iscapture=True,
frequency=44100,
audioformat=AUDIO_F32,
numchannels=2,
chunksize=512,
allowed_changes=AUDIO_ALLOW_FORMAT_CHANGE,
callback=callback,
)
# start recording.
audio.pause(0)

print(audio)

print(audio)
print(f"recording with '{names[0]}'")
time.sleep(5)

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

print("playing back recorded sound")
sound.play()
time.sleep(5)
pygame.quit()

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

print("playing back recorded sound")
sound.play()
time.sleep(5)
pygame.quit()
if __name__ == "__main__":
main()
7 changes: 4 additions & 3 deletions examples/font_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def render_fonts(self, text="A display of font &N"):
line = text.replace("&N", name)
try:
surf = font.render(
line, 1, color, self.back_color, self.screen_size[0] - 20
line, True, color, self.back_color, self.screen_size[0] - 20
)
except pygame.error as e:
print(e)
Expand Down Expand Up @@ -281,5 +281,6 @@ def handle_events(self):
return True


viewer = FontViewer()
pygame.quit()
if __name__ == "__main__":
viewer = FontViewer()
pygame.quit()
64 changes: 36 additions & 28 deletions examples/go_over_there.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
SCREEN_SIZE = pygame.Vector2(1000, 600)
CIRCLE_RADIUS = 5

pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
clock = pygame.Clock()

target_position = None
balls = []

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


reset()
delta_time = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
def main():
global target_position
global balls

if event.type == pygame.MOUSEBUTTONUP:
target_position = pygame.mouse.get_pos()
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
clock = pygame.Clock()

if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
reset()
delta_time = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

if event.key == pygame.K_r:
reset()
if event.type == pygame.MOUSEBUTTONUP:
target_position = pygame.mouse.get_pos()

if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False

screen.fill((31, 143, 65))
if event.key == pygame.K_r:
reset()

screen.fill((31, 143, 65))

for o in balls:
if target_position is not None:
o.position.move_towards_ip(target_position, o.speed * delta_time)
pygame.draw.circle(screen, (118, 207, 145), o.position, CIRCLE_RADIUS)

pygame.display.flip()
delta_time = clock.tick(60)
pygame.display.set_caption(
f"fps: {round(clock.get_fps(), 2)}, ball count: {len(balls)}"
)

for o in balls:
if target_position is not None:
o.position.move_towards_ip(target_position, o.speed * delta_time)
pygame.draw.circle(screen, (118, 207, 145), o.position, CIRCLE_RADIUS)
pygame.quit()

pygame.display.flip()
delta_time = clock.tick(60)
pygame.display.set_caption(
f"fps: {round(clock.get_fps(), 2)}, ball count: {len(balls)}"
)

pygame.quit()
if __name__ == "__main__":
main()
Loading

0 comments on commit 87aeb2d

Please sign in to comment.