Ok fixed it ( kind of! ), only problem now is when you exit the game, it displays an error message telling me that the buffer is overloaded, but it works
I modified it a bit more, now the first player to 10 wins, I changed the player 2 controls to 'a' and 'd'.
// Dark GDK - The Game Creators - www.thegamecreators.com
// Pong!
// Written By Scarface
// My First Ever Program :)
#include "DarkGDK.h"
// Declare Global Vars // Do NOT Adjust These Values
const int BAT_SPEED = 10;
const int BALL_SPEED = 4;
const int BALL_SIZE = 5;
const int P1_BAT_POS_Y = 30;
const int P2_BAT_POS_Y = 435;
// Declare Struct
struct BallMovement
{
int movement_state;
int pos_x;
int pos_y;
int dir_x;
int dir_y;
};
struct BallMovement MoveBall( int ball_pos_x, int ball_pos_y, int ball_dir_x, int ball_dir_y, int game_state );
// Declare Functions
char* P1NameInput( );
char* P2NameInput( );
void DrawGameBoundries( );
void DisplayScores( char* p1_name$, char* p2_name$, int p1_score, int p2_score );
void DrawBall( int ball_pos_x, int ball_pos_y );
void DrawPlayer1Bat( int bat_pos_x );
void DrawPlayer2Bat( int bat_pos_x );
bool P1CollisionDetection( int bat_pos_x, int ball_pos_x, int ball_pos_y );
bool P2CollisionDetection( int bat_pos_x, int ball_pos_x, int ball_pos_y );
int DoPlayer1Movement( int bat_pos_x );
int DoPlayer2Movement( int bat_pos_x );
// the main entry point for the application is this function
void DarkGDK( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn( );
dbSyncRate( 60 );
// Load Media
dbLoadSound ( "D:\\Visual Studio 2008\\Projects\\Dark GDK - Game3\\pong_bounce.wav", 1 );
// Name Input
char p1_name$[255];
char p2_name$[255];
sprintf( p1_name$, P1NameInput( ) );
sprintf( p2_name$, P2NameInput( ) );
// Initilize Vars
int ball_movement_state = 0;
int p1_score = 0;
int p2_score = 0;
int p1_bat_pos_x = 270;
int p2_bat_pos_x = 270;
int ball_dir_x = dbRnd( 2 );
int ball_dir_y = dbRnd( 2 );
int ball_pos_x = 320;
int ball_pos_y = 220;
int reset_round = false;
int reset_game = false;
// Initialize Struct
struct BallMovement Ball;
// our main loop
while ( LoopGDK( ) )
{
// Clear Screen
dbCLS( );
// Draw Game Boundries & Net
DrawGameBoundries( );
// Bat Movement
p1_bat_pos_x = DoPlayer1Movement( p1_bat_pos_x );
p2_bat_pos_x = DoPlayer2Movement( p2_bat_pos_x );
// Ball Movement
Ball = MoveBall( ball_pos_x, ball_pos_y, ball_dir_x, ball_dir_y, ball_movement_state );
ball_movement_state = Ball.movement_state;
if ( ball_movement_state == 1 )
{
ball_pos_x = Ball.pos_x;
ball_pos_y = Ball.pos_y;
ball_dir_x = Ball.dir_x;
ball_dir_y = Ball.dir_y;
}
// Draw Bats
DrawPlayer1Bat( p1_bat_pos_x );
DrawPlayer2Bat( p2_bat_pos_x );
// Draw Ball
DrawBall( ball_pos_x, ball_pos_y );
// Collision Detection & Sound Effects
if ( !P1CollisionDetection( p1_bat_pos_x, ball_pos_x, ball_pos_y ) )
{
// Incriment Score
p2_score ++;
// Check if player has won the game
if ( p2_score > 9 )
{
// Display Message
dbCenterText ( 313, 330, p2_name$ );
dbCenterText ( 313, 345, "Has won the game, congratulations!!" );
dbCenterText ( 313, 120, "Press [Return] to start a new game..." );
// Switch to reset game variables
reset_game = true;
reset_round = true;
}
else
{
// Display Message
dbCenterText ( 313, 330, p2_name$ );
dbCenterText ( 313, 345, "Wins! this round!" );
dbCenterText ( 313, 120, "Press [Return] to start the next round..." );
// Switch to reset round variables
reset_round = true;
}
}
if ( !P2CollisionDetection( p2_bat_pos_x, ball_pos_x, ball_pos_y ) )
{
// Incriment Score
p1_score ++;
// Check if player has won the game
if ( p1_score > 9 )
{
// Display Message
dbCenterText ( 313, 330, p1_name$ );
dbCenterText ( 313, 345, "Has won the game, congratulations!!" );
dbCenterText ( 313, 120, "Press [Return] to exit..." );
// Switch to reset game variables
reset_game = true;
reset_round = true;
}
else
{
// Display Message
dbCenterText ( 313, 120, p1_name$ );
dbCenterText ( 313, 135, "Wins this round!" );
dbCenterText ( 313, 330, "Press [Return] to start the next round..." );
// Modify Var
reset_round = true;
}
}
// Score Displays
DisplayScores( p1_name$, p2_name$, p1_score, p2_score );
// update the screen
dbSync( );
// Reset Vars
if ( reset_round )
{
ball_movement_state = 0;
p1_bat_pos_x = 270;
p2_bat_pos_x = 270;
ball_dir_x = dbRnd( 2 );
ball_dir_y = dbRnd( 2 );
ball_pos_x = 320;
ball_pos_y = 220;
reset_round = false;
while ( !dbKeyState( 28 ) )
{
// Pause Game Until Return Is Pressed - Should It Be Done This Way? Seems Weird To Me
}
if ( reset_game )
{ return; }
}
}
// return back to windows
return;
}
char* P1NameInput( )
{
dbCLS( );
dbText( 170, 100, "Player 1, please enter your name: " );
dbSetCursor ( 170, 130 );
dbSync( );
return dbInput();
}
char* P2NameInput( )
{
dbCLS( );
dbText( 170, 100, "Player 2, please enter your name: " );
dbSetCursor ( 170, 130 );
dbSync( );
return dbInput();
}
void DrawGameBoundries( )
{
dbLine ( 20, 40, 20, 435 );
dbLine ( 620, 40, 620, 435 );
dbLine ( 20, 220, 620, 220 );
}
void DisplayScores( char* p1_name$, char* p2_name$, int p1_score, int p2_score )
{
int text_size = dbTextSize( );
dbCenterText( 320, 5, p1_name$ );
dbCenterText( 320, 455, p2_name$ );
dbSetTextSize ( 80 );
dbText( 80, 85, dbStr( p1_score ) );
dbText( 500, 300, dbStr( p2_score ) );
dbSetTextSize ( text_size );
}
void DrawBall( int ball_pos_x, int ball_pos_y )
{
dbCircle( ball_pos_x, ball_pos_y, BALL_SIZE );
}
void DrawPlayer1Bat( int bat_pos_x )
{
dbBox( bat_pos_x, P1_BAT_POS_Y, bat_pos_x + 100, P1_BAT_POS_Y + 10 );
}
void DrawPlayer2Bat( int bat_pos_x )
{
dbBox( bat_pos_x, P2_BAT_POS_Y, bat_pos_x + 100, P2_BAT_POS_Y + 10 );
}
int DoPlayer1Movement( int bat_pos_x )
{
if ( dbLeftKey( ) )
{
bat_pos_x -= BAT_SPEED;
if ( bat_pos_x < 20 )
{ bat_pos_x = 20; }
}
else if ( dbRightKey( ) )
{
bat_pos_x += BAT_SPEED;
if ( bat_pos_x > 520 )
{ bat_pos_x = 520; }
}
return bat_pos_x;
}
int DoPlayer2Movement( int bat_pos_x )
{
if ( dbKeyState( 30 ) )
{
bat_pos_x -= BAT_SPEED;
if ( bat_pos_x < 20 )
{ bat_pos_x = 20; }
}
else if ( dbKeyState( 32 ) )
{
bat_pos_x += BAT_SPEED;
if ( bat_pos_x > 520 )
{ bat_pos_x = 520; }
}
return bat_pos_x;
}
bool P1CollisionDetection( int bat_pos_x , int ball_pos_x, int ball_pos_y )
{
if ( ball_pos_y < 34 )
{
if ( ( ball_pos_x < bat_pos_x ) || ( ball_pos_x > bat_pos_x + 100 ) )
{ return false; } // Round End - Player 1 Loses
else
{ dbPlaySound( 1 ); }
}
return true;
}
bool P2CollisionDetection( int bat_pos_x , int ball_pos_x, int ball_pos_y )
{
if ( ball_pos_y > 439 )
{
if ( ( ball_pos_x < bat_pos_x ) || ( ball_pos_x > bat_pos_x + 100 ) )
{ return false; } // Round End - Player 2 Loses
else
{ dbPlaySound( 1 ); }
}
return true;
}
struct BallMovement MoveBall( int ball_pos_x, int ball_pos_y, int ball_dir_x, int ball_dir_y, int ball_movement_state )
{
BallMovement Ball;
if ( dbSpaceKey( ) )
{ ball_movement_state = 1; }
if ( ball_movement_state )
{
if ( ball_dir_x )
{
ball_pos_x += BALL_SPEED;
if ( ball_pos_x > 615 )
{
ball_pos_x = 615;
ball_dir_x = 0;
}
}
else
{
ball_pos_x -= BALL_SPEED;
if ( ball_pos_x < 25 )
{
ball_pos_x = 25;
ball_dir_x = 1;
}
}
if ( ball_dir_y )
{
ball_pos_y += BALL_SPEED;
if ( ball_pos_y > 440 )
{
ball_pos_y = 440;
ball_dir_y = 0;
}
}
else
{
ball_pos_y -= BALL_SPEED;
if ( ball_pos_y < 33 )
{
ball_pos_y = 33;
ball_dir_y = 1;
}
}
}
Ball.pos_x = ball_pos_x;
Ball.pos_y = ball_pos_y;
Ball.dir_x = ball_dir_x;
Ball.dir_y = ball_dir_y;
Ball.movement_state = ball_movement_state;
return Ball;
}
So any advice on the coding please?
- Scarface