1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include "DarkGDK.h"
const int BAT_SPEED = 10;
const int P1_BAT_POS_Y = 435;
const int BALL_SPEED = 3;
const int BALL_SIZE = 5;
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 );
// y<-=33 y+=>440 x<-=25 x+= > 615
int DoPlayer1Movement ( int bat_pos_x );
void DrawPlayer1Bat ( int bat_pos_x );
void DrawBall( int ball_pos_x, int ball_pos_y );
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
int p1_bat_pos_x = 270;
int ball_movement_state = 0;
int ball_dir_x = dbRnd( 2 );
int ball_dir_y = dbRnd( 2 );
int ball_pos_x = 310;
int ball_pos_y = 220;
struct BallMovement Ball;
while ( LoopGDK ( ) )
{
dbCLS ( );
dbText ( 280, 10, "Pong!!" );
p1_bat_pos_x = DoPlayer1Movement ( p1_bat_pos_x );
DrawPlayer1Bat ( p1_bat_pos_x );
Ball = MoveBall( ball_pos_x, ball_pos_y, ball_dir_x, ball_dir_y, ball_movement_state );
ball_movement_state = Ball.movement_state;
ball_pos_x = Ball.pos_x;
ball_pos_y = Ball.pos_y;
ball_dir_x = Ball.dir_x;
ball_dir_y = Ball.dir_y;
DrawBall( ball_pos_x, ball_pos_y );
dbSync ( );
}
}
void DrawPlayer1Bat ( int bat_pos_x )
{
dbBox ( bat_pos_x, P1_BAT_POS_Y, bat_pos_x + 100, P1_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;
}
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;
}
void DrawBall( int ball_pos_x, int ball_pos_y )
{
dbCircle( ball_pos_x, ball_pos_y, BALL_SIZE );
}
|