All examples

Example app

Reach the coin

Arrow keys move a square; touch the gold square to win and press R to restart.

Run this example

Stop the app and save your current Main and helpers outside src/. Copy the files shown below into the listed destinations in your standalone starter, creating resource folders when needed. Replace Main and the listed helpers. Keep the zero/ framework folder and build files. If an older downloaded kit does not have these examples, use the source shown here.

Run App to rebuild. Click the canvas before testing input. These examples run in VS Code, in a separate JavaFX window.

Open beginner toolkit

Read the source

reach-the-coin/Main.java
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import zero.SketchApp;

/** Move the blue square to the gold square. */
public class Main extends SketchApp {
    private Player player;
    private double coinX = 520;
    private double coinY = 180;
    private double coinSize = 24;
    private boolean won;
    private Color backgroundColor = Color.web("#f6f5ff");
    private Color textColor = Color.web("#22213b");
    private Font headingFont = Font.font("System", FontWeight.BOLD, 28);
    private Font bodyFont = Font.font("System", 18);

    @Override
    public void settings() {
        size(640, 400);
        title("Reach the coin");
    }

    @Override
    public void setup() {
        restart();
    }

    public void restart() {
        player = new Player(40, 180);
        won = false;
    }

    @Override
    public void update(double seconds) {
        if (keyDown(KeyCode.R)) {
            restart();
            return;
        }
        if (won) return;

        int horizontal = 0;
        int vertical = 0;
        if (keyDown(KeyCode.LEFT)) horizontal--;
        if (keyDown(KeyCode.RIGHT)) horizontal++;
        if (keyDown(KeyCode.UP)) vertical--;
        if (keyDown(KeyCode.DOWN)) vertical++;

        // Main explicitly updates its ordinary Player object.
        player.update(horizontal, vertical, seconds, width(), height());
        if (player.touches(coinX, coinY, coinSize)) {
            won = true;
        }
    }

    @Override
    public void draw() {
        background(backgroundColor);
        fill(textColor);
        graphics().setFont(headingFont);
        text("Reach the coin", 24, 42);
        graphics().setFont(bodyFont);
        text("Arrow keys: move     R: restart", 24, 74);
        if (won) {
            text("You win! Press R to play again.", 24, 106);
        }
        fill(Color.GOLD);
        rect(coinX, coinY, coinSize, coinSize);
        player.draw(this);
    }

    public static void main(String[] args) {
        launch(args);
    }
}
reach-the-coin/Player.java
import javafx.scene.paint.Color;
import zero.SketchApp;

/** An ordinary Java object: it moves only when Main calls update. */
public class Player {
    private double x;
    private double y;
    private double size = 24;
    private double speed = 180;
    private Color color = Color.web("#6457e8");

    public Player(double startX, double startY) {
        x = startX;
        y = startY;
    }

    public void update(int horizontal, int vertical, double seconds,
                       double canvasWidth, double canvasHeight) {
        x += horizontal * speed * seconds;
        y += vertical * speed * seconds;

        // Keep the whole square inside the canvas.
        x = Math.max(0, Math.min(x, canvasWidth - size));
        y = Math.max(0, Math.min(y, canvasHeight - size));
    }

    public boolean touches(double coinX, double coinY, double coinSize) {
        // The squares touch when their horizontal and vertical ranges meet.
        return x + size >= coinX && x <= coinX + coinSize
                && y + size >= coinY && y <= coinY + coinSize;
    }

    public void draw(SketchApp app) {
        app.fill(color);
        app.rect(x, y, size, size);
    }
}