d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > [C++] Pong Project > Ascii Art Game
1239Next
Add Reply New Topic New Poll
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 21 2009 02:29pm
Hello! I think somebody might get a kick out of seeing Pong done in C++ via ASCII art (I'm a personal fan of ASCII art games so... ha!). :)
PS: I understand it was poorly constructed, I was trying to use an Object-Oriented paradigm, and failed miserably!
PROJECT NOTES: This project was created, compiled, ran, and tested on Windows XP: Home Edition Service Pack 2. The compiler used with Dev-C++ and now Code::Blocks.

Here's a screen shot of the game!


Code
#include <iostream>
#include <TextControl.h>
using namespace std;

#define TOP_WALL 4
#define BOTTOM_WALL 20

class cBall {

   private:
       int iForeground; /* Ball's foreground color */
       int iBackground; /* Ball's Background color */

       int iBall_X; /* Ball's X coordinate */
       int iBall_Y; /* Ball's Y coordinate */

       int iVelocity_X; /* Ball's X velocity (speed) */
       int iVelocity_Y; /* Ball's Y velocity (speed) */

       SMALL_RECT smWalls;

   public:
       cBall() { };

       void ResetBall(const int X, const int Y) {
           PlaceCursor(iBall_X, iBall_Y);
           printf(" ");

           iBall_X = X;
           iBall_Y = Y;
           iVelocity_X = 1;
           iVelocity_Y = 1;
       }

       void UpdateBall() {

           /* Erase the old ball */
           PlaceCursor(iBall_X, iBall_Y);
           printf(" ");

           /* Bounce if the ball hits a wall */
           if (iBall_X > smWalls.Right || iBall_X < smWalls.Top) { iVelocity_X = -iVelocity_X; }
           if (iBall_Y > smWalls.Bottom || iBall_Y < smWalls.Top) { iVelocity_Y = -iVelocity_Y; }

           /* Update the position */
           iBall_X += iVelocity_X;
           iBall_Y += iVelocity_Y;

           return;
       }

       void DrawBall() {

           /* Drew the ball at the new position */
           PlaceCursor(iBall_X, iBall_Y);
           SetColor(iForeground, iBackground);
           printf("ù");

           return;
       }

       void ReverseBall() { if (rand() % 9) { iVelocity_X = -iVelocity_X; } else { iVelocity_X = (-iVelocity_X + 1); } return; }

       int GetXVelocity() { return iVelocity_X; }
       int GetYVelocity() { return iVelocity_Y; }
       int GetXBall() { return iBall_X; }
       int GetYBall() { return iBall_Y; }

       void SetForeground(const int Color) { iForeground = Color; return; }
       void SetBackground(const int Color) { iBackground = Color; return; }
       void SetXBall(const int X) { iBall_X = X; return; }
       void SetYBall(const int Y) { iBall_Y = Y; return; }
       void SetXVelocity(const int Velocity) { iVelocity_X = Velocity; return; }
       void SetYVelocity(const int Velocity) { iVelocity_Y = Velocity; return; }
       void SetLeftWall(const int Wall) { smWalls.Left = Wall; return; }
       void SetRightWall(const int Wall) { smWalls.Right = Wall; return; }
       void SetTopWall(const int Wall) { smWalls.Top = Wall; return; }
       void SetBottomWall(const int Wall) { smWalls.Bottom = Wall; return; }
};

class cPaddle {

   private:
       int iForeground; /* Paddle's foreground color */
       int iBackground; /* Paddle's Background color */

       int iVelocity_Y; /* Paddle's Y velocity (speed) */
       int iPaddle_Y;   /* Paddle's Y coordinate */
       int iPaddle_X;   /* Paddle's X coordinate */

       int iLives;      /* Players lives left */
       
       int iPaddleUp;   /* The key to move the Paddle up */
       int iPaddleDown; /* The key to move the Paddle down */

       bool bComputer;

   public:
       cPaddle() { bComputer = false; iLives = 4; };

       void UpdatePaddle() {

           if (!bComputer) {
               if (GetAsyncKeyState(iPaddleDown) && iPaddle_Y < BOTTOM_WALL) {
                   /* Erase the old Paddle */
                   PlaceCursor(iPaddle_X, iPaddle_Y);
                   printf(" ");
                   PlaceCursor(iPaddle_X, iPaddle_Y + 1);
                   printf(" ");

                   iPaddle_Y += 1;
               }

               if (GetAsyncKeyState(iPaddleUp) && iPaddle_Y > TOP_WALL) {
                   /* Erase the old Paddle */
                   PlaceCursor(iPaddle_X, iPaddle_Y);
                   printf(" ");
                   PlaceCursor(iPaddle_X, iPaddle_Y + 1);
                   printf(" ");

                   iPaddle_Y -= 1;
               }
           }

           return;
       }

       void DrawPaddle() {

           /* Drew the Paddle at the new position */
           PlaceCursor(iPaddle_X, iPaddle_Y);
           SetColor(iForeground, iBackground);
           printf("Û");
           PlaceCursor(iPaddle_X, iPaddle_Y + 1);
           SetColor(iForeground, iBackground);
           printf("Û");

           return;
       }

       void ReduceLife() { iLives--; }
       void SetPaddleType(bool Type) { bComputer = Type; }

       int GetXPaddle() { return iPaddle_X; }
       int GetYPaddle() { return iPaddle_Y; }
       int GetLives() { return iLives; }

       void SetXPaddle(int X) { iPaddle_X = X; }
       void SetYPaddle(int Y) { iPaddle_Y = Y; }
       void SetUpArrow(const int Up) { iPaddleUp = Up; }
       void SetDownArrow(const int Down) { iPaddleDown = Down; }
       void SetForeground(int Color) { iForeground = Color; }
       void SetBackground(int Color) { iBackground = Color; }
};

class cController {

   public:
       int iInitialBallX; /* A copy of the Balls original X position */
       int iInitialBallY; /* A copy of the Balls original Y position */

       cBall Ball;       /* The game's ball */
       cPaddle Player;   /* The players paddle */
       cPaddle Computer; /* The computers paddle */

       void UpdatePositions(const int X, const int Y) {

           PlaceCursor(X, Y);
           SetColor(WHITE, BLACK);
           printf("Ball X: %i  ", Ball.GetXBall());
           PlaceCursor(X, Y + 1);
           printf("Ball Y: %i  ", Ball.GetYBall());
           PlaceCursor(X, Y + 2);
           printf("Ball X Velocity: %i  ", Ball.GetXVelocity());
           PlaceCursor(X, Y + 3);
           printf("Ball Y Velocity: %i  ", Ball.GetYVelocity());

           PlaceCursor(X, Y + 4);
           printf("Player X: %i  ", Player.GetXPaddle());
           PlaceCursor(X, Y + 5);
           printf("Player Y: %i  ", Player.GetYPaddle());
           PlaceCursor(X, Y + 6);
           printf("Player Lives: %i  ", Player.GetLives());

           PlaceCursor(X, Y + 7);
           printf("Computer X: %i  ", Computer.GetXPaddle());
           PlaceCursor(X, Y + 8);
           printf("Computer Y: %i  ", Computer.GetYPaddle());
           PlaceCursor(X, Y + 9);
           printf("Computer Lives: %i  ", Computer.GetLives());

           return;
       }

       void CheckHit() {

           /* Ball hits player's (top half) paddle */
           if (Ball.GetXBall() == Player.GetXPaddle() - 1 && Ball.GetYBall() == Player.GetYPaddle()) { Ball.ReverseBall(); }

           /* Ball hits player's (bottom half) paddle */
           else if (Ball.GetXBall() == Player.GetXPaddle() - 1 && Ball.GetYBall() == Player.GetYPaddle() + 1) { Ball.ReverseBall(); }

           /* Ball hits computer's paddle */
           if (Ball.GetXBall() == Computer.GetXPaddle() + 1 && Ball.GetYBall() == Computer.GetYPaddle()) { Ball.ReverseBall(); }

           /* Ball hits computer's (bottom half) paddle */
           else if (Ball.GetXBall() == Computer.GetXPaddle() + 1 && Ball.GetYBall() == Computer.GetYPaddle()) { Ball.ReverseBall(); }

           /* Computer lost */
           if (Ball.GetXBall() < Computer.GetXPaddle()) {
               Ball.ResetBall(iInitialBallX, iInitialBallY);
               Computer.ReduceLife();
               if (Computer.GetLives() == 0) { printf("COMPUTER OUT!"); cin.get(); exit(0); }
           }

           /* Player lost */
           if (Ball.GetXBall() > Player.GetXPaddle()) {
               Ball.ResetBall(iInitialBallX, iInitialBallY);
               Player.ReduceLife();
               if (Player.GetLives() == 0) { printf("PLAYER OUT!"); cin.get(); exit(0); }
           }

           return;
       }

       void AI(cPaddle& Paddle) {

           /* Move paddle down */
           if (Paddle.GetYPaddle() < Ball.GetYBall() && Paddle.GetYPaddle() < BOTTOM_WALL) {
               PlaceCursor(Paddle.GetXPaddle(), Paddle.GetYPaddle());
               printf(" ");
               Paddle.SetYPaddle(Paddle.GetYPaddle() + 1);
           }

           /* Move paddle up */
           if (Paddle.GetYPaddle() > Ball.GetYBall() && Paddle.GetYPaddle() > TOP_WALL) {
               PlaceCursor(Paddle.GetXPaddle(), Paddle.GetYPaddle() + 1);
               printf(" ");
               Paddle.SetYPaddle(Paddle.GetYPaddle() - 1);
           }

           return;
       }
};

int main() {

   HANDLE OutHandle = GetStdHandle(STD_OUTPUT_HANDLE);

   SetConsoleTitle("Pong");
   SMALL_RECT windowSize = {0, 0, 79, 49};
   SetConsoleWindowInfo(OutHandle, TRUE, &windowSize);

   RemoveCursor();
   ClearConsole(BLACK, BLACK);

   cController Game;

   Game.Player.SetForeground(WHITE);
   Game.Player.SetBackground(BLACK);
   Game.Player.SetXPaddle(70);
   Game.Player.SetYPaddle(15);
   Game.Player.SetUpArrow(VK_UP);
   Game.Player.SetDownArrow(VK_DOWN);
   Game.Player.SetPaddleType(false);

   Game.Computer.SetForeground(WHITE);
   Game.Computer.SetBackground(BLACK);
   Game.Computer.SetXPaddle(10);
   Game.Computer.SetYPaddle(15);
   Game.Computer.SetPaddleType(true);

   Game.Ball.SetXVelocity(1);
   Game.Ball.SetYVelocity(1);
   Game.Ball.SetXBall(25);
   Game.Ball.SetYBall(5);
   Game.iInitialBallX = 25;
   Game.iInitialBallY = 5;
   Game.Ball.SetForeground(WHITE);
   Game.Ball.SetBackground(BLACK);
   Game.Ball.SetTopWall(5);
   Game.Ball.SetBottomWall(20);
   Game.Ball.SetLeftWall(20);
   Game.Ball.SetRightWall(72);

   SetColor(WHITE, BLACK);
   DrawBox(65, 20, 8, 3);

   while (true) {
       Game.Ball.UpdateBall();
       Game.Ball.DrawBall();

       //Game.AI(Game.Player);
       Game.Player.UpdatePaddle();
       Game.Player.DrawPaddle();

       Game.AI(Game.Computer);
       Game.Computer.UpdatePaddle();
       Game.Computer.DrawPaddle();

       Game.CheckHit();
       Game.UpdatePositions(0, 25);
       Sleep(50);
   }

   return 0;
}


Again! Hope somebody gets a kick out of this. :D

This post was edited by Muted on Jun 21 2009 02:32pm
Banned
Posts: 2,806
Joined: Apr 17 2009
Gold: 0.00
Warn: 20%
Jun 21 2009 04:20pm
i was trying to make a curveball type game in java a couple of weeks ago
http://www.albinoblacksheep.com/flash/curveball

but the collision dynamics escape me

i.e.

how does the ball exactly change direction when it hits walls?
what would i do about corners?
would i have to create some sort of artificial vector?
does the ball change direction depending on which area of the paddle i hit it with?
if not, what actually causes the ball to change direction?

i can come up with at least a page of these types of questions


maybe i should have started with pong too

This post was edited by Dr_Grignard71 on Jun 21 2009 04:31pm
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 21 2009 04:32pm
Curveball? I think I saw (a few months ago!) the source-code for something like that in C++. Pong isn't entirely as simple as you may think at first... I went through two or three routines before I came up with this one to reverse the ball's direction. Before this, it was static (quite boring). I'm sorry you couldn't get your Curveball thingy to work in Java... You really have to put thought into it. It's really fun too, and SUPER rewarding when it all pans out. :)

- The ball could randomly change directions when hitting a wall
- Corners? It hits one wall, changes directions at random; If it hits the 2nd wall (in the one corner) randomize again until it escapes! :)
- Java doesn't have "vectors" like C++'s STL? (I don't use Java... sorry, not all that familiar!)
- That I didn't do, but I thought about... I think it should matter. If it hits the top, it should have a higher chance of flying in a certain direction. Same for the middle/bottom.

I like answering questions! :) And yes, perhaps you should have started with Pong first! Then work you way from there...! Pong's a really fun demonstration.

