Please Help! Totally lost here!
in
Programming Questions
•
2 years ago
Ok, I've been posting in another topic about a project I've been working on, and I'm almost done with it but I've hit a roadblock on probably one of the most important parts. I've been making a mini version of Frogger for class, and everything else was working decent enough, but when trying to create a collision I messed something up and now I can't get the game to work at all. I know what part of the code messed up the game and can take it out to make it run again, but my question is this...
How can I correctly make it so that if my frog collides with the car, I can display the frogsplat.gif. I'm also trying to make it so the frog cannot be moved once it has either won or been killed. I have a boolean in there and I keep getting an error message once I tried to create...something...but I'm really confused with this at the moment, and hopefully someone can help me debug this and tell me what I'm doing wrong to get this error. Thanks in advance for anyone who can help.
Here is my code, I have 3 seperate classes
Frogger (I'm getting a nullerpoint exception here, it highlights frogLeft = frog.x)
- final int WIDTH = 200;
- final int HEIGHT = WIDTH;
- final int MIDDLE = WIDTH / 2;
- final color BLACK = color(0);
- final color RED = color(255, 0, 0);
- final color GREEN = color(0, 255, 0);
- final color BLUE = color(0, 0, 255);
- final int CAR_WIDTH = 30;
- final int CAR_HEIGHT = 10;
- int frogx = MIDDLE;
- int frogy = 170;
- PImage frogimg;
- PImage frogdeadimg;
- Car car1;
- Car car2;
- Car car3;
- Frog frog;
- boolean livefrog = true;
- void setup()
- {
- size(WIDTH, HEIGHT);
- rectMode(CENTER);
- car1 = new Car(RED, 0, 100, CAR_WIDTH, CAR_HEIGHT, 2);
- car2 = new Car(GREEN, 50, 50, CAR_WIDTH, CAR_HEIGHT, 1);
- car3 = new Car(BLUE, 150, 150, CAR_WIDTH, CAR_HEIGHT, 2);
- frogimg = loadImage("frog.gif");
- frogdeadimg = loadImage("frogsplat.gif");
- }
- void draw()
- {
- background(BLACK);
- line(200, 20, 0, 20);
- line(200, 180, 0, 180);
- if (frogy < 0) {
- //game over
- fill(180, 180, 0);
- text("Game Over! Press Enter", width/ 5, height / 2);
- if(keyCode == ENTER ){
- frogx = MIDDLE;
- frogy = 170;
- }
- }
- else {
- //game on
- }
- for(int i = 10; i < width; i+=17){
- noStroke();
- fill (190);
- rect(i, height/2.6, 10, 4);
- }
- for(int i = 10; i < width; i+=17){
- noStroke();
- fill (190);
- rect(i, height/1.6, 10, 4);
- }
- for(int i = 10; i < width; i+=17){
- noStroke();
- fill(190);
- rect(i, height/1.16, 10, 4);
- }
- for(int i = 10; i < width; i+=17){
- noStroke();
- fill(190);
- rect(i, height/7, 10, 4);
- }
- if (livefrog){
- car1.move();
- car2.move();
- car3.move();
- }
- car1.draw();
- car2.draw();
- car3.draw();
- if (isHit(car1) || isHit(car2) || isHit(car3)){
- image(frogdeadimg, frogx, frogy, 32, 32);
- livefrog = false;
- }
- image(frogimg, frogx, frogy, 32, 32);
- }
- void keyPressed(){
- if(keyCode == 38){
- frogy = frogy - 20;
- }
- if(keyCode == 40){
- frogy = frogy + 20;
- }
- }
- boolean isHit(Car car)
- {
- int carLeft = car.x - car.w / 2;
- int carRight = car.x + car.w / 2;
- int carTop = car.y - car.h / 2;
- int carBottom = car.y = car.y + car.h / 2;
- int frogLeft = frog.x;
- int frogRight = frog.x + frog.w;
- int frogTop = frog.y;
- int frogBottom = frog.y + frog.h;
- if (frogLeft > carRight || frogRight < carLeft) return false;
- if (frogTop > carBottom || frogBottom < carTop) return false;
- return true;
- }
Car
- class Car
- {
- color col;
- int x, y, w, h;
- int speed;
- Car(color c, int x, int y, int w, int h, int s)
- {
- this.col = c;
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- this.speed = s;
- }
- void draw()
- {
- noStroke();
- fill(this.col);
- rect(this.x, this.y, this.w, this.h);
- }
- void move()
- {
- this.x += this.speed;
- if (this.x > width) {
- this.x = 0;
- }
- }
- }
Frog (I feel like I may have also did something wrong in here, because it seems too blank...)
- class Frog
- {
- int x, y, w, h;
- Frog(int x, int y, int w, int h)
- {
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- }
- }
1