I'm gonna leave this here and prepare to be amazed.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
User avatar
Alacaster
Posts: 81
Joined: October 16th, 2016, 5:08 pm
Location: Spokane, WA

I'm gonna leave this here and prepare to be amazed.

Post by Alacaster » March 31st, 2017, 4:39 am

I am making a pong game.
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;}
    }
}
Have fun and thanks! :D :mrgreen:
You can't be betrayed if you don't have any friends.
Why live? Cause why not.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: I'm gonna leave this here and prepare to be amazed.

Post by albinopapa » March 31st, 2017, 8:25 am

Hmm, I feel you are fishing for something, but can't quite figure it out.

Honestly, to each their own when it comes to coding style, but YIKES! I want to format it differently so badly.

Ok, seriously, instead of having enums for ball direction, how about using int vx, vy? This way if ball hits, you swap the sign of the variable.

Code: Select all

If ball hits left, make vx positive 
else if ball hits right, make vx negative
If ball hits top, make vy positive
else if ball hits bottom, make vy negative
In this way, you just add vx to px and vy to py each time you update.

Classes:
What you have is fine if it works, but the idea of a class is separation of responsibilities. For instance, your ball class shouldn't be handling scores.

TIP:
If you really want one line conditionals like

Code: Select all

int getscore(int p){if(p == 0){return Leftscore;}else{return Rightscore;} }
Try this

Code: Select all

int getscore( int p ){ return ( p == 0 ) ? Leftscore : Rightscore; }
The ?: operator is pretty useful for if/else assignments without all the clutter. Just don't try nesting them, it can be done, but it gets confusing quick.

Code: Select all

int ClampValue( int MinValue, int MaxValue, int CurrentValue )
{
     return ( CurrentValue >= MinValue ) ?
          ( CurrentValue < MaxValue ) ? CurrentValue : MaxValue : MinValue;
}
Try to stick with one convention when coding:

Code: Select all

    bDir Direction;
    int ypos = 0;
    int xpos = 0;
    const int xOr = 0;
    const int yOr = 0;
    int Leftscore = 0;
    int Rightscore = 0;
Here you have Direction, Leftscore and Rightscore capitalized, but the rest aren't. You also kind of use camelCase on some like xOr, yOr and bDir, but not on the scores or positions.

Code: Select all

// I think this should be an OR ( || ) condition check
             if(game.getX() > 49 || game.getX() < -49){
                game.ballhhit(game.getdirect());game.getpoints();}

            if(game.getY() > 49 || game.getY() < -49){
                game.ballhhit(game.getdirect());}

// I think this would be a better fit
if( game.getX() > 49 || game.getX() < -49 ||
    game.getY() > 49 || game.getY() < -49 )
{
     game.ballhhit( game.getdirect() ); 
}
I really don't see where the issue would lie. Honestly, I'm not entirely sure what you mean by the ball gets stuck in a loop.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: I'm gonna leave this here and prepare to be amazed.

Post by chili » March 31st, 2017, 9:44 am

Yeah, keeping the score in the ball class is not the true path forward broseph. Also, like papa says, storing the velocities as actual numbers will allow for more elegant and straight-forward manipulation.
Chili

Post Reply