Hi guys, thanks again for your help over the last week or so, I'd have been lost without it. The game I've been working on is now finished and I thought I'd stick it on here (I'll put it in the "Share your Work" section too").
However I do have one final, small, query. If the player loses the final boss battle, a laughter sound plays. For some reason a small part of it loops endlessly and I can't see why. Any suggestions would be greatly appreciated and I promise this will be the last thing I ask of you.
I've been searching the forums and everyone complains about how many countdown/timer questions are asked, yet I haven't found a thread which has been of any use.
I'm trying to implement a countdown which will begin, not at the start of the program, but later on (once int currentLevel = 3 to be precise). The countdown has to begin at 60 seconds and return a boolean once it finishes.
As I say, Im having no luck coding this on my own or through searching the forums so any advice, explanations, or examples would be greatly appreciated :)
Sorry to ask so many questions in such a short space of time, but I'm really struggling with this. I require the "pirates" in my code to intersect the "fuel cells" and when they do the cells must disappear. No matter how I try it, the pirates just go straight through the fuel as if no collision is recorded. Both the pirates and fuel cells are generated by arrays.
Here's the Pirate class
//Create pirate class class Pirate { float xPos; float yPos; float xSpeed; float ySpeed;
//Pirate radius int pr = 20;
Pirate(float tempXPos, float tempYPos, float tempXSpeed, float tempYSpeed) { xPos = tempXPos; yPos = tempYPos; xSpeed = tempXSpeed; ySpeed = tempYSpeed; } //Hit function for pirates when they collide with the player void hit() { xPos = random(width); yPos = random(60, height); }
//Display function for pirates void display() { image(imgCar, xPos, yPos); pr = 20; xPos = xPos + xSpeed; yPos = yPos + ySpeed; if (xPos > width) { //Pirates reset if they go off screen xPos = 0; } if (xPos < 0) { xPos = width; } if (yPos > height) { yPos = 45; } if (yPos < 45) { yPos = height; } }
//Specify intersect conditions for player and pirates boolean intersect(Player a) { float distance = dist(xPos, yPos, a.x_, a.y_); if (distance <= pr + a.r) { return true; } else { return false; } }
Ok, I've modified the code I submitted earlier to use arrays a bit, however I'm struggling with specifying the variables of objects within an array.
For example: I want the cars in this code to appear at y=135, y=235, y=335, and y=445. Furthermore, I want all those which appear at y=135 to have speed=-12; those at y=235 to have speed = 8; those at y=335 to have speed -5; and those at y=445 to have speed 4.
Furthermore, I can't find a resource which tells me how to implement intersecting the "cars" with the player when they are generated by an array like this.
//Display game info image(img, 0, 0); fill(0); text("Lives: " + lives, 100, 35);
//Initialise cars for (int i = 0; i < cars.length; i++) { cars[i].move(-8); } }
//Initialise player alien.show();
//Initialise collectable parts part1.appear1(); part2.appear2(); part3.appear3();
// Check for player getting hit by cars // if (cars.intersect(alien)) { // alien.die(); // } // if (car2.intersect(alien)) { // alien.die(); // } // if (car3.intersect(alien)) { // alien.die(); // } // if (car4.intersect(alien)) { // alien.die(); // } // if (car5.intersect(alien)) { // alien.die(); // } // if (car6.intersect(alien)) { // alien.die(); // } // if (car7.intersect(alien)) { // alien.die(); // } // if (car8.intersect(alien)) { // alien.die(); // } // if (car9.intersect(alien)) { // alien.die(); // } // if (car10.intersect(alien)) { // alien.die(); // } // if (car11.intersect(alien)) { // alien.die(); // } // if (car12.intersect(alien)) { // alien.die(); // } // if (car13.intersect(alien)) { // alien.die(); // } // if (car14.intersect(alien)) { // alien.die(); // }
//Check for player collecting ship parts if (part1.intersect(alien)) { part1.collect1(); } if (part2.intersect(alien)) { part2.collect2(); } if (part3.intersect(alien)) { part3.collect3(); } } }
if (y_ < 50) { //Prevent player from going off top of screen y_ = 50; } if (y_ > 550) { //Prevent player from going off bottom of screen y_ = 550; } if (x_ <= 0 + r) { //Lose a life if player goes off left of screen alien.die(); } if (x_ >= width - r) { //Lose a life if player goes off right of screen alien.die(); } if (x_ > 650 && y_ < 100) { x_ = 650; } } //Specify movement speeds for player void Down() { y_= y_+100; }
void Up() { y_= y_-100; }
void Left() { x_= x_-30; }
void Right() { x_= x_+30; }
//Specify "respawn" point and die function void die() { x_= 400; y_= 550; lives --; } } //Player movement controls void keyPressed() { if (key==CODED) { if (keyCode==DOWN) { alien.Down(); } else if (keyCode==UP) { alien.Up(); } else if (keyCode==LEFT) { alien.Left(); } else if (keyCode==RIGHT) { alien.Right(); } } }
Player alien;
Part part1; Part part2; Part part3;
Here's the Cars class
class Car { float xPos; int yPos; int speed; int cr = 15; Car(float tempXPos, int tempYPos, int tempSpeed) { xPos = tempXPos; yPos = tempYPos; speed = tempSpeed; }
//Create move function for cars void move(int speed) { image(imgCar, xPos, yPos); cr = 15; xPos = xPos +speed; if (xPos > width) { //Cars reset if they go off screen xPos = 0; } if (xPos < 0) { xPos = width; } }
//Specify intersect conditions for player and cars boolean intersect(Player a) { float distance = dist(xPos, yPos, a.x_, a.y_); if (distance < cr + a.r) { return true; } else { return false; } } }
//Initialise collectable parts part1.appear1(); part2.appear2(); part3.appear3();
//Check for player getting hit by cars if (car1.intersect(alien)) { alien.die(); } if (car2.intersect(alien)) { alien.die(); } if (car3.intersect(alien)) { alien.die(); } if (car4.intersect(alien)) { alien.die(); } if (car5.intersect(alien)) { alien.die(); } if (car6.intersect(alien)) { alien.die(); } if (car7.intersect(alien)) { alien.die(); } if (car8.intersect(alien)) { alien.die(); } if (car9.intersect(alien)) { alien.die(); } if (car10.intersect(alien)) { alien.die(); } if (car11.intersect(alien)) { alien.die(); } if (car12.intersect(alien)) { alien.die(); } if (car13.intersect(alien)) { alien.die(); } if (car14.intersect(alien)) { alien.die(); }
//Check for player collecting ship parts if (part1.intersect(alien)) { part1.collect1(); } if (part2.intersect(alien)) { part2.collect2(); } if (part3.intersect(alien)) { part3.collect3(); } } } }
//Create class Car class Car { float xPos; float yPos; float speed; float cr; Car(float tempXPos, float tempYPos, float tempSpeed) { xPos = tempXPos; yPos = tempYPos; speed = tempSpeed; }
//Create display function for cars void display() { cr = 15; image(imgCar, xPos, yPos); }
//Create move function for cars void move() { xPos = xPos +speed; if (xPos > width) { //Cars reset if they go off screen xPos = 0; } if (xPos < 0) { xPos = width; } }
//Specify intersect conditions for player and cars boolean intersect(Player a) { float distance = dist(xPos, yPos, a.x_, a.y_); if (distance < cr + a.r) { return true; } else { return false; } } }
//Create class part class Part { float x_; float y_; float d;
if (y_ < 50) { //Prevent player from going off top of screen y_ = 50; } if (y_ > 550) { //Prevent player from going off bottom of screen y_ = 550; } if (x_ <= 0 + r) { //Lose a life if player goes off left of screen alien.die(); } if (x_ >= width - r) { //Lose a life if player goes off right of screen alien.die(); } if (x_ > 650 && y_ < 100) { x_ = 650; } } //Specify movement speeds for player void Down() { y_= y_+100; }
void Up() { y_= y_-100; }
void Left() { x_= x_-30; }
void Right() { x_= x_+30; }
//Specify "respawn" point and die function void die() { x_= 400; y_= 550; lives --; } } //Player movement controls void keyPressed() { if (key==CODED) { if (keyCode==DOWN) { alien.Down(); } else if (keyCode==UP) { alien.Up(); } else if (keyCode==LEFT) { alien.Left(); } else if (keyCode==RIGHT) { alien.Right(); } } }
Player alien;
Part part1; Part part2; Part part3;
Car car1; Car car2; Car car3; Car car4; Car car5; Car car6; Car car7; Car car8; Car car9; Car car10; Car car11; Car car12; Car car13; Car car14;