Adding a score system

Hello,
I have been trying to add a score system to my pong game coding. Where if the ball goes out it gives a point to the opposite side and when a player hits 5 points that player wins.

However, I do not know how to add this to the code; been trying ways but everything I try it doesn't work (probably just me doing it wrong and have no clue what I am doing).

Is there any ideas in how I can add a scoring system???

Add scoring system.
Test.
Revise code.
Rinse and repeat.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

What programming language did you program the game in? Depending on the language, this would be done differently, but you could create a "while" loop, to detect when the x or y coordinates go beyond the size of the window. This would then reset the game and increment the score variable for the respective player. Another statement would detect when the score variable for either player is equal to 5, and end the game.

hello,
I am using c++ language and this is my code so far: it would be great if someone can help and guide me to create some sort of a scoring system.

#include <TFT.h> // Arduino LCD library
#include <SPI.h>

// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8

TFT TFTscreen = TFT(cs, dc, rst);

// variables for the position of the ball and paddle
int paddle1X = 0;
int paddle1Y = 0;
int oldPaddle1X, oldPaddle1Y;
int paddle2X = 0;
int paddle2Y = TFTscreen.height()-10/2;
int oldPaddle2X, oldPaddle2Y;
int ballDirectionX = 1;
int ballDirectionY = 1;

int ballSpeed = 20; //lower the number the faster the ball moves

int ballX, ballY, oldBallX, oldBallY;
 
void setup() {
  TFTscreen.begin(); //initialize the display
  TFTscreen.background(50, 50, 50); //black background
}

void loop() {
  //width and height of the screen
  int myWidth = TFTscreen.width()*2;
  int myHeight = TFTscreen.height();

  // location of the paddles
  paddle1X = map(analogRead(A0), 1024, -1024, 0, myWidth) - 20 / 2;
  paddle1Y == -5 / 2;
  paddle2X = map(analogRead(A1), 1024, -1024, 0, myWidth) - 20 / 2;
  paddle2Y == TFTscreen.height()- - 5 / 2;

  //fill colour black and earase the previous and position of paddle if different  from present
  TFTscreen.fill(50, 50, 50);
  
  if (oldPaddle1X != paddle1X || oldPaddle1Y != paddle1Y) {
    TFTscreen.rect(oldPaddle1X, oldPaddle1Y, 20, 5);
  }
  if (oldPaddle2X != paddle2X || oldPaddle2Y != paddle2Y) {
    TFTscreen.rect(oldPaddle2X, oldPaddle2Y, 20, 5);
  }
  
  // draw paddle on screen and save the current position
  TFTscreen.fill(255, 255, 255);
  
  TFTscreen.rect(paddle2X, paddle2Y, 20, 5);
  oldPaddle2X = paddle2X;
  oldPaddle2Y = paddle2Y;
  
  TFTscreen.rect(paddle1X, paddle1Y, 20, 5);
  oldPaddle1X = paddle1X;
  oldPaddle1Y = paddle1Y;
  
  // update the balls's position and draw it on screen
  if (millis() % ballSpeed <2) {
    moveBall();
  }
}

//reset game when ball drops, so give the ball a random direction
void reset()
{
  ballX = 50;
  ballY = 50;
  ballDirectionX = random(-5,5);
  ballDirectionY = 1;
  if (ballDirectionX == 0 ) {
    ballDirectionX == -5;
    }
    else{
      ballDirectionX = 1;
    }
    ballDirectionY = random(0,1);
    if (ballDirectionY == 0 ) {
      ballDirectionY = -1;
    }
    else
    {
      ballDirectionY = 1;
    } 
    ballSpeed = 20;
    delay(1000);
}

//determines the ball's position on screen
void moveBall() {
// if the ball goes offscreen reset the game 
  if (ballX > TFTscreen.width() || ballX < 0) {
    ballDirectionX = -ballDirectionX;
  }
  
  if (ballY > TFTscreen.height() || ballY < 0) {
    reset();
  }
  
 //check the ball and paddle position. 
if (inPaddle(ballX, ballY, paddle1X, paddle1Y, 20, 5)) {
 ballDirectionX = random(-5,5);
 ballDirectionY = -ballDirectionY;
}

if (inPaddle(ballX, ballY, paddle2X, paddle2Y, 20, 5)) {
  ballDirectionX = random(-5,5);
  ballDirectionY = -ballDirectionY;
}

// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;

// erase the ball's previous position
TFTscreen.fill(50, 50, 50);
if (oldBallX != ballX || oldBallY != ballY) {
  TFTscreen.rect(oldBallX, oldBallY, 5, 5);
}

// draw the ball's current position
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(ballX, ballY, 5, 5);

oldBallX = ballX;
oldBallY = ballY;
}

// checks the position of the ball, intersects with the paddles
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
  boolean result = false;
  
  if ((x >= rectX && x <= (rectX + rectWidth)) &&
  (y >= rectY && y <= (rectY + rectHeight))) {
    result = true;
  }
 return result;
}

A simple scoring System - PSEUDO

int score1 = 0;
int score2 = 0;

String winner = "none";

void resetScore(){
score1  = 0;
score 2 = 0;
winner = "none";
}

void addToScore(int score){
score++;
}

