Welcome To Processing 2019 An Introduction Into Programming - Michael Q
Welcome To Processing 2019 An Introduction Into Programming - Michael Q
● background (grayscale);
● size(screenWidth, screenHeight);
fill(150,200,150); fill(240,200,200);
Why is 255 the maximum number?
● It is used because 255 is a number that is important to
binary. Early developers of the binary computers used
bytes as a standard unit.
}
text()
● Draws some text on the screen!
● text(“text example”, x, y)
https://forms.gle/tHreweCxS7fXsGq68
Review Two
Shape Modes, Precise Coordinates,
Void Functions, & Introduction into
Text.
Precise Coordinates, void Functions,
and text() review!
● Draw three ellipses and one Hi there!
rectangle to make a person.
Attempt to make two people!
● text(“text example”, x, y)
textAlign([x-coordinate],[y-coordinate])
[34] "Berlin Sans FB" [69] "Californian FB" [102] "Consolas" [195] "Javanese Text" [367] "Symbol"
Example Fonts: [35] "Berlin Sans FB Bold" [70] "Californian FB Bold" [106] "Constantia" [196] "Jokerman" [371] "Times New Roman"
[2] "Algerian"
[22] "Bahnschrift SemiBold" [76] "Cambria" [117] "Courier New" [289] "Parchment" [396] "Wingdings 2"
[3] "Arial"
[39] "Bodoni MT" [81] "Candara" [130] "Ebrima" [290] "Perpetua" [397] "Wingdings 3"
[13] "Bahnschrift"
[60] "Britannic Bold" [98] "Comic Sans MS" [154] "French Script MT" [300] "Ravie"
The random() function can not only be used as a void but can be used in
other voids to generate random numbers. Every time the function is
called, an unexpected value gets generated between 0 and the highest
function you put into there.
For example, if you put random(5), it will choose a number between 0 and
5. If you put random(-10, 15), it will return values starting at -10 and up to
15. (However, not equal to 15.) Basically, -10 < x < 15.
random()
Consider this function:
------------------------------------------------>
Making a object have its own function:
------------------------------------------------>
How the function should look:
------------------------------------------------>
Creating a new void move function:
Make a new void move function by naming it what fits best with you. Add
the name of this function to draw() above displayicon().
Then, copy and paste your definitions of xLoc and yLoc. Example, xLoc =
width/2, etc. Then comment those out.
{
// xLoc = width/2
//yLoc = height/2
}
Moving your icon / object
To move your object, you simply need to add something the x-coordinates
and the y-coordinates while the program is running. To do this, you will
write a formula in the moveicon() function using xLoc and yLoc.
For example:
To fix this, go back to the void draw() function add the following:
void draw()
{
background(255);
...
}
Delete background from void setup()
Since void draw() makes the background every 60 frames,
void setup() doesn’t need the background function so you may remove it!
void setup()
{
size(500,500);
colorMode(HSB);
b̶̶a̶c̶k̶g̶r̶o̶u̶n̶d̶(̶2̶5̶5̶)̶;̶
}
Practice with movement!
Now that you’ve learned how to move your objects with the moveicon()
function using xLoc and yLoc, test out how the object moves by editing
your code this far.
For example, what happens when you change what is being added or
subtracted by xLoc and yLoc.
xLoc = xLoc + 1
yLoc = yLoc +1
Review Three
Finishing Text, Creating Variables,
& Introduction to Movement
Before we start...
● Find the Processing file stored into your computer and
move it into your flash drive.
● void draw()
text review!
Float Variables
● Variables allows programmers to put a “name” for a data
type. This variable is called a “float” which holds
numbers.
Define xLoc and yLoc by using a new // Add moveicon() into void
formula, for example: draw() but make sure to put
it above your design
“xLoc = xLoc + 1; function!
yLoc = yLoc + 1;”
Fixing background issue void setup()
{
size(500,500);
To fix your “black blur” issue with their colorMode(HSB);
object. All you would need to do is move rectMode(CENTER);
your background command from void b̶̶a̶c̶k̶g̶r̶o̶u̶n̶d̶(̶2̶5̶5̶)̶;̶
setup() and move that over to void draw() }
void draw()
This fixes the issue because the object {
was being made every 60 frames per background(255);
second, with the background command moveObject();
occuring at the same speed, the drawObject();
background basically restores itself. }
Lesson Four:
Explanations, Object Movement,
& a brief introduction into Controls!
Global Variables
Pfont myFont;
The reason that PFont, xLoc, and yLoc
float xLoc;
are all on the top of the program is float yLoc;
because they are being used as Global
Variables. void setup()
{
size(500,500);
So the difference between Global
colorMode(HSB);
Variables and normal variables is that rectMode(CENTER);
they are available throughout the }
program.
Movement Explanation
void moveObject()
The assignment statement of xLoc states {
that the statement is taking the current xLoc = xLoc + 1
xLoc value and adds it by 1. yLoc = yLoc + 1
}
void moveObject()
Update both void setup() and void {
xLoc = xLoc + xVel;
moveObject() to include xVel. }
Brief Introduction into controls.
Now that you’ve learned how to void moveObject()
define the x location, y location, {
and the x velocity. How about xLoc = mouseX;
you take a break and get a brief yLoc = mouseY;
introduction into controls. println(xLoc, yLoc);
// The println() function
Use the built-in system variables: prints the coordinates of the
mouseX & mouseY! Make sure object which will be helpful
you print information on this in later.
the console log! }
Lesson Five:
if Statements & Wall Collision
EJECT YOUR FLASH DRIVES
Remember to press eject on your flash drives before you
remove them from your computer! This is crucial for the
security of your flash drives so that all your files don’t become
corrupted!
// Look for your flash drives in these two locations!
The flash drive should appear in both places!
if Statements
Like this…
if Statements are used to determine the condition
of which the code is to be executed. if Statements if (x > 180)
are used when a condition needs a true function {
when present or a false function when the [Something happens]
condition isn’t present. }
Now let’s learn about else if statements. Think of if (xLoc > width)
else if statements as the opposite of if statements! {
xVel = xVel * -1;
if statements look for when the condition is true, }
else if looks for when the condition is false.
else if (xLoc < 0)
We want to use this boolean expression to make {
sure that when our object reaches below 0, we can xVel = xVel * -1;
make our object bounce back onto the screen! }
//We are just changing
the direction with this!
yLoc > height Expression xLoc = xLoc + xVel;
yLoc = yLoc + yVel;
Now uncomment the yLoc and add the float
variable, yVel! if (yLoc > height)
{
yVel = yVel * -1;
After you do that, use the Boolean Expression to
make your object go on the opposite y location! } //changes direction
Instead of using xLoc > width, we are using else if (yLoc < 0)
yLoc > height because the yLoc uses the height of {
yVel = yVel * -1;
the screen.
} //changes direction
Everything else should be the same for this boolean expression.
A radical part of Wall Collision
Now onto the float variable that everyone is float diam;
asking about! float rad;
void setup()
Say hello to the rad float variable! {
…
This variable is crucial for a precise collision, diam = [Circle Size]
rad = diam * 0.5
either with two objects or with the collision of a
//Define here so you can
wall or surface. give the initial value at the
start every time.
Make sure you set up the rad variable }
appropriately!
Including rad into Wall Collision
After you define the rad variable, we
get to use it for our code! if ([Loc] > [Width or Height] - rad)
{
Input rad into our wall collision code [Vel] = [Vel] * -1;
to make the program take the radius } //changes direction
or your half length of your shape into
else if ([Loc] < rad)
consideration before bouncing your
{
ball back onto screen.
[Vel] = [Vel] * -1;
} //changes direction
This allows your program to keep
your object on screen!
Test out your new Wall Collision Code!
Does your object collide with the walls and bounce off of
them perfectly?
} class Character
{
Since you declared your class for your character, float xLoc;
float yLoc;
now you need to put your global variables for your
float diam;
character inside this class. Cut them from the main …
program and put them into the class. }
Class Constructor
class Character
{ Now that you declared your class and
float xLoc;
float yLoc;
moved all your global variables from the
... main program to your class, you can set up
Character()
{ the initial setup of your character.
xLoc = width/2;
yLoc = height/2;
... Think of this as your void setup() for your
} character!
// When an object is created,
it runs a constructor that
constructs the object. The
constructor is similar than a
Import all your void setup() code from your
function. main program and put in inside your class.
}
Class Methods
Character() After you added the constructor of your Character
{
... Class, now you’ll insert the Methods of your class!
}
…
Cut your void drawCharacter() function and your
//Methods void movementCharacter() function from your main
void drawCharacter()
{...}
program and insert it into your class below the
constructor.
void moveCharacter()
{...}
The Methods of your class acts includes the custom
} void functions of the main program!
Defining your Class!
Character Char; Let’s go back to our main program because our
void setup() program hasn’t defined our new tab yet!
{...}
In order to do that, we need to define the class in
void draw()
{...} the main program so that the program can run our
class when the program is running!
}
}
...
Now onto the final part!
}
Draw your Class!
Character Char;
After we setup our class, we’ll have to make
void setup()
{ sure that our program actually runs the
… commands and functions inside the Character
Char = new Character(); Class.
}
void draw()
To do that, make sure you put the name of your
{
… variable, add a period, and then include the
Char.moveCharacter(); void functions of your character to allow your
Char.drawCharacter();
}
program to run the functions needed for your
character.
}
Making a new similar object
Character Char;
Character Char2;
Now that your original object is finished, now
void setup() you can make a duplicate of your object!
{
Char = new Character();
Char 2 = new Character();
}
By making a new variable for your second
void draw()
object and including the initial setup and
{ drawing of said object, now you can have two
…
Char.moveCharacter();
object with identical code on your program at
Char.drawCharacter(); the same time.
Char2.moveCharacter();
Char2.drawCharacter();
} How about you run your program to test it?
}
The Second Object Problem
Character Char;
Character Char2; The problem is that now we have two exact
void setup() same objects being overlapped by each other.
{
Char = new Character(xVel, yVel);
We just want objects with similar behavior
Char 2 = new Character(xVel, yVel); but having them in the exact same location
}
won’t help.
void draw()
{
… So instead, we’ll change the velocity of which
Char.moveCharacter();
Char.drawCharacter();
the circles are moving by changing the
Char2.moveCharacter(); parameters of the objects.
Char2.drawCharacter();
}
Unfortunately, we have to edit the class first!
}
Parameters for your class!
The parameters are the numerical factors
// Constructor for Character
class Character(float xV, float yV, float di)
that set the conditions of a operation.
{
xLoc = width/2; In this case, we are making three float
yLoc = height/2;
variables as parameters for the class. We
xVel = xV; //Definition of xVel
yVel = yV; //Definition of yVel can use these to set the conditions for each
diam = di; //Definition of diam object we create.
rad = diam * 0.5;
hue = 160;
} Make sure you match the definitions of
... each of the variables you use with the new
variables you added.
Fixing the problem
Character Char;
Character Char2; Now plug in your values for your characters!
void setup()
{
Char = new Character(xVel, yVel, di); An example for values you can put is:
Char2 = new Character(xVel, yVel, di); Char = new Character(1, -2, 100);
} Char2 = new Character(random(-5,5), random(5,-5), 100);
void draw()
Char3 = new Character(20, 40, 50);
{
…
Char.moveCharacter(); Feel free to mess around with values! You
Char.drawCharacter(); can make as much objects as you’d like!
Char2.moveCharacter();
Char2.drawCharacter();
}
Make new parameters or new objects?
}
Processing Class Survey 2
https://forms.gle/Xktcz9E4LHFVTbVu6
https://docs.google.com/forms/d/e/1FAIpQLSdWM0SdNLaWTNG206lM_eKefwGe5RTque8Ic6g4ho9rOvSyiQ/
viewform?usp=sf_link
Lesson Seven:
Ping Pong - Introduction to
Collision Detection
Review of Parameters!
Remember that the parameters are
// Constructor for Character the numerical factors that set the
class Character(float xV, float yV, float di, float _ )
{
conditions of a operation.
xLoc = width/2;
yLoc = height/2; These are the parameters that we
xVel = xV; //Definition of xVel
yVel = yV; //Definition of yVel
made yesterday for our lesson.
diam = di; //Definition of diam
rad = diam * 0.5; Now to test what we learned, how
hue = 160;
}
about you make one more
... parameter for the color of each
object! (Or three if using RGB!)
Setup two new objects!
First, comment out all your characters that you’ve made for now.
(Comment the global variable, the setup, and the methods for the Char.)
Afterwards, make two new objects called the “Ball” and the “Paddle” to
proceed with our plan to make a single player pong game today!
The next thing to do is to make the class, constructor, and methods for
each of the objects.
If the ball collides with the paddle, the collision will be reported as true, otherwise it
will be reported as false. This will be important for the program to note later on!
What’s up with “this”?
We are going to be using something called “this”. But what does
this do or what does this mean?
https://forms.gle/vszsvQ63aErcZYkw5
https://docs.google.com/forms/d/e/1FAIpQLSd-to2_EuCu_5AqPBVfWN-H0
4hotUciedRUfc_utINdzTBGTw/viewform
Removing the left-side barrier!
if (xLoc+rad > width) First things first, you want to comment out the code
{
concerning the expression: xLoc is less than the radius!
xVel = xVel * - 1;
}
else if (pad.collide(this) )
This code will keep the ball inside the window by
{ knocking it back inside by having a barrier on the left. For
xVel = xVel * -1; the left, we want to use the paddle to knock it back
} instead. Comment out what causes the ball to bounce
// else if (xLoc < rad) off the left side of the window.
{
xVel = xVel * -1; Next, print the coordinates of the x location and the y
} location to know where the ball is at while the program
println(xLoc, yLoc); is running.
The use of “&&”
To access the “&” symbol, all you need to do is hold down the shift button
and press the number 7. To use the operator, “&&” you need to press the
number 7 twice while holding down shift.
This operator will allow you to include two or more conditions in one
statement, however all the conditions included in the statement must be
true conditions. If two conditions end up with the same result but they
aren’t both true, using “&&” won’t work for the statement.
This operator will let you manipulate two boolean values. Consider “||”
to be like the word “OR”. If [this] happens OR [this] happens, then
[something command] will occur. Now, think of “||” instead of OR.
A good example to use this in is with your movement code for your ball.
if(xLoc + rad > width || xLoc < rad) {xVel = xVel * -1;}
if(yLoc > height-rad || yLoc < rad) {yVel = yVel * -1;}
Acceleration
float xLoc;
float yLoc; To make and edit the acceleration of
float xVel;
float yVel; your ball so that the ball moves faster
float Accel;
… over time, all you need to do is make
Ball(float xL, float yL, float xV, float yV, ...) another float!
{ //You can also make a float for Accel!
xLoc = xL;
yLoc = yL;
xVel = xV; Once you make another float, you can
yVel = yV;
Accel = -1.01; write a negative value for the
}
… acceleration and then change the
else if (pad.collide(this) )
{ value from one.
xVel = xVel * Accel;
}
Adding a “Game Over” Situation
else if (xLoc < rad) Let’s make our game an actual game!
{
x̶V̶e̶l̶ ̶=̶ ̶x̶V̶e̶l̶ ̶*̶ ̶-̶1̶;̶ The first thing to do is edit your code
println("Game Over!"); by editing your former code to make
} a barrier for the left side of the
screen.
Ball ball;
boolean playOn; //Determines if the program is running, true or false.
Paddle pad;
Now we are going to use a int variable which only uses number
values that are integers. Integers are numbers that are full
numbers like 1, 2, 3, 4, etc. Decimal numbers are not integers.
For our first int variable, make a new global variable that is a int
variable for scores in your game. Then, set that variable to zero
in the void setup().
Scores to FrameCount
This is one way of using the score
variable in your code.
We’ll add some sound effects to the collisions that occur to the
objects in our game.
First before we set up the code for our sound effects and music,
we need to make a library for them!
Importing sound code!
Now that we’ve made our data folder, we can move onto the next
step! So before we actually start coding, we need to important
the classes and variables for our code with sound!
Import the sound library by pressing: Sketch -> Import Library ->
Sound. (You might have to add and download it if it isn’t already!
Sound Effect Variables
After we imported the
sound library, all we need
to do is use the SoundFile
method and make three
variables for our three
sound files.
When holding a key down, it’ll make multiple calls to that key as the action is
being repeated multiple times. The action will stop when the key button is let
go. That’s a key piece of info isn’t it?
Now the system variable, key, contains the value of the key when pressed or
released. keyCode does something similar, however, it specializes in special
keys.
key vs keyCode
key is useful for defining a key’s purpose in your program. Since most of the keys
you use for the system variable are used pretty often, the program isn’t required to
check what the key is. You just need to tell the program which key to use and what
the command is for that key. Pretty simple if you ask me.
keyCode is used to detect “special keys” that aren’t commonly used in the program
so we would need to define them. These special keys include the arrow keys, “UP,
DOWN, LEFT, RIGHT”, and also “ALT, CONTROL, and SHIFT.”
// keyCode also lists the keys in the console log as numbers!
keyCode is required to have the program check to see if the key is coded. To do
this, we need to add this conditional, “if (key == CODED)” to use special keys.
key can use all the letter keys and also “BACKSPACE, TAB, ENTER, RETURN, ESC,
and DELETE.”
Setting Movement to keys!
void move()
Now the first step with using keys for
{ your paddle’s movement is to comment
//yLoc = mouseY; out the movement code we already
//Comment this out so you can use keys to move!
have.
}
We need to use a new boolean function because we don’t want it to interfere with our display and movement
code. (If we do it in character movement, it won’t count the collision when holding down the mouse.)
We’ll name this boolean function, “no Collision.”
We want to make a if statement in our collision code to tell our boolean function if there isn’t a collision or if there
is a collision. If it collides, then the function is false.
Collision with Rectangles Pt.3 (Enemies)
void draw()
{ And finally, we can finish things up in the main program. We
if (PlayOn)
{
want to create a game over screen for when the collision occurs.
background(200);
if (character.noCollision() )
{
Now we want to create the variable PlayOn again but this time,
... we want to use the noCollision boolean that we used earlier.
character.display();
character.move();
Insert your noCollision boolean into your if statement
enemy1.display(); conditional.
enemy1.move();
… // You can add more enemies here!
} Then, you want to include all your display and movement code
else
{ into the if statement.
PlayOn = false;
textAlign(CENTER,CENTER);
text("Game Over!", width/2, height/2); After that, make an else and include the gameover screen that
} you desire to create! Have fun with your gameover screen and
}
} run your program to test the results.
Finishing things up!
Enemy enemy1;
Enemy enemy2; Now we are pretty much
Enemy enemy3; finished with the code that we
void setup() intended to create!
{
size(700,700);
colorMode(HSB); You want to create more
...
PlayOn = true;
enemies to make the game
harder!
character = new Character();
enemy1 = new Enemy(0, 5, random(0.001,0.01)); //Hue, Easing, Appearance Time.
enemy2 = new Enemy(170, 15, random(0.008, 0.02)); Also make sure you add the
enemy3 = new Enemy(100, 30, random(0.02, 0.06));
music and sound effects you
} want because we are making a
new game soon!
GAME CLEAR!
You’ve completed your point
and click game!
Now you might as well set up your void setup() code to test the code you
have this far! Make sure the x coordinates of your players are in their
proper locations and include any differences between your players in your
player class’s parameters as well. Include your class’s functions in the
program as well!
Creating your puck!
class Puck Next, we want to create our puck class.
{
… // Include variables
Puck(float __) // Include parameters Just like with our player class, we are
// for your puck.
{ going to include the variables needed to
… // Include declarations create our puck code!
}
puck = new Puck(width/2, height/2, random(1,2), random(1,4), 30, 0); //xLoc, yLoc, xVel, yVel, diam, hue
p1 = new Player(6, 20, 45); // Paddle(xLoc, hue, diam);
p2 = new Player(width-6, 20, 45); // Paddle(xLoc, hue, diam);
P1Score = 0;
P2Score = 0;
}
Yeah! Now you can put your puck into the program by declaring the puck
class as a new puck object so that you can test your code!
}
… // Added already. statements.
else if (p1.collide(this))
{
collidePuck.play(); //Play sound effect for paddle collision.
xVel = xVel * Accel; When we have done so, we need to
}
else if (p2.collide(this)) make the puck know that the collision
{
collidePuck.play(); //Play sound effect for paddle collision.
xVel = xVel * Accel;
had occured. We can do this by
}
else if (xLoc < rad)
making an else if statement stating
{
… // Added already. this. You can also combine the two
}
if (yLoc > height-rad || yLoc < rad)
{
else if statements into one else if by
}
… // Added already. using the || operator!
}
playOn & GameOver
void draw()
{
if (playOn) Now we want to include the boolean
{
background(100);
if(GameContinue() )
playOn as we’ve used a couple times
{
puck.move(); before. Make sure to create, declare,
puck.display();
p1.display();
p2.display();
and add your playOn boolean into
}
else
your code.
{
Music.stop();
playOn = false;
GameOver.play();
playagain = createFont("Impact", 30);
Next, create your playOn if statement
textFont(playagain);
fill(190,255,255); and create the gameover screen to go
textAlign(CENTER,CENTER);
fill(randomhue, 255, 255); //Makes the text a random color.
text("Play again?", width/2, height/2);
with it. If you want, include a name to
fill(randomhue, 255, 255); //Makes the text a random color.
text("Press Enter to continue!", width/2, height/1.7); your font like how my code used
}
} “playagain” as the font name.
Start again and key presses!
void keyPressed()
{ Now that we’ve created our
println("Key code pressed: " + keyCode);
gameover screen, we might as well
if (key == ENTER || key == RETURN) create a method to start over the
{
Music.stop();
game with a key!
GameOver.stop();
setup();
} Include an if statement for what key
} should be pressed for the method.
void keyReleased()
{
Also if you choose to, you can print
println("Key has been released!"); what key is being pressed and when!
}
boolean GameContinue()
boolean GameContinue()
{
if(P1Score > 9)
Now that we completed our playOn and
{ gameover code, we can create the
Gameover = createFont("Arial", 20);
textFont(Gameover); condition of when the game should
fill(180,255,200);
textAlign(CENTER,CENTER); continue or not.
text("Player 1 wins!", width/2, height/3);
text("P1 Score: " + P1Score + " P2 Score: " + P2Score, width/2, height/2.5);
}
return false; We want to make an if statement for
if(P2Score > 9) when the score becomes big enough to
{
Gameover = createFont("Arial", 20); end the game. You can choose the score
textFont(Gameover);
fill(180,255,200); to end the game.
textAlign(CENTER,CENTER);
text("Player 2 wins!", width/2, height/3);
text("P1 Score: " + P1Score + " P2 Score: " + P2Score, width/2, height/2.5);
return false;
Create your game over text explaining
} who won and how many points each
return true; //If either player doesn't have over 9 points.
} player got.
Finishing our program code!
void draw() Now you can include your
{
if (playOn) GameContinue() condition into your void
{
background(100);
draw() code and make sure you put the
if (GameContinue() ) background on top to prevent the classes
{
puck.move();
being displayed when the game is over.
puck.display();
p1.display();
p2.display();
Be careful where you put your brackets
} and make sure your condition holds all
else
{ your display and move functions that you
Music.stop(); need for your game.
playOn = false;
GameOver.play();
… // Game over screen text. Include your own music and sound effects
}
} to personalize your game! Have fun with
} the game you create!
GAME CLEAR!
You’ve completed your air
hockey game!
void setup() Next, click File > Save As… to create the next
{ game we are going to create!
}
Then, add all your code you want into your
void draw() character class. We’ll get to the ground in a
{ second.
}
Set up your void setup() and void draw() as well!
Character Class!
class Character
{
Next we need to include out global variables for
float xLoc; your character class!
float yLoc;
float xVel;
float yVel;
float Speedx; We are going to include xVel and yVel for future
float widthLength; use and make sure you define xVel and yVel in the
float heightLength;
declarations. yVel will start at 0 for reasons that
Character()
{ will be explained later.
xLoc = width/3;
yLoc = height/2;
xVel = 20; We’ll also need to have a speed for x which can be
yVel = 0;
Speedx = 1.5; whatever speed you want.
widthLength = 20;
heightLength = 20;
} After that, create your display() function!
Ground Class!
class Ground
{ Next we need to create your ground class and
float xLoc1;
float xLoc2; make the global variables for your ground class!
float yLoc;
float hue;
If we are using a line, we’ll need at least to x values
Ground(float x1, float x2, float y, float h)
{ to make the length of the platform. However, since
xLoc1 = x1;
xLoc2 = x2;
the platform is horizontal, we don’t need another y
yLoc = y; value because the y value will stay the same!
hue = h;
}
void display()
Make sure you include two x values and one y
{ value to your ground class and make the void
stroke(hue, 55, 155);
strokeWeight(0); display() as well.
line(xLoc1, yLoc, xLoc2, yLoc);
}
Ground Collision
// If there is a collision with the platform. Now for ground collision. We need to make a
void groundCollision()
{ void function for when the collision occurs
if (character.yLoc + character.heightLength > this.yLoc)
{
between your ground and your character.
character.yLoc = this.yLoc - character.heightLength;
//Keeps character at ground position.
Then, make an if statement for when the
character.yVel = 0; character reaches the ground and make the
//Stops falling.
character fall onto the ground. You can make
Jumping = false; the character fall to the ground by subtracting
//Allows Jumping
} the distance between them.
else {
character.yVel ++;
} // Increment = increase. Include a method to stop your character from
falling and make your character able to jump
}
when they reach the floor.
Character: Platform Movement
void Up()
{ Now we just need to create your movement code in your
if(!Jumping) { character class. We can do this by including the three directions
yVel = -15;
Jumping = true; that the character will move in.
}
} With up, we’ll use an if statement to see when you aren’t able
void Left()
{ to jump. This allows you to not jump over and over again. The
xLoc = xLoc - xVel * Speedx; “!” is a operator that means “Not”. So when you aren’t able to
}
jump, you are telling the program that you are still in the air.
void Right() You won’t gain the ability to jump again until you reach the
{ floor.
xLoc = xLoc + xVel * Speedx;
}
Next, we will include some movement using xVel and Speedx.
xVel is the amount of space we move and Speedx is how quickly
you move that amount of space.
Update program!
Character character;
Ground ground; Include the boolean for jumping to
boolean Jumping = false; //Allows Jumping allow the character to jump at the
void setup() beginning of the game.
{
// Include class declarations.
}
Also include the declarations,
void draw() functions, and key presses for your
{
// Include functions. classes to finish up your code.
}
void keyPressed()
{
With all of this, you should be able
// Include character movement. to successfully jump with gravity.
}
Save your program!
● Click File > Save...