main.py
def play(self):
        """ An inifinite loop that manages the display and the robot interactions
        """
        print("Current mode is '{}'".format(self.get_mode()))
        speed_multiplier = 1
        while True:
            mode = self.get_mode()
            # 'e' for 'event'
            for e in pygame.event.get():
                if e.type == QUIT:
                    sys.exit()
                elif e.type == KEYUP:
                    if e.key == pygame.K_LCTRL:
                        speed_multiplier = 1
                elif e.type == KEYDOWN:
                    key = e.dict["unicode"]
                    if e.key == pygame.K_LCTRL:
                        speed_multiplier = 10
                    if key == "q":
                        print("Rage quit!")
                        return 0
                    if key == "a":
                        self.is_artist = not (self.is_artist)
                        if self.is_artist:
                            self.screen.fill(BLACK)
                    if e.key == pygame.K_TAB:
                        self.set_next_mode()
                    if e.key == pygame.K_SPACE:
                        self.m.m1.speed = 0
                        self.m.m2.speed = 0
                    if e.key == pygame.K_UP:
                        self.m.m1.speed = (
                            self.m.m1.speed + WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_DOWN:
                        self.m.m1.speed = (
                            self.m.m1.speed - WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_RIGHT:
                        self.m.m2.speed = (
                            self.m.m2.speed + WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_LEFT:
                        self.m.m2.speed = (
                            self.m.m2.speed - WHEEL_SPEED_INC * speed_multiplier
                        )
                elif e.type == pygame.MOUSEMOTION:
                    mx, my = e.pos
                elif e.type == MOUSEBUTTONDOWN and e.button == 1:
                    self.m.x_goal = (mx - WINCENTER[0]) / METERS_TO_PIXEL
                    self.m.y_goal = -(my - WINCENTER[1]) / METERS_TO_PIXEL
            if not (self.is_artist):
                self.screen.fill(BLACK)
            if mode == XY_GOAL:
                self.asserv()
            elif mode == WHEEL_CONTROL:
                None
            else:
                print("ERROR: mode '{}' is unknown".format(mode))
                sys.exit()
            self.m.update(self.update_period)

            # Creating a fake robot to trace the future :)
            fake_m = copy.deepcopy(self.m)
            for i in range(30):
                if mode == XY_GOAL:
                    self.asserv(m=fake_m)
                fake_m.update(10 * self.update_period)
                self.draw_robot(m=fake_m, fake=True)

            self.draw_state()
            # print(self.m)
            t = pygame.time.get_ticks() / 1000.0 - self.t0
            linear_speed, rotation_speed = self.m.dk()

            # Writing new text
            self.screen.blit(
                self.font.render("Simple Robot Simulator", 1, gold), [0, 0]
            )
            if self.is_artist:
                self.screen.blit(
                    self.font.render("Artist mode!", 1, (0, 150, 0)), [20, 30]
                )
            else:
                self.screen.blit(
                    self.small_font.render(
                        "q: quit, tab: next mode, mouse: goal position, arrows: wheel speed control, space: speed 0 to wheels, a: artist toggle",
                        1,
                        (0, 150, 0),
                    ),
                    [20, 30],
                )
                self.screen.blit(
                    self.small_font.render("Time: {0:.2f}".format(t), 1, (0, 150, 0)),
                    [20, 60],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Mode: {}, artist: {}".format(
                            self.get_mode(), "True" if self.is_artist else "False"
                        ),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 90],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Left wheel speed: {0:.2f} mm/s".format(1000 * self.m.m1.speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 120],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Right wheel speed: {0:.2f} mm/s".format(
                            1000 * self.m.m2.speed
                        ),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 150],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Linear speed: {0:.2f} mm/s".format(1000 * linear_speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 180],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Angular speed: {0:.3f} rad/s".format(rotation_speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 210],
                )

            pygame.display.update()
            # That juicy 60 Hz :D
            self.clock.tick(1 / self.update_period)
 An inifinite loop that manages the display and the robot interactions
        