void checkScore(){
if(score1 >= 5){
winner = "Player1";
}else if(score2 >= 5){
winner = "Player2";
}

void loop(){


if(winner == "none"){ // While no one has won

checkScore(); // Check the scores

if(condition is met){ // If the first player scores
addToScore(score1);
}

if(2nd condition is met){// If the second player scores
addToScore(score2);
}

}else{
delay(5000); // wait 5 seconds 
resetScore(); // reset game
}

}

Using methods was unnecessary but whatever.

void addToScore(int score){

Did you mean to use a reference there?

What reference?

The one for "score"

Jessie645:
hello,
I am using c++ language and this is my code so far: it would be great if someone can help and guide me to create some sort of a scoring system.

#include <TFT.h> // Arduino LCD library

#include <SPI.h>

// pin definition for the Uno
#define cs 10
#define dc 9
#define rst 8

TFT TFTscreen = TFT(cs, dc, rst);

// variables for the position of the ball and paddle
int paddle1X = 0;
int paddle1Y = 0;
int oldPaddle1X, oldPaddle1Y;
int paddle2X = 0;
int paddle2Y = TFTscreen.height()-10/2;
int oldPaddle2X, oldPaddle2Y;
int ballDirectionX = 1;
int ballDirectionY = 1;

int ballSpeed = 20; //lower the number the faster the ball moves

int ballX, ballY, oldBallX, oldBallY;

void setup() {
  TFTscreen.begin(); //initialize the display
  TFTscreen.background(50, 50, 50); //black background
}

void loop() {
  //width and height of the screen
  int myWidth = TFTscreen.width()*2;
  int myHeight = TFTscreen.height();

// location of the paddles
  paddle1X = map(analogRead(A0), 1024, -1024, 0, myWidth) - 20 / 2;
  paddle1Y == -5 / 2;
  paddle2X = map(analogRead(A1), 1024, -1024, 0, myWidth) - 20 / 2;
  paddle2Y == TFTscreen.height()- - 5 / 2;

//fill colour black and earase the previous and position of paddle if different  from present
  TFTscreen.fill(50, 50, 50);
 
  if (oldPaddle1X != paddle1X || oldPaddle1Y != paddle1Y) {
    TFTscreen.rect(oldPaddle1X, oldPaddle1Y, 20, 5);
  }
  if (oldPaddle2X != paddle2X || oldPaddle2Y != paddle2Y) {
    TFTscreen.rect(oldPaddle2X, oldPaddle2Y, 20, 5);
  }
 
  // draw paddle on screen and save the current position
  TFTscreen.fill(255, 255, 255);
 
  TFTscreen.rect(paddle2X, paddle2Y, 20, 5);
  oldPaddle2X = paddle2X;
  oldPaddle2Y = paddle2Y;
 
  TFTscreen.rect(paddle1X, paddle1Y, 20, 5);
  oldPaddle1X = paddle1X;
  oldPaddle1Y = paddle1Y;
 
  // update the balls's position and draw it on screen
  if (millis() % ballSpeed <2) {
    moveBall();
  }
}

//reset game when ball drops, so give the ball a random direction
void reset()
{
  ballX = 50;
  ballY = 50;
  ballDirectionX = random(-5,5);
  ballDirectionY = 1;
  if (ballDirectionX == 0 ) {
    ballDirectionX == -5;
    }
    else{
      ballDirectionX = 1;
    }
    ballDirectionY = random(0,1);
    if (ballDirectionY == 0 ) {
      ballDirectionY = -1;
    }
    else
    {
      ballDirectionY = 1;
    }
    ballSpeed = 20;
    delay(1000);
}

//determines the ball's position on screen
void moveBall() {
// if the ball goes offscreen reset the game
  if (ballX > TFTscreen.width() || ballX < 0) {
    ballDirectionX = -ballDirectionX;
  }
 
  if (ballY > TFTscreen.height() || ballY < 0) {
    reset();
  }
 
//check the ball and paddle position.
if (inPaddle(ballX, ballY, paddle1X, paddle1Y, 20, 5)) {
ballDirectionX = random(-5,5);
ballDirectionY = -ballDirectionY;
}

if (inPaddle(ballX, ballY, paddle2X, paddle2Y, 20, 5)) {
  ballDirectionX = random(-5,5);
  ballDirectionY = -ballDirectionY;
}

// update the ball's position
ballX += ballDirectionX;
ballY += ballDirectionY;

// erase the ball's previous position
TFTscreen.fill(50, 50, 50);
if (oldBallX != ballX || oldBallY != ballY) {
  TFTscreen.rect(oldBallX, oldBallY, 5, 5);
}

// draw the ball's current position
TFTscreen.fill(255, 255, 255);
TFTscreen.rect(ballX, ballY, 5, 5);

oldBallX = ballX;
oldBallY = ballY;
}

// checks the position of the ball, intersects with the paddles
boolean inPaddle(int x, int y, int rectX, int rectY, int rectWidth, int rectHeight) {
  boolean result = false;
 
  if ((x >= rectX && x <= (rectX + rectWidth)) &&
  (y >= rectY && y <= (rectY + rectHeight))) {
    result = true;
  }
return result;
}

If you know enough to write all that, then you know enough to count points.
Or do you mean display the number of points?
And you didn't actually write all that code, did you? How much of it did you just copy verbatim from the Internet, and how much (if any) of it did you write? How much of it do you understand?