Please improve the Ren'Py game code below to make the cards in the player's hand less jumpy when the cursor is close the edges of a card.
# 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)
at transform:
rotate base_angle
transform_anchor True
on hover:
linear 0.1 rotate 0
linear 0.1 ypos 150
linear 0.1 yoffset -30
on idle:
linear 0.2 rotate base_angle
linear 0.2 ypos base_ypos
linear 0.2 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),]
hovered SetVariable("hovered_card_index", i)
unhovered 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))
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:
area (0, 0, actual_size[0], actual_size[1])
unhovered SetDict(hover_state, 0, False)
hovered [SetDict(hover_state, 0, True), 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)]