i need help with a school lesson

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

Hello I started studying programming this year and the teacher passed this lesson

"Implement a 2D application in Godot, where elements arranged in an MxN matrix are placed on the stage, and using the mouse, every time the user clicks on an element, it changes to a random color.***

Rules: the use of an arrangement is mandatory, so that, when it comes to knowing where the user clicked with the mouse, to recognize the clicked element, one must go through the arrangement and identify the proximity between the position of the element in the scene and the position of the mouse click, and if the proximity is within a limit, it must be considered that the user clicked on the element."

So far I’ve made this code:

extends Node

var colors = [Color( 0.93, 0.51, 0.93, 1 ),
 Color( 1, 1, 0, 1 ),
 Color( 0, 1, 0, 1 ),
 Color( 1, 0.75, 0.8, 1),
 Color( 1, 0, 0, 1 )]
var colors2 = [Color( 0, 0, 1, 1 ) ,
 Color( 1, 0.84, 0, 1 ),
 Color( 0.82, 0.41, 0.12, 1 ),
 Color( 0, 1, 1, 1 ),
 Color( 0.75, 0.75, 0.75, 1 )]

var array_a = ["Node2D/Sprite1","Node2D2/Sprite2"]
var array_b = ["Node2D3/Sprite3","Node2D4/Sprite4"]

var imagens_2d = [array_a, array_b]

func _input(event):
   
 if event.is_action_pressed("click"):
   
  get_node("Node2D/Sprite1").modulate = colors[randi() % colors.size()]

	
 
  get_node("Node2D2/Sprite2").modulate = colors2 [randi() % colors.size()]
 
  get_node("Node2D3/Sprite3").modulate = colors2 [randi() % colors.size()]

  get_node("Node2D4/Sprite4").modulate = colors2 [randi() % colors.size()]



pass

If anyone has any ideas or suggestions I would appreciate it and one more thing I am Brazilian so I’m sorry for my English from Google translator

I am not going to write this as an answer because it does not follow given rules, but:

In Godot you are not supposed to “go through an arrangement and identify a proximity”. Ask your teacher to give meaningful homeworks (tasks in common) or quit lessons like this. I really hate semiteachers who are trying to adapt old good hello world lessons to modern engines. I believe you should not to reinvent the wheels, when there are built-in features to achieve something even during the lessons.

Simplest way of arranging items in grid is using GridContainer node (green one).
Simplest way of detecting click on item is using whatever Control node (green one) and overriding _gui_input function like this:

func _gui_input(event):
   if event is InputEventMouseButton:
      if event.button_index == BUTTON_LEFT and event.pressed:
         print('clicked')

“Simplest” should allways be the correct way, if possible. Of course, you may fulfill the lesson by given conditions, it’s up to you, but I am not going to code it for you, sorry.

Wonderfully I wrote mini-game for my 2yo today where tiles are aligned to grid having click interaction also. You may get inspired there: colors.zip - Google Drive

Reloecc | 2020-09-12 18:55