Happy codin', Dr_Grignard71!
Banned
Posts: 2,806
Joined: Apr 17 2009
Gold: 0.00
Warn: 20%
Jun 21 2009 04:36pm
i posted the link to the game

vectors as in the physics sense(for the ball, since the game is going to be 3d)

not in the programming sense, although i think the equivalent of the C++ vector is the Java ArrayList


i am going to revert back to C++ though. I learned the java GUI first, so that's why I was using it.

This post was edited by Dr_Grignard71 on Jun 21 2009 04:36pm
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 21 2009 04:59pm
Oh, ok! I remember watching a video of AutoIt playing that game. It was pretty interesting. Pixel detection's neat like that, but that's a whole other discussion via programming (GetPixel in C++). Lol! Anyhow! A bit off topic...
I just got 17k (level 6?) points from that game real quick before suiciding. I could play it all day and never lose. It's so simple, but it's still a fun game, and trying to duplicate it, boy that'd be so much fun!

If you're going to "revert back to C++," try looking into these APIs after you learn C++: SDL, Allegro, DirectX, and OpenGL, also a few others but I won't name them. :)

Good luck on duplicating it, Dr_Grinard71. If you ever have any questions about C++, feel free to post them (not PM! PM's only help you nobody else) and I'll try my best to answer them.

This post was edited by Muted on Jun 21 2009 04:59pm
Member
Posts: 2,065
Joined: Feb 21 2008
Gold: 0.02
Warn: 10%
Jun 21 2009 05:40pm
I believe you meant for the name of this thread to be Pong Project. It's the same name and description as your Snake Project thread.
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Jun 21 2009 05:48pm
Quote (xRainx @ Sun, 21 Jun 2009, 18:40)
I believe you meant for the name of this thread to be Pong Project. It's the same name and description as your Snake Project thread.


Yes, I'm aware. I PM'd a Jr. Moderator the moment I made it but, I don't know if they're able to change the topic name or not. I don't know why I was being such an air-head when I made it. :(
Sorry if it's confusing or anything.
Member
Posts: 2,065
Joined: Feb 21 2008
Gold: 0.02
Warn: 10%
Jun 21 2009 07:00pm
Quote (Muted @ Sun, Jun 21 2009, 05:48pm)
Yes, I'm aware. I PM'd a Jr. Moderator the moment I made it but, I don't know if they're able to change the topic name or not. I don't know why I was being such an air-head when I made it. :(
Sorry if it's confusing or anything.


Not that confusing. I can't count the times I've screwed up a thread at one time or another.
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Sep 20 2009 01:22am
Quote (xRainx @ Sun, 21 Jun 2009, 18:40)
I believe you meant for the name of this thread to be Pong Project. It's the same name and description as your Snake Project thread.


They apparently can change it, and have between now and then (100ish days ago?)... :)
Member
Posts: 11,637
Joined: Feb 2 2004
Gold: 434.84
Sep 28 2009 07:44am
So what object oriented stuff did you not get? I can probably help out.
Go Back To Programming & Development Topic List
1239Next
Add Reply New Topic New Poll