Created
January 11, 2018 06:30
-
-
Save schlerp/525d37fcdbd8d3ed5b14313592eb565b to your computer and use it in GitHub Desktop.
game engine
This file contains 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 time | |
import pygame | |
from pygame.locals import * | |
''' | |
brainstorm: | |
level: collection of maps | |
game: collection of levels | |
progress: collection of things to keep track of progress | |
engine: runs game (which is levels), and keeps track of progress | |
''' | |
class Engine(object): | |
''' | |
Engine | |
------ | |
This is the engine of the schlengine package. | |
It uses the following technology: | |
* pyTMX | |
* pygame | |
It supports the following features: | |
* something about something | |
''' | |
def __init__(self): | |
'''set up all basic internal variables''' | |
self._running = False | |
self._current_time = 0.0 | |
self._surface = None | |
self._size = self.length, self.height = (0,0) | |
self._map = None | |
self.player = None | |
self._tile_size = 0 | |
self._scale = 0 | |
self._scaled_tile_size = 0 | |
self._keymap = None | |
self._initilised = False | |
def on_init(self, x, y, repeat=100, keymap='keymap.json'): | |
'''initialise a surface and set key repeats etc...''' | |
pygame.init() | |
self._surface = pygame.display.set_mode((x, y), HWSURFACE|DOUBLEBUF|RESIZABLE) | |
pygame.key.set_repeat(repeat, repeat) | |
self._running = True | |
self._initilised = True | |
def add_player(self, player): | |
'''adds a player to the engine''' | |
self.player = player | |
def on_event(self, event): | |
'''handle user interaction''' | |
if event.type == QUIT: | |
self._running = False | |
user_input = pygame.key.get_pressed() | |
if user_input[K_UP]: | |
pass | |
if user_input[K_DOWN]: | |
pass | |
if user_input[K_LEFT]: | |
pass | |
if user_input[K_RIGHT]: | |
pass | |
if user_input[K_SPACE]: | |
self._running = False | |
def on_loop(self): | |
'''handles non interactive changes to the state of environemnt''' | |
pass | |
def on_render(self): | |
'''control the rendering of the map and entities''' | |
pass | |
def _render_base(self): | |
'''render the base map (layers below entities)''' | |
pass | |
def _render_entities(self): | |
'''renders the entity layer of the map, items and characters''' | |
pass | |
def _render_top(self): | |
'''renders layers above the entity layer (things that will obscure entities)''' | |
pass | |
def on_cleanup(self): | |
'''handles any cleanup on quit''' | |
pygame.quit() | |
def on_execute(self): | |
'''this represents the main loop of the engine''' | |
if self._initilised: | |
while self._running: | |
# set internal time | |
self._current_time = time.time() | |
# handle events | |
for event in pygame.event.get(): | |
self.on_event(event) | |
# game loop | |
self.on_loop() | |
# render | |
self.on_render() | |
# cleanup and exit | |
self.on_cleanup() | |
if __name__ == '__main__': | |
engine = Engine() | |
engine.on_init(100, 100) | |
engine.on_execute() |
This file contains 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 pytmx | |
class Level(object): | |
'''this is a collection of tmx maps''' | |
def __init__(self, name, maps=[]): | |
self.name = name | |
self.maps = maps | |
def __repr__(self): | |
return self.name | |
def add_map(self, _map): | |
if _map not in self.maps: | |
self.maps.append(_map) | |
def replace_map(self, _map): | |
self.maps.remove(_map) | |
self.maps.append(_map) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment