from pod import pods,world,gui import math #### MAIN CODE ######################### # world definition file worldFile="worlds/rectWorld.world" # use a control to activate the car. control=pods.Control() # create the world world=world.World(worldFile) pod=pods.CarPod(world) simple_gui=gui.SimpleGui(frames_per_sec=int(1.0/world.dt),world=world,pods=[pod]) while True: keys=simple_gui.get_pressed() #http://thepythongamebook.com/en:glossary:p:pygame:keycodes # The car has 4 controls that can have a value between 0-1 # up- accelerate # down - brake # left - steer left
def tester(world_name, punter_names, fout): global simple_gui global FPS_FACT N = len(punter_names) ### START OF PROGRAM world_test = world.World(world_name) pod_list = [] zombies = [] cnt = 0 default_dir = os.getcwd() for name in punter_names: pod = pods.CarPod(world_test) pod_list.append(pod) pod.score = 0.0 pod.stat = "-" pod.name = name pod.mess = "Uninitialized" try: punters_path = 'punters_test/' + name os.chdir(punters_path) plug = importlib.import_module('punters_test.' + name + '.plugin') # call the plugin to equip the car # set the current path to the punters directory plug.equip_car(pod) os.chdir(default_dir) pod.controller = plug.controller hue = (360.0 * cnt) / N col = pygame.Color(0) col.hsla = (hue, 100, 50, 0) pod.col = (col.r, col.g, col.b) cnt += 1 except: print name print "Unexpected error:", sys.exc_info() # fout.write(name+" Error "+ str(sys.exc_info()[0])) pod.mess = "Unable to run" + str(sys.exc_info()[0]) pod.score = 0.0 pod.stat = "E" zombies.append(pod) os.chdir(default_dir) runners = copy.copy(pod_list) # remove zombies for pod in zombies: runners.remove(pod) if GUI: simple_gui = gui.SimpleGui(frames_per_sec=int(FPS_FACT / world_test.dt), world=world_test, pods=runners, back_ground=(5, 5, 5)) # use a control to activate the car. control = pods.Control() while runners: zombies = [] for pod in runners: try: control.left = 0 control.right = 0 control.up = 0 control.down = 0 pod.controller(pod, control) pod.step(control) score, kill, mess = evaluate(pod) pod.score = score pod.mess = mess except: print name + ": Unexpected error:", sys.exc_info() pod.score = 0 pod.mess = "Error ->" + str(sys.exc_info()) kill = True pod.stat = "e" if kill: zombies.append(pod) # remove crashed for pod in zombies: runners.remove(pod) ranked = sorted(pod_list, key=lambda x: x.score, reverse=True) if GUI: disp = "" pos = [0, 10] simple_gui.clear() for pod in ranked: col = pod.col gui_base.draw_string(simple_gui.screen, pod.stat + ":" + pod.name, pos, col, FONT_SIZE, 'Courier New') pos[1] += FONT_SIZE simple_gui.display(clear=False, fps=int(FPS_FACT / world_test.dt)) if simple_gui.check_for_quit(): sys.exit(0) if simple_gui.get_pressed()[gui.keys.K_p]: pause = True if simple_gui.get_pressed()[gui.keys.K_EQUALS]: FPS_FACT = min(FPS_FACT * 2, 200) print FPS_FACT if simple_gui.get_pressed()[gui.keys.K_MINUS]: FPS_FACT = max(int(FPS_FACT / 2), 1) print FPS_FACT if simple_gui.get_pressed()[gui.keys.K_s]: pause = False ranked = sorted(pod_list, key=lambda x: x.score, reverse=True) for pod in ranked: buff = "%15s %6.3f %s" % (pod.name + ":", pod.score, ":" + pod.mess + "\n") fout.write(buff) return pod_list