コード例 #1
0
def win(display_surface, msg):
    font = _load_font('./fonts/American Captain.ttf', 32)
    win_image = _load_image('./images/trophy.png')
    car_lap = font.render(msg, True, (255, 255, 255))
    display_surface.blit(win_image, (700, 300))
    display_surface.blit(car_lap, (1050, 500))
    pygame.display.update()
    pygame.time.delay(5000)
    mainmenu.main_menu(display_surface)
コード例 #2
0
ファイル: T2_AI.py プロジェクト: Vishvak365/Karting-Pros
def win(display_surface, msg):
    win = mixer.Sound(os.path.join(os.path.abspath(os.path.dirname(__file__)), './sounds/win.wav'))
    mixer.Sound.play(win)
    font = _load_font('./fonts/American Captain.ttf', 32)
    if msg == "car1":
        win_image = _load_image('./images/p1_trophy.png')
    else:
        win_image = _load_image('./images/ai_trophy.png')
    win_image = pygame.transform.scale(win_image, (500, 500))
    car_lap = font.render(msg, True, (255, 255, 255))
    display_surface.blit(win_image, (700, 300))
    # display_surface.blit(car_lap, (1050, 500))
    pygame.display.update()
    pygame.time.delay(5000)
    mixer.music.stop()
    mainmenu.main_menu(display_surface)
コード例 #3
0
def RaceCars(display_surface):
    # window = screen.Screen()
    track1 = track.Track()
    white = (0, 128, 0)
    start_car1 = (1010, 144)
    start_car2 = (1010, 75)
    clock = pygame.time.Clock()
    t0 = time.time()

    car = Car('./images/f1sprite.png', start_car1)
    car_group = pygame.sprite.Group(car)

    car2 = Car('./images/f1sprite.png', start_car2)
    car_group2 = pygame.sprite.Group(car2)

    pad_group = track1.getPads()
    finish_line = track1.getFinishLine()
    checkpoint = (960, 845, 10, 125)
    lap_car1 = 0
    checkpoint_car1 = 0
    lap_car2 = 0
    checkpoint_car2 = 0
    # Music for countdown sound
    current_path = os.path.abspath(os.path.dirname(__file__))
    absolute_image_path = os.path.join(current_path,
                                       './sounds/race_coundown.mp3')
    mixer.init()
    mixer.music.load(absolute_image_path)
    mixer.music.set_volume(0.7)
    mixer.music.play()
    while True:
        # Draw the Track
        display_surface.fill(white)
        pad_group.draw(display_surface)
        track.checkpoint(display_surface)
        delta_t = clock.tick(30)
        font = _load_font('./fonts/American Captain.ttf', 32)
        for event in pygame.event.get():
            if not hasattr(event, 'key'):
                continue
            getEvent1(car, event, display_surface)
            getEvent2(car2, event, display_surface)

        # Update car and draw track
        carlap1 = font.render("Car 1 Laps completed: " + str(lap_car1) + "/5",
                              True, (255, 255, 255))
        carlap2 = font.render("Car 2 Laps completed: " + str(lap_car2) + "/5",
                              True, (255, 255, 255))
        display_surface.blit(carlap1, (0, 0))
        display_surface.blit(carlap2, (0, 30))
        car_group.update(delta_t)
        car_group.draw(display_surface)
        pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2)
        car_group2.update(delta_t)
        car_group2.draw(display_surface)
        pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2)
        # Check if car is on track
        SetCarMaxSpeed(car, pad_group, car_group)
        SetCarMaxSpeed(car2, pad_group, car_group2)

        pygame.display.flip()
        collision(car, car2, display_surface)
        if checkOutOfBounds(car):
            car.reset(start_car1)
        if checkOutOfBounds(car2):
            car2.reset(start_car2)
        checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1)
        checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2)
        if checkpoint_car1 >= 1:
            previouslapcar1 = lap_car1
            lap_car1 = carLap(car, finish_line, lap_car1,
                              "Lap finished for car 1!")
            if lap_car1 > previouslapcar1:
                if lap_car1 == 5:
                    win(display_surface, "Car 1 Wins!")
                checkpoint_car1 = 0
        if checkpoint_car2 >= 1:
            previouslapcar2 = lap_car2
            lap_car2 = carLap(car2, finish_line, lap_car2,
                              "Lap finished for car 2!")
            if lap_car2 > previouslapcar2:
                if lap_car2 == 5:
                    win(display_surface, "Car 2 Wins!")
                checkpoint_car2 = 0
        # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125))
        pygame.display.update()