An inifinite loop that manages the display and the robot interactions
An inifinite loop that manages the display and the robot interactions
def play(self):
        print("Current mode is '{}'".format(self.get_mode()))
        speed_multiplier = 1
        while True:
            mode = self.get_mode()
            for e in pygame.event.get():
                if e.type == QUIT:
                    sys.exit()
                elif e.type == KEYUP:
                    if e.key == pygame.K_LCTRL:
                        speed_multiplier = 1
                elif e.type == KEYDOWN:
                    key = e.dict["unicode"]
                    if e.key == pygame.K_LCTRL:
                        speed_multiplier = 10
                    if key == "q":
                        print("Rage quit!")
                        return 0
                    if key == "a":
                        self.is_artist = not (self.is_artist)
                        if self.is_artist:
                            self.screen.fill(BLACK)
                    if e.key == pygame.K_TAB:
                        self.set_next_mode()
                    if e.key == pygame.K_SPACE:
                        self.m.m1.speed = 0
                        self.m.m2.speed = 0
                    if e.key == pygame.K_UP:
                        self.m.m1.speed = (
                            self.m.m1.speed + WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_DOWN:
                        self.m.m1.speed = (
                            self.m.m1.speed - WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_RIGHT:
                        self.m.m2.speed = (
                            self.m.m2.speed + WHEEL_SPEED_INC * speed_multiplier
                        )
                    if e.key == pygame.K_LEFT:
                        self.m.m2.speed = (
                            self.m.m2.speed - WHEEL_SPEED_INC * speed_multiplier
                        )
                elif e.type == pygame.MOUSEMOTION:
                    mx, my = e.pos
                elif e.type == MOUSEBUTTONDOWN and e.button == 1:
                    self.m.x_goal = (mx - WINCENTER[0]) / METERS_TO_PIXEL
                    self.m.y_goal = -(my - WINCENTER[1]) / METERS_TO_PIXEL
            if not (self.is_artist):
                self.screen.fill(BLACK)
            if mode == XY_GOAL:
                self.asserv()
            elif mode == WHEEL_CONTROL:
                None
            else:
                print("ERROR: mode '{}' is unknown".format(mode))
                sys.exit()
            self.m.update(self.update_period)
            fake_m = copy.deepcopy(self.m)
            for i in range(30):
                if mode == XY_GOAL:
                    self.asserv(m=fake_m)
                fake_m.update(10 * self.update_period)
                self.draw_robot(m=fake_m, fake=True)
            self.draw_state()
            t = pygame.time.get_ticks() / 1000.0 - self.t0
            linear_speed, rotation_speed = self.m.dk()
            self.screen.blit(
                self.font.render("Simple Robot Simulator", 1, gold), [0, 0]
            )
            if self.is_artist:
                self.screen.blit(
                    self.font.render("Artist mode!", 1, (0, 150, 0)), [20, 30]
                )
            else:
                self.screen.blit(
                    self.small_font.render(
                        "q: quit, tab: next mode, mouse: goal position, arrows: wheel speed control, space: speed 0 to wheels, a: artist toggle",
                        1,
                        (0, 150, 0),
                    ),
                    [20, 30],
                )
                self.screen.blit(
                    self.small_font.render("Time: {0:.2f}".format(t), 1, (0, 150, 0)),
                    [20, 60],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Mode: {}, artist: {}".format(
                            self.get_mode(), "True" if self.is_artist else "False"
                        ),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 90],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Left wheel speed: {0:.2f} mm/s".format(1000 * self.m.m1.speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 120],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Right wheel speed: {0:.2f} mm/s".format(
                            1000 * self.m.m2.speed
                        ),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 150],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Linear speed: {0:.2f} mm/s".format(1000 * linear_speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 180],
                )
                self.screen.blit(
                    self.small_font.render(
                        "Angular speed: {0:.3f} rad/s".format(rotation_speed),
                        1,
                        (0, 150, 0),
                    ),
                    [20, 210],
                )
            pygame.display.update()
            self.clock.tick(1 / self.update_period)
