I know there are already other posts about this out there, but my movement system is a little from the ones that I have found, so subsequently I am asking this question.
My movement system is based on a named tuple called Move(up,left,right,down)
Then there is this:
def update(self, move, blocks):
# check if we can jump
if move.up and self.on_ground:
self.yvel -= self.jump_speed
# simple left/right movement
if move.left:
self.xvel = -self.move_speed
if move.right:
self.xvel = self.move_speed
# if in the air, fall down
if not self.on_ground:
self.yvel += 0.3
# but not too fast
if self.yvel > max_gravity: self.yvel = max_gravity
# if no left/right movement, x speed is 0, of course
if not (move.left or move.right):
self.xvel = 0
# move horizontal, and check for horizontal collisions
self.rect.left += self.xvel
self.collide(self.xvel, 0, blocks)
# move vertically, and check for vertical collisions
self.rect.top += self.yvel
self.on_ground = False
self.collide(0, self.yvel, blocks)
def collide(self, xvel, yvel, blocks):
# all blocks that we collide with
for block in [blocks[i] for i in self.rect.collidelistall(blocks)]:
# if xvel is > 0, we know our right side bumped
# into the left side of a block etc.
if xvel > 0:
self.rect.right = block.rect.left;self.xvel=0
if xvel < 0:
self.rect.left = block.rect.right;self.xvel=0
# if yvel > 0, we are falling, so if a collision happpens
# we know we hit the ground (remember, we seperated checking for
# horizontal and vertical collision, so if yvel != 0, xvel is 0)
if yvel > 0:
self.rect.bottom = block.rect.top
self.on_ground = True
self.yvel = 0
# if yvel < 0 and a collision occurs, we bumped our head
# on a block above us
if yvel < 0: self.rect.top = block.rect.bottom;self.yvel=0
I tried adding a fifth variable to the tuple called upUp
, and when that was called it would trigger another jump, regardless of if on_ground
was true or not.
To trigger it I used this in the event loop:
if e.type==KEYUP:
if dj==0:
dj=-1
if dj=-1:
dj='true'
Move(K_w,K_a,K_d,K_s,dj)
But this didn't work well at all! Anyone got any suggestions?
You have to maintain some kind of state to keep track of which "phase" of jumping you're in.
These "phases" are:
So you should be able to do something like:
def update(self, move, blocks):
if self.on_ground:
self.jump_state = 'on_ground'
if move.up and self.on_ground:
# jump!
self.yvel -= self.jump_speed
self.jump_state = 'jumped'
if self.jump_state = 'jumped' and not move.up and not self.on_ground:
self.jump_state = 'ready_for_double_jump'
if self.jump_state = 'ready_for_double_jump' and move.up:
# jump!
self.yvel -= self.jump_speed
self.jump_state = 'double_jumped'
...
You'll get the idea.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With