Right, I got this far with my pong game:
// Dark GDK - The Game Creators - www.thegamecreators.com
// Pong!
// Written By Scarface ( First EVER Program )
#include "DarkGDK.h"
// Declare Global Vars
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 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 );
// Declare Functions
void DrawBall( int ball_pos_x, int ball_pos_y );
void DrawPlayer1Bat( int bat_pos_x );
void DrawPlayer2Bat( int bat_pos_x );
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 );
// Initilize Vars
int p1_bat_pos_x = 270;
int p2_bat_pos_x = 270;
int ball_dir_x = 1;
int ball_dir_y = 1;
int ball_pos_x = 310;
int ball_pos_y = 220;
// Initialize Struct
struct BallMovement Ball;
// our main loop
while ( LoopGDK( ) )
{
// Clear Screen
dbCLS( );
// Debug
dbText( 10, 10, dbStr ( dbScanCode ( ) ) );
// Draw Game Boundries
dbLine ( 20, 40, 20, 435 );
dbLine ( 620, 40, 620, 435 );
// Check Bat Movement
p1_bat_pos_x = DoPlayer1Movement( p1_bat_pos_x );
p2_bat_pos_x = DoPlayer2Movement( p2_bat_pos_x );
// Check Ball Movement
Ball = MoveBall( ball_pos_x, ball_pos_y, ball_dir_x, ball_dir_y );
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 );
// update the screen
dbSync( );
}
// return back to windows
return;
}
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( 44 ) )
{
bat_pos_x -= BAT_SPEED;
if ( bat_pos_x < 20 )
{ bat_pos_x = 20; }
}
else if ( dbKeyState( 45 ) )
{
bat_pos_x += BAT_SPEED;
if ( bat_pos_x > 520 )
{ bat_pos_x = 520; }
}
return bat_pos_x;
}
struct BallMovement MoveBall( int ball_pos_x, int ball_pos_y, int ball_dir_x, int ball_dir_y )
{
BallMovement Ball;
if ( ball_dir_x )
{
ball_pos_x += BALL_SPEED;
if ( ball_pos_x > 618 )
{
ball_pos_x = 618;
ball_dir_x = 0;
}
}
else
{
ball_pos_x -= BALL_SPEED;
if ( ball_pos_x < 18 )
{
ball_pos_x = 18;
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 < 28 )
{
ball_pos_y = 28;
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;
return Ball;
}
So far it does what it's supposed to, however I am now in a dilemma as to the best way to go about collision detection between the ball and the player bats.
- Scarface