# Hey guys, was wondering how i can make a scoring system and get my character to reset? I also need to be able to change the max score

int pedestrianX;
int pedestrianY;
int vehicleX;
int vehicleY;
PFont f;
int lineX = 0;
int lineY = 80; // Vertical location of each line
int spacing = 50; // How far apart is each line
int len = 50; // Length of each line
float speed = random(0, 10);

void setup() {
size(1200, 400);
f = createFont(“Arial”, 100, true);
}

void draw() {
background(255);
boolean dash = true;
for (int y = 0; y < height; y+=20) {
for (int i = 0; i < width; i+=20) {
if (dash) stroke(0);
else stroke(255);
line(i, 100, i + 20, 100);
dash = !dash;
}
}

//line(0, height / 4, 1200, height / 4);

vehicleX = (frameCount * 5) % width;
vehicleY = 0;

drawPedestrian(pedestrianX, pedestrianY); // This is used to create the pedestrian
drawVehicle(vehicleX, vehicleY); // This is used to create the vehicle.
// The speed can be altered by changing the number multipied by the frameCount.
// The % is used to reset the vehicle

collisionDetection();
fill(0);
if ( collisionDetection()) {
stop();
background(#F70000);
textFont(f);
text(“Game over!”, 300, 200);
}
if (pedestrianY > height) {
pedestrianY = 314;
}
}

void drawVehicle(int vehicleX, int ypos) {
int x[] = { 0 + vehicleX, 150 + vehicleX, 100 + vehicleX, 105 + vehicleX, 70 + vehicleX, 30 + vehicleX, 40 };
int y[] = { 0, 80, 70, 40, 30, 10, 60, 78, 50 }; // These are all common points in the shapes. The vehicleX variable enables the vehicle to move.

stroke(0);
fill(#DE482B);
quad(x[4], y[3], x[2], y[4], x[2], y[1], x[4], y[2]); //This is the hood of the vehicle

fill(#1AB718);
quad(x[0], y[4], x[5], y[3], x[5], y[2], x[0], y[1]); //This is the boot of the vehicle

rectMode(CORNER);

fill(#144EA7);
rect(x[5], y[3], x[6], y[3]); //This is the roof of the vehicle

fill(#E2E359);
quad(x[0], y[1], x[5], y[2], x[4], y[2], x[2], y[1]);
quad(x[0], y[4], x[5], y[3], x[4], y[3], x[2], y[4]); //These are the doors of the vehicle

stroke(#E2E359);
triangle(x[2], y[3], x[1], y[4], x[1], y[6]);
triangle(x[2], y[2], x[1], y[8], x[1], y[7]); //These are the headlights of the vehicle

stroke(0);
}

void drawPedestrian(int pedestrianX, int pedestrianY) {

int x[] = { 11, 31, 600 + pedestrianX};
int y[] = { 10, 20, 30, 30, 314 + pedestrianY, 347 + pedestrianY, 375 + pedestrianY, 392 + pedestrianY };

stroke(0);
fill(#FF1803);
ellipse(x[2], y[4], x[1], y[2]); // This is the head of the pedestrian

fill(#1FC14E);
rectMode(CENTER);
rect(x[2], y[5], x[1], y[3]); // This is the body of the pedestrian

fill(#341FC1);
rect(x[2], y[6], x[0], y[1]); // This is the pillar of the pedestrian

fill(#F5FA17);
ellipse(x[2], y[7], x[0], y[0]); // This is the sphere on which the pedestrian glides on
}

void keyPressed() {
if ((keyPressed == true) && (key == CODED)) {
if (keyCode == UP) {
pedestrianY = pedestrianY - height/4;
} else if (keyCode == DOWN ) {
pedestrianY = pedestrianY + height/4;
} else if (keyCode == LEFT) {
pedestrianX = pedestrianX - 50;
} else if (keyCode == RIGHT) {
pedestrianX = pedestrianX + 50;
}
}
}

boolean collisionDetection() {
float vx = vehicleX + 5;
float vy = vehicleY + 30;
float vw = 150;
float vh = 50;
float px = pedestrianX + 585;
float py = pedestrianY + 300;
float pw = 30;
float ph = 95;
// Turn the lines below on to see borders on the collsion zones.
noFill();
noStroke();
rect(vx, vy, vw, vh);
rect(px, py, pw, ph);
stroke(0);

if (vx + vw >= px && vx <= px + pw && vy + vh >= py && vy <= py + ph) {
return true;
} else {
return false;
}
}

How would the scoring system work? Do you mean if the pedestran successfully manages to dodge the car?

Assuming you have a boolean that determens if you can increase the score you can use something like

``````if(higherScore){
if(score<=hardCap) score++;
reset();
}
``````

You just have to definie score and hardCap as a global field and reset as method.

You can change the max score by changing the hardCap field.
If you want some input from the user though you look at this code:

``````import javax.swing.*;
int inp=int(JOptionPane.showInputDialog(null, "Window Text", "Window name", JOptionPane.INFORMATION_MESSAGE));
println(inp);
``````

Basicly when you enter a number it will be put in inp. Keep in mind that this will interrupt the sketch so you can’t give it inputs anymore and the car won’t move.

Also, do you want to store the score on the hard drive? This changes the thing a bit too.

Yea it cant hit the car

So the condition to increment the score is that the y-coordinate of the character reaches or exceeds a certain value. I think

``````if (pedestranY<=0){
if(score<=hardCap) score++;
reset();
}
``````

The next thing is thinking about what you want to actually reset.
I would absolutly reset pedestranY. But maybe reseting the pedesstranX might also be a good idea.

I’ve tried this but for some reason it doesn’t work at all. Like the character doesnt get reset

I just tested it myself. I thought that pedestrianY was the Y-position of the sprite but I was wrong. Checking if `pedestrianY<=-400` should do the trick.

You can put this code in draw:

``````  if(pedestrianY<=-400){
pedestrianY=0;
if(score<hardCap) score++;
println(score);
}
``````

And dont forget to create the score and hard-cap fields!

I hope that helps!

1 Like

Oh my god thank you so much. I’ve been stuck on this for days and you finally helped me work it out. Thank you so much