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.
This post was edited by Muted on Jun 21 2009 02:32pm