示例#1
0
 def draw_cars(self):
     """Call the update the positions and draw each car."""
     for car in self.cars:
         car.update()
         car.draw()
     for car in self.cars:
         if car.solution.fittest:
             car.draw()
def driving(qRead):
    global state
    global running
    while running:
        s = state
        if s == DRIVING:
            #start the car if it is stopped
            if car.stopped:
                car.start()
            try:
                #read the steering angle from the remote control and update the car
                angle = qRead.get(True, 0.2)
                car.steeringAngle = angle
                car.update()
            except queue.Empty as e:
                print('Timeout on get angle from queue')
        elif s == AI:
            #start the car if it is stopped
            if car.stopped:
                car.start()
            #use the neural network to compute the steering angle from the current video frame
            angle = car_ai.compute_steering_angle(cap)
            print("angle " + str(angle))
            car.steeringAngle = angle
            car.update()
        elif s == LD:
            #start the car if it is stopped
            if car.stopped:
                car.start()
            #use the lane algorithm to compute the steering angle from the current video frame
            angle = car_ai.computeSteeringAngleWithHough(cap)
            print("angle " + str(angle))
            car.steeringAngle = angle
            car.update()
        else:
            if not car.stopped:
                #stop the car
                print('STOP CAR')
                car.stop()
示例#3
0
    def run(self):
        screen = pygame.display.set_mode(self.window_size)
        stat_font = pygame.font.Font(None, 24)
        stat_color = (255, 255, 0)
        stat_pos = (self.window_size[0] - 200, 20)

        running = True
        stop_trial = False
        total_time = 0.0
        generation_number = 1
        while running:
            # time_passed = self.clock.tick(self.max_fps) * .001
            time_passed = self.clock.tick() * .001
            total_time += time_passed

            # Handle events.
            for event in pygame.event.get():
                if event.type is pygame.QUIT:
                    running = False
                elif event.type is pygame.KEYDOWN:
                    if event.key is pygame.K_ESCAPE:
                        running = False
                    elif event.key is pygame.K_r:
                        stop_trial = True
                elif event.type is pygame.MOUSEMOTION or event.type is pygame.MOUSEBUTTONDOWN:
                    pos = event.pos
                    pressed = pygame.mouse.get_pressed()
                    if pressed[0]:  # Left mouse button.
                        self.map.set_cell_at(pos, True)
                    elif pressed[2]:  # Right mouse button.
                        self.map.set_cell_at(pos, False)

            # Update the objects.
            nr_cars_alive = 0
            for car in self.cars:
                car.move(time_passed)
                car.update(time_passed)
                if not car.crashed:
                    nr_cars_alive += 1

            # Draw the objects.
            screen.fill(self.background_color)

            self.map.draw(screen)

            for car in self.cars:
                car.draw(screen)

            fps = stat_font.render("FPS: %3.0f" % self.clock.get_fps(), True,
                                   stat_color)
            screen.blit(fps, stat_pos)
            time = stat_font.render("Time: %2.0f" % total_time, True,
                                    stat_color)
            screen.blit(time,
                        (stat_pos[0], stat_pos[1] + stat_font.get_linesize()))
            gen = stat_font.render("Generation: %3d" % generation_number, True,
                                   stat_color)
            screen.blit(
                gen, (stat_pos[0], stat_pos[1] + 2 * stat_font.get_linesize()))
            alive = stat_font.render("Alive: %3d" % nr_cars_alive, True,
                                     stat_color)
            screen.blit(
                alive,
                (stat_pos[0], stat_pos[1] + 3 * stat_font.get_linesize()))

            pygame.display.flip()

            # Check if the current run is over.
            if total_time >= 20.0 or nr_cars_alive is 0 or stop_trial:
                stop_trial = False
                total_time = 0.0
                generation_number += 1
                self.evolve()
import car
import time

car.init()
car.start()
for angle in range(0, 180, 10):
    car.steeringAngle = float(angle)
    car.update()
    time.sleep(1)

car.stop()
car.cleanup()
示例#5
0
 def update(self):
     """ Update all the active objects """
     for car in d.cars:
         if not car.dead:
             car.update()