Jump to content

User:MartinS163

From Wikipedia, the free encyclopedia

Martin Ivanov student in Glasgow Caledonian , student number is S1630109 made a simple pong game for his programming classes.

The game is just plain code it doesn't have graphics and is created using Visual Studio 2017 with the C++ coding language .

The code used in this game is as follows:

#include <iostream>

#include <time.h>

#include <conio.h>

#include <SDL_image.h>

//includes the header files that i had to use in the code.

//Adds the directions in which the ball can move around the window.

using namespace std;

enum eDir { STOP = 0, LEFT = 1, UPLEFT = 2, DOWNLEFT = 3, RIGHT = 4, UPRIGHT = 5, DOWNRIGHT = 6 };

//creates a class called cball that can move in the random directions shown above

class cBall

{

//creates a private class for the ball than can only be called in this main.cpp file

private:

int x, y;

int originalX, originalY;

eDir direction;

//the public part can be called in every code not only in the main.cpp file

public:

cBall(int posX, int posY)

{

originalX = posX;

originalY = posY;

x = posX; y = posY;

direction = STOP;

}

//returns the ball to the center of the screeen at the start of the game as well as after scoring a point

void Reset()

{

x = originalX; y = originalY;

direction = STOP;

}

void changeDirection(eDir d)

{

direction = d;

}

//randomises the direction of the ball

void randomDirection()

{

direction = (eDir)((rand() % 6) + 1);

}

inline int getX() { return x; }

inline int getY() { return y; }

inline eDir getDirection() { return direction; }

void Move()

{

switch (direction)

//after the hit depending on which paddle part the ball was hit , descibes the direction of the ball on the x and y 

{

case STOP:

break;

case LEFT:

x--;

break;

case RIGHT:

x++;

break;

case UPLEFT:

x--; y--;

break;

case DOWNLEFT:

x--; y++;

break;

case UPRIGHT:

x++; y--;

break;

case DOWNRIGHT:

x++; y++;

break;

default:

break;

}

}

friend ostream & operator<<(ostream & o, cBall c)

{

o << "Ball [" << c.x << "," << c.y << "][" << c.direction << "]";

return o;

}

};

//creates a paddle class with which the player can hit the ball in the game

class cPaddle

{

private:

int x, y;

int originalX, originalY;

public:

cPaddle()

{

x = y = 0;

}

//creates the paddles for both sides left and right and clreates them in the center of the 2 sides

cPaddle(int posX, int posY) : cPaddle()

{

originalX = posX;

originalY = posY;

x = posX;

y = posY;

}

//after a point resets the paddle position and returns it to the startin position

inline void Reset() { x = originalX; y = originalY; }

inline int getX() { return x; }

inline int getY() { return y; }

inline void moveUp() { y--; }

inline void moveDown() { y++; }

friend ostream & operator<<(ostream & o, cPaddle c)

{

o << "Paddle [" << c.x << "," << c.y << "]";

return o;

}

};

class cGameManger

//the game manager class manages all the functions in the games as the height width score etc

