- 1

python pygame sprint

well, i'm probably using it wrong....but here i'm trying to make my character(char) sprint only when i hold down LSHIFT with RIGHT but when i press it, the speed does increases, but it doesn't goes back to 5 from 10 if keys[pygame.K_RIGHT]: anim = walk_right[walk] px += char_speed walk += 1 if walk >= 9: walk = 1 if px >= W: px = 0 if keys[pygame.K_LSHIFT]: char_speed = 10 if event.type == pygame.KEYUP: if event.type == pygame.K_LSHIFT: char_speed = 5 if keys[pygame.K_LEFT]: anim = walk_left[walk] px -= char_speed walk += 1 if walk >= 9: walk = 0 if px <= 0: px = W

17th Aug 2021, 2:44 PM
Tomoe
Tomoe - avatar
3 ответов
0
I think it's just a typo: if event.key == pygame.K_LSHIFT: char_speed = 5
17th Aug 2021, 4:42 PM
Angelo
Angelo - avatar
0
No, definitely not a typo-
17th Aug 2021, 7:35 PM
Tomoe
Tomoe - avatar
0
Ok, you're right, it's not just a typo, if you KEYUP LSHIFT after RIGHT it won't work So, looking better at your code, there are a couple things you should consider changing: Chack for event.type == KEYDOWN and than handle each key instead of using the get_pressed() dictionary (This should solve the problem) Use a generator to handle the animation, and just use next() or better yet define a class with a walk method and handle the animation there (this should make the code cleaner)
17th Aug 2021, 8:05 PM
Angelo
Angelo - avatar