Reputation: 43
Im sorta new to python and pygame, and I need help with something relating to acceleration. I followed a tutorial on youtube on how to make a base for a platforming game, and I've been using it to create a game that plays like a Kirby game. One of the tiny little details i notice in a Kirby game is how he skids when you move in a direction and then quickly move turn to the other direction, and for the past few days I was figuring out how to get it to work. The solution I came up with was to make it so instead of the character just moving whenever a key is pressed, the character will accelerate and then stop accelerating once it's reached its max speed, and then quickly slow down and accelerate again once you press another direction key. Problem is, I dont know how to program acceleration. Can anyone help me with this?
Here is the code i wrote for the game (first bit is for collision, second bit is for actually moving the player):
def move(rect, movement, tiles):
collide_types = {'top': False, 'bottom': False, 'right': False, 'left': False}
rect.x += movement[0]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
if movement[0] > 0:
rect.right = tile.left
collide_types['right'] = True
if movement[0] < 0:
rect.left = tile.right
collide_types['left'] = True
rect.y += movement[1]
hit_list = collide_test(rect, tiles)
for tile in hit_list:
if movement[1] > 0:
rect.bottom = tile.top
collide_types['bottom'] = True
if movement[1] < 0:
rect.top = tile.bottom
collide_types['top'] = True
return rect, collide_types
Second bit:
player_move = [0, 0]
if move_right:
player_move[0] += 2.5
elif run:
player_move[0] += -3
if move_left:
player_move[0] -= 2
elif run:
player_move[0] -= -3
player_move[1] += verticle_momentum
verticle_momentum += 0.4
if verticle_momentum > 12:
verticle_momentum = 12
elif slow_fall == True:
verticle_momentum = 1
if fly:
verticle_momentum = -2
slow_fall = True
if verticle_momentum != 0:
if ground_snd_timer == 0:
ground_snd_timer = 20
Upvotes: 3
Views: 803
Reputation: 183
Instead of directly changing the position of the character on button press, you should change the velocity. So for example, taking movement on the X axis only:
acc = 0.02 # the rate of change for velocity
if move_right:
xVel += acc
if move_left:
xVel -= acc
# And now change your character position based on the current velocity
character.pose.x += xVel
Other things you can add: make it so when you don't press any keys you lose momentum so you can come at a stop. You can do this by substracting from velocity or adding to it a certain decay factor (which is smaller than your acceleration constant, but you will have to tune it as you experiment with your game).
Upvotes: 3