JavaScript is the language you’re using in Code.org to create animations, games, and interactive programs. You write instructions for the computer to follow using blocks or text.
What is a variable?
A variable stores information (like numbers or text) so your program can
use or change it later.
// Create a variable var score = 0; // Update variable score = score + 1; // Display variable on screen text(score, 200, 100);
Block | Purpose |
---|---|
var x = 0; | Declare a new variable |
x = x + 1; | Change variable value |
text(x, xPos, yPos); | Show variable value on screen |
sprite.x = horizontal position sprite.y = vertical position
Block | Parameters | Example |
---|---|---|
createSprite(x, y) | Position of sprite | createSprite(100, 200) |
sprite.x = value | Set horizontal position | ball.x = 250 |
sprite.y = value | Set vertical position | ball.y = 150 |
Create and control images/characters on the screen.
var player = createSprite(100, 200); player.setAnimation("hero_idle"); player.scale = 0.5;
Block | Parameters | Example |
---|---|---|
createSprite(x, y) | X and Y position | createSprite(100, 200) |
setAnimation("name") | Costume/image | s.setAnimation("cat_1") |
sprite.scale = value | Size (1 = normal) | sprite.scale = 0.5 |
sprite.rotation = deg | Rotate sprite | sprite.rotation = 45 |
sprite.x = value | Move left/right | ball.x = 3 |
sprite.y = value | Move up/down | ball.y = -3 |
What is it?
The draw() function runs continuously, about 30–60 times per second.
Everything inside it updates on the screen.
The draw() function runs again and again (like a flipbook). This is where animation happens!
function draw() { background("white"); drawSprites(); // shows all your sprites }
Example:
// Moves right sprite.velocityX = 3; // Moves up sprite.velocityY = -2;
The counter pattern is used to increase or decrease a variable by a consistent amount—most commonly used for things like:
variable = variable + 1;
variable = variable - 1;
Pattern Code | Description | Example Use |
---|---|---|
score = score + 1 | Adds 1 to score | Track points |
x = x + 1 | Moves sprite right | Sprite movement |
y = y - 1 | Moves sprite up | Sprite movement |
time = time - 1 | Countdown timer | Game timer |
speed = speed + 2 | Increases speed by 2 each frame | Acceleration effect |
size = size - 0.1 | Shrinks something slowly | Animation effect |
Conditionals let your program make decisions.
if (score > 10) { console.log("You're doing great!"); } else { console.log("Keep trying!"); }
if (condition) { // do this if true } else { // do this if false }
Symbol | Meaning | Example |
---|---|---|
== | Equal to | score == 10 |
!= | Not equal | lives != 0 |
> | Greater than | points > 50 |
< | Less than | speed < 5 |
>= | Greater or equal | score >= 100 |
<= | Less or equal | level <= 3 |