コード例 #4
0
ファイル: T2_AI.py プロジェクト: Vishvak365/Karting-Pros
def T2_AI(display_surface):
    track1 = track2.Track2()
    white = (0, 128, 0)
    track = _load_image('./images/track2.png')

    # Official timer
    clock = pygame.time.Clock()
    t0 = time.time()

    # Setup car objects
    start_car1 = (1010, 75)
    car = Car('./images/f1sprite.png', start_car1)
    car_group = pygame.sprite.Group(car)
    car.setDefaultValues()

    # AI Car
    start_car2 = (1010, 144)
    car2 = Car('./images/f1sprite2.png', start_car2)
    car_group2 = pygame.sprite.Group(car2)
    car2.setDefaultValues()

    # AI Moves
    current_path = os.path.abspath(os.path.dirname(__file__))
    if settings.getSetting('ai_difficulty_hard') == False:
        absolute_image_path = os.path.join(
            current_path, './ArtificalIntelligence/track2_easy.npy')
    else:
        absolute_image_path = os.path.join(
            current_path, './ArtificalIntelligence/track2_hard.npy')
    moves = np.load(absolute_image_path)
    moveNum = 0

    # Groups for pads and finish line
    pad_group = track1.getPads()
    finish_line = track1.getFinishLine()

    # Setup lap logic
    checkpoint = (960, 845, 10, 125)
    lap_car1 = 0
    checkpoint_car1 = 0
    lap_car2 = 0
    checkpoint_car2 = 0

    # Countdown timer logic
    countdownTimerStart = time.time()
    countdownFinished = False

    collisions = settings.getSetting('collision')
    draw_hitbox = settings.getSetting('draw_hitbox')

    # Music for countdown sound
    current_path = os.path.abspath(os.path.dirname(__file__))
    absolute_image_path = os.path.join(
        current_path, './sounds/race_coundown.mp3')
    mixer.init()
    mixer.music.load(absolute_image_path)
    mixer.music.set_volume(0.7)
    mixer.music.play()
    crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav'))

    right_press, left_press, up_press, down_press = 0, 0, 0, 0
    while True:

        # Draw the Track
        display_surface.fill(white)
        display_surface.blit(track, (0, 0))
        track2.checkpoint(display_surface)
        delta_t = clock.tick(30)
        font = _load_font('./fonts/American Captain.ttf', 32)
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            if not hasattr(event, 'key'):
                continue
            if event.key == K_RIGHT:
                right_press = 1
            elif event.key == K_SPACE:
                car.speed = 0
            elif event.key == K_LEFT:
                left_press = 1
            elif event.key == K_UP:
                mixer.music.load(os.path.join(os.path.abspath(os.path.dirname(__file__)), './sounds/rev.mp3'))
                mixer.music.play(-1)
                up_press = 1
            elif event.key == K_DOWN:
                down_press = 1
            elif event.key == K_ESCAPE:
                mixer.music.stop()
                mixer.Sound.stop(crowd)
                mainmenu.main_menu(display_surface)
            if event.type == KEYUP:
                if event.key == pygame.K_RIGHT:
                    right_press = 0
                elif event.key == pygame.K_LEFT:
                    left_press = 0
                elif event.key == pygame.K_UP:
                    mixer.music.stop()
                    up_press = 0
                elif event.key == pygame.K_DOWN:
                    down_press = 0

        # Human Movement System
        car.k_right = right_press * -5
        car.k_left = left_press * 5
        car.k_up = up_press * 2
        car.k_down = down_press * -2

        # Friction component
        if up_press == 0 and down_press == 0 and int(car.speed) != 0:
            car.k_down = -.2
            car.k_up = 0

        # AI Movement System
        car2.k_right = moves[moveNum][0] * -5
        car2.k_left = moves[moveNum][1] * 5
        car2.k_up = moves[moveNum][2] * 2
        car2.k_down = moves[moveNum][3] * -2
        moveNum += 1

        # Update car and draw track
        carlap1 = font.render("Car 1 Laps completed: " +
                              str(lap_car1) + "/5", True, (255, 255, 255))
        carlap2 = font.render("Car 2 Laps completed: " +
                              str(lap_car2) + "/5", True, (255, 255, 255))
        # Lap count for Cars
        display_surface.blit(carlap1, (0, 0))
        display_surface.blit(carlap2, (0, 30))

        # Update and draw car
        car_group.update(delta_t)
        car_group.draw(display_surface)

        # Update and draw car
        car_group2.update(delta_t)
        car_group2.draw(display_surface)

        if draw_hitbox:
            pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2)
            pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2)
        # Check if car is on track
        SetCarMaxSpeed(car, pad_group, car_group)
        SetCarMaxSpeed(car2, pad_group, car_group2)

        pygame.display.flip()
        if collisions:
            collision(car, car2, display_surface)

        if checkOutOfBounds(car):
            car.reset(start_car1)
        if checkOutOfBounds(car2):
            car2.reset(start_car2)
        checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1)
        checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2)
        if checkpoint_car1 >= 1:
            previouslapcar1 = lap_car1
            lap_car1 = carLap(car, finish_line, lap_car1,
                              "Lap finished for car 1!")
            if lap_car1 > previouslapcar1:
                mixer.Sound.play(crowd)
                if lap_car1 == 5:
                    mixer.music.stop()
                    mixer.Sound.stop(crowd)
                    win(display_surface, "car1")
                checkpoint_car1 = 0
        if checkpoint_car2 >= 1:
            previouslapcar2 = lap_car2
            lap_car2 = carLap(car2, finish_line, lap_car2,
                              "Lap finished for car 2!")
            if lap_car2 > previouslapcar2:
                if lap_car2 == 5:
                    mixer.music.stop()
                    mixer.Sound.stop(crowd)
                    win(display_surface, "ai")
                checkpoint_car2 = 0

        while (time.time() - countdownTimerStart < 4):
            image = _load_image('./images/starting_lights/lights' +
                                str(int(time.time() - countdownTimerStart) + 1) + '.png')
            display_surface.blit(image, ((1920 / 2) - (768 / 2), 50))
            fontBig = _load_font('./fonts/American Captain.ttf', 64)
            countdown_text = font.render(
                "Time: " + str(4 - t0), True, (255, 255, 255))
            t0 = time.time()
            t1 = time.time()
            dt = t1 - t0
            countdownFinished = True
            pygame.display.update()
        # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125))
        pygame.display.update()
