Overview
Space Pong uses two main GDScript files:ball.gd for ball physics and game start logic, and player.gd for paddle movement control. Both scripts extend CharacterBody2D to leverage Godot’s physics engine.
Script Files
ball.gd
22 lines - Physics, collision, and game start
player.gd
20 lines - Input handling and movement
Ball Script
File:res://scenes/ball.gd (22 lines)
The ball script handles game initialization, physics simulation, and collision response.
Complete Source Code
Variables
started (bool)
Tracks whether the game has begun. Initialized to
false, set to true when spacebar is pressed.start_speed (int)
Initial vertical speed of the ball when the game starts. Set to 500 pixels/second.
incremental_speed (float)
Speed multiplier applied after each collision. Set to 1.05 (5% increase per bounce).
This creates progressively faster gameplay as the ball bounces more.
Physics Process
The_physics_process(delta) function runs every physics frame (~60 FPS):
Game Start Detection
- Input.is_action_pressed(“Start”): Is the spacebar (custom “Start” action) pressed?
- started == false: Has the game not yet started?
start_game() is called.
Movement and Collision
Move the Ball
move_and_collide(velocity * delta) moves the ball by its velocity scaled by delta time.- Returns
nullif no collision occurred - Returns a
KinematicCollision2Dobject if collision detected
Handle Collision
When collision occurs:
collision.get_normal()returns the surface normal vectorvelocity.bounce()reflects the velocity vector across the normal- Multiply by
incremental_speed(1.05) to increase speed
Start Game Function
pick_random() method selects either -250 (left) or +250 (right) from the angle array, ensuring varied gameplay.
Ball Physics Summary
| Property | Initial Value | After 1st Bounce | After 2nd Bounce |
|---|---|---|---|
| Vertical Speed | 500 px/s | 525 px/s | 551.25 px/s |
| Horizontal Speed | ±250 px/s | ±262.5 px/s | ±275.63 px/s |
| Speed Multiplier | 1.0x | 1.05x | 1.1025x |
The ball accelerates by 5% with each collision, creating increasing difficulty.
Player Script
File:res://scripts/player.gd (20 lines)
The player script handles paddle movement using arrow keys or touch input.
Complete Source Code
Variables
speed (@export int)
Exported variable for paddle movement speed. Default is 100 px/s, but overridden to 730 px/s in the scene file.
The
@export decorator makes this variable editable in the Godot editor. The scene file sets speed = 730.Ready Function
Physics Process
Velocity Reset
(0, 0) every frame. This prevents momentum accumulation and ensures the paddle only moves when input is pressed.
Input Handling
- Arrow key pressed: Built-in
ui_leftorui_rightactions - Game started:
ball.started == trueprevents movement before game begins
The paddle cannot move until the player presses spacebar to start the game. This prevents cheating by positioning the paddle before the ball launches.
Movement Application
- Handles collision detection with walls
- Stops movement when hitting boundaries
- Returns collision data (unused in this script)
Player Movement Summary
| Input | Velocity | Speed (px/s) | Direction |
|---|---|---|---|
| None | (0, 0) | 0 | Stationary |
| Left Arrow | (-730, 0) | 730 | Left |
| Right Arrow | (+730, 0) | 730 | Right |
| Both Arrows | (0, 0) | 0 | Cancelled out |
Input Actions Reference
Both scripts use Godot’s input action system:| Action | Key Binding | Used In | Purpose |
|---|---|---|---|
Start | Spacebar (keycode 32) | ball.gd | Start the game |
ui_left | Left Arrow (built-in) | player.gd | Move paddle left |
ui_right | Right Arrow (built-in) | player.gd | Move paddle right |
ui_left and ui_right are built-in Godot actions that automatically support keyboard, gamepad, and touch input.Script Interaction Flow
Code Quality Notes
Strengths
- Simple and readable: Both scripts are under 25 lines
- Clear variable names:
started,start_speed,incremental_speed - Good use of Godot features:
@export,move_and_collide(),velocity.bounce()
Potential Improvements
Remove Debug Print
The
print(velocity) statement in ball.gd:16 should be removed for production builds.Remove Empty Pass
The
pass statement at player.gd:19 serves no purpose and can be removed.Use Signals
Instead of
get_parent().get_node("Ball"), use signals to communicate game start.Add Speed Cap
The ball speed can grow infinitely. Consider adding a maximum speed limit.
Performance Characteristics
- Physics FPS: Both scripts run at 60 FPS (Godot default)
- Collision Detection:
move_and_collide()uses Godot’s optimized physics engine - Memory Usage: Minimal - no dynamic allocations during gameplay
- CPU Impact: Very low - simple vector math and conditional checks
Next Steps
Scenes
See how these scripts attach to scene nodes
Project Structure
Understand the overall project organization