Can't see the problem
in
Programming Questions
•
1 year ago
Can someone tell me why I get the error "The function level3() does not exist." when I try to run this?
- 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;
boolean level0 = true;
boolean level1 = false;
boolean level2 = false;
boolean level3 = false;
boolean level4 = false;
boolean level5 = false;
boolean level6 = false;
boolean level7 = false;
boolean level8 = false;
boolean level9 = false;
boolean level10 = false;
boolean level11 = false;
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
car1 = new Car(0, 135, -12);
car2 = new Car(200, 135, -12);
car3 = new Car(400, 135, -12);
car4 = new Car(600, 235, 8);
car5 = new Car(0, 235, 8);
car6 = new Car(150, 235, 8);
car7 = new Car(300, 335, -6);
car8 = new Car(450, 335, -6);
car9 = new Car(600, 435, 5);
car10 = new Car(0, 435, 5);
car11 = new Car(300, 435, 5);
car12 = new Car(200, 335, -6);
car13 = new Car(0, 335, -6);
car14 = new Car(500, 435, 5);
//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 (level0 == true) {
level0();
}
else if (level1 == true) {
level1();
}
else if (level2 == true) {
level2();
}
else if (level3 == true) {
level3();
}
else if (level4 == true) {
level4();
}
else if (level5 == true) {
level5();
}
else if (level6 == true) {
level6();
}
else if (level7 == true) {
level7();
}
else if (level8 == true) {
level8();
}
else if (level9 == true) {
level9();
}
else if (level10 == true) {
level10();
}
else if (level11 == true) {
level11();
}
}
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 == ' ') {
level0 = true;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//LEVEL 0
void level0() {
image(imgSplash1, 0, 0);
if (mousePressed) {
level0 = false;
level1 = true;
}
}
//LEVEL 1
void level1() {
if (lives == 0) {
gameOver();
}
else {
if (score >= 3) {
level0 = false;
level1 = false;
level2 = true;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
else {
//Display game info
image(img, 0, 0);
fill(0);
text("Lives: " + lives, 100, 35);
//Initialise cars
car1.move();
car1.display();
car2.move();
car2.display();
car3.move();
car3.display();
car4.move();
car4.display();
car5.move();
car5.display();
car6.move();
car6.display();
car7.move();
car7.display();
car8.move();
car8.display();
car9.move();
car9.display();
car10.move();
car10.display();
car11.move();
car11.display();
car12.move();
car12.display();
car13.move();
car13.display();
car14.move();
car14.display();
//Initialise player
alien.show();
//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();
}
}
}
}
//LEVEL 2
void level2() {
image(imgSplash2, 0, 0);
if (mousePressed) {
level0 = false;
level1 = false;
level2 = false;
level3 = true;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = true;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = true;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = true;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = true;
level8 = false;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//LEVEL 7
void level3() {
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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = true;
level9 = false;
level10 = false;
level11 = false;
}
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = true;
level10 = false;
level11 = false;
}
}
}
//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 == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = true;
level11 = false;
}
}
}
//LEVEL 10
void level10() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 10", width/2, height/2);
text("SPACEBAR TO WIN", width/2, 350);
if (keyPressed) {
if (key == ' ') {
level0 = false;
level1 = false;
level2 = false;
level3 = false;
level4 = false;
level5 = false;
level6 = false;
level7 = false;
level8 = false;
level9 = false;
level10 = false;
level11 = true;
}
}
}
//LEVEL 11
void level11() {
textFont(f, 42);
textAlign(CENTER);
background(255);
fill(0);
text("THIS IS LEVEL 11", width/2, height/2);
text("YOU HAVE WON", width/2, 350);
}
//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;
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;
}
}
}
//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;
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;
1