To reduce the "jumpy" behavior of cards in the player's hand, especially when the cursor is near the edges, we can make several improvements to the Ren'Py code. The jumpiness is likely caused by rapid hover/unhover events triggering animations repeatedly, especially when the cursor is near card boundaries. Here are the key improvements:
- Add a Hover Delay: Introduce a small delay before triggering hover/unhover animations to prevent rapid toggling when the cursor is near card edges.
- Smooth Transitions: Use smoother and slightly slower animations to reduce the perception of jumpiness.
- Expand Hover Area: Slightly expand the hover area of each card to prevent the cursor from accidentally leaving the card's hover zone, especially near edges.
- Debounce Hover Events: Use a debouncing mechanism to ensure hover/unhover events are not triggered too frequently.
- Improve Card Spacing and Positioning: Adjust card spacing and positioning to reduce overlap and improve hover behavior.
Below is the improved version of your code with these changes implemented:
Improved Code
# Player Hand
vbox:
align (0.5, 0.9)
spacing 10
fixed:
xsize 800
ysize 200
pos (0.5, 0)
anchor (0.5, 1)
$ sorted_hand = sorted(enumerate(player.hand), key=lambda x: 1 if x[0] == hovered_card_index else 0)
$ card_count = len(player.hand)
$ center_position = (card_count - 1) / 2.0
$ max_height_adjustment = min(20, 5 + card_count * 6) if card_count > 1 else 0
for i_card in sorted_hand:
$ i, card = i_card
$ base_angle = -20 + (40.0 / max(1, card_count - 1)) * i if card_count > 1 else 0
$ base_xpos = 250 - (card_count * 30) + (i * 120) if card_count > 1 else 250
if card_count <= 1:
$ height_adjustment = 0
else:
$ normalized_pos = (i - center_position) / max(1, center_position)
$ height_adjustment = int(max_height_adjustment * (1 - (normalized_pos ** 4)))
$ base_ypos = 150 - height_adjustment
button:
id "player_hand_card_{i}"
xsize 180
ysize 252
pos (base_xpos, base_ypos)
anchor (0.5, 1)
# Slightly expand the hover area to prevent edge jumpiness
xpadding 10
ypadding 10
at transform:
rotate base_angle
transform_anchor True
on hover:
# Smoother and slightly slower animations
linear 0.3 rotate 0
linear 0.3 ypos 150
linear 0.3 yoffset -30
on idle:
# Smoother and slightly slower animations
linear 0.4 rotate base_angle
linear 0.4 ypos base_ypos
linear 0.4 yoffset 0
if selected_card == i:
action [SetVariable("active_targeting_session", None), SetVariable("selected_card", None)]
else:
action [SetVariable("selected_card", i), Function(player.initiate_play_card, i), SetVariable("selected_creature", None)]
# Add a hover delay to prevent rapid toggling
hovered [SetVariable("hovered_card_index", i), If(hovered_card_index != i, true=Delay(0.1, SetVariable("hovered_card_index", i)))]
unhovered [SetVariable("hovered_card_index", None), If(hovered_card_index == i, true=Delay(0.1, SetVariable("hovered_card_index", None)))]
use card_preview(card, selected=(selected_card == i), playable=player.can_play_card(i), hoverable=(hovered_card_index == i), size=(180, 252))
# Improved card_preview screen
screen card_preview(card, selected=False, playable=False, size=(150, 210), hoverable=False, on_field=False, no_enlarge=False):
default hover_state = [False]
if on_field or no_enlarge:
$ enlarged = None
else:
$ enlarged = selected or (hoverable and hover_state[0])
if selected and not hover_state[0]:
$ zoom_factor = 1.2 if enlarged else 1.0
else:
$ zoom_factor = 1.8 if enlarged else 1.0
$ actual_size = (int(size[0] * zoom_factor), int(size[1] * zoom_factor))
$ offset_x = -((actual_size[0] - size[0]) // 2) if enlarged else 0
$ offset_y = -((actual_size[1] - size[1]) // 0.74) if enlarged else 0
$ outline_thickness = max(3, int(size[0] * 0.02 * zoom_factor))
$ outline_color = "#00ff00aa" if playable else ("#ffff00aa" if selected else None)
fixed:
xysize actual_size
xoffset offset_x
yoffset offset_y
if hoverable:
mousearea:
# Slightly expand the hover area to prevent edge jumpiness
area (-10, -10, actual_size[0] + 20, actual_size[1] + 20)
unhovered [SetDict(hover_state, 0, False), Delay(0.1, SetVariable("hovered_card", None))]
hovered [SetDict(hover_state, 0, True), Delay(0.1, SetVariable("hovered_card", card))]
if outline_color:
add Solid(outline_color) xsize (actual_size[0] + outline_thickness*2) ysize (actual_size[1] + outline_thickness*2) xpos (-outline_thickness) ypos (-outline_thickness)
# Card image
if on_field and isinstance(card, CreatureCard):
add "images/[card.card_id]-creature.png" xysize actual_size
else:
add "images/[card.card_id].png" xysize actual_size
# Mana cost
if not on_field:
fixed:
xpos int(size[0] * 0.02 * zoom_factor)
ypos int(size[0] * 0.02 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.mana]" size int(size[0] * 0.15 * zoom_factor) xalign 0.5 yalign 0.5 color "#FFFFFF" outlines [(int(outline_thickness/2), "#000000", 0, 0)]
# Attack value
if isinstance(card, CreatureCard):
$ attack_color = "#FFFFFF"
if hasattr(card, "original_attack"):
if card.attack < card.original_attack:
$ attack_color = "#FF4444"
elif card.attack > card.original_attack:
$ attack_color = "#44FF44"
if on_field:
fixed:
xpos int(size[0] * 0.02 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.25 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.attack]" size int(size[0] * 0.20 * zoom_factor) xalign 0.5 yalign 0.5 color attack_color outlines [(int(outline_thickness/2), "#000000", 0, 0)]
else:
fixed:
xpos int(size[0] * 0.0 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.18 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.attack]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color attack_color outlines [(int(outline_thickness/2), "#000000", 0, 0)]
# Health value
if isinstance(card, CreatureCard):
$ health_color = "#FFFFFF"
if hasattr(card, "original_health"):
if card.health < card.original_health:
$ health_color = "#FF4444"
elif card.health > card.original_health:
$ health_color = "#44FF44"
if on_field:
fixed:
xpos actual_size[0] - int(size[0] * 0.27 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.25 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.health]" size int(size[0] * 0.20 * zoom_factor) xalign 0.5 yalign 0.5 color health_color outlines [(int(outline_thickness/2), "#000000", 0, 0)]
else:
fixed:
xpos actual_size[0] - int(size[0] * 0.21 * zoom_factor)
ypos actual_size[1] - int(size[1] * 0.18 * zoom_factor)
xsize int(size[0] * 0.25 * zoom_factor)
ysize int(size[0] * 0.25 * zoom_factor)
text "[card.health]" size int(size[0] * 0.12 * zoom_factor) xalign 0.5 yalign 0.5 color health_color outlines [(int(outline_thickness/2), "#000000", 0, 0)]