コード例 #5
0
def RaceCars(display_surface):
    track1 = track2.Track2()
    white = (0, 128, 0)
    track = _load_image('./images/track2.png')
    # Official timer
    clock = pygame.time.Clock()
    t0 = time.time()

    # Setup car objects
    start_car1 = (1010, 144)
    car = Car('./images/f1sprite.png', start_car1)
    car_group = pygame.sprite.Group(car)

    start_car2 = (1010, 75)
    car2 = Car('./images/f1sprite2.png', start_car2)
    car_group2 = pygame.sprite.Group(car2)

    # Groups for pads and finish line
    pad_group = track1.getPads()
    finish_line = track1.getFinishLine()

    # Setup lap logic
    checkpoint = (960, 845, 10, 125)
    lap_car1 = 0
    checkpoint_car1 = 0
    lap_car2 = 0
    checkpoint_car2 = 0

    # Collision Check
    collisions = settings.getSetting('collision')
    draw_hitbox = settings.getSetting('draw_hitbox')

    # Countdown timer logic
    countdownTimerStart = time.time()
    countdownFinished = False

    # Music for countdown sound
    current_path = os.path.abspath(os.path.dirname(__file__))
    absolute_image_path = os.path.join(current_path,
                                       './sounds/race_coundown.mp3')
    mixer.init()
    mixer.music.load(absolute_image_path)
    mixer.music.set_volume(0.7)
    mixer.music.play()
    crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav'))

    # Movement System
    right_press1, left_press1, up_press1, down_press1 = 0, 0, 0, 0
    right_press2, left_press2, up_press2, down_press2 = 0, 0, 0, 0
    while True:
        # Draw the Track
        display_surface.fill(white)
        display_surface.blit(track, (0, 0))
        track2.checkpoint(display_surface)
        delta_t = clock.tick(30)
        font = _load_font('./fonts/American Captain.ttf', 32)
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            if not hasattr(event, 'key'):
                continue
            # Player 1 Movement
            if event.key == K_RIGHT:
                right_press1 = 1
            elif event.key == K_SPACE:
                car.speed = 0
            elif event.key == K_LEFT:
                left_press1 = 1
            elif event.key == K_UP:
                mixer.music.load(os.path.join(current_path,
                                              './sounds/rev.mp3'))
                mixer.music.play(-1)
                up_press1 = 1
            elif event.key == K_DOWN:
                down_press1 = 1
            elif event.key == K_ESCAPE:
                mixer.music.stop()
                mixer.Sound.stop(crowd)
                mainmenu.main_menu(display_surface)

            if event.type == KEYUP:
                if event.key == pygame.K_RIGHT:
                    right_press1 = 0
                elif event.key == pygame.K_LEFT:
                    left_press1 = 0
                elif event.key == pygame.K_UP:
                    mixer.music.stop()
                    up_press1 = 0
                elif event.key == pygame.K_DOWN:
                    down_press1 = 0
            # Player 2 Movement
            if event.key == K_d:
                right_press2 = 1
            elif event.key == K_LSHIFT:
                car.speed = 0
            elif event.key == K_a:
                left_press2 = 1
            elif event.key == K_w:
                mixer.music.load(os.path.join(current_path,
                                              './sounds/rev.mp3'))
                mixer.music.play(-1)
                up_press2 = 1
            elif event.key == K_s:
                down_press2 = 1
            elif event.key == K_ESCAPE:
                mixer.Sound.stop(crowd)
                mixer.music.stop()
                mainmenu.main_menu(display_surface)

            if event.type == KEYUP:
                if event.key == pygame.K_d:
                    right_press2 = 0
                elif event.key == pygame.K_a:
                    left_press2 = 0
                elif event.key == pygame.K_w:
                    mixer.music.stop()
                    up_press2 = 0
                elif event.key == pygame.K_s:
                    down_press2 = 0

        car.k_right = right_press1 * -5
        car.k_left = left_press1 * 5
        car.k_up = up_press1 * 2
        car.k_down = down_press1 * -2

        if up_press1 == 0 and down_press1 == 0 and int(car.speed) != 0:
            car.k_down = -.2
            car.k_up = 0

        car2.k_right = right_press2 * -5
        car2.k_left = left_press2 * 5
        car2.k_up = up_press2 * 2
        car2.k_down = down_press2 * -2

        if up_press2 == 0 and down_press2 == 0 and int(car2.speed) != 0:
            car2.k_down = -.2
            car2.k_up = 0

        # Update car and draw track
        carlap1 = font.render("Car 1 Laps completed: " + str(lap_car1) + "/5",
                              True, (255, 255, 255))
        carlap2 = font.render("Car 2 Laps completed: " + str(lap_car2) + "/5",
                              True, (255, 255, 255))
        # Lap count for Cars
        display_surface.blit(carlap1, (0, 0))
        display_surface.blit(carlap2, (0, 30))

        # Update and draw car
        car_group.update(delta_t)
        car_group.draw(display_surface)

        # Update and draw car
        car_group2.update(delta_t)
        car_group2.draw(display_surface)

        if draw_hitbox:
            pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2)
            pygame.draw.rect(display_surface, (255, 0, 0), car2.hitbox, 2)

        # Check if car1 is on track
        on_track = pygame.sprite.groupcollide(car_group, pad_group, False,
                                              False)

        # Slow down car1 if not on track
        if not on_track:
            car.setOffTrackSpeed()
        else:
            car.setRegularSpeed()

        # Slow down car2 if not on track
        on_track = pygame.sprite.groupcollide(car_group2, pad_group, False,
                                              False)

        # Slow down car2 if not on track
        if not on_track:
            car2.setOffTrackSpeed()
        else:
            car2.setRegularSpeed()

        pygame.display.flip()
        # Check for collisions only if enabled
        if collisions:
            collision(car, car2, display_surface)

        if checkOutOfBounds(car):
            car.reset(start_car1)
        if checkOutOfBounds(car2):
            car2.reset(start_car2)
        checkpoint_car1 = checkpoint1(car, checkpoint, checkpoint_car1)
        checkpoint_car2 = checkpoint1(car2, checkpoint, checkpoint_car2)
        if checkpoint_car1 >= 1:
            previouslapcar1 = lap_car1
            lap_car1 = carLap(car, finish_line, lap_car1,
                              "Lap finished for car 1!")
            if lap_car1 > previouslapcar1:
                mixer.Sound.play(crowd)
                if lap_car1 == 5:
                    mixer.music.stop()
                    mixer.Sound.stop(crowd)
                    win(display_surface, "p1")
                checkpoint_car1 = 0
        if checkpoint_car2 >= 1:
            previouslapcar2 = lap_car2
            lap_car2 = carLap(car2, finish_line, lap_car2,
                              "Lap finished for car 2!")
            if lap_car2 > previouslapcar2:
                mixer.Sound.play(crowd)
                if lap_car2 == 5:
                    mixer.music.stop()
                    mixer.Sound.stop(crowd)
                    win(display_surface, "p2")
                checkpoint_car2 = 0

        while (time.time() - countdownTimerStart < 4):
            image = _load_image(
                './images/starting_lights/lights' +
                str(int(time.time() - countdownTimerStart) + 1) + '.png')
            display_surface.blit(image, ((1920 / 2) - (768 / 2), 50))
            fontBig = _load_font('./fonts/American Captain.ttf', 64)
            countdown_text = font.render("Time: " + str(4 - t0), True,
                                         (255, 255, 255))
            t0 = time.time()
            t1 = time.time()
            dt = t1 - t0
            countdownFinished = True
            pygame.display.update()
        # pygame.draw.rect(display_surface, (255, 255, 255), (960, 0, 30, 125))
        pygame.display.update()
