Simple car game
in
Programming Questions
•
2 years ago
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();
player();
}
void player(){
fill(10,50,100);
player_x = (width - player_h) * val[3] / 127;
image(player, player_x, player_y);
}
void noteOn(int channel, int pitch, int velocity) {
// データが読み込まれたときに呼び出される関数
// chanel 0: X方向
// chanel 1: Y方向
// chanel 2: X方向
// chanel 3: ボリューム
val[channel] = velocity;
/* println();
println("Note On:");
println("--------");
println("Channel:"+channel);
println("Pitch:"+pitch);
println("Velocity:"+velocity);
*/
}
class Car
{
PImage carImage;
int x, y, w, h;
int speed;
Car(PImage c, int x, int y, int w, int h, int s)
{
this.carImage = c;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = s;
}
void draw()
{
noStroke();
rect(this.x, this.y, this.w, this.h);
image(this.carImage, this.x, this.y);
hitCheck(this.x, this.y, this.w, this.h);
}
void move()
{
this.y += this.speed;
if (this.y > height) {
this.y = 0;
}
}
}
void hitCheck(int x, int y, int h, int w){
if( x + (w) >= player_x &&
y + (h) >= player_y &&
x <= player_x + player_w &&
y <= player_y + player_h){
score = score + 10;
println (score);
}
}
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();
player();
}
void player(){
fill(10,50,100);
player_x = (width - player_h) * val[3] / 127;
image(player, player_x, player_y);
}
void noteOn(int channel, int pitch, int velocity) {
// データが読み込まれたときに呼び出される関数
// chanel 0: X方向
// chanel 1: Y方向
// chanel 2: X方向
// chanel 3: ボリューム
val[channel] = velocity;
/* println();
println("Note On:");
println("--------");
println("Channel:"+channel);
println("Pitch:"+pitch);
println("Velocity:"+velocity);
*/
}
class Car
{
PImage carImage;
int x, y, w, h;
int speed;
Car(PImage c, int x, int y, int w, int h, int s)
{
this.carImage = c;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.speed = s;
}
void draw()
{
noStroke();
rect(this.x, this.y, this.w, this.h);
image(this.carImage, this.x, this.y);
hitCheck(this.x, this.y, this.w, this.h);
}
void move()
{
this.y += this.speed;
if (this.y > height) {
this.y = 0;
}
}
}
void hitCheck(int x, int y, int h, int w){
if( x + (w) >= player_x &&
y + (h) >= player_y &&
x <= player_x + player_w &&
y <= player_y + player_h){
score = score + 10;
println (score);
}
}
1