def play ( self ) : print ( "Current mode is '{}'" . format ( self . get_mode ( ) ) ) speed_multiplier = 1 while True : mode = self . get_mode ( ) for e in pygame . event . get ( ) : if e . type == QUIT : sys . exit ( ) elif e . type == KEYUP : if e . key == pygame . K_LCTRL : speed_multiplier = 1 elif e . type == KEYDOWN : key = e . dict [ "unicode" ] if e . key == pygame . K_LCTRL : speed_multiplier = 10 if key == "q" : print ( "Rage quit!" ) return 0 if key == "a" : self . is_artist = not ( self . is_artist ) if self . is_artist : self . screen . fill ( BLACK ) if e . key == pygame . K_TAB : self . set_next_mode ( ) if e . key == pygame . K_SPACE : self . m . m1 . speed = 0 self . m . m2 . speed = 0 if e . key == pygame . K_UP : self . m . m1 . speed = ( self . m . m1 . speed + WHEEL_SPEED_INC * speed_multiplier ) if e . key == pygame . K_DOWN : self . m . m1 . speed = ( self . m . m1 . speed - WHEEL_SPEED_INC * speed_multiplier ) if e . key == pygame . K_RIGHT : self . m . m2 . speed = ( self . m . m2 . speed + WHEEL_SPEED_INC * speed_multiplier ) if e . key == pygame . K_LEFT : self . m . m2 . speed = ( self . m . m2 . speed - WHEEL_SPEED_INC * speed_multiplier ) elif e . type == pygame . MOUSEMOTION : mx , my = e . pos elif e . type == MOUSEBUTTONDOWN and e . button == 1 : self . m . x_goal = ( mx - WINCENTER [ 0 ] ) / METERS_TO_PIXEL self . m . y_goal = - ( my - WINCENTER [ 1 ] ) / METERS_TO_PIXEL if not ( self . is_artist ) : self . screen . fill ( BLACK ) if mode == XY_GOAL : self . asserv ( ) elif mode == WHEEL_CONTROL : None else : print ( "ERROR: mode '{}' is unknown" . format ( mode ) ) sys . exit ( ) self . m . update ( self . update_period ) fake_m = copy . deepcopy ( self . m ) for i in range ( 30 ) : if mode == XY_GOAL : self . asserv ( m = fake_m ) fake_m . update ( 10 * self . update_period ) self . draw_robot ( m = fake_m , fake = True ) self . draw_state ( ) t = pygame . time . get_ticks ( ) / 1000.0 - self . t0 linear_speed , rotation_speed = self . m . dk ( ) self . screen . blit ( self . font . render ( "Simple Robot Simulator" , 1 , gold ) , [ 0 , 0 ] ) if self . is_artist : self . screen . blit ( self . font . render ( "Artist mode!" , 1 , ( 0 , 150 , 0 ) ) , [ 20 , 30 ] ) else : self . screen . blit ( self . small_font . render ( "q: quit, tab: next mode, mouse: goal position, arrows: wheel speed control, space: speed 0 to wheels, a: artist toggle" , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 30 ] , ) self . screen . blit ( self . small_font . render ( "Time: {0:.2f}" . format ( t ) , 1 , ( 0 , 150 , 0 ) ) , [ 20 , 60 ] , ) self . screen . blit ( self . small_font . render ( "Mode: {}, artist: {}" . format ( self . get_mode ( ) , "True" if self . is_artist else "False" ) , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 90 ] , ) self . screen . blit ( self . small_font . render ( "Left wheel speed: {0:.2f} mm/s" . format ( 1000 * self . m . m1 . speed ) , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 120 ] , ) self . screen . blit ( self . small_font . render ( "Right wheel speed: {0:.2f} mm/s" . format ( 1000 * self . m . m2 . speed ) , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 150 ] , ) self . screen . blit ( self . small_font . render ( "Linear speed: {0:.2f} mm/s" . format ( 1000 * linear_speed ) , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 180 ] , ) self . screen . blit ( self . small_font . render ( "Angular speed: {0:.3f} rad/s" . format ( rotation_speed ) , 1 , ( 0 , 150 , 0 ) , ) , [ 20 , 210 ] , ) pygame . display . update ( ) self . clock . tick ( 1 / self . update_period )
An inifinite loop that manages the display and the robot interactions
An inifinite loop that manages the display and the robot interactions
""" An inifinite loop that manages the display and the robot interactions
        """ # 'e' for 'event' # Creating a fake robot to trace the future :) # print(self.m) # Writing new text # That juicy 60 Hz :D
self