コード例 #6
0
ファイル: timetrial2.py プロジェクト: Vishvak365/Karting-Pros
def timeTrial(display_surface):

    best_lap_time = 30000
    track = _load_image('./images/track2.png')
    track1 = track2.Track2()
    white = (0, 128, 0)

    clock = pygame.time.Clock()
    t0 = time.time()

    # Car Setup
    start_position = (1010, 144)
    car = Car('./images/f1sprite.png', start_position)

    car_group = pygame.sprite.Group(car)

    # Lap logic
    checkpoint_check = 0
    pad_group = track1.getPads()
    finish_line = (960, 50, 20, 125)
    checkpoint = (960, 845, 10, 125)

    # Countdown timer logic
    countdownTimerStart = time.time()
    countdownFinished = False

    # Music for countdown sound
    current_path = os.path.abspath(os.path.dirname(__file__))
    absolute_path = os.path.join(
        current_path, './sounds/race_coundown.mp3')
    print(absolute_path)
    mixer.init()
    mixer.music.load(absolute_path)
    mixer.music.set_volume(0.7)
    mixer.music.play()
    crowd = mixer.Sound(os.path.join(current_path, './sounds/crowd.wav'))

    data_collection = settings.getSetting('collect_data_for_AI')
    draw_hitbox = settings.getSetting('draw_hitbox')
    i = 0
    if data_collection:
        # Data collection for machine learning
        features = []
        labels = []
    right_press, left_press, up_press, down_press = 0, 0, 0, 0
    while True:
        if data_collection:
            # Machine Learning Features
            # Direction (%360), Position.X, Position.Y
            feature = []
            # Label(right,left,up,down)(1 or 0 for all)
            label = []

        # Draw the Track
        display_surface.fill(white)
        display_surface.blit(track, (0, 0))
        font = _load_font('./fonts/American Captain.ttf', 32)

        if data_collection:
            feature.append(car.direction % 360)
            feature.append(int(car.position[0]))
            feature.append(int(car.position[1]))
            feature = np.array(feature)
            feature = feature / feature.max(axis=0)
            features.append(feature)

        track2.checkpoint(display_surface)
        deltat = clock.tick(30)

        # Update Car and draw
        car_group.update(deltat)
        car_group.draw(display_surface)

        t1 = time.time()
        dt = t1-t0
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit(0)
            if not hasattr(event, 'key'):
                continue
            if event.key == K_RIGHT:
                right_press = 1
            elif event.key == K_SPACE:
                car.speed = 0
            elif event.key == K_LEFT:
                left_press = 1
            elif event.key == K_UP:
                mixer.music.load(os.path.join(
                    current_path, './sounds/rev.mp3'))
                mixer.music.play(-1)
                up_press = 1
            elif event.key == K_DOWN:
                down_press = 1
            elif event.key == K_ESCAPE:
                mixer.music.stop()
                mixer.Sound.stop(crowd)
                if data_collection:
                    np.save('features.npy', np.array(features))
                    np.save('labels.npy', np.array(labels))
                mainmenu.main_menu(display_surface)
            if event.type == KEYUP:
                if event.key == pygame.K_RIGHT:
                    right_press = 0
                elif event.key == pygame.K_LEFT:
                    left_press = 0
                elif event.key == pygame.K_UP:
                    mixer.music.stop()
                    up_press = 0
                elif event.key == pygame.K_DOWN:
                    down_press = 0

        car.k_right = right_press * -5
        car.k_left = left_press * 5
        car.k_up = up_press * 2
        car.k_down = down_press * -2

        if up_press == 0 and down_press == 0 and int(car.speed) != 0:
            car.k_down = -.2
            car.k_up = 0

        if data_collection:
            labels.append([right_press, left_press, up_press, down_press])

        # Check if car is on track
        on_track = pygame.sprite.groupcollide(
            car_group, pad_group, False, False)

        # Slow down car if not on track
        if not on_track:
            car.setOffTrackSpeed()
        else:
            car.setRegularSpeed()

        if draw_hitbox:
            pygame.draw.rect(display_surface, (255, 0, 0), car.hitbox, 2)

        pygame.display.flip()

        checkpoint_check = checkpoint1(car, checkpoint, checkpoint_check)

        # Countdown Timer Logic (program does not move forward until this is finished)
        while(time.time()-countdownTimerStart < 4):
            image = _load_image('./images/starting_lights/lights' +
                                str(int(time.time()-countdownTimerStart)+1)+'.png')
            display_surface.blit(image, ((1920/2)-(768/2), 50))
            fontBig = _load_font('./fonts/American Captain.ttf', 64)
            t0 = time.time()
            t1 = time.time()
            dt = t1-t0
            countdownFinished = True
            pygame.display.update()

        if(countdownFinished):
            # Timer
            timer_text = font.render(
                "Time: " + str(round(dt, 3)), True, (255, 255, 255))
            display_surface.blit(timer_text, (0, 0))

            # Time to Beat
            if best_lap_time != 30000:
                best_lap_text = font.render(
                    "Time to Beat: "+str(best_lap_time), True, (255, 255, 255))
                display_surface.blit(best_lap_text, (0, 30))

        if checkpoint_check >= 1:
            if completeLap(car, finish_line):
                mixer.Sound.play(crowd)
                if dt < best_lap_time:
                    best_lap_time = round(dt, 3)
                t0, t1 = time.time(), time.time()
                checkpoint_check = 0

        # If car is out of screen
        if checkOutOfBounds(car):
            car.reset(start_position)

        pygame.display.update()