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.

Revisions

  1. rolika revised this gist Jun 23, 2022. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions Pygame_boilerplate.py
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,46 @@
    State = Enum("State", "TITLE INSTRUCTIONS PLAY PAUSED OVER")


    class Timer:
    """Timer for games."""
    def __init__(self, cooldown:int) -> None:
    """Initialize a timer object.
    cooldown: time in milliseconds the timer has to expire"""
    self._cooldown = cooldown
    self._last_update = time.get_ticks()
    self._counter = 1

    @property
    def cooldown(self) -> int:
    """Return the cooldown time in milliseconds."""
    return self._cooldown

    @cooldown.setter
    def cooldown(self, value:int) -> None:
    """Set the cooldown time in milliseconds."""
    self._cooldown = value

    @property
    def counter(self) -> int:
    """Return the count of how many times the timer has expired."""
    return self._counter

    def reset(self) -> None:
    """Reset the timer."""
    self._last_update = time.get_ticks()

    def reset_counter(self) -> None:
    """Reset the counter."""
    self._counter = 1

    def is_expired(self) -> bool:
    """Check if the timer has expired."""
    self._counter += 1
    now = time.get_ticks()
    time_since_last_update = now - self._last_update
    return time_since_last_update >= self._cooldown


    class Main():
    def __init__(self, screen_size:tuple, framerate:int, state:State, background:pygame.Color) -> None:
    screen = pygame.display.set_mode(screen_size)
  2. rolika revised this gist Apr 21, 2022. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions Pygame_boilerplate.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    import pygame
    from pygame.locals import *
    from pygame import freetype, time # import freetype here to initialize it
    from pygame import freetype, time, Surface # import freetype here to initialize it
    from enum import Enum
    from game import Game

    @@ -65,10 +65,6 @@ def _run(self, screen:pygame.Surface, state:State, framerate:int, clock:time.Clo
    pygame.display.update(changed)
    clock.tick(framerate)


    from pygame import Surface
    from text import SimpleText


    class Game:
    def __init__(self, screen:Surface):
  3. rolika created this gist Apr 21, 2022.
    82 changes: 82 additions & 0 deletions Pygame_boilerplate.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    import pygame
    from pygame.locals import *
    from pygame import freetype, time # 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)


    from pygame import Surface
    from text import SimpleText


    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)