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
GroupSingle
and 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()

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