Skip to content

Instantly share code, notes, and snippets.

@Craigson
Created September 24, 2014 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Craigson/1293cec5e8c76d33fbaa to your computer and use it in GitHub Desktop.
Save Craigson/1293cec5e8c76d33fbaa to your computer and use it in GitHub Desktop.
Jet Game
//notes for later version:
//add shooting sounds & music
//add obstacles for the jet to dodge
//add explosions for hits
PImage img; // create image for jet
PImage imgTwo; //create image for clouds
Cloud[] clouds = new Cloud[5]; //an array of clouds
Bullet[] bullets = new Bullet[1]; //one bullet object
Jet jet; //one jet object
Target[] targets; //array of target objects
int totalTargets = 0; //sets total number of targets in game to 0
Timer timer; // one time object
int totalClouds = 10;
boolean gameOver = false; //a boolean that let's us know if the game is over
//the following variables keep track of score, level, ammo, no of targers, and lives
int targetTime = 2000; //the time in millis between each new target appearing
int targetNums = 10; // how many targets will fill the targets[] array
int score = 0; //users score
int level = 1; //what level are we on
int lives = 10; //10 lives per level (1o targets can reach the side
int ammo = 200; //amount of ammunition left to shoot at targets
int levelCounter = 0;
int cloudNumber = 5; //how many clouds fill the cloud array
PFont f;
//----------------------------------SETUP-----------------------------------------
void setup() {
size(500, 600);
smooth();
imageMode(CENTER);
img = loadImage("jet.png"); //load jet image into sketch
imgTwo = loadImage("cloud.png"); //load cloud image
//hides the mouse cursor
noCursor();
bullets[0] = new Bullet(); //initialise bullet object
jet = new Jet(); //initialise jet object
targets = new Target[targetNums]; //create an array of 200 targets
timer = new Timer(2000); // timer goes off every 'n' seconds, determined by variable targetTime
// clouds = new Clouds[cloudNumber]; //create an array of 5 clouds
//Cloud[] clouds = new Cloud(); //an array of clouds
timer.start(); //starts the timer
f = createFont("Arial", 12, true); //a font to write text on the screen
}
//----------------------------------DRAW----------------------------------
void draw() {
background(142, 226, 245);
//if the game is over
if (gameOver) {
textFont(f, 48);
textAlign(CENTER);
fill(0);
text("GAME OVER", width/2, height/2);
} else {
image(img, jet.x, jet.y); //draw image to screen at jet's x and y pos
//set the jet's location and display the jet
jet.display();
jet.setPosition(mouseX, height-50);
//move and display all targets
for (int i = 0; i < totalTargets; i++) {
if (!targets[i].finished) {
targets[i].move();
targets[i].display();
if (targets[i].reachedEdge()) {
//levelCounter++;
targets[i].finished();
lives--; //if the target reaches the edge, a life is lost
if (lives <= 0) {
gameOver = true;
}
}
}
}
//move and display clouds
/* for (int j =0; j < totalClouds; j++){
// image(imgTwo,10,clouds.x, clouds.y);
cloud[j].move();
cloud[j].display();
if (cloud[j].reachedBottom()){
cloud[j].finished();
}
}
*/
//call the bullet object's functions
for (int i = 0; i < bullets.length; i++) {
bullets[i].setPosition(mouseX); // set the bullet object's location to mouseX (same as jet)
bullets[i].display();
bullets[i].shoot();
//check the timer
if (timer.isFinished ()) {
//initialise a single target
targets[totalTargets] = new Target();
//increment total targets
totalTargets++;
//if we hit the end of the array, reset it
if (totalTargets >= targets.length) {
totalTargets = 0;
}
timer.start();
}
//for every bullet i, and every target j, calls the hit() function
//within the bullet class to check if any bullet i
//intersects any target j
for (int j = 0; j < totalTargets; j++) {
if (bullets[i].hit(targets[j])) {
targets[j].destroyed();
// bullets[i].busted();
//every time a target is destroyed, score goes up
targets[j].finished();
levelCounter++;
score++;
}
}
}
if (levelCounter >= targets.length) {
//go up a level
level++;
//reset all game elements
levelCounter = 0;
lives = 10;
timer.setTime(constrain(targetTime - level*(level/2)*25, 0, targetTime));
totalTargets = 0;
ammo = 200 - level*10;
// textFont(f,30);
// fill(0);
// textAlign(CENTER);
// text("LEVEL: " + level + "!!",width/2,height/2);
}
//display number of lives left
textAlign(LEFT);
textFont(f, 12);
fill(0);
text("Lives Left: " + lives, 20, height-25);
text("Ammo: " + ammo, 20, height-10);
text("Level: " + level, width-80, height-10);
text("Score: " + score, width-80, height-25);
}
println("lives:" + lives + " levelCounter:" + levelCounter + " score:" + score + " level:" + level + " targetTime:" + timer.runningTime + " ammo:" + ammo);
}
//}
//---------------------------------MOUSE PRESSED--------------------------
//the mousepressed function changes the "fire" boolean
//to true/false, thus changing the "speed" of the bullet
//from 0 to make it move.. this is done using the "click"
//function from inside the bullet class
void mousePressed() {
for (int i = 0; i < bullets.length; i++) {
bullets[i].click();
}
ammo--;
if (ammo <= 0) {
gameOver = true;
}
//create a new bullet object
Bullet newBullet = new Bullet();
//append bullet array
bullets = (Bullet[]) append(bullets, newBullet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment