-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDot.cpp
More file actions
127 lines (110 loc) · 3.05 KB
/
Copy pathDot.cpp
File metadata and controls
127 lines (110 loc) · 3.05 KB
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
#include "Dot.h"
Dot::Dot(SDL_Surface *screen)
{
red = load_image( "red.bmp" );
green = load_image( "green.bmp" );
blue = load_image( "blue.bmp" );
//Set alpha
SDL_SetAlpha( red, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( blue, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
SDL_SetAlpha( green, SDL_SRCALPHA | SDL_RLEACCEL, 192 );
dot = load_image( "dot.bmp" );
this->screen=screen;
//Initialize the offsets
x = 0;
y = 0;
angle = -45;
velocity = 5;
//Initialize particles
for( int p = 0; p < TOTAL_PARTICLES; p++ )
{
switch( rand() % 3 )
{
case 0: particles[ p ] = new Particle( x, y, screen,red); break;
case 1: particles[ p ] = new Particle( x, y, screen,green); break;
case 2: particles[ p ] = new Particle( x, y, screen,blue); break;
}
}
}
Dot::~Dot()
{
//Delete particles
for( int p = 0; p < TOTAL_PARTICLES; p++ )
{
delete particles[ p ];
}
SDL_FreeSurface( dot );
SDL_FreeSurface( red );
SDL_FreeSurface( green );
SDL_FreeSurface( blue );
}
void Dot::handle_input()
{
// //If a key was pressed
// if( event.type == SDL_KEYDOWN )
// {
// //Adjust the velocity
// switch( event.key.keysym.sym )
// {
// case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
// case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
// case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
// case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
// }
// }
// //If a key was released
// else if( event.type == SDL_KEYUP )
// {
// //Adjust the velocity
// switch( event.key.keysym.sym )
// {
// case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
// case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
// case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
// case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
// }
// }
}
void Dot::move()
{
this->x += (cos (angle*PI/180) * velocity );
this->y -= sin (angle*PI/180) * velocity;
if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) )
{
angle=-angle+180;
}
if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) )
{
angle=-angle;
}
}
void Dot::show_particles()
{
//Go through particles
for( int p = 0; p < TOTAL_PARTICLES; p++ )
{
//Delete and replace dead particles
if( particles[ p ]->is_dead() == true )
{
delete particles[ p ];
switch( rand() % 3 )
{
case 0: particles[ p ] = new Particle( x, y, screen,red); break;
case 1: particles[ p ] = new Particle( x, y, screen,green); break;
case 2: particles[ p ] = new Particle( x, y, screen,blue); break;
}
}
}
//Show particles
for( int p = 0; p < TOTAL_PARTICLES; p++ )
{
particles[ p ]->show();
}
}
void Dot::show()
{
//Show the dot
apply_surface( x, y, dot, screen );
//Show the particles
show_particles();
}