so far the ball bounces around in a 100x100 box but gets stuck in a loop on the x coordinates which continually gives the second player (or any player once i implement srand(time(NULL))) but I have tried many things like different logic gates and various ways of forcing the ball out of the loop but I keep coming up short. Excuse me for not programming paddles or a GUI yet I just started on the game mechanics this night. For paddles I am Going to make the Hit box smaller and allow it to move with an AI for now. and there is a major issue with game recursion where you can only play one round. I cannot really describe it so just play around with ending the game inputting y's and n's and you'll get it. Im not too concerned with graphics it was just me learning how to use classes my spare time. (and enumerators)
here's the code!
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
using namespace std;
enum bDir {STOP,LEFT,UPLEFT,DOWNLEFT,RIGHT,UPRIGHT,DOWNRIGHT};
class Ball
{
private:
bDir Direction;
int ypos = 0;
int xpos = 0;
const int xOr = 0;
const int yOr = 0;
int Leftscore = 0;
int Rightscore = 0;
public:
Ball()
{
Direction = STOP;
}
void reset(){ xpos = xOr; ypos = yOr;
Direction = STOP;
Leftscore=0;Rightscore=0;}
void ballstart(){Direction = (bDir)((rand()%6)+1);}
void ballhhit(bDir direc)
{
if(Direction == LEFT || Direction == UPLEFT || Direction == DOWNLEFT)
{Direction = (bDir)((rand()%3)+4);} else{
{Direction = (bDir)((rand()%3)+1);}
}
}
inline int getX() {return xpos;}
inline int getY() {return ypos;}
inline bDir getdirect() { return Direction; }
void ballmove()
{
if(Direction == LEFT){xpos--;}else
if(Direction == RIGHT){xpos++;}else
if(Direction == UPLEFT){xpos--; ypos++;}else
if(Direction == DOWNLEFT){xpos--; ypos--;}else
if(Direction == UPRIGHT){xpos++; ypos++;}else
if(Direction == DOWNRIGHT){xpos++; ypos--;}
}
void getpoints()
{
if(xpos < 0){Leftscore++;}else{Rightscore++;}
}
int getscore(int p){if(p == 0){return Leftscore;}else{return Rightscore;} }
void Force_Edit_Position(){
if(xpos > 49){xpos=-2;}
if(xpos < -49){xpos=+2;}
if(ypos > 49){ypos=-2;}
if(ypos < -49){ypos=+2;}
}
};
int main()
{
bool gameend = false;
Ball game;
for(int i = 0;i<=10;i++){
char play = 'Y';
cout << "Would you like to play pong? (Y)es (N)o" << endl;
scanf("%c", &play);
if(play == 'N' || play == 'n')
{
return 0;
}
game.ballstart();
do{
cout << "X position: " << game.getX() << endl;
cout << "Y position: " << game.getY() << endl << endl;
cout << "Player 1 Score: " << game.getscore(0) << endl;
cout << "Player 2 Score: " << game.getscore(1) << endl << endl;
system("pause");
system("cls");
game.ballmove();
if(game.getX() > 49 || game.getX() < -49){
game.ballhhit(game.getdirect());game.getpoints();}
if(game.getY() > 49 || game.getY() < -49){
game.ballhhit(game.getdirect());}
if(game.getscore(0)>=10 || game.getscore(1)>=10)
{
gameend = true;
}
}while(gameend == false);
game.reset();
{gameend == false;}
}
}