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
//<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);
              }
       }
}
//-----------------------------------------------------------------------------------------------------------------------------------------