Coding Intro
Coding Intro
to
coding
using JavaScript and codeguppy.com
Agenda
• Text-based coding
• JavaScript language
• Free!
Why learn JavaScript?
• JavaScript is a super-popular real-world programming language
used both by beginners but also by professional software
developers. (The entire codeguppy.com was coded in JavaScript)
Password:
3
Note: After registration, you should get an email from codeguppy.com. REGISTER NOW
Please verify your email by clicking the link in the email.
Run / Stop Button
Assets Toolbar
Code Editor
You don’t have to write the above code. Just drag and drop a sprite from the palette in the code editor to have this code generated for you.
Build a program using drag-and-drop
(400, 300)
Our first type-in program
// Draw nose
circle(400, 400, 90);
circle(400, 350, 20);
Other graphical instructions
200
(400, 300)
circle(400, 300, 200);
200
(400, 300)
300
rect(400, 300, 300, 200); 200
(400, 300)
(400, 100)
point(400, 300);
(400, 300)
Note: Programs from the “Draw with code” booklet may have more instructions then presented here. Try to discover their purpose.
Introducing variables
• JavaScript allows users to define variables to hold various values or the result of complex calculations
• You can name variables however you prefer… but don’t start them with number or weird symbols
• Variables are super important in programming
let r = 100;
let r2 = 100 - 4;
OR
circle(100, 100, r);
circle(100, 100, r2);
Expressions and variables instead of scalars
let r2 = 100 – 4;
You can let the computer do
calculations. At the end of the
circle(100, 100, 100);
day, your computer is also a
powerful calculator.
circle(100, 100, r2);
Building our own custom instructions
circle2(); 3
function circle2() 2
{
circle(100, 100, 100); 1
circle(100, 100, 100 - 4);
}
Exercise: Design your own custom instruction (e.g. function) to draw something useful.
Modularizing code with functions
• Functions can also be used to modularize big pieces of code into smaller pieces
• The smaller pieces can then be assembled or reused exactly like Lego pieces
… …
Step 3: Increase
position x by 1
}
Increasing / decreasing variables
x++; x--;
OR OR
x += 1; x -= 1;
OR OR
x = x + 1; x = x - 1;
listing {
clear();
noStroke();
fill(180, 199, 231);
rect(0, 0, 800, 500);
background(); fill(191, 144, 0);
sun(); rect(0, 500, 800, 100);
car(x); }