Bricks in Breakout

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

Hey I did wite the following code for the ball in a breakout game:

extends KinematicBody2D

export var speed = 200
var movement = Vector2(0, speed)
var can_move = true

func _ready():
    position.x = 250
    position.y = 470

func _physics_process(delta):
    if can_move == true:
	   var collision_info = move_and_collide(movement * delta)

	   if collision_info:
		   if == "Player":
			   speed = -speed
			   var diff = collision_info.collider.position.x - position.x
			   var new_movement = Vector2(-diff * 5, speed)
			   movement = new_movement
			   movement = movement.bounce(collision_info.normal)

Now I would like to add a bricks with a collision with the ball, wich get destroyed when they collide with the ball.
So my questions are:
Wich Node should I use for the bricks? and
How can I program the brick to be destroyed when it collides with the ball?

Thank you

:bust_in_silhouette: Reply From: kidscancode

If you don’t need the bricks to move, use StaticBody2D. If you do, use KinematicBody2D.

When you move the ball, you’re already detecting that you hit the paddle. You can also check if the thing you hit was a brick, and if so, call queue_free() on it to remove it.

Ok. Thank you

But how can I write my code, for detecting many bricks. For exemple I have 18 bricks how can I code the collission without writing 18 lines of code for each brick?

KeksKanone | 2019-12-29 11:27

I’m not sure what you’re saying - your brick doesn’t need code on it at all.

Put the brick in a group called “bricks”, and put the following on the ball:

var collision_info = move_and_collide(movement * delta)
if collision_info:
    if collision_info.collider.is_in_group("bricks"):

kidscancode | 2019-12-29 18:48

Nice. Thank you

KeksKanone | 2019-12-30 13:30