How Do I Add A Score to My Game?!

So, i'm creating a Keepy uppy game, you have to keep the ball from hitting the ground. I need to do a lot more to finish this, but one of the things i want to add is a score tally to tell the person playing the game how many times he kicks the ball without it hitting the ground. So for every time the ball makes contact with the boot, which is controlled by the cursor, the score should go up by one each time. This is my code at the moment, please help.

import ddf.minim.*;
Minim minim;
AudioPlayer song;

float xSpeed = 3;
float ySpeed = 0.2;
float ballX = 20;
float ballY = 400;
float ballRadius = 30;
float blockSize = 25;
float boot = 20;
PImage LFC;
PImage Anfield;
int stage;
PFont text;
float gravity = 0.3;



void setup() {
  size(900, 600);
  LFC=loadImage ("LFC.jpg");
  Anfield=loadImage ("Anfield.jpg");
  minim = new Minim(this);
  song = minim.loadFile("MOTD.mp3");
  song.play();
}
void draw() {
  background(LFC);
  fill(0, 0, 0);
  blockCollisionCheck(mouseX, height-50);
  ellipse(mouseX-boot, height-20, 50, 100);
  fill(255, 255, 255);
  rect(mouseX-30, height-40, 20, 2.5);
  rect(mouseX-30, height-30, 20, 2.5);
  rect(mouseX-30, height-20, 20, 2.5);
  rect(mouseX-30, height-10, 20, 2.5);

  // background (255);
  ballX = ballX + xSpeed;
  ballY = ballY + ySpeed;

  ySpeed = ySpeed + gravity;

  if ((ballX + ballRadius > width && xSpeed > 0) || (ballX - ballRadius < 0 && xSpeed < 0)) {
    xSpeed = xSpeed * -1;
  }
  if ((ballY + ballRadius > height && ySpeed > 0) || (ballY - ballRadius < 0 && ySpeed < 0)) {
    ySpeed = ySpeed * -1;
  }
  stroke(0);
  fill(255, 255, 255);
  ellipse(ballX, ballY, ballRadius * 2, ballRadius * 2);
}

void blockCollisionCheck(float x, float y) {
  if (ballY + ballRadius >= y && ballY - ballRadius <= y + blockSize && ballX + ballRadius >= x && ballX - ballRadius <= x + blockSize) {

    float pballX = ballX - xSpeed; 
    float pballY = ballY - ySpeed;


    if ((pballY + ballRadius < y) || (pballY - ballRadius > y + blockSize)) {
      ySpeed = -ySpeed;
    }

    if ((pballX + ballRadius < x) || (pballX - ballRadius > x + blockSize)) {
      xSpeed = -xSpeed;
    }
  }
}

Answers

  • Have a global int variable named score. Increment it each time the hit is right. Display it with the text() function.

Sign In or Register to comment.