Jigsaw puzzle

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ashish

I want to make a simple jigsaw puzzle game.
Should I make pieces of the puzzle Area2Ds? Then allow the player to move the Area2Ds in process function till the pieces reach a certain Vector2D position?
Will this work?

I don’t have an example to show you, but assuming it’s a jigsaw with regular shapes I’d make the “board” a grid container of size X * Y (where X is the number of columns and Y the number of rows). Then you drag and drop pieces onto the grid and the grid takes care of lining them up.

For a drag and drop function, look at this: https://kidscancode.org/godot_recipes/physics/rigidbody_drag_drop/

Bernard Cloutier | 2020-10-16 03:23

Thanks. This a great answer.
Do you need a rigidbody for this? How will the piece that needs to go beneath a cell reach its place? This will force the player to start with the bottom most row in the Grid first, and then work upwards.
Is there an example where one can pick up and drop a Sprite?
Thanks in advance.

ashish | 2020-10-16 08:14

Hi Bernard,
I tried to replicate the code in kidscancode.org with Sprites. However, I haven’t being able to invoke the drop function there. I’ve just separated the pickup and drop with a call to yield(), which not very useful.
I am not able to invoke the input-event function.
Other thing is that I am not able to see all the 3 Sprites (which are child nodes of World) during runtime. Only, one shows up during runtime. The Sprites are not instances, though. Is that the problem? Here is the code of my World and Ball script:

extends Sprite
signal clicked

var held = true

func _process(delta):
	drop_in_Grid()
	
	
	
func drop_in_Grid():
	while held:
		global_transform.origin = get_global_mouse_position()
		pickup()
		yield(get_tree().create_timer(1), "timeout")
		drop()
	held = false
		
		
func _input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton:
		if event.button_index == BUTTON_LEFT and event.pressed:
			
			emit_signal("clicked", self)

extends Node2D

var held_object = null

func _ready():
	for node in get_tree().get_nodes_in_group("pickable"):
		node.connect("clicked", self, "_on_pickable_clicked")

func _on_pickable_clicked(object):
	if !held_object:
		held_object = object
		held_object.pickup()
		print('hear')

func _unhandled_input(event):
	if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
		if held_object and !event.pressed:
			held_object.drop(Input.get_last_mouse_speed())
			held_object = null

Node Hierarchy

ashish | 2020-10-16 17:08