Menu
CoddyTech

Keep the Paddle on Screen

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

Hold Right and the paddle slides off the screen. clamp(value, low, high) keeps a number between two limits. Below low it gives low. Above high it gives high. Otherwise it gives the number unchanged.

position.x = clamp(position.x, 40, 600)

The paddle is 80 pixels wide. At x = 40 its left side touches the left edge. At x = 600 its right side touches the right edge, at 640.

Clamp after the move. If you clamp first, the move can push the paddle past the edge.

challenge icon

Challenge

Easy

Keep the paddle fully on the screen.

  1. In paddle.gd, after the paddle moves, clamp position.x.
  2. Keep it between 40 and 600.

Press Play and hold Right: the paddle stops at the edge. Then Check.

Try it yourself

extends CharacterBody2D

const SPEED = 400

func _physics_process(delta):
	var direction = Input.get_axis("move_left", "move_right")
	position.x += direction * SPEED * delta

All lessons in Projects

Practice on your own: Godot playground