#include <acknex.h>
#include <default.c>
/////////////////////////////////////These are the VARIABLES for the scores./////////////////////////
var top_score = 0;
var bottom_score = 0;
///////////////////////////////////////These are the BMAP DEFINITIONS for the BMAPS./////////////////////
BMAP* left_border = "border_left.bmp";
BMAP* right_border = "border_right.bmp";
BMAP* center_line = "crossed_line.bmp";
BMAP* up_pong = "pong_up.bmp";
BMAP* down_pong = "pong_down.bmp";
BMAP* pongd_ball = "pong_ball.bmp";
/////////////////////////////////////These are PANELS for the BMAPS.////////////////////////////
PANEL* the_left_border =
{ // This is PANEL for Left Border
pos_x = 0;
pos_y = 0;
layer = 2;
digits(0, 0, 2, *, 1, top_score);
bmap = left_border;
flags = VISIBLE;
}
PANEL* border_right =
{ //This is a PANEL for Right Border
pos_x = 620;
pos_y = 0;
layer = 2;
digits(0, 0, 2, *, 1, bottom_score);
bmap = right_border;
flags = VISIBLE;
}
PANEL* crossed_line =
{//This is a PANEL for Center Line
pos_x = 0;
pos_y = 225;
layer = 1;
bmap = center_line;
flags = VISIBLE;
}
PANEL* upper_pong =
{//This is a PANEL for Upper Pong
pos_x = 225;
pos_y = 20;
layer = 6;
bmap = up_pong;
flags = VISIBLE;
}
PANEL* downed_pong =
{//This is a PANEL for Downed Pong
pos_x = 225;
pos_y = 430;
layer = 6;
bmap = down_pong;
flags = VISIBLE;
}
PANEL* ponged_ball =
{//This is a PANEL for Pong Ball
pos_x = 300;
pos_y = 225;
layer = 6;
bmap = pongd_ball;
flags = VISIBLE;
}
////////////////////////////////////These are the SOUNDS for the effects./////////////////////////
SOUND* beep_1 = "beep.wav";
SOUND* goal = "score.wav";
////////////////////////////////////This defines the computer and user./////////////////////////
#define USER 1
#define COMPUTER 0
STRING* sTitle = "";
TEXT* tName = { string("computer", "user"); }
VECTOR* ball_speed;
var mode_top = COMPUTER;
var mode_bottom = COMPUTER;
////////////////////////////////////This updates the ball.//////////////////////////////
function update_ball()
{// This function updates ball
ponged_ball.pos_x += 7*ball_speed.x*time_step;
ponged_ball.pos_y += 7*ball_speed.y*time_step;
/////////////////////////////////See if ball hit paddle/border.//////////////////////
if (ponged_ball.pos_x < 0) //If ball hits the left border on X axis
{
ball_speed.x = -3 - random(3);
snd_play (beep_1, 50, 0);
}
else if (ponged_ball.pos_x > 620) //If ball hits the right border on X axis
{
ball_speed.x = 3 - random(3);
snd_play (beep_1, 70, 0);
}
if (ponged_ball.pos_y < 0) //If ball hits the upper edge of the window on Y axis
{
ball_speed.y = -3 - random(3);
snd_play (goal, 70, 0);
top_score += 1;
ponged_ball.pos_y = 0;
}
else if (ponged_ball.pos_y > 450) //If ball hits the lower edge of the window on Y axis
{
ball_speed.y = 3 - random(3);
snd_play (goal, 70, 0);
bottom_score += 1;
}
if ((ponged_ball.pos_y > 430) && (ponged_ball.pos_y < 440) && (ponged_ball.pos_x > downed_pong.pos_x - 12) && (ponged_ball.pos_x < downed_pong.pos_x + 90))
{//Bottom player has blocked the ball.
snd_play (beep_1, 70, 0);
ball_speed.x = 3 + random(3);
ball_speed.y = 3 - random(3);
}
else if ((ponged_ball.pos_y < 20) && (ponged_ball.pos_y > 10) && (ponged_ball.pos_x > upper_pong.pos_x - 12) && (ponged_ball.pos_x < upper_pong.pos_x + 90))
{
snd_play (beep_1, 70, 0);
ball_speed.x = 3 - random(3);
ball_speed.y = 3 + random(3);
}
////////////////////////////////This section updates the paddles.///////////////////
function update_paddle(var paddle_pos,var paddle_mode,var key)
{
if (COMPUTER == paddle_mode)
{
if ((ponged_ball.pos_x > 10) && (ponged_ball.pos_x < 600)) // Gives computer boundries.
*paddle_pos = ponged_ball.pos_y - 42;
}
else if (USER == paddle_mode)
{
*paddle_pos += 30*time_step*key;
*paddle_pos = clamp(*paddle_pos,35,470);
}
}
///////////////////////////////////This is the function for the screen.//////////////////////////
function main()
{//This is the blue screen color
screen_color.blue = 150;
wait (1);
randomize();
ball_speed.x = 3 - 6 * (random(6) % 2); // -3 or 3, random ball direction at game start
ball_speed.y = 3 - random(6); // -3...3, random vertical speed at game start
while ((bottom_score != 10) && (top_score != 10))
{
if (key_esc) { sys_exit(NULL); }
//Switch to user mode if a paddle key is pressed
if (key_s || key_d) mode_top = USER;
if (key_cul || key_cur) mode_bottom = USER;
// Update paddles and ball, and count scores
update_ball();
update_paddle(upper_pong.pos_x,mode_top,key_s-key_d);
update_paddle(downed_pong.pos_x,mode_bottom,key_cul-key_cur);
// compose the sTitle string
str_cpy(sTitle,"Pong Demo - ");
str_cat(sTitle,(tName.pstring)[mode_top]);
str_cat(sTitle," vs. ");
str_cat(sTitle,(tName.pstring)[mode_bottom]);
video_window(NULL,NULL,0,sTitle);
wait (1);
}
}