← Pixel Dreamer Studio

A hand of cards that always fits, without spreading out when it is small

Keep the normal gap while the cards fit, overlap them evenly when they do not, and centre the hand.

Placing cards a fixed distance apart works until the hand grows: in our Godot 4.7 test 120 px cards 130 px apart made a hand 900 px wide at 7 cards, 1,290 px at 10 and 2,590 px at 20, in 800 px of room. Sharing the room by the number of gaps fixes big hands but breaks small ones: one card got no usable position at all, because count - 1 is 0 and Godot divided by zero, and two cards were pushed 680 px apart. Using the normal gap while the hand fits, overlapping evenly only when it does not, and centring the result kept every hand we tried, from 0 to 20 cards, within the 800 px.

The code

Save it as hand_layout.gd, load it with const HandLayout := preload("res://hand_layout.gd") and position the cards with var xs := HandLayout.positions(hand.size(), 120.0, 800.0), then card.position.x = centre_x + xs[i] for each card. Call it again whenever a card is drawn or played. Tween the cards to the new x if you want them to slide.

extends RefCounted
## Lays out a hand of cards centred, side by side while they fit, and
## overlapping evenly once they do not, so the hand never runs off the screen.
##   var xs := HandLayout.positions(hand.size(), 120.0, 800.0)
##   for i in hand.size():
##       hand[i].position.x = hand_centre_x + xs[i]      # left edge of card i
## Works for 0 and 1 card too.


static func positions(count: int, card_width: float, max_width: float, gap := 10.0) -> PackedFloat32Array:
	var step := card_width + gap
	if count > 1:
		step = minf(step, (max_width - card_width) / (count - 1))  # squeeze only when needed
	var total := card_width + step * maxi(count - 1, 0)
	var xs := PackedFloat32Array()
	for i in count:
		xs.append(-total / 2.0 + i * step)
	return xs


static func width(count: int, card_width: float, max_width: float, gap := 10.0) -> float:
	if count == 0:
		return 0.0
	var xs := positions(count, card_width, max_width, gap)
	return xs[count - 1] + card_width - xs[0]

What we measured

Cards 120 px wide, a 10 px gap and 800 px of room, laid out three ways. Two runs gave the same numbers.

Why it happens

A fixed step only knows the gap, not the room, so the hand just keeps growing. Dividing the room by count - 1 always uses all of it, which looks fine for a full hand but pushes two or three cards to the far edges, and for one card it divides by zero. The script uses whichever step is smaller, the normal one or the one that fits, and centres the hand on the total width it ends up with.

What to change

What a played card looks like: the Card Game Feel Kit for Godot 4 plays cards with swoops, burns and shatters, with damage numbers and screen shake built in.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Card Game Feel Kit for Godot 4 plays cards with swoops, burns and shatters, with damage numbers and screen shake built in.

More how-tos for Card Game Feel Kit for Godot 4: Screen shake that returns to center

More how-tos: UI and HUD