def advanced_init(): window2 = Tk() arena = Canvas(window2, width = 500, height = 500, bg = 'white') # generates a canvas of 500px x 500px for the arena for i in range(0,5): generate_big_obstacle(arena_list, arena) arena.update() for i in range(0,3): generate_mud_object(arena_list,arena) arena.update() for i in range(0,3): generate_traffic_light_object(arena_list,arena,traffic_list) arena.update() for i in range(0,3): generate_speed1_boost_object(arena_list,arena) arena.update() for i in range(0,30): generate_obstacle(arena_list, arena) arena.update() print arena_list robot1 = robot.spawn_robot(arena) arena.update_idletasks() speed1=0.1 print arena_list arena.pack() arena.update_idletasks() speed=0.1 go = True while go == True: print go direction = 0 x0,y0,x1,y1 = arena.coords(robot1) if x0 >= 480: go = False elif x0 <= 20: go = False elif y0 >= 480: go = False elif y0 <= 20: go =False robot.check(robot1, direction, int(x0),int(y0)) robot.move(robot1, direction, arena) arena.update() time.sleep(0.1) print arena.coords(robot1) print go window2.mainloop() # runs everything
def test_correct(self): with captured_output() as (out, err): robot.move() output = out.getvalue().strip() file = open('tests/test_output.txt', 'r') expectedOutput = file.read() self.assertEqual(expectedOutput, output)
def keyboardMove(robot): if keyboardMove.turning_right: robot.turn(0.1) if keyboardMove.turning_left: robot.turn(-0.1) if keyboardMove.moving: robot.move() for event in pygame.event.get(): if not hasattr(event, 'key'): continue if event.key == pygame.K_ESCAPE: pygame.display.quit() sys.exit(0) if event.key == pygame.K_UP: keyboardMove.moving = (event.type == pygame.KEYDOWN) if event.key == pygame.K_LEFT: keyboardMove.turning_left = (event.type == pygame.KEYDOWN) if event.key == pygame.K_RIGHT: keyboardMove.turning_right = (event.type == pygame.KEYDOWN)
def update(self): for robot in self.world.robots: robot.move(self.world) self.notify_worldupdate() self.gui.statuslabel.config(text="survivors: %d" % len(self.world.robots)) if len(self.world.robots) < 1: print("[server] No results.") self.round_ends() elif len(self.world.robots) == 1: self.survivor = self.world.robots[0] self.world.remove(self.survivor) self.survivor.popuptext("I WIN! HURRAH!", True) print("[server] %s wins!" % self.survivor.name) self.gui.statuslabel.config(text="winner: %s" % self.survivor.name) self.notify_winner(self.survivor) self.round_ends() else: self.gui.tk.after(40, self.update)
def update(self): for robot in self.world.robots: robot.move(self.world) self.notify_worldupdate() self.gui.statuslabel.config(text="survivors: %d" % len(self.world.robots)) if len(self.world.robots)<1: print("[server] No results.") self.round_ends() elif len(self.world.robots)==1: self.survivor=self.world.robots[0] self.world.remove(self.survivor) self.survivor.popuptext("I WIN! HURRAH!", True) print("[server] %s wins!" % self.survivor.name) self.gui.statuslabel.config(text="winner: %s" % self.survivor.name) self.notify_winner(self.survivor) self.round_ends() else: self.gui.tk.after(40, self.update)
def testExplosion(self): robot = AdvancedRobot("robot") robot.move(10) robot.explode() robot.recharge() robot.explode()
#!/usr/bin/env python import robot robot.move('d') robot.move('f')
import robot #adds custom functions you can use to drive the robot robot.move(forward) #move forward robot.delay(2) #do previous command for 2 seconds robot.stop() #stop print "test"
#Echo server program import socket import robot import time HOST = '' PORT = 50007 commands = ['F', 'R', 'L'] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn, addr = s.accept() while 1: data = conn.recv(1024) if not data: time.sleep(1) print data if data in commands: robot.move(data) conn.close()
def test_performs_correct_transformation_for_heading(self): start = (0, 0) self.assertEqual(robot.move(start, 0), (0, 1)) self.assertEqual(robot.move(start, 1), (1, 0)) self.assertEqual(robot.move(start, 2), (0, -1)) self.assertEqual(robot.move(start, 3), (-1, 0))
def test_handles_negative_coordinates(self): start = (-12, 2) self.assertEqual(robot.move(start, 3), (-13, 2))
import robot #adds custom functions you can use to drive the robot robot.move("forward") #move forward robot.delay(2) #do previous command for 2 seconds robot.stop() #stop robot.delay(1) robot.move("left") robot.delay(2) robot.stop() #stop robot.delay(1) robot.move("right") robot.delay(2) robot.stop() #stop robot.delay(1) robot.move("back") robot.delay(2) robot.stop() #stop
#Sugestion by ragnar based on Ericks idea import robot import world #import ai_algoritm import pfm as ai_algoritm import plot poslogx=[] poslogy=[] #while 1: for j in range(100): current_pos = robot.get_pos() current_ori = robot.get_ori() poslogx.append(current_pos[0]) poslogy.append(current_pos[1]) current_sen = world.read_sensors(current_pos, current_ori) #The ai-algoritmh output an instance of the CommandClass command = ai_algoritm.update(current_pos, current_ori, current_sen, [5,5]) #The robot can both act acording to a position command or trajectory command robot.move(command) plot.addobstacles(current_sen) plot.plotall(poslogx,poslogy) # print() # so we can see what is happening