I am working on a simple game for my class involving cars that move down the screen towards a player.
It uses graphics and a midi controller for input so sorry if it becomes hard to test the code yourself.
The problem I am having is with the car collision.
I want the cars on collision to reset to their starting positions but cant seem to work it out.
I would also like for the other cars to start in a random position each time (and preferably not on top of each other) but the code I implemented only does this for the first instance of the cars.
any help much appreciated.
import themidibus.*; //MIDIライブラリの読み込み
MidiBus myBus; // The MidiBus
int[] val = new int[4];
int player_x = 500;
int player_y = 500;
int player_w = 10;
int player_h = 10;
int score = 10;
int x;
int xRandom;
PImage bg;
PImage car;
PImage player;
PImage bus;
PImage truck;
Car car1;
Car car2;
Car car3;
// Define car dimensions.
final int TRUCK_WIDTH = 60;
final int TRUCK_HEIGHT = 360;
int truck_position_x = 125;
int truck_position_y = 50;
final int CAR_WIDTH = 50;
final int CAR_HEIGHT = 100;
int car_position_x = 185;
int car_position_y = 50;
final int BUS_WIDTH = 60;
final int BUS_HEIGHT = 160;
int bus_position_x = 240;
int bus_position_y = 50;
void setup() {
size(600,800);
colorMode(HSB,100);
//background(0);
bg = loadImage("background.png");
player = loadImage("f1.png");
truck = loadImage("truck.png");
bus = loadImage("bus.png");
car = loadImage("car.png");
xRandom = (int)random(125, 250);
car1 = new Car(truck, xRandom, truck_position_y, TRUCK_WIDTH, TRUCK_HEIGHT, 1);
car2 = new Car(bus, xRandom, bus_position_y, BUS_WIDTH, BUS_HEIGHT, 2);
car3 = new Car(car, xRandom, car_position_y, CAR_WIDTH, CAR_HEIGHT, 3);
smooth();
MidiBus.list(); //MIDIデバイス一覧の表示
myBus = new MidiBus(this, 0, 3);//通信用オブジェクトの生成
}
void draw(){
background(bg);
strokeWeight(4);
stroke(255,247,3);
//////////////////////
//fill(0);
for(int i = 14; i<width; i+=14) {
noStroke();
fill(190);
rect (i,height/2.6,7,4);
}
///////////////////////
for(int i = 14; i<width; i+=14) {
noStroke();
fill(190);
rect (i,height/1.6,7,4);
}
//////////////////////
car1.draw();
car1.move();
car2.draw();
car2.move();
car3.draw();
car3.move();