040 : Adding a goal

function init_game() { ... // Game state. _game.platform = create_platform(0, 360, _game.width, 40); _game.goal = create_goal(500, 360); // Global world parameters. ... }
Balthazar:

Add create_goal() after create_platform()

function create_platform(x, y, width, height) { ... } function create_goal(x, y) { var goal = {}; goal.x = x; goal.y = y; goal.width = 20; goal.height = 20; goal.origin_x = goal.width / 2; goal.origin_y = goal.height; return goal; }
Balthazar:

Add draw_goal()

function draw_platforms(ctx) { ... } function draw_goal(ctx) { var goal = _game.goal; ctx.fillStyle = "green"; ctx.fillRect(goal.x - goal.origin_x, goal.y - goal.origin_y, goal.width, goal.height); } // Draw the player. function draw_player(ctx) { ... }
Balthazar:

And update draw() to call draw_goal().

function draw() { var canvas = document.getElementById("stage"); var ctx = canvas.getContext("2d"); erase(ctx); draw_platforms(ctx); draw_goal(ctx); draw_player(ctx); }

RUN your code in a browser and verify that it loads without errors.

GOTO 043 if you already have the Movement III - Gravity badge.

GOTO 041