def main(): game_state = game.GameState() img_processor = imageProcessor() start_time = None action = zeros(ACTIONS) action[0] = 1 game_round = 0 while 1: x_t, r_0, terminal = game_state.frame_step(action) img_processor.detectObjects(x_t, SAMPLE_FPS) action[0] = 1 action[1] = 0 if not terminal: if img_processor.jumping or img_processor.dropping: # print img_processor.tRex continue birds, cacti = img_processor.getObstacles() # print birds, cacti, img_processor.tRex if len(birds) == 0 and len(cacti) == 0: continue if len(birds) == 0: firstObstcale = cacti[0] elif len(cacti) == 0: firstObstcale = birds[0] elif cacti[0].x < birds[0].x: firstObstcale = cacti[0] else: firstObstcale = birds[0] # print firstObstcale.x, img_processor.tRex.x if firstObstcale is None: # controller.jump() action[0] = 0 action[1] = 1 continue if img_processor.tRex is None: # controller.jump() action[0] = 0 action[1] = 1 continue if ((firstObstcale.x + firstObstcale.w) - img_processor.tRex.x) < 200: action[0] = 0 action[1] = 1 elif firstObstcale.speed > 0: if (firstObstcale.x - img_processor.tRex.x) / firstObstcale.speed < 0.3: action[0] = 0 action[1] = 1 if ((firstObstcale.x + firstObstcale.w) - img_processor.tRex.x) / firstObstcale.speed < 0.4: action[0] = 0 action[1] = 1 else: print "round: %d, score: %d" % (game_round, game_state.lastScore) game_round += 1
def main(): controller = TrexGameController(game_path) img_processor = imageProcessor() start_time = None while 1: time.sleep(0.0005) if controller.hasStart(): if start_time is not None: delta_time = time.time() - start_time else: delta_time = 0 start_time = time.time() distanceRan = controller.getDistanceRan() crashed = controller.getCrashed() if not crashed: # print controller.getObstacles() # print distanceRan img_processor.detectObjects(controller.getImage(), delta_time) if img_processor.jumping or img_processor.dropping: continue birds, cacti = img_processor.getObstacles() # print birds, cacti if len(birds) == 0 and len(cacti) == 0: continue if len(birds) == 0: firstObstcale = cacti[0] elif len(cacti) == 0: firstObstcale = birds[0] elif cacti[0].x < birds[0].x: firstObstcale = cacti[0] else: firstObstcale = birds[0] # print firstObstcale.x, img_processor.tRex.x if firstObstcale is None: controller.jump() continue if img_processor.tRex is None: controller.jump() continue if ((firstObstcale.x + firstObstcale.w) - img_processor.tRex.x) < 200: controller.jump() elif firstObstcale.speed > 0: if (firstObstcale.x - img_processor.tRex.x) / firstObstcale.speed < 0.3: controller.jump() if ((firstObstcale.x + firstObstcale.w) - img_processor.tRex.x) / firstObstcale.speed < 0.4: controller.jump() else: controller.restart()
else: if (self.img_processor.jumping): self.controller.duck() self.LastParams = params#last move activation #self.LastRealParams = params#last frame activation #well well, our human mind can retain image upto 25ms i.e. 40fps #obviously no brain is ideal so :p 20ms :D ! else: time.sleep(0.1) while True: if self.controller.hasStart(): if start_time is not None: delta_time = time.time() - self.start_time else: delta_time = 0 Update() time.sleep(0.01) if __name__ == '__main__': game_path = 'file://' + os.path.abspath(os.path.join(os.getcwd(), '..')) + '/game/index.html' controller = TrexGameController(game_path) img_processor = imageProcessor() start_time = time.time() mlp = MLP() mlp.BuildModel() #mlp = None ng = NewGame(controller, img_processor, start_time, mlp) ng.StartGame()