Skip to content

Instantly share code, notes, and snippets.

@holdenrehg
Created June 23, 2021 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holdenrehg/fb49af3d3a1ca17b096ab8a7d3a6a5ec to your computer and use it in GitHub Desktop.
Save holdenrehg/fb49af3d3a1ca17b096ab8a7d3a6a5ec to your computer and use it in GitHub Desktop.
Menu/scene example using pygame
"""
Sample game showing a pattern for rendering scenes and menus. This was written
for the blog entry at holdenrehg.github.io/catchergames/2021/06/22/menus-and-scenes.html
Requirements for running:
python3.8
pygame==2.*
@author github.com/holdenrehg
"""
import abc
import traceback
import pygame
# ------------------------------------------------------------------------------
# Globals
# ------------------------------------------------------------------------------
current_game = None
# ------------------------------------------------------------------------------
# The Engine
# ------------------------------------------------------------------------------
class Game:
def __init__(self):
self.clock = pygame.time.Clock()
self.next_scene = None
self.screen = pygame.display.set_mode((600, 400))
class Scene(abc.ABC):
running = True
@abc.abstractmethod
def play(self):
...
def run(self):
self.running = True
while self.running:
current_game.screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
try:
self.play()
except Exception as e:
traceback.print_exc(e)
self.quit()
current_game.clock.tick(60) # frames per second
pygame.display.flip() # render the display
def quit(self):
self.running = False
# ------------------------------------------------------------------------------
# Scene's
# ------------------------------------------------------------------------------
class MainMenuScene(Scene):
def __init__(self):
self.frames = 0
def play(self):
"""
Count down from 5 to 0, and once the timer hits 0 we start the game.
"""
self.frames += 1
countdown = max(5 - round(self.frames / 60), 0)
if countdown > 0:
current_game.screen.blit(
pygame.font.SysFont(None, 80).render(
f"Game starting in {countdown}", False, (255, 255, 255)
),
(5, 5),
)
else:
self.quit()
current_game.next_scene = GameplayScene()
class GameplayScene(Scene):
def play(self):
current_game.screen.blit(
pygame.font.SysFont(None, 60).render(
"Welcome to the game.", False, (255, 255, 255)
),
(5, 5),
)
# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------
def main():
pygame.init()
pygame.display.init()
pygame.mixer.init()
global current_game
current_game = Game()
current_game.next_scene = MainMenuScene()
while current_game.next_scene:
next_scene = current_game.next_scene
current_game.next_scene = None
next_scene.run()
pygame.quit()
raise SystemExit
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment