Add jump cooldown, make left and right diagonals slide you down correctly, improve the map a bit, add high score, score and try count

This commit is contained in:
csd4ni3l
2025-11-28 19:05:20 +01:00
parent 88f18b200d
commit 21b1d04369
4 changed files with 105 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
import arcade, arcade.gui, json
import arcade, arcade.gui, json, time, os
from utils.constants import FOLLOW_DECAY_CONST, GRAVITY, PLAYER_MOVEMENT_SPEED, PLAYER_JUMP_SPEED, GRID_PIXEL_SIZE
from utils.constants import FOLLOW_DECAY_CONST, GRAVITY, PLAYER_MOVEMENT_SPEED, PLAYER_JUMP_SPEED, GRID_PIXEL_SIZE, PLAYER_JUMP_COOLDOWN, LEFT_RIGHT_DIAGONAL_ID, RIGHT_LEFT_DIAGONAL_ID
from utils.preload import tile_map, player_still_animation, player_jump_animation, player_walk_animation, freeze_sound, background_sound
class Game(arcade.gui.UIView):
@@ -35,6 +35,35 @@ class Game(arcade.gui.UIView):
with open("settings.json", "r") as file:
self.settings = json.load(file)
if os.path.exists("data.json"):
with open("data.json", "r") as file:
self.data = json.load(file)
else:
self.data = {}
self.high_score = self.data.get("high_score", 9999)
self.tries = self.data.get("tries", 1)
if self.high_score == 9999:
self.no_highscore = True
self.high_score = 0
else:
self.no_highscore = False
self.last_jump = time.perf_counter()
self.start = time.perf_counter()
self.right_left_diagonal_sprites = [
sprite for sprite in self.scene["ice"]
if hasattr(sprite, 'properties') and
sprite.properties.get('tile_id') == RIGHT_LEFT_DIAGONAL_ID
]
self.left_right_diagonal_sprites = [
sprite for sprite in self.scene["ice"]
if hasattr(sprite, 'properties') and
sprite.properties.get('tile_id') == LEFT_RIGHT_DIAGONAL_ID
]
if self.settings.get("sfx", True):
self.freeze_player = freeze_sound.play(loop=True, volume=self.settings.get("sfx_volume", 100) / 100)
self.freeze_player.pause()
@@ -44,10 +73,14 @@ class Game(arcade.gui.UIView):
def on_show_view(self):
super().on_show_view()
self.info_label = self.anchor.add(arcade.gui.UILabel(text=f"Time took: 0s High Score: {self.high_score}s Tries: {self.tries}", font_size=20), anchor_x="center", anchor_y="top")
def reset(self):
self.warmth = 100
self.player.change_x, self.player.change_y = 0, 0
self.player.position = self.spawn_position
self.start = time.perf_counter()
self.tries += 1
def create_scene(self) -> arcade.Scene:
self.camera_bounds = arcade.LRBT(
@@ -92,42 +125,47 @@ class Game(arcade.gui.UIView):
hit_list = self.physics_engine.update()
self.center_camera_to_player()
if self.warmth <= 0:
self.reset()
if self.no_highscore:
self.high_score = round(time.perf_counter() - self.start, 2)
if self.player.collides_with_list(self.scene["spikes"]):
self.reset()
self.info_label.text = f"Time took: {round(time.perf_counter() - self.start, 2)}s High Score: {self.high_score}s Tries: {self.tries}"
if self.player.center_x < 0 or self.player.center_x > tile_map.width * GRID_PIXEL_SIZE:
self.reset()
elif self.player.center_y < 0:
if self.warmth <= 0 or self.player.collides_with_list(self.scene["spikes"]) or self.player.center_x < 0 or self.player.center_x > tile_map.width * GRID_PIXEL_SIZE or self.player.center_y < 0:
self.reset()
ice_touch = any([ice_sprite in hit_list for ice_sprite in self.scene["ice"]]) and self.physics_engine.can_jump()
moved = False
if self.window.keyboard[arcade.key.UP] or self.window.keyboard[arcade.key.SPACE]:
if self.physics_engine.can_jump():
if time.perf_counter() - self.last_jump >= PLAYER_JUMP_COOLDOWN and self.physics_engine.can_jump():
self.last_jump = time.perf_counter()
self.player.change_y = PLAYER_JUMP_SPEED
if self.window.keyboard[arcade.key.LEFT] or self.window.keyboard[arcade.key.A]:
moved = True
self.player.change_x = -PLAYER_MOVEMENT_SPEED
self.direction = "left"
elif self.window.keyboard[arcade.key.RIGHT] or self.window.keyboard[arcade.key.D]:
moved = True
self.player.change_x = PLAYER_MOVEMENT_SPEED
self.direction = "right"
else:
if ice_touch:
if self.direction == "right":
self.player.change_x = self.clamp(self.player.change_x * 0.7, PLAYER_MOVEMENT_SPEED * 0.05, PLAYER_MOVEMENT_SPEED)
on_left_right_diagonal = any([True for hit in hit_list if hit in self.left_right_diagonal_sprites])
on_right_left_diagonal = any([True for hit in hit_list if hit in self.right_left_diagonal_sprites])
if on_left_right_diagonal or (self.direction == "right" and not on_right_left_diagonal):
self.player.change_x = self.clamp(self.player.change_x * 0.75, PLAYER_MOVEMENT_SPEED * 0.25, PLAYER_MOVEMENT_SPEED)
else:
self.player.change_x = self.clamp(self.player.change_x * 0.7, -PLAYER_MOVEMENT_SPEED, -PLAYER_MOVEMENT_SPEED * 0.05)
self.player.change_x = self.clamp(self.player.change_x * 0.75, -PLAYER_MOVEMENT_SPEED, -PLAYER_MOVEMENT_SPEED * 0.25)
else:
self.player.change_x = 0
if ice_touch:
self.player.change_x *= 1.3
if moved and ice_touch:
self.player.change_x *= 1.2
if abs(self.player.rect.distance_from_bounds(self.spawn_position)) > GRID_PIXEL_SIZE * 5:
if self.settings.get("sfx", True) and not self.freeze_player.playing:
@@ -140,9 +178,20 @@ class Game(arcade.gui.UIView):
if self.player.change_y > 0:
self.change_player_animation(player_jump_animation)
elif abs(self.player.change_x) > 0:
elif abs(self.player.change_x) > PLAYER_MOVEMENT_SPEED * 0.25:
self.change_player_animation(player_walk_animation)
else:
self.change_player_animation(player_still_animation)
self.player.update_animation()
self.player.update_animation()
def on_key_press(self, symbol, modifiers):
if symbol == arcade.key.ESCAPE:
with open("data.json", "w") as file:
file.write(json.dumps({
"high_score": self.high_score,
"tries": self.tries
}, indent=4))
from menus.main import Main
self.window.show_view(Main(self.pypresence_client))