rajgoel/Breakout.jl

A simple Breakout clone for fun and reinforcement learning on internal game state.

★ 9Forks 0JuliaGitHub ↗Compare

README

Breakout.jl

A simple Breakout clone for fun and reinforcement learning on internal game state.

Features

  • Interactive gameplay with keyboard control (← and →).
  • Automatic gameplay with heuristic control.
  • Automatic gameplay with custom control.
  • CommonRLInterface for learning with different state representations.

Installation

using Pkg
Pkg.add("Breakout")

Game

Breakout is a classic arcade game where the player controls a paddle to bounce a ball and break all the bricks on the screen. The objective is to clear all bricks without letting the ball fall past the paddle.

Breakout game

This version features 6 rows of 14 bricks each, with point values assigned by color:

  • Red – 10 points
  • Orange – 8 points
  • Yellow – 6 points
  • Green – 4 points
  • Blue – 2 points
  • Cyan – 1 point

The bricks sum to 434 points, and players earn a 66-point bonus for clearing all 84 bricks.

Quick Start

Human control

using Breakout
breakout()  # Normal speed (default: 1.0)
breakout(speed=0.5)  # Slower
breakout(speed=2.0)  # Faster
breakout(speed=nothing)  # Maximum speed

Heuristic control

using Breakout
breakout(Breakout.heuristic_action, speed=nothing)

Custom control

To create a custom controller, check out the controller implementations in the control/ folder.

API Reference

Main Functions

  • breakout(control_func=keyboard_action;autorestart=true, speed=1.0, max_steps=nothing) - Launch the game
  • BreakoutEnv(; frame_skip=4, max_steps=20000, representation=:full) - Create RL environment

State Representations for RL Training

The environment supports multiple state representations for reinforcement learning:

  • :minimal (2 features): paddle_x, ball_x for basic ball following
  • :brickless (5 features): paddle_x, ball_x, ball_y, ball_vx, ball_vy for advanced ball following without brick complexity
  • :full (89 features): Full internal game state including one-hot encoded brick positions (default)
  • :pixels (160 x 210 features): Grayscale pixel values of screenshot as flattened vector

RL Usage Examples

import CommonRLInterface as RL

# Environment with full game representation
env = BreakoutEnv()
state = RL.observe(env)  # Returns 89-element vector

# Environment with minimal representation
env = BreakoutEnv(:minimal)
state = RL.observe(env)  # Returns 2-element vector

Game State

The game state is a mutable struct containing:

  • score::Int - Current score
  • paddle_cx::Float64 - Paddle center x-coordinate
  • ball_cx::Float64 - Ball center x-coordinate
  • ball_vx::Float64 - Ball x-velocity
  • ball_cy::Float64 - Ball center y-coordinate
  • ball_vy::Float64 - Ball y-velocity
  • bricks::Vector - Array of remaining brick objects

Actions

The environment supports both discrete and continuous action spaces:

Discrete actions (default):

  • -1: Move paddle left
  • 0: Keep paddle stationary
  • 1: Move paddle right

Continuous actions:

  • Range (-1, 1): Continuous paddle movement speed
# Discrete actions (default)
env = BreakoutEnv(discrete=true)
actions = RL.actions(env)  # Returns [-1, 0, 1]

# Continuous actions  
env = BreakoutEnv(discrete=false)
actions = RL.actions(env)  # Returns (-1, 1)

License

MIT License

Contributors

rajgoel

Issues