I made this pong game in allegro c++. It compiles and works well for my standards. I’m posting so all the great minds here can judge it and tell me what I did wrong and what can make it better.
EDITED:: Right now I’m trying to do this.
How can I make the ball bounce off the paddle with a more realistic feeling? For example I’m doing it with rand, shouldn’t the ball be affected by the direction the paddle was moving before it hit it?
Thank you for your time.
Main.cpp
#include "Pong.h"
int main()
{
allegro_init();
install_keyboard();
Pong P;
P.setGame();
return 0;
}
END_OF_MAIN()
Pong.h
#ifndef H_Pong
#define H_Pong
#include <allegro.h>
#include <cstdlib>
#include <time.h>
class Pong
{
public:
BITMAP *buffer;
time_t secs;
int ball_x;
int ball_y;
int ball_tempX;
int ball_tempY;
int AI_x;
int AI_y;
int AI_tempX;
int AI_tempY;
int p2_x;
int p2_y;
int p2_tempX;
int p2_tempY;
int speed; // for ball speed
int hits;
int AIScore;
int p2Score;
// Keeps track of circles direction
// 1 = up and left, 2 = down and left, 3 = up and right, 4 = down and right
int dir;
void setGame();
void startNew();
void AIMove(); // Player 1
void p2Move(); // Player 2
void moveBall(); // Controls ball
void checkWinner(); // Checks for winner and points scored
void menu(); // Updates score and hits
// Constructor
Pong();
};
#endif
Pong.cpp
#include "Pong.h"
void Pong::setGame()
{
ball_x = 400;
ball_y = 240;
ball_tempX = 400;
ball_tempY = 240;
AI_x = 31;
AI_y = 255;
AI_tempX = 31;
AI_tempY = 255;
p2_x = 753;
p2_y = 255;
p2_tempX = 753;
p2_tempY = 255;
speed = 1;
hits = 0;
AIScore = 0;
p2Score = 0;
set_color_depth(16);
set_gfx_mode( GFX_AUTODETECT, 800, 600, 0, 0);
buffer = create_bitmap( 800, 600 );
acquire_screen();
// background
clear_to_color ( buffer, makecol( 197, 219, 191 ));
draw_sprite ( screen, buffer, 0, 0 );
release_screen();
time(&secs);
srand( (unsigned int)secs);
dir = rand() % 4 + 1;
// Check for key press to exit
while( !key[KEY_ESC])
{
AIMove();
p2Move();
moveBall();
checkWinner();
}
}
void Pong::startNew()
{
// comment this later
clear_keybuf();
readkey();
// background
clear_to_color ( buffer, makecol ( 197, 219, 191 ));
// resets ball back to the middle
ball_x = 400;
ball_y = 240;
// resets paddles after victory.
AI_x = 31;
AI_y = 255;
p2_x = 753;
p2_y = 255;
speed = 1;
hits = 0;
}
// Controls the ball
void Pong::moveBall()
{
ball_tempX = ball_x;
ball_tempY = ball_y;
// Hits test for ball speed
if (hits >= 30)
{
speed = 5;
}
else
if (hits >= 20)
{
speed = 4;
}
else
if (hits >= 10)
{
speed = 3;
}
else
if (hits >= 5)
{
speed = 2;
}
if (( dir == 1 ) && ( ball_x > 5 )&& ( ball_y > 5 ))
{
if (( ball_x == AI_x + 15 ) && ( ball_y >= AI_y ) && ( ball_y <= AI_y + 60 ))
{
dir = rand() % 2 + 3;
hits++;
}
else
{
ball_x -= speed;
ball_y -= speed;
}
}
else
if (( dir == 2 ) && ( ball_x > 5 ) && ( ball_y < 600 ))
{
if (( ball_x == AI_x + 15 ) && ( ball_y >= AI_y ) && ( ball_y <= AI_y + 60 ))
{
dir = rand()% 2 + 3;
hits++;
}
else
{
ball_x -= speed;
ball_y += speed;
}
}
else
if (( dir == 3 ) && ( ball_x < 800 ) && ( ball_y > 5 ))
{
if(( ball_x + 5 == p2_x ) && ( ball_y >= p2_y )&& ( ball_y <= p2_y + 60 ))
{
dir = rand()% 2 + 1;
hits++;
}
else
{
ball_x += speed;
ball_y -= speed;
}
}
else
if ((dir == 4 ) && ( ball_x < 800 ) && ( ball_y < 600 ))
{
if(( ball_x + 5 == p2_x ) && ( ball_y >= p2_y ) && ( ball_y <= p2_y + 60 ))
{
dir = rand()% 2 + 1;
hits++;
}
else
{
ball_x += speed;
ball_y += speed;
}
}
else
{
if (( dir == 1 ) || ( dir == 3 ))
{
++dir;
}
else
if (( dir == 2 ) || ( dir == 4 ))
{
--dir;
}
}
acquire_screen();
circlefill ( buffer, ball_tempX, ball_tempY, 5, makecol( 197, 219, 191 ));
circlefill ( buffer, ball_x, ball_y, 5, makecol( 22, 136, 11));
draw_sprite( screen, buffer, 0, 0);
release_screen();
// Updates menu
menu();
rest(5);
}
// Player 1
void Pong::AIMove()
{
AI_tempY = AI_y;
//if on cpu's side of court
if (( ball_x < 391 ) && ( AI_y > 0 ))
{
// 391?? i could gmake it 400 but
//this makes it m,ore natural
//attempt to position in the middle
if ( AI_y < 183 )
{
AI_y +=4;
}
if ( AI_y > 217 )
{
AI_y -= 4;
}
}
//if ball is close to player
if (( ball_x < 117 ) && ( AI_y < 540 ))
{
// attempt y-axis allignment
if ( AI_y < ball_y )
{
AI_y += 10;
}
if ( AI_y > ball_y )
{
AI_y -= 10;
}
}
acquire_screen();
rectfill( buffer, AI_tempX, AI_tempY, AI_tempX + 10, AI_tempY + 60, makecol ( 197, 219, 191 ));
rectfill( buffer, AI_x, AI_y, AI_x + 10, AI_y + 60, makecol ( 218, 173, 50 ));
release_screen();
}
// Player 2
void Pong::p2Move()
{
p2_tempY = p2_y;
if (( key[KEY_UP] ) && ( p2_y > 0 ))
{
//--p2_y;
p2_y -= 3;
}
else
if (( key[KEY_DOWN] ) && ( p2_y < 540 ))
{
//++p2_y;
p2_y += 3;
}
acquire_screen();
rectfill( buffer, p2_tempX, p2_tempY, p2_tempX + 10, p2_tempY + 60, makecol ( 197, 219, 191 ));
// actual player 2
rectfill( buffer, p2_x, p2_y, p2_x + 10, p2_y + 60, makecol ( 29, 98, 255));
release_screen();
}
// Checks for winner
void Pong::checkWinner()
{
if ( AIScore == 11 )
{
textprintf_centre_ex(screen, font, 400, 250, makecol(0,0,255),-1, "GAME OVER!!\n\n PLAYER 1 WINS!");
clear_keybuf();
readkey();
AIScore = 0;
}
else
if ( p2Score == 11 )
{
textprintf_centre_ex(screen, font, 400, 300, makecol(255,0,0),-1, "GAME OVER!!\n\n PLAYER 2 WINS!");
clear_keybuf();
readkey();
p2Score = 0;
}
// Checks for goal score
if ( ball_x > p2_x + 20 )
{
AIScore++;
startNew();
}
else
if ( ball_x < AI_x - 20 )
{
p2Score++;
startNew();
}
}
void Pong::menu()
{
textprintf_centre_ex(screen, font, 50, 10, makecol(218, 173, 50),-1, "Score: %d", AIScore);
textprintf_centre_ex(screen, font, 750, 10, makecol(29, 98, 255),-1, "Score: %d", p2Score);
textprintf_centre_ex(screen, font, 400, 10, makecol(22, 136, 11),-1, "Hits: %d", hits);
}
Pong::Pong()
{
}
[Edited by - neob on June 6, 2007 6:44:10 PM]