| //<NOTE:> This is just one of the 4 source files used to compile the game. The rest are included with the zip file download. |
| //--------------------- |
| //Atomic Balls |
| //--------------------- |
| //created by Mike Marek |
| //ball.cpp |
| //A ball that will bounce around the screen |
| //----------------------------------------------------------------------------------------------------------------------------------------- |
| |
| #include "DarkGDK.h" |
| #include "ball.h" |
| #include "atomic.h" |
| |
| #include <cmath> |
| //----------------------------------------------------------------------------------------------------------------------------------------- |
| |
| //current number of instances on screen |
| int ball_instances = 0; |
| |
| //ball radius (distance between center and edge) |
| int ball_radius = 25; |
| |
| //ball movement speed |
| int ball_speed = 5; |
| |
| //check to see if the ball is active or not |
| bool ball_active[ball_totalInstances]; |
| |
| //ball position on the x-axis |
| int ball_x[ball_totalInstances]; |
| |
| //ball position on the y-axis |
| int ball_y[ball_totalInstances]; |
| |
| //ball movement direction (in degrees: 0 - 360) |
| float ball_direction[ball_totalInstances]; |
| |
| //ball movement direction along the x-axis |
| int ball_xdir[ball_totalInstances]; |
| |
| //ball movement direction along the y-axis |
| int ball_ydir[ball_totalInstances]; |
| //----------------------------------------------------------------------------------------------------------------------------------------- |
| |
| //setup ball variables |
| void ball_setup() |
| { |
| //load ball image |
| SetCurrentDirectory("Resource"); |
| dbLoadImage("ball.png", ID_BALL, 1); |
| } |
| //----------------------------------------------------------------------------------------------------------------------------------------- |
| |
| //add a new ball to the screen |
| void create_ball() |
| { |
| //make sure we have an available space to create a new ball |
| if (ball_instances < ball_totalInstances) |
| { |
| //scroll through each spot in our ball array to check which space is the empty one |
| for (unsigned int i = 1; i < ball_totalInstances; i++) |
| { |
| if (!ball_active[i]) |
| { |
| ball_active[i] = true; |
| ball_x[i] = dbRND(window_width - (ball_radius * 2)); |
| ball_y[i] = dbRND(window_height - (ball_radius * 2)); |
| ball_direction[i] = dbRND(360.0f); |
| ball_xdir[i] = (dbRND(1) ? 1 : -1); |
| ball_ydir[i] = (dbRND(1) ? 1 : -1); |
| ball_instances++; |
| |
| //we add/subtract one from the x and y positions so the ball doesn't lie directly on the boundries |
| dbSprite(i, ball_x[i], ball_y[i], ID_BALL); |
| |
| //exit out of the loop so we don't run any uncessessary itations |
| break; |
| } |
| } |
| } |
| } |
| //----------------------------------------------------------------------------------------------------------------------------------------- |
| |
| //move ball around the screen |
| void move_ball() |
| { |
| //loop through each ball and move it around the screen |
| for (unsigned int i = 1; i < ball_totalInstances; i++) |
| { |
| //make sure the ball is active before we apply any actions to it |
| if (ball_active[i]) |
| { |
| //here we do some simple triginometry to move the ball around in a 360 degree fashion |
| if (ball_direction[i] > 180.0f) |
| { |
| ball_x[i] = ball_x[i] - sin((pi / 180) * ball_direction[i]) * (ball_speed * ball_xdir[i]); |
| ball_y[i] = ball_y[i] + cos((pi / 180) * ball_direction[i]) * (ball_speed * ball_ydir[i]); |
| } |
| else |
| { |
| ball_x[i] = ball_x[i] + sin((pi / 180) * ball_direction[i]) * (ball_speed * ball_xdir[i]); |
| ball_y[i] = ball_y[i] - cos((pi / 180) * ball_direction[i]) * (ball_speed * ball_ydir[i]); |
| } |
| |
| //make sure the ball doesn't go through any of the boundries |
| if (ball_x[i] < 0 || ball_x[i] > (window_width - (ball_radius * 2))) |
| { |
| ball_xdir[i] = ball_xdir[i] * -1; |
| } |
| if (ball_y[i] < 0 || ball_y[i] > (window_height - (ball_radius * 2))) |
| { |
| ball_ydir[i] = ball_ydir[i] * -1; |
| } |
| |
| //refresh sprite with new coordinates |
| if (ball_active[i]) dbSprite(i, ball_x[i], ball_y[i], ID_BALL); |
| } |
| } |
| } |
| //----------------------------------------------------------------------------------------------------------------------------------------- |