Browse Source

Removed Application class. Added some new systems in game/gbe

master
Bryan Miller 6 years ago
parent
commit
66d1828c06
9 changed files with 165 additions and 45 deletions
  1. +1
    -0
      .gitignore
  2. +11
    -1
      game/__init__.py
  3. BIN
      game/__pycache__/__init__.cpython-36.pyc
  4. BIN
      game/__pycache__/application.cpython-36.pyc
  5. +0
    -41
      game/application.py
  6. +2
    -0
      game/gbe/__init__.py
  7. +78
    -0
      game/gbe/core.py
  8. +69
    -0
      game/gbe/events.py
  9. +4
    -3
      gb.py

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
__pycache__/

+ 11
- 1
game/__init__.py View File

@@ -1 +1,11 @@
from game.application import Application
import game.gbe

def start():
t = game.gbe.Time()
t.reset()

d = game.gbe.Display()
d.init()
while t.aliveSeconds < 5.0:
print(t.aliveSeconds)
d.close()

BIN
game/__pycache__/__init__.cpython-36.pyc View File


BIN
game/__pycache__/application.cpython-36.pyc View File


+ 0
- 41
game/application.py View File

@@ -1,41 +0,0 @@

import time
import pygame
from pygame.locals import *

class Application:
def __init__(self, width=640, height=480):
self._running = False
self._init = False
self._resolution = width, height
self.lastFrameTime = 0

def init(self):
pygame.init()
self._display_surface = pygame.display.set_mode(self._resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)
self._init = True

def on_event(self, event):
if event.type == pygame.QUIT:
self._running = False

def on_cleanup(self):
pygame.quit()

def execute(self):
# We want to automatically exit if app is already running or if app hasn't yet been init.
if self._running or not self._init:
return False
self._running = True

while self._running:
# Calculate delta time since last frame.
currentTime = time.time()
dt = 0
if self.lastFrameTime != 0:
dt = currentTime - self.lastFrameTime
self.lastFrameTime = currentTime

for event in pygame.event.get():
self.on_event(event)
self.on_cleanup()

+ 2
- 0
game/gbe/__init__.py View File

@@ -0,0 +1,2 @@
from .core import Time
from .core import Display

+ 78
- 0
game/gbe/core.py View File

@@ -0,0 +1,78 @@
'''
Filename core.py
Author: Bryan "ObsidianBlk" Miller
Date Created: 8/1/2018
Python Version: 3.7
'''
import time
import pygame


class Time:
def __init__(self):
self._dticks = 0
self._ldelta = 0
self._lastTick = 0

@property
def delta(self):
tick = int(round(time.time() * 1000))
dt = 0
if self._lastTick > 0:
dt = tick - self._lastTick
self._lastTick = tick
self._ldelta = dt
self._dticks += dt
return dt

@property
def last_delta(self):
return self._ldelta

@property
def aliveTicks(self):
tick = int(round(time.time() * 1000))
dt = 0
if self._lastTick > 0:
dt = tick - self._lastTick
return self._dticks + dt

@property
def aliveSeconds(self):
return self.aliveTicks / 1000.0

def reset(self):
self.dticks = 0
self._lastTick = int(round(time.time() * 1000))





class Display:
def __init__(self, width=640, height=480):
self._init = False
self._resolution = width, height

@property
def surface(self):
return self._display_surface

@property
def width(self):
return self._resolution[0]

@property
def height(self):
return self._resolution[1]

def init(self):
if self._init == False:
self._init = True
pygame.init()
self._display_surface = pygame.display.set_mode(self._resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)

def close(self):
pygame.quit()


+ 69
- 0
game/gbe/events.py View File

@@ -0,0 +1,69 @@

import weakref
import pygame

def _getWeakRef(fn):
if not hasattr(fn, "__func__"):
return None
if hasattr(fn, "__self__"):
return weakref.WeakMethod(fn)
return weakref.ref(fn)


class EventError(Exception):
pass

class _Events:
def __init__(self):
self._signals = {}

def _ClearUnreferenced(self):
def isempty(r):
return r() is not None

for signal, sigs in self._signals:
self._signals[signal] = filter(isempty, sigs)

def listen(self, signal, fn):
ref = _getWeakRef(fn)
if ref is None or ref() is None:
raise EventError("Expected a function callback.")
if not signal in self._signals:
self._signals[signal] = []
if not ref in self._signals[signal]:
self._signal[signal].append(ref)

def unlisten(self, signal, fn):
ref = _getWeakRef(fn)
if res is None or ref() is None:
return # Not a function. Nothing to do.
if signal in self._signals:
if ref in self._signals[signal]:
self._signals[signal].remove(ref)

def unlisten_all(self, signal):
if signal in self._signals:
del self._signals[signal]

def emit(self, signal, data):
if signal in self._signals:
for r in self._signals[signal]:
fn = r()
if fn is not None:
fn(signal, data)
self._ClearUnreferenced()

# Create the actual Events instance :)
Events = _Events()




def pollEmitter():
for event in pygame.event.get():
#TODO: For each event obtains, convert it for the above Event dispatcher
pass





+ 4
- 3
gb.py View File

@@ -3,6 +3,7 @@ import game


if __name__ == "__main__":
app = game.Application()
app.init()
app.execute()
game.start()
#app = game.Application()
#app.init()
#app.execute()

Loading…
Cancel
Save