//MY PROJECT :D
//Creates the Variables
float fallSpeed=5;
float x = 50, y = 450; //ninja variable
float vx = 40;
float z = 500, p = 100;
float starx = 50, stary = 300; //ninja star variable
int score = 0; //score variable
int playButtonWidth, playButtonHeight; //play button
int playButtonX, playButtonY;
boolean bStarted; //variable shows the starting screen
boolean showGameOver; //variable shows that game is over
boolean isMouseOverPlayButton()
{
//Allows You to Click the Play Button
int hw = playButtonWidth / 2;
int hh = playButtonHeight / 2;
// Returns true if mouse position is within the bounds of the button
return mouseX >= playButtonX - hw && mouseX <= playButtonX + hw &&
mouseY >= playButtonY - hh && mouseY <= playButtonY + hh;
}
PImage ninja; //Images
PImage cityscene;
PImage ninjastar;
PImage biggerninja;
ArrayList<FallingObject> objects = new ArrayList <FallingObject> ();
class FallingObject {
float x, y, vy;
//Makes the Ninja Stars Fall Randomly
void display () {
image (ninjastar, x, y);
}
FallingObject () {
float rand = random(0, 1);
if (rand < 0.5) x = 50;
else x = 450; //falls either from 50 or 450 pixels
y = 50;
vy=random(fallSpeed-4, fallSpeed+4); //Averages Fall Speeds
}
void move () {
y=y+vy;
}
}
void setup () {
//Loads the Images from the Internet With Transparent Backgrounds
size (500, 600);
ninja=loadImage ("http://i.imgur.com/8EOqWWh.png");
cityscene=loadImage ("http://7428.net/wp-content/uploads/2013/09/Cartoon-City-Building-PSD.jpg");
ninjastar=loadImage ("http://i.imgur.com/aC1BamH.png");
biggerninja=loadImage ("http://i.imgur.com/5KTmQX0.png");
imageMode(CENTER);
rectMode(CENTER);
//Button Dimensions
playButtonWidth = width / 5;
playButtonHeight = height / 8;
playButtonX = width / 2;
playButtonY = 4 * height / 5;
}
void draw () {
if (frameCount%30==0)fallSpeed+=0.1; //Slowly Increases Fall Speed by 0.1
rect(playButtonX, playButtonY, playButtonWidth, playButtonHeight);
{
//Design Aspects for Text on Game Over Page
background(#AAFFEE);
if (showGameOver) { //refers back to variable
background(167, 181, 255); //lavender background
fill(255);
textSize(55); //to create a white shadow I put the shadow letters first
text("Game Over!", 253, 150);
fill(255);
textSize(32);
text("Press the Spacebar to Restart", 252, 212);
fill(255);
textSize(37);
text("Your Score: "+score, 252, 265);
fill(0);
image (biggerninja, 250, 420);
fill(0);
textSize(55);
text("Game Over!", 250, 147);
fill(0);
textSize(32);
text("Press the Spacebar to Restart", 250, 210);
fill(0);
textSize(37);
text("Your Score: "+score, 250, 263);
if (keyPressed) {
//If the Space Bar is Pressed
if (key == ' ') {
showGameOver = false;
score = 0;
fallSpeed=5;
objects.clear(); //clears data to reset information
}
}
return;
} else if (!bStarted) {
showWelcomeScreen(); //shows welcome screen again
return;
}
}
if (frameCount % 60 == 0) {
//continuously adds new stars with modulo thing
objects.add(new FallingObject()); //function slash method
}
image(cityscene, width/2, height/2); //City Scene Background
strokeWeight(2);
fill(34, 58, 126);
rect(45, height/2, 70, height); //Wall 1
rect(455, height/2, 70, height); //Wall 2
image(ninja, x, y);
x = x+vx;
if (x >= 450) {
vx = 0;
x = 450; //Ninja Moves to Two Places
}
if (x <= 50) {
vx = 0;
x = 50;
}
for (int i = 0; i < objects.size (); i++) {
FallingObject object = objects.get (i);
object.move();
object.display();
if (dist(x, y, object.x, object.y)<50) {
objects.remove (i); //If Distance Between Star and Ninja Is Less Than 50 Then Remove The Star
score = score + 1; //Counts Number of Stars Caught
}
if (object.y>height) { //If You Miss a Star, Game Over
showGameOver = true;
}
}
fill(255);
textSize(32);
text(score+" Caught", 250, 30); //Displays the Number Caught
}
void keyPressed () {
if (key == 'j') {
vx = -30-fallSpeed;
} //Keys 'j' and 'l' Control the Movement of the Ninja
if (key == 'l') {
vx = 30+fallSpeed;
if (key == ' ') {
}
}
}
void showWelcomeScreen()
{
image(cityscene, width/2, height/2);
// Title, Text Ninja Jumper with Shadow
textAlign(CENTER, CENTER);
textSize(56);
strokeWeight(3);
fill(255);
text("Ninja Jumper", 253, 163);
fill(0);
text("Ninja Jumper", 250, 160);
// Play Button: Background, Location, etc
fill(0);
stroke(#AAAA55);
rectMode(CENTER);
rect(width / 2, 4 * height / 5, width / 5, height / 8);
fill(#FF5500);
textSize(32);
text("PLAY", width / 2, 4 * height / 5);
// Instructions
textSize(20);
fill(255);
text("Press 'j' and 'l' to Move Left and Right", 250, 535);
textSize(25);
text("*Catch All the Ninja Stars!*", 250, 570);
}
void mousePressed() //When You Press Play Button, It Starts the Game
{
if (!bStarted && isMouseOverPlayButton())
{
bStarted = true;
//Need To Reset All Variables
objects.clear();
score = 0;
return;
}
}