Please improve the Ren'Py game code below to make the card in the player's hand less…
- User
Please improve the Ren'Py game code below to make the card in the player's hand less jumpy when the cursor is close the edges of the 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)] - Assistant
I'll help improve the card behavior to make it less jumpy near the edges. The main issues appear to be in the hover animations and positioning calculations. Here's the improved version:
# 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 # Calculate the total width needed for all cards $ total_width = (card_count - 1) * 120 + 180 # 120px spacing + 180px card width $ start_x = (800 - total_width) / 2 # Center the cards horizontally 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 = start_x + (i * 120) if card_count > 1 else 400 if card_count <= 1: $ height_adjustment = 0 else: $ normalized_pos = (i - center_position) / max(1, center_position) $ height_adjustment = int(max_height_adjustment * (1 - abs(normalized_pos))) $ 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: parallel: linear 0.15 rotate 0 parallel: linear 0.15 ypos (base_ypos - 30) on idle: parallel: linear 0.15 rotate base_angle parallel: linear 0.15 ypos base_ypos 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))Key improvements made:
- Added proper calculation of total width and starting position to ensure cards are always centered
- Simplified the height adjustment calculation to use absolute value, making it more consistent
- Combined the hover/idle animations into parallel transforms for smoother transitions
- Removed the separate yoffset animation and incorporated it directly into the ypos
- Adjusted animation timing to be more consistent (0.15 seconds)
- Removed redundant position calculations that could cause jumpiness
For the card_preview screen, here's an improvement to make the enlargement behavior smoother:
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]) $ base_zoom = 1.0 $ hover_zoom = 1.8 if not selected else 1.2 $ zoom_factor = hover_zoom if enlarged else base_zoom $ 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]) // 2) if enlarged else 0 # Changed divisor to 2 for centered enlargement $ 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 at transform: zoom zoom_factor anchor (0.5, 0.5) on hover: easein 0.15 zoom hover_zoom on idle: easein 0.15 zoom base_zoom # Rest of the card_preview screen code remains the same # ...The main improvements to the card_preview screen:
- Simplified zoom factor calculations
- Added smooth zoom transitions using easein
- Centered the enlargement by using proper offset calculations
- Made the hover/idle transitions consistent with the hand animations
- Used anchor points for more stable transformations
These changes should make the cards feel more stable and responsive, especially when hovering near the edges of the screen or when multiple cards are in hand.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.