Yo!
This is also a pong movement type, but this is in 3D.
But the code for moving is the same for the 2D version, so you may examine and experiment with it.
I hope that it will help you out.. if not post another message or try my site.
// Title : Move ball, Ping-Pong :-))
// Author: Yerrel
// © www.binary-coder.com 2008
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
// 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 );
dbRandomize(dbTimer());
// Create a new camera and move it to back.
dbMakeCamera(1); dbPositionCamera(1, 0.0, 0.0, -50.0);
// Create a 'Sphere' to use as the ball, scale it a little, and color it Yellow
dbMakeObjectSphere(1, 4); dbScaleObject(1, 100.0, 100.0, 30.0);
dbColorObject(1, dbRGB(255, 255, 0));
// Create a box to use as the Paddle and color it Red
dbMakeObjectBox(2, 16, 2, 4);
dbColorObject(2, dbRGB(255, 0, 0));
// The needed variables
float padd_X = 0.0, padd_Y = -20.0, padd_Z = 0.0, padd_Speed = 1.5;;
float ball_X = 0.0, ball_Y = 0.0, ball_Z = 0.0;
float ball_Angle = 0.1, temp = 0.0;
int ball_Direction = 1; // Direction 1 = Down, -1 = Up
// our main loop
while ( LoopGDK ( ) )
{
// Controlling Ball movement
if(ball_Direction == 1)
{
dbPositionObject(1, dbObjectPositionX(1) + ball_Angle,dbObjectPositionY(1)+1.0, dbObjectPositionZ(1));
}
else{
dbPositionObject(1, dbObjectPositionX(1) + ball_Angle,dbObjectPositionY(1)-1.0, dbObjectPositionZ(1));
}
// If the ball is going offscreen, ( left/right/top ) revers the direction
if(dbObjectPositionX(1) < -37.0 || dbObjectPositionX(1) > 37.0)
{
ball_Angle = ball_Angle * -1.0;
}// Test top offscreen.
if(dbObjectPositionY(1) >30.0 || dbObjectPositionY(1) <- 30.)
{
ball_Direction = ball_Direction * -1;
}
// Check collision between Ball & Paddle
if(dbObjectHit(1, 2))
{ // if ball hits Paddle than reverse the ball direction
ball_Direction = ball_Direction * -1;
// Change ball_Angle speed a bit ( speed for left and right ).
temp = (float)dbRND(100) / 100.0; // generates a value between 0.0 and 1.0
if(dbRnd(1) == 1) temp = temp * -1.0; // if random = 1, than set ball_angle to 1.0
ball_Angle = temp * 1.5; // if random isn't 1 put ball_angle to 1.5
}
// Check for keyboard input ( Left and Right key)
if(dbLeftKey() && padd_X >-30.0) padd_X = padd_X - padd_Speed;
if(dbRightKey() && padd_X <30.0) padd_X = padd_X + padd_Speed;
// Position the Paddle
dbPositionObject(2, padd_X, padd_Y, padd_Z);
// update the screen
dbSync ( );
}
// return back to windows
return;
}
MSI K8T NEO-2 F2.0, 8XAGP, 2x512Mb Dual Channel... Dark GDK..... http://www.binary-coder.com