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 * deltaA 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
MediumMake the ball fly and bounce off the walls.
- In
ball.gd, addvar velocity = Vector2(110, -280). - Move the ball by
velocity * deltaevery physics frame. - Flip
velocity.xwhenposition.xis under 8 or over 632. - Flip
velocity.ywhenposition.yis 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