///////////////////////////////
#include <acknex.h>
#include <default.c>
var ball_dir;
var ballx;
var bally;
var vBall[3];
var spd = 20;
var game_start = 0;
var game_minx = 267;
var game_maxx = 533;
PANEL* P_player =
{
//pos_x = 0;
pos_y = 550;
size_x = 100;
size_y = 10;
flags = SHOW | LIGHT;
}
PANEL* P_ai =
{
pos_x = 0;
pos_y = 50;
size_x = 100;
size_y = 10;
flags = SHOW | LIGHT;
}
PANEL* P_ball =
{
pos_x = 0;
pos_y = 0;
size_x = 6;
size_y = 6;
flags = SHOW | LIGHT;
}
function rect_collision(var x1, var y1, var w1, var h1, var x2, var y2, var w2, var h2)
{
if (x1 > (x2 + w2) | (x1 + w1) < x2){return (0);}
if (y1 > (y2 + h2) | (y1 + h1) < y2){return (0);}
return (1);
}
function hcenter_panel(PANEL* panel)
{
panel.pos_x = (screen_size.x - panel.size_x)/2;
//panel.pos_y = (screen_size.y - panel.size_y)/2;
}
function launch_ball()
{
ball_dir = -60; //-random2(30, 150);
vBall = vector(spd*cosv(ball_dir)*time_step, spd*sinv(ball_dir)*time_step, 0);
}
function trace_mouse(PANEL* panel)
{
panel.pos_x = mouse_pos.x-50;
panel.pos_x = clamp(panel.pos_x,game_minx, game_maxx-100);
}
//==============================================================================================
function main()
{
video_mode = 7;
mouse_mode = 4;
random_seed(0);
wait(3);
hcenter_panel(P_player);
hcenter_panel(P_ai);
vec_set(P_ball.blue,vector(0,0,200));
while(1)
{
trace_mouse(P_player); //move platform with the mouse
if (game_start == 0){
//ball is moving with the platform. Works!
vBall[1] = P_player.pos_x + (P_player.size_x/2) - 3;
vBall[2] = P_player.pos_y - P_ball.size_y - 1;
//Hitting a left button launches the ball
if (mouse_left == 1){
launch_ball();
game_start = 1;
}
}else{
//Keep the ball moving. Wroks!
vBall[1] += spd*cosv(ball_dir)*time_step;
vBall[2] += spd*sinv(ball_dir)*time_step;
//Collision with walls! ------------------------------------
if (vBall[1] < game_minx){ //Left side of the game area
vec_cross(vBall, 2*abs(vec_dot(vBall, vector(1,0,0))), vector(1,0,0));
//vec_bounce(vBall, vector(1,0,0));
}
if (vBall[1]+6 > game_maxx){ //Right side of the game area
vec_cross(vBall, 2*abs(vec_dot(vBall, vector(-1,0,0))), vector(-1,0,0));
//vec_bounce(vBall, vector(-1,0,0));
}
if (rect_collision(P_ball.pos_x, P_ball.pos_y, P_ball.size_x, P_ball.size_y, P_ai.pos_x, P_ai.pos_y, P_ai.size_x, P_ai.size_y)
| rect_collision(P_ball.pos_x, P_ball.pos_y, P_ball.size_x, P_ball.size_y, P_player.pos_x, P_player.pos_y, P_player.size_x, P_player.size_y)){
//ball_dir = (180 - abs(ball_dir));
}
//-----------------------------------------------------------
}
//Keep the panel moving along those coordinates
P_ball.pos_x = vBall[1]; //ballx;
P_ball.pos_y = vBall[2]; //bally;
//moving AI Platform.
P_ai.pos_x = P_ball.pos_x - ((P_ai.size_x-P_ball.size_x)/2);
P_ai.pos_x = clamp(P_ai.pos_x,game_minx, game_maxx-100);
draw_line(vector(game_minx,0,0),NULL,100); // move to first corner
draw_line(vector(game_maxx,0,0),vector(0,0,255),100);
draw_line(vector(game_maxx,599,0),vector(0,0,255),100);
draw_line(vector(game_minx,599,0),vector(0,0,255),100);
draw_line(vector(game_minx,0,0),vector(0,0,255),100);
wait(1);
}
}
//==============================================================================================