def bank(): blue_goal = GameObject(X=0, Y=120) red_goal = GameObject(X=0, Y=-120) blue_goal_dist = my_tank.distance_to_object(blue_goal) red_goal_dist = my_tank.distance_to_object(red_goal) print("Blue goal dist", blue_goal_dist) print("Red goal dist", red_goal_dist) if blue_goal_dist > red_goal_dist: print("We go to the red goal") GameServer.sendMessage(ServerMessageTypes.TURNTOHEADING, {'Amount': my_tank.target_heading(red_goal)}) GameServer.sendMessage(ServerMessageTypes.TURNTURRETTOHEADING, {'Amount': my_tank.target_heading(red_goal)}) GameServer.sendMessage( ServerMessageTypes.MOVEFORWARDDISTANCE, {'Amount': my_tank.distance_to_object(red_goal)}) else: print("We go to the blue goal") GameServer.sendMessage(ServerMessageTypes.TURNTOHEADING, {'Amount': my_tank.target_heading(blue_goal)}) GameServer.sendMessage(ServerMessageTypes.TURNTURRETTOHEADING, {'Amount': my_tank.target_heading(red_goal)}) GameServer.sendMessage( ServerMessageTypes.MOVEFORWARDDISTANCE, {'Amount': my_tank.distance_to_object(blue_goal)}) go_to(0, 0)
def bank(): blue_goal = GameObject(X=0, Y=120) red_goal = GameObject(X=0, Y=-120) blue_goal_dist = my_tank.distance_to_object(blue_goal) red_goal_dist = my_tank.distance_to_object(red_goal) print("Blue goal dist", blue_goal_dist) print("Red goal dist", red_goal_dist) if blue_goal_dist > red_goal_dist: print("We go to the red goal") turn_move(my_tank.target_heading(red_goal), red_goal_dist) go_to(0, 0) else: print("We go to the blue goal") turn_move(my_tank.target_heading(blue_goal), blue_goal_dist) go_to(0, 0)
def go_to(x, y): goto_object = GameObject(X=x, Y=y) GameServer.sendMessage(ServerMessageTypes.TURNTOHEADING, {'Amount': my_tank.target_heading(goto_object)}) time.sleep(0.02) GameServer.sendMessage(ServerMessageTypes.MOVEFORWARDDISTANCE, {'Amount': my_tank.distance_to_object(goto_object)}) return
def find_close_obj(type): goal_obj = GameObject(X=1000, Y=1000) while True: GameServer.sendMessage(ServerMessageTypes.TOGGLETURRETLEFT) for i in range(10): message_type, message = GameServer.readMessage() print(message) if message_type == ServerMessageTypes.OBJECTUPDATE and message.get( "Type") == type: current_obj = GameObject(X=message.get('X'), Y=message.get('Y')) if my_tank.distance_to_object( current_obj) < my_tank.distance_to_object(goal_obj): goal_obj = current_obj print("found closer item", goal_obj.position[0]) GameServer.sendMessage(ServerMessageTypes.STOPTURRET) if goal_obj.position[0] != 1000: break return goal_obj
def handle_object_update(msg): if message_type == ServerMessageTypes.OBJECTUPDATE: if args.name == msg.get("Name", "?"): my_tank.update(msg) GameServer.sendMessage(ServerMessageTypes.STOPTURN) if (message_type == ServerMessageTypes.KILL): bank() if my_tank.health <= 2: GameServer.sendMessage(ServerMessageTypes.STOPALL) health_pickup = find_close_obj("HealthPickup") print(health_pickup.position) GameServer.sendMessage( ServerMessageTypes.TURNTOHEADING, {"Amount": my_tank.target_heading(health_pickup)}) time.sleep(2) GameServer.sendMessage( ServerMessageTypes.MOVEFORWARDDISTANCE, { "Amount": my_tank.distance_to_object(health_pickup) / 1.9 }) if my_tank.ammo == 0: GameServer.sendMessage(ServerMessageTypes.STOPALL) ammo_pickup = find_close_obj("AmmoPickup") # print(ammo_pickup.position) # GameServer.sendMessage(ServerMessageTypes.TURNTOHEADING, # {"Amount": my_tank.target_heading(ammo_pickup)}) # time.sleep(2) # GameServer.sendMessage(ServerMessageTypes.MOVEFORWARDDISTANCE, # {"Amount": my_tank.distance_to_object(ammo_pickup) / 1.9}) go_to(ammo_pickup) elif msg.get("Type", "") == "Tank": target = GameObject(X=msg.get("X"), Y=msg.get("Y"), Id=msg.get("Id")) print(my_tank.target_heading(target)) GameServer.sendMessage(ServerMessageTypes.TURNTURRETTOHEADING, {"Amount": my_tank.target_heading(target)}) GameServer.sendMessage(ServerMessageTypes.FIRE) print("FIRE!") else: print(message_type)