def __init__(self, max_entities=config.load_var('max_entities'), size=[config.load_var('map_x_size'), config.load_var('map_y_size')]): #constants from config file self.maxEntities = max_entities self.size = size #dynamic game variables self.gameObjects = [] self.playerSortedObjectDict = {}
def runGame(tickFreq=.1): for _ in range(config.load_var('max_asteroids')):#Creates Asteroids Before game starts loc = [random.randint(0, config.load_var('map_x_size')), random.randint(0, config.load_var('map_y_size'))] asteroid = Asteroid(loc=loc, player='map') gameMap.addObject(asteroid) currentTime = time.time() startTime = currentTime tickCount = 0 t = threading.currentThread() print("Game loop has started.") while getattr(t, "do_run", True): if(time.time() - currentTime > tickFreq): #10 ticks per second. This number can be changed as necessary tick() tickCount += 1 currentTime = time.time() # if currentTime - startTime > 10: break #quick line to time out the code after 10 seconds print("Game loop has been terminated.")
from GameMap import GameMap from GameObject import GameObject, Asteroid import config from functools import wraps def delay(delay=0.): # sets a timer to run a function later on - http://fredericiana.com/2014/11/14/settimeout-python-delay/ def wrap(f): @wraps(f) def delayed(*args, **kwargs): timer = threading.Timer(delay, f, args=args, kwargs=kwargs) timer.start() return delayed return wrap gameMap = GameMap(config.load_var('max_entities')) def runGame(tickFreq=.1): for _ in range(config.load_var('max_asteroids')):#Creates Asteroids Before game starts loc = [random.randint(0, config.load_var('map_x_size')), random.randint(0, config.load_var('map_y_size'))] asteroid = Asteroid(loc=loc, player='map') gameMap.addObject(asteroid) currentTime = time.time() startTime = currentTime tickCount = 0 t = threading.currentThread() print("Game loop has started.") while getattr(t, "do_run", True): if(time.time() - currentTime > tickFreq): #10 ticks per second. This number can be changed as necessary