car game

edited May 2018 in Questions about Code

Hi im trying to make a car game where the car needs to do a zig zag course through other cars. I cant moove the image for some reason.

`Ship ship;
PImage img;
void setup() {
  size(800, 800);
  img = loadImage("Ecar.jpg");
  ship = new Ship();
  }

void draw() {
  background(51);
  ship.show();
  ship.move();
}
void keyPressed() {


  if (keyCode == RIGHT) {
    ship.setDir(1);
  } else if (keyCode == LEFT) {
    ship.setDir(-1);
  }
}`


`class Ship {
  float x, xdir;
  PShape car;

  Ship() {
    this.x = width/2-40;
    this.xdir = 0;
    noStroke();
    fill(255,0,0);
    car = createShape(RECT, this.x, height-300, 100, 200);
    car.setTexture(img);
  }

  void show() {
    fill(255);
    rectMode(CENTER);
    shape(car);
    //rect(this.x, height-50, 20, 60);
  }

  void setDir(float dir) {
    this.xdir = dir;
  }

  void move() {
    this.x += this.xdir*5;
  }
}`

Answers

  • I think your Ship class's show() function is missing a closing bracket.

    That your code has a syntax error like this in it is not promising. Start with code that runs. If you make any changes to it, make sure it still runs. If it breaks, revert those changes immediately (or fix it). Do this for even the smallest changes. In this manner you will always have code that, at the very least, runs.

  • Next, you are talking about cars and zig-zagging, but your code has a Ship class in it. What are the Ships even for? What even is a car? Is "Car" going to be an type of object? What kind of zig-zagging are you trying to get it to do?

  • some improvement on how to display a car / ship using shape() command: you need to use the position x here!

    Ship ship;
    // PImage img;
    
    void setup() {
      size(800, 800);
      //  img = loadImage("Ecar.jpg");
      ship = new Ship();
    }
    
    void draw() {
      background(51);
    
      ship.show();
      ship.move();
    }
    
    void keyPressed() {
      if (keyCode == RIGHT) {
        ship.setDir(1);
      } else if (keyCode == LEFT) {
        ship.setDir(-1);
      }
    }
    
    // =========================================
    
    class Ship {
    
      float x, xdir;
      PShape car;
    
      Ship() {
        x = width/2-40;
        xdir = 0;
    
        car = createShape(RECT, 0, 0, 100, 200);
        car.setFill(color(255, 0, 0)); // red 
        //  car.setTexture(img);
      }//constr 
    
      void show() {
        shapeMode(CENTER);
        shape(car, x, height/2);
      }
    
      void setDir(float dir) {
        xdir = dir;
      }
    
      void move() {
        x += xdir;
      }
      //
    }//class
    //
    
  • edited May 2018

    ok so i found an exaple but every time i run it it says thet there is a nullPointerExpectation on this point

    imageMode(CENTER);
        image(myCar.car[0], 100, 150);
        image(myCar.car[1], 200, 150);
        image(myCar.car[2], 300, 150);
    

    this is the code

    Car myCar;
    Road mainRoad;
    OtherCar[] enemy1, enemy2, enemy3;
    Button bstart, brestart, beasy, bmedium, bhard;
    float spd;
    int CarSelect;
    boolean crash;
    float[] distance = new float[3];
    PFont myFont ; //= createFont("Arial", 25);
    //int aux=1;
    
    void setup() {
      size (800, 800);
      frameRate(60);
      myFont = createFont("Arial", 25);
      mainRoad = new Road();
      myCar = new Car(400);
      bstart = new Button(); 
      brestart = new Button();
      beasy = new Button(); 
      bmedium = new Button(); 
      bhard = new Button();
    
      enemy1 = new OtherCar[110];
      enemy2 = new OtherCar[110];
      enemy3 = new OtherCar[110];
    
      for (int i=1; i<=109; i++) {
        distance[0]=random(800, 1500);
        enemy1[i] = new OtherCar(1, 0-(i*distance[0]));
    
        distance[1]=random(800, 1500);
        println(i);
        if (distance[1]<=distance[0]+200 || distance[1]<=distance[2]+200) {
          distance[1]+=500;   
          print(" Distance 0: " + distance[0]); 
          print(" Distance 1: " + distance[1]); 
          println(" Distance 2: " + distance[2]);
        }
        enemy2[i] = new OtherCar(2, 0-(i*distance[1]));
    
        distance[2]=random(800, 1500);
        enemy3[i] = new OtherCar(3, 0-(i*distance[2]));
      }
    }
    
    void draw() {
    
      mainRoad.startRoad();
    
      //CRASH TEST
      for (int i=1; i<=109; i++) {
        enemy1[i].startCar();
        if (myCar.posX==200 && enemy1[i].pY>=myCar.posY-100  && enemy1[i].pY<=height+20) {
          crash=true;
        }
    
        enemy2[i].startCar();
        if (myCar.posX==400 && enemy2[i].pY>=myCar.posY-100 && enemy2[i].pY<=height+20) {
          crash=true;
        }
    
        enemy3[i].startCar();
        if (myCar.posX==600 && enemy3[i].pY>=myCar.posY-100 && enemy3[i].pY<=height+20) {
          crash=true;
        }
      }
    
      //LAST SCREEN
      if (crash==true) 
      {
        fill(255);
        textFont(myFont);
        textAlign(CENTER, CENTER);
        text("GAME OVER", width/2, height/2);
        spd=0;
      }
    
      //FIRST SCREEEN
      if (bstart.clicked==false) {
        spd=0;
    
        fill(255);
        textFont(myFont);
        textAlign(CENTER, CENTER);
        text("Select the level:", 400, 100); 
        text("Easy", 200, 160);
        text("Medium", 400, 160);
        text("Hard", 600, 160);
        fill(255,200);
        noStroke();
        rectMode(CENTER);
        rect(400,700,500,100);
        fill(0);
        text("To move the car use the keys:\nA                     S                     D", 400, 700);
    
        beasy.create(200, 250, 115, 115, " ");
        bmedium.create(400, 250, 115, 115, " ");
        bhard.create(600, 250, 115, 115, " ");
    
        imageMode(CENTER);
        image(myCar.car[0], 100, 150);
        image(myCar.car[1], 200, 150);
        image(myCar.car[2], 300, 150);
    
        bstart.create(400, 400, 115, 50, "START GAME");
      }
    
    
      //LEVEL CAR SELECTION AND START CAR
      if (beasy.clicked==true || bmedium.clicked==true || bhard.clicked==true) { //AVOID START WITHOUT SELECT A LEVEL
        if (bstart.clicked==true && crash==false) {
          if (beasy.clicked==true) {
            CarSelect=0; 
            spd=5;
          }
          if (bmedium.clicked==true) {
            CarSelect=1; 
            spd=10;
          }
          if (bhard.clicked==true) {
            CarSelect=2; 
            spd=15;
          }
    
          myCar.display();
        }
      } else {
        bstart.clicked = false; //AVOID START WITHOUT SELECT A LEVEL
      }
    }<del></del>
    
    class Button {
      float posX, posY, xSize, ySize;
      color cButton, cNormal=#FFFFFF, cOver=#0087FF, cPressed=#001478;
      PFont myFont = createFont("Arial", 14);
      boolean clicked;
    
      void update() {
        if (mouseX>=posX-xSize/2 && mouseX<=posX+xSize/2 && mouseY>=posY-ySize/2 && mouseY<=posY+ySize/2) {
          cButton = cOver;
          if (mousePressed==true) {
            cButton=cPressed;
            clicked=true;
          }
        } else
        {
          cButton=cNormal;
        }
      }
    
    
      void display(int x, int y, float xs, float ys, String label) {
        fill(cButton);
        rectMode(CENTER);
        rect(x, y, xs, ys);
        fill(0);
        textFont(myFont);
        textAlign(CENTER, CENTER);
        text(label, x, y);
        posX=x; 
        posY=y; 
        xSize=xs; 
        ySize=ys;
      }
    
    
      void create(int x, int y, int xs, int ys, String label) {
        this.display(x, y, xs, ys, label);
        this.update();
      }
    }
    class Car {
      float posX;
      float posY;
      PImage[] car;
    
    
      //------------constructor
      Car (int position) {
        car = new PImage[3];
        posX = position;
        posY = height-60;
        for (int i =0; i<=2; i++) {
          car[i]=loadImage("car"+i+".jpg");
        }
      }
      //------------
    
    
      void display() {
        imageMode(CENTER);
        image(car[CarSelect], posX, posY);
    
        if ( key == 'A' || key == 'a')
          posX=200;
        if ( key == 'S' || key == 's')
          posX=400;
        if ( key == 'D' || key == 'd')
          posX=600;
        //end display
      }
    
    
      //end class
    }
    
    class OtherCar {
      float pX;
      float pY;
      PImage Ecar;
    
    
      //------------constructor
    
        OtherCar(float positionX, float positionY) {
    
        Ecar=loadImage("Ecar"+int(random(0, 3))+".png");
    
    
        if (positionX == 1) pX = 200;
        if (positionX == 2) pX = 400;
        if (positionX == 3) pX = 600;
    
        pY=positionY;
      }
      //------------
    
      void startCar() {
    
        imageMode(CENTER);
        image(Ecar, pX, pY);
        pY+=spd;
      }
    }
    class Road {
      float pos1Y=0-height/2;
      float pos2Y=-1600;
      PImage  road1, road2, grass1, grass2;
    
      //------------constructor
    
    
    
      Road () { 
        road1=loadImage("road.png");
        road2=loadImage("road2.jpg");
        grass1=loadImage("grass1.jpg");
        grass2=loadImage("grass2.png");
      }
      //------------
    
      void startRoad()
      { 
        imageMode(CORNERS);
        image(outroad, 0, pos1Y);
        image(outroad2, 0, pos2Y);
        image(road1, 100, pos1Y);
        image(road2, 100, pos2Y); 
    
    
        if (pos1Y>=800) pos1Y=-1600;
        if (pos2Y>=800) pos2Y=-1600;
    
    
        pos1Y+=spd;
        pos2Y+=spd;
      }
      }
    
  • Better instead of searching code that you didn’t write and don’t understand work with your own code or with my improved version of it.

Sign In or Register to comment.