Return to repo list

heart-of-gold

Tactical RPG written in python, using pygame.
Return to HMagellan.com

commit 5a0d1c691110692d32ff5a9ad67926678746592f
parent 0a0e2d4e85b29aa797c1bf6974253a8f327b182b
Author: Erik Letson <hmagellan@hmagellan.com>
Date:   Sun,  6 Sep 2020 04:53:44 -0500

Rebuild of game.py

Diffstat:
Asrc/game.py | 141+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 141 insertions(+), 0 deletions(-)

diff --git a/src/game.py b/src/game.py @@ -0,0 +1,141 @@ +import pygame + +########### +# game.py # +########### + +# This file contains: +# 1. The 'Game' class, which defines the overarching game object +# 2. The generic 'GameSubsystem' class which defines general behavior for all subsystems +# 3. The 'GameInterface' subsystem, which handles all pygame events + +########################### +# Section 1 - Game object # +########################### + +class Game(object): + """ + Game is an object that represents the entire running game + instance. Everything is a sub-component of and subordinate + to Game, and there is only a single instance of Game. It + mostly acts as a container for the most basic PyGame obects, + such as the screen, and for the GameSubsystem objects. + """ + + def __init__(self, screen_dimensions, framerate): + + # Basic values + self.screen_dimensions = screen_dimensions + self.framerate = framerate + self.on = True + + # PyGame objects + self.screen = pygame.display.set_mode(self.screen_dimensions) + self.frame_clock = pygame.time.Clock() + + # Subsystems + self.interface = GameInterface(self) + + def shift_frames(self): + """ + Shift to the next frame using the PyGame Clock object. + This must be the FIRST thing that happens in the mainloop. + """ + self.frame_clock.tick(self.framerate) + + def update_screen(self): + """ + Update the entire game screen. + """ + + # First, fill the entire screen with black + self.screen.fill((0, 0, 0)) + + # Next, update all the subsurfaces/game objects and draw them + # TODO + + # Last, update the screen + pygame.display.update() + + def quit_game(self): + """ + Turn off so the game stops in a clean way. + """ + self.on = False + + def mainloop(self): + """ + The loop where all game logic takes place. This + method must be called once from somewhere after + the Game object is initialized in order to start + the game itself. This method cleans up after + itself when the Game object is turned off. + """ + + # While we are on, run the loop + while self.on: + self.shift_frames() + self.interface.handle_events(pygame.event.get()) + self.update_screen() + + # When we turn off, cleanup and end + pygame.quit() + +############################################ +# Section 2 - Generic GameSubsystem Object # +############################################ + +class GameSubsystem(object): + """ + Generic class for all top-level game element controllers + that are used by the Game object for various things. This + class is relatively simple. All subsystems can talk to + Game directly, but should avoid changing values in other + objects and rely on calling methods in those objects. + """ + + def __init__(self, game): + + # Universal Subsystem values + self.game = game + +############################################## +# Section 3 - GameInterface Subsystem Object # +############################################## + +class GameInterface(GameSubsystem): + """ + GameInterface handles all PyGame events, meaning it is + responsible for all input and the responses to that + input. + """ + + def __init__(self, game): + + # Parent init + super().__init__(game) + + def handle_events(self, events): + """ + Handle any kind of PyGame event and react appropriately. + """ + + for event in events: + if event.type == pygame.KEYPRESS: + self.handle_key_press(event) + elif event.type == pygame.KEYRELEASE: + self.handle_key_release(event) + elif event.type == pygame.QUIT: + self.game.quit_game() + + def handle_key_press(self, event): + """ + React to a key being pressed. + """ + pass + + def handle_key_release(self, event): + """ + React to a key being released. + """ + pass