Skip to content

Fix multi-jump logic with cooldown and logic fix #2729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions arcade/examples/platform_tutorial/15_ladders_moving_platforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
python -m arcade.examples.platform_tutorial.15_ladders_moving_platforms
"""
import arcade
import time

# Constants
WINDOW_WIDTH = 1280
Expand Down Expand Up @@ -65,6 +66,10 @@ def __init__(self):
self.jump_sound = arcade.load_sound(":resources:sounds/jump1.wav")
self.gameover_sound = arcade.load_sound(":resources:sounds/gameover1.wav")

# Jump interval (seconds)
self.jump_cooldown = 0.2
self.last_jump_time = 0.0

def setup(self):
"""Set up the game here. Call this function to restart the game."""
layer_options = {
Expand Down Expand Up @@ -113,6 +118,9 @@ def setup(self):
ladders=self.scene["Ladders"]
)

# Enables multi-jump for users to double jump
self.physics_engine.enable_multi_jump(2)

# Initialize our camera, setting a viewport the size of our window.
self.camera = arcade.Camera2D()

Expand Down Expand Up @@ -175,17 +183,22 @@ def on_update(self, delta_time):

def on_key_press(self, key, modifiers):
"""Called whenever a key is pressed."""
current_time = time.time()

if key == arcade.key.ESCAPE:
self.setup()

if key == arcade.key.UP or key == arcade.key.W:
if self.physics_engine.is_on_ladder():
self.player_sprite.change_y = PLAYER_MOVEMENT_SPEED
elif self.physics_engine.can_jump():
self.player_sprite.change_y = PLAYER_JUMP_SPEED
elif self.physics_engine.can_jump() and (current_time - self.last_jump_time) > (self.jump_cooldown):
# Calls jump function and maintains velocity while not
# allowing jumps to be repeated
self.physics_engine.jump(PLAYER_JUMP_SPEED)
arcade.play_sound(self.jump_sound)

self.last_jump_time = current_time
else:
pass
if key == arcade.key.DOWN or key == arcade.key.S:
if self.physics_engine.is_on_ladder():
self.player_sprite.change_y = -PLAYER_MOVEMENT_SPEED
Expand All @@ -203,7 +216,12 @@ def on_key_release(self, key, modifiers):
elif key == arcade.key.RIGHT or key == arcade.key.D:
self.player_sprite.change_x = 0
elif key == arcade.key.UP or key == arcade.key.W:
self.player_sprite.change_y = 0
if self.physics_engine.is_on_ladder():
# makes it so change_y only zeroes when on ladder
# fixes error with user pressing up while falling
# leading to a issue where fall speed is reset
# and user could basically "glide"
self.player_sprite.change_y = 0
elif key == arcade.key.DOWN or key == arcade.key.S:
self.player_sprite.change_y = 0

Expand Down
Loading