Menu
CoddyTech

Breakout Ball

Part of the Projects section of Coddy's Godot journey. Lesson 3 of 36.

Next comes the ball, in ball.gd. It moves by its velocity: pixels per second, sideways and down.

var velocity = Vector2(110, -280)

func _physics_process(delta):
	position += velocity * delta

A negative y is up, so this ball starts up and to the right.

To bounce, flip one part of the velocity at a wall. A side wall flips velocity.x. The top wall flips velocity.y. The ball turns when its middle reaches x = 8, x = 632 or y = 42, under the score bar.

There is no bottom wall. The paddle guards that side.

challenge icon

Challenge

Medium

Make the ball fly and bounce off the walls.

  1. In ball.gd, add var velocity = Vector2(110, -280).
  2. Move the ball by velocity * delta every physics frame.
  3. Flip velocity.x when position.x is under 8 or over 632.
  4. Flip velocity.y when position.y is under 42.

Press Play: the ball bounces around the top and sides. Then Check.

Try it yourself

extends Area2D

func _physics_process(delta):
	pass

All lessons in Projects

Practice on your own: Godot playground