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?