Advertisement

How to make character acceleration in pygame ?

Started by July 28, 2019 10:01 PM
0 comments, last by dMn420 5 years, 4 months ago

I'm doing well with my first game that I'm making and I had no problem with understanding, how to do something but now, I don't even have a clue about How to accelerate my character.

 


    def move(rect, movement, tiles):
        collision_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
        rect.x += movement[0]
        hit_list = collision_test(rect, tiles)
        for tile in hit_list:
            if movement[0] > 0:
                rect.right = tile.left
            elif movement[0] < 0:
                rect.left = tile.right
                collision_types['left'] = True
        rect.y += movement[1]
        hit_list = collision_test(rect, tiles)
        for tile in hit_list:
            if movement[1] > 0:
                rect.bottom = tile.top
                collision_types['bottom'] = True
            elif movement[1] < 0:
                rect.top = tile.bottom
                collision_types['top'] = True
        return rect, collision_types

    player_movement = [0, 0]
    if moving_right == True:
        player_movement[0] += 2 
    if moving_left == True:
        player_movement[0] -= 2 
    player_movement[1] += vertical_momentum
    vertical_momentum += 0.3
    if vertical_momentum > 3:
        vertical_momentum = 3
    player_rect, collisions = move(player_rect, player_movement, tile_rects)

    if collisions['bottom'] == True:
        air_timer = 0
        vertical_momentum = 0
    else:
        air_timer += 1

    display.blit(player_img, (player_rect.x - scroll[0], player_rect.y - scroll[1]))

    for event in pygame.event.get():  # event loop
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                moving_right = True
            if event.key == K_LEFT:
                moving_left = True
            if event.key == K_UP:
                if air_timer < 6:
                    vertical_momentum = -5
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                moving_right = False
            if event.key == K_LEFT:
                moving_left = False

    screen.blit(pygame.transform.scale(display, WINDOW_SIZE), (0, 0))
    pygame.display.update()
    clock.tick(60)
(initial speed is 2 player_movement[0] += 2). I'm thinking about doing it like - > add 0.1 speed every 0.1 second or every 1 tick so it smooth and make it maximally reach to 5.
I was looking at car related games but they are to different.

This topic is closed to new replies.

Advertisement