Skip to content

Instantly share code, notes, and snippets.

@rolika
Last active June 23, 2022 14:00
Show Gist options
  • Select an option

  • Save rolika/b0f70e18fb562e050ae58ea31327f769 to your computer and use it in GitHub Desktop.

Select an option

Save rolika/b0f70e18fb562e050ae58ea31327f769 to your computer and use it in GitHub Desktop.
import pygame
from pygame.locals import *
from pygame import freetype, time, Surface # import freetype here to initialize it
from enum import Enum
from game import Game
SCREEN_SIZE = (640, 480)
FRAMERATE = 60
BLACK = (0, 0, 0)
State = Enum("State", "TITLE INSTRUCTIONS PLAY PAUSED OVER")
class Main():
def __init__(self, screen_size:tuple, framerate:int, state:State, background:pygame.Color) -> None:
screen = pygame.display.set_mode(screen_size)
clock = time.Clock()
pygame.init()
self._run(screen, state, framerate, clock, background)
def _run(self, screen:pygame.Surface, state:State, framerate:int, clock:time.Clock, background:pygame.Color) -> None:
game = Game(screen)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
state = State.OVER
if event.type == KEYDOWN:
if event.key == K_SPACE:
if state == State.TITLE:
state = State.PLAY
elif state == State.OVER:
state = State.TITLE
elif state == State.PAUSED:
state = State.PLAY
if event.key == K_ESCAPE:
if state == State.TITLE:
state = State.OVER
elif state == State.INSTRUCTIONS:
state = State.TITLE
elif state == State.PLAY:
state = State.PAUSED
elif state == State.PAUSED:
state = State.PLAY
elif state == State.OVER:
state = State.TITLE
screen.fill(background)
changed = []
if state == State.TITLE:
changed += game.show_title()
elif state == State.INSTRUCTIONS:
pass
elif state == State.PLAY:
pass
elif state == State.PAUSED:
pass
elif state == State.OVER:
pygame.quit()
return
pygame.display.update(changed)
clock.tick(framerate)
class Game:
def __init__(self, screen:Surface):
self._screen = screen
def show_title(self):
pass
if __name__ == "__main__":
Main(SCREEN_SIZE, FRAMERATE, State.TITLE, BLACK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment