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.
What am I missing?
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.
What am I missing?
- Car[] cars = new Car[15];
PImage img;
PImage imgCar;
PImage imgSplash1;
PImage imgSplash2;
PImage imgSplash3;
PFont f;
//Declare variables for initial game conditions
int fuel = 0;
int lives = 5;
int score = 0;
int currentLevel = 0;
boolean gameOver = false;
void setup() {
size(800, 600);
frameRate(20);
lives = 5;
f = createFont("Arial", 12, true);
//Load the background image
img = loadImage("road.jpg");
imgCar = loadImage("car.jpg");
imgSplash1 = loadImage("splash1.jpg");
imgSplash2 = loadImage("splash2.jpg");
imgSplash3 = loadImage("splash3.jpg");
//Car starting positions, direction, and speed
for (int i=0; i<cars.length; i++) {
cars[i] = new Car(random(width), 135, -12);
}
//Player starting poition
alien = new Player(400, 550);
//Collectable ship parts position
part1 = new Part(175, 150);
part2 = new Part(620, 250);
part3 = new Part(399, 350);
}
void draw() {
if (currentLevel == 0) {
level0();
}
else if (currentLevel == 1) {
level1();
}
else if (currentLevel == 2) {
level2();
}
else if (currentLevel == 3) {
level3();
}
else if (currentLevel == 4) {
level4();
}
else if (currentLevel == 5) {
level5();
}
else if (currentLevel == 6) {
level6();
}
else if (currentLevel == 7) {
level7();
}
else if (currentLevel == 8) {
level8();
}
else if (currentLevel == 9) {
level9();
}
else if (currentLevel == 10) {
level10();
}
}
void gameOver() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("GAME OVER", width/2, height/2);
text("SPACEBAR TO RESTART", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 0;
}
}
}
//LEVEL 0
void level0() {
image(imgSplash1, 0, 0);
if (mousePressed) {
currentLevel = 1;
}
}
//LEVEL 1
void level1() {
if (lives == 0) {
gameOver();
}
else {
if (score >= 3) {
currentLevel = 2;
}
else {
//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();
}
}
}
//LEVEL 2
void level2() {
image(imgSplash2, 0, 0);
if (mousePressed) {
currentLevel = 3;
}
}
//LEVEL 3
void level3() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 3", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 4;
}
}
}
//LEVEL 4
void level4() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 4", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 5;
}
}
}
//LEVEL 5
void level5() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 5", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 6;
}
}
}
//LEVEL 6
void level6() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 6", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 7;
}
}
}
//LEVEL 7
void level7() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 7", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 8;
}
}
}
//LEVEL 8
void level8() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 8", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 9;
}
}
}
//LEVEL 9
void level9() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 9", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
currentLevel = 10;
}
}
}
//LEVEL 10
void level10() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 10", width/2, height/2);
text("YOU HAVE WON", width/2, 350);
}
//Create class player
class Player {
int x_, y_, r;
Player(int pXPos, int pYPos) {
x_ = pXPos;
y_ = pYPos;
}
//Create show function for player
void show() {
r = 15; //Player size
smooth();
fill(255, 246, 180);
stroke(0);
strokeWeight(2);
ellipse(x_, y_, r*2, r*2); //Body
fill(255);
ellipse(x_-10, y_-5, 15, 15); //Eye #1
ellipse(x_+10, y_-5, 15, 15); //Eye #2
fill(0);
ellipse(x_-10, y_-5, 7, 7); //Pupil #1
ellipse(x_+10, y_-5, 7, 7); //Pupil #2
fill(255, 0, 0);
ellipse(x_-15, y_+ r, 18, 10); //Foot #1
ellipse(x_+15, y_+ r, 18, 10); //Foot #2
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;
- 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;
}
}
}
- class Part {
float x_;
float y_;
float d;
Part(int posX, int posY) {
x_ = posX;
y_ = posY;
}
//Create appearance of first part
void appear1() {
d = 20;
strokeWeight(1);
smooth();
fill(117, 203, 155);
ellipse(x_, y_, d, d*2);
fill(206, 206, 206);
strokeWeight(2);
ellipse(x_, y_-5, d/2, d/2);
fill(255, 0, 0);
strokeWeight(1);
ellipse(x_, y_+5, d/4, d/4);
ellipse(x_, y_+10, d/4, d/4);
}
//Create collect function for first part
void collect1() {
x_ = 700;
y_ = 50;
score++;
}
//Create appearance of second part
void appear2() {
smooth();
triangle(x_+5, y_+4, x_+5, y_-10, x_+20, y_+15);
triangle(x_-5, y_+4, x_-5, y_-10, x_-20, y_+15);
fill(117, 203, 155);
rectMode(CENTER);
rect(x_, y_-2, 10, 12);
fill(255, 255, 0);
noStroke();
triangle(x_-4, y_+4, x_+5, y_+4, x_, y_+18);
fill(255, 68, 0);
triangle(x_-2, y_+4, x_+3, y_+4, x_, y_+12);
}
//Create collect function for second part
void collect2() {
x_ = 745;
y_ = 50;
score++;
}
//Create appearance of third part
void appear3() {
fill(86, 86, 86);
stroke(0);
strokeWeight(1);
triangle(x_, y_-10, x_-2, y_+8, x_+2, y_+8);
fill(117, 203, 155);
ellipse(x_, y_+7, 10, 4);
ellipse(x_, y_-11, 4, 4);
}
//Creat ecollect function for third part
void collect3() {
x_ = 775;
y_ = 50;
score++;
}
//Specify intersect conditions for player and parts
boolean intersect(Player a) {
float distance = dist(x_, y_, a.x_, a.y_);
if (distance < d + a.r ) {
return true;
}
else {
return false;
}
}
}
1