2

I'm new to python and pygame and am trying to teach myself, so I'm consequentially a noob. Right now, I'm trying to program a 'cutscene' of sorts (it's just going to be a black screen with sound effects and text that play and show up at certain times) that can be skipped at any time by pressing escape. This is what I have for this portion so far (I haven't added the text yet):

def play_crash():
    pygame.time.delay(500)
    cutsc_crash.play(0)

def play_fwoosh():
    pygame.time.delay(2000)
    cutsc_fwoosh.play(0, 0, 50)

def play_burn():
    pygame.time.delay(2050)
    cutsc_fire.play(0, 15000, 0)

def play_run():
    pygame.time.delay(2500)
    cutsc_run.play()

def intro_cutscene():

    pygame.mixer.music.stop()
    gameDisplay.fill(black)
    pygame.display.update()

    Skip_Cut = False
    play_audio = True

    while not Skip_Cut:
        Clock.tick(60)
        pygame.key.get_pressed()
        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    Skip_Cut = True
                if event.key == pygame.K_RETURN:
                    print("in a perfect universe, this would do something more significant.")
            if event.type == pygame.QUIT:
                print("Ending program.")
                pygame.quit()
                quit()
        if play_audio:
            play_crash()
            play_fwoosh()
            play_burn()
            play_run()
            play_audio = False
            pygame.display.update()

    if Skip_Cut:
        pygame.display.update()
        pygame.mixer.stop()
        print("Skipping cutscene...")
        print(game_test())

As of right now, I'm using pygame.time.delay() to make the sounds wait a while before playing, but of course, this delays all output from appearing and consequentially doesn't actually skip the cutscene by pressing escape until all sounds have been played, however brief. I know this issue can be fixed by using pygame.time.set_timer (or at least I hope). However, I can't figure out for the life of me how exactly to use that command.

Katelin
  • 21
  • 1
  • 3
  • If the cut-scene audio track were a single sound (file), then it could simply be played (in the background) and would stop with `pygame.mixer.stop()`. The issue in your code is the `pygame.time.delay()` in each sound-playing function (presumably so they come after one-another). A PyGame timer inserts events into the event-queue, I can't see how it would really help with this. – Kingsley Jan 21 '19 at 01:32
  • Ah, I was under the impression that by using `pygame.time.set_timer()` I could insert playing the audio into the queue after a certain interval of time without using `pygame.time.delay()`. If that's not the case, what exactly is the usage of `pygame.time.set_timer()`? I'm a little unclear on it. – Katelin Jan 21 '19 at 03:05
  • Well it could probably be done this way. The timer is more for some periodic function that needs to be handled in the event loop. It might post the event `CAT_FOOD_EMPTY` every 30 seconds to the event queue. See the doco: https://www.pygame.org/docs/ref/time.html#pygame.time.set_timer I guess it could be set, cancelled, set, cancelled with different times for your use-case. – Kingsley Jan 21 '19 at 03:17

1 Answers1

3

You basically have two things to do:

  • keep track of the current scene that you game is running. Typical scenes are something like intro, main menu, the actual game, credits etc.
  • keep track of time so you can maintain a list of points in time and actions you'll want to run.

Below is a simple, runnable example (note the comments).

The idea is to

  • have an easy way to switch between scenes. This is done by using a GroupSingleand each scene having a way to enable the next one.
  • have a list of timed actions and run them at the right point in time

Here's the code:

import pygame
import pygame.freetype
import random

# Just a ball that falls down
class Ball(pygame.sprite.Sprite):
    def __init__(self, *groups):
        super().__init__(*groups)
        self.image = pygame.Surface((32, 32))
        self.image.set_colorkey((0, 0, 0))
        self.image.fill((0, 0, 0))
        self.rect = self.image.get_rect()
        pygame.draw.circle(self.image, pygame.Color('orange'), self.rect.center, 15)
        self.pos = pygame.Vector2(random.randint(0, 200), -10)
        self.rect.center = self.pos
        self.direction = pygame.Vector2(0, 0.1)

    def update(self, events, dt):
        self.direction += pygame.Vector2(0, 0.02)
        self.pos += self.direction * dt
        self.rect.center = self.pos
        if not pygame.display.get_surface().get_rect().colliderect(self.rect):
            self.kill()

# The actual game. Well, actually, it does nothing but switching back to the cutscene
class Game(pygame.sprite.Sprite):
    def __init__(self, font):
        super().__init__()
        self.switch = None
        self.image = pygame.display.get_surface().copy()
        self.image.fill(pygame.Color('darkred'))
        font.render_to(self.image, (10, 30), 'playing a game...', fgcolor=pygame.Color('orange'))
        self.rect = self.image.get_rect()

    def update(self, events, dt):
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE and self.switch:
                    self.switch()

# the scripted cutscene
class Cutscene(pygame.sprite.Sprite):
    def __init__(self, font):
        super().__init__()
        self.font = font
        self.switch = None
        self.image = pygame.display.get_surface().copy()
        self.back_colors = {-1: pygame.Color('grey12'), 1: pygame.Color('black')}
        self.back_color = 1
        self.background()
        self.rect = self.image.get_rect()
        # we keep track of time to know when to do what action
        self.timer = 0
        self.sprites = pygame.sprite.Group()
        # we keep this list of actions, so after 500ms we create the first ball etc
        # after 3 seconds, we change the background color etc.
        # after 4 seconds, we start all over again
        self.org_actions = [
            (500, lambda: Ball(self.sprites)),
            (600, lambda: Ball(self.sprites)),
            (1000, lambda: Ball(self.sprites)),
            (2000, lambda: Ball(self.sprites)),
            (2100, lambda: Ball(self.sprites)),
            (2400, lambda: Ball(self.sprites)),
            (3000, lambda: self.switch_background()),
            (3200, lambda: Ball(self.sprites)),
            (4000, lambda: self.reset_timer())
        ]
        self.actions = self.org_actions

    def reset_timer(self):
        self.timer = 0
        self.actions = self.org_actions

    def switch_background(self):
        self.back_color *= -1

    def background(self):
        self.image.fill(self.back_colors[self.back_color])
        self.font.render_to(self.image, (10, 30), 'press [ESC] to quit', fgcolor=pygame.Color('white'))

    def update(self, events, dt):
        # we switch to a different scene when the player presses ESC
        for event in events:
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE and self.switch:
                    self.switch()

        # keep track of time
        self.timer += dt

        # do all actions 
        for action in [action for action in self.actions if action[0] <= self.timer]:
            action[1]()

        # remove all actions that are in the past
        self.actions = [action for action in self.actions if action[0] > self.timer]

        # update our own sprites and draw stuff
        self.sprites.update(events, dt)
        self.background()
        self.sprites.draw(self.image)

def main():
    font = pygame.freetype.SysFont(None, 20)
    font.origin = True
    screen = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    scene = pygame.sprite.GroupSingle()

    game = Game(font)
    cutscene = Cutscene(font)
    game.switch = lambda: scene.add(cutscene)
    cutscene.switch = lambda: scene.add(game)
    scene.add(cutscene)
    dt = 0

    while True:
        events = pygame.event.get()
        for event in events:
            if event.type == pygame.QUIT:
                return
        screen.fill((200,200,200))
        scene.draw(screen)
        pygame.display.update()
        scene.update(events, dt)
        dt = clock.tick(60)

if __name__ == '__main__':
    pygame.init()
    main()

enter image description here


If you want to know more about pygame.time.set_timer, you could take a look at this answer.

sloth
  • 99,095
  • 21
  • 171
  • 219