Practice 2.7: Turkey
Make a program with a jumping turkey that displays a growing message
REQUIREMENTS
Your turkey must move left using the 'a' key and right using the 'd' key
The turkey can jump when it is on the ground by pressing the spacebar.
When in the air, the turkey will fall to the ground
The program outputs a message that gets larger every time the turkey jumps
You can change the theme of this project any way you like! Be sure to rename variables accordingly.
STEP BY STEP
Part A - Background Image
Find a background image. Feel free to use the example image found at the bottom of this page.
Set your window size to match the image size. The sample image is sized 1140 x 771 and is a jpg.
Note: If this is too late or small, you'll need to resize your image in a photo editing program to match.
Declare, initialize, and draw the image. See the Coder's Handbook on Images for more details.
Starting Outline
PImage bg
void setup()
{
size(1140, 771);
bg = loadImage("bg.jpg");
}
void draw()
{
image(bg, 0, 0);
}
After this step, your program will just show your background image taking up the entire window.
Part B - Turkey Image
At the top of the program create variables for our turkey:
xPos and yPos
In setup, assign them starting values so that the turkey begins in the center of the screen.
Create a new image for the turkey, just like you did with the background image.
The only difference is that you'll draw this image at xPos, yPos.
The turkey appears in the middle of the screen
Part C - Turkey Movement
At the top of the program create variables for our turkey:
xSpeed and ySpeed
Add the usual "physics" to our program so the turkey moves based on speed
Set xPos equal to xPos plus xSpeed
Set yPos equal to yPos plus ySpeed
Write a method called move() and call it inside of jump. This method should...
Set xSpeed to 0
If the user presses the 'd' key --> set xSpeed to 8
If the user pressed the 'a' key ---> set xSpeed to -8
The turkey can move left and right
Part D - Gravity
In draw()
Increase ySpeed by one
This will make the turkey fall when it isn't on the ground
If the turkey is at the bottom of the screen, set the turkey's height and speed
This will keep the turkey from sinking into the ground
void draw()
{
// If I am on the ground or below the ground
if (yPos + turkey.height >= height)
{
// Set speed to zero and position to the ground level
yPos = height - turkey.height;
ySpeed = 0;
}
}
The turkey falls to the ground but doesn't "sink in" to the ground
Part E - Jumping
At the top of your program, create an integer variable called jumps.
We'll use this later to keep track of how many times the turkey has jumped.
In draw()
When you're on the ground:
If the user presses the spacebar, set the ySpeed to -30 and increase jump by one.
Otherwise, set the speed and position to zero
void draw()
{
if (y + turkey.height >= height)
{
if(keyPressed && key == ' ')
{
ySpeed = -30;
jumps++;
}
else
{
y = height - turkey.height;
ySpeed = 0;
}
}
}
When the user presses space, the turkey leaps up.
Every frame the turkey will fall downward automatically - faster and faster over time.
This should *only* work when the turkey is on the ground - no double jumps!
Try changing some of the numbers for falling and jumping speeds to see what happens.
Part E - Basic Message
Declare and initialize a font. See the Coder's Handbook on Text for more details.
Experiment with fonts - you can use anything installed on the computer!
PFont myFont;
void setup()
{
fullScreen();
myFont = createFont("Georgia", 30);
}
At the end of your draw method, write a message to the user.
The message should appear near the top center of the screen and be centered.
To see which fonts you have available, you can try opening up Word on any school computer.
You must match the font's name *exactly* for it to load. This might be complicated with fonts that have spaces in the name, so be precise and patient.
Part F - Message Scaling
Modify your program using textSize so that the text gets bigger based on jumps.
The text must still be centered and top aligned as it grows!
void draw()
{
textSize(30 + jumps * 5);
}
EXAMPLE: RUNNING PROGRAM
Click here to download a running version of the program.
SAMPLE IMAGE FILES