Last active
June 23, 2022 14:00
-
-
Save rolika/b0f70e18fb562e050ae58ea31327f769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 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) | |
| 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