{

//adds the score for both players , width and height as well as the movement for both paddles adn creates the paddles for both sides as well as the ball

private:

int width, height;

int score1, score2;

char up1, down1, up2, down2;

bool quit;

cBall * ball;

cPaddle *player1;

cPaddle *player2;

public:

cGameManger(int w, int h)

//add the sizes of the ball and the 2 paddles and also declares which with which keys to move the paddles up and down,also declares where the ball an paddles will be placed

{

srand(time(NULL));

quit = false;

up1 = 'w'; up2 = 'i';

down1 = 's'; down2 = 'k';

score1 = score2 = 0;

width = w; height = h;

ball = new cBall(w / 2, h / 2);

player1 = new cPaddle(1, h / 2 - 3);

player2 = new cPaddle(w - 2, h / 2 - 3);

}

~cGameManger()

{

delete ball, player1, player2;

}

//adds a point to the player that has scored a point after scoring such 

void ScoreUp(cPaddle * player)

{

if (player == player1)

score1++;

else if (player == player2)

score2++;

//after the point of either player resets the position of the 2 paddles and the ball in their starting positions

ball->Reset();

player1->Reset();

player2->Reset();

}

//creates the draw function for the game trought the console

void Draw()

{

system("cls");

//prints the walls of the game

for (int i = 0; i < width + 2; i++)

cout << "\xB2";

cout << endl;

for (int i = 0; i < height; i++)

{

for (int j = 0; j < width; j++)

{

int ballx = ball->getX();

int bally = ball->getY();

int player1x = player1->getX();

int player2x = player2->getX();

int player1y = player1->getY();

int player2y = player2->getY();

if (j == 0)

cout << "\xB2";

if (ballx == j && bally == i)

cout << "O"; //ball

else if (player1x == j && player1y == i)

cout << "\xDB"; //player1

else if (player2x == j && player2y == i)

cout << "\xDB"; //player2

else if (player1x == j && player1y + 1 == i)

cout << "\xDB"; //player1

else if (player1x == j && player1y + 2 == i)

cout << "\xDB"; //player1

else if (player1x == j && player1y + 3 == i)

cout << "\xDB"; //player1

else if (player2x == j && player2y + 1 == i)

cout << "\xDB"; //player1

else if (player2x == j && player2y + 2 == i)

cout << "\xDB"; //player1

else if (player2x == j && player2y + 3 == i)

cout << "\xDB"; //player1

else

cout << " ";

if (j == width - 1)

cout << "\xB2";

}

cout << endl;

}

for (int i = 0; i < width + 2; i++)

cout << "\xB2";

cout << endl;

cout << "Score 1: " << score1 << endl << "Score 2: " << score2 << endl;

}

void Input()

//movement in the game of the paddles depending on the input of the key that was declared above 

{

ball->Move();

int ballx = ball->getX();

int bally = ball->getY();

int player1x = player1->getX();

int player2x = player2->getX();

int player1y = player1->getY();

int player2y = player2->getY();

if (_kbhit())

{

char current = _getch();

if (current == up1)

if (player1y > 0)

player1->moveUp();

if (current == up2)

if (player2y > 0)

player2->moveUp();

if (current == down1)

if (player1y + 4 < height)

player1->moveDown();

if (current == down2)

if (player2y + 4 < height)

player2->moveDown();

if (ball->getDirection() == STOP)

ball->randomDirection();

if (current == 'q')

quit = true;

}

}

void Logic()

//creats the physics of the game when the ball hits a paddle or a wall

{

int ballx = ball->getX();

int bally = ball->getY();

int player1x = player1->getX();

int player2x = player2->getX();

int player1y = player1->getY();

int player2y = player2->getY();

//left paddle

for (int i = 0; i < 4; i++)

if (ballx == player1x + 1)

if (bally == player1y + i)

ball->changeDirection((eDir)((rand() % 3) + 4));

//right paddle

for (int i = 0; i < 4; i++)

if (ballx == player2x - 1)

if (bally == player2y + i)

ball->changeDirection((eDir)((rand() % 3) + 1));

//bottom wall

if (bally == height - 1)

ball->changeDirection(ball->getDirection() == DOWNRIGHT ? UPRIGHT : UPLEFT);

//top wall

if (bally == 0)

ball->changeDirection(ball->getDirection() == UPRIGHT ? DOWNRIGHT : DOWNLEFT);

//right wall

if (ballx == width - 1)

ScoreUp(player1);

//left wall

if (ballx == 0)

ScoreUp(player2);

}

void Run()

{

while (!quit)

{

Draw();

Input();

Logic();

}

}

};

int main()

{

//the width and lenght of the walls

cGameManger c(40, 20);

c.Run();

return 0;

}

I confirm that the code contained in this file (other than that provided or authorised) is all my own work and has not been submitted elsewhere in fulfilment of this or any other award.

Martin Ivanov