-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacman.cpp
257 lines (233 loc) · 7.83 KB
/
pacman.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "pacman.h"
#include "externVariables.h"
#include "Motion_Collision.h"
//Create Pacman
Pacman pacman;
Pacman::Pacman()
{
//Initialize the offsets
mPosX = 20;
mPosY = 20;
//Set collision box dimension
mCollider.w = PACMAN_WIDTH;
mCollider.h = PACMAN_HEIGHT;
mCollider.x = mPosX;
mCollider.y = mPosY;
//Initialize direct
direct = Right;
//Initialize the velocity
mVelX = 0;
mVelY = 0;
}
void Pacman::doEatCherry() {
pacman.eatCherry = true;
pacman.timeEatCherry = SDL_GetTicks();
}
void Pacman::reset() {
doEatCherry();
//Initialize the offsets
mPosX = 20;
mPosY = 20;
//Set collision box dimension
mCollider.w = PACMAN_WIDTH;
mCollider.h = PACMAN_HEIGHT;
mCollider.x = mPosX;
mCollider.y = mPosY;
//Initialize direct
direct = Right;
//Initialize the velocity
mVelX = 0;
mVelY = 0;
}
void Pacman::handleEvent( SDL_Event& e )
{
//If a key was pressed
if( e.type == SDL_KEYDOWN && e.key.repeat == 0 )
{
switch( e.key.keysym.sym )
{
case SDLK_UP:
{
direct = Up;
mVelY = -PACMAN_VEL;
mVelX = 0;
break;
}
case SDLK_DOWN:
{
direct = Down;
mVelY = PACMAN_VEL;
mVelX = 0;
break;
}
case SDLK_LEFT:
{
direct = Left;
mVelX = -PACMAN_VEL;
mVelY = 0;
break;
}
case SDLK_RIGHT:
{
direct = Right;
mVelX = PACMAN_VEL;
mVelY = 0;
break;
}
}
}
}
void Pacman::move( SDL_Rect wall[], int numbers_Wall)
{
//If pacman dies
if (isDead == true) return;
//Move Pacman left or right
mPosX += mVelX;
mCollider.x = mPosX;
bool checkColl = false;
int posWall = -1;
for (int i = 1; i <= numbers_Wall; i++)
{
checkColl |= checkCollision(mCollider, wall[i]);
if (checkColl == 1)
{
posWall = i;
break;
}
}
if (posWall != -1) // If char is blocked by walls[posWall]
{
for (int movePixel = 1; movePixel <= 15; movePixel++) {
// Move char up movePixel pixels;
mCollider.y -= movePixel;
if (checkCollision(mCollider, wall[posWall]) == false) {
mPosX -= mVelX;
mPosY -= movePixel;
return;
}
mCollider.y += movePixel;
// Move char down movePixel pixels;
mCollider.y += movePixel;
if (checkCollision(mCollider, wall[posWall]) == false)
{
mPosX -= mVelX;
mPosY += movePixel;
return;
}
mCollider.y -= movePixel;
}
}
//If Pacman went too far to the left or right
if( ( mPosX < 0 ) || ( mPosX + PACMAN_WIDTH > 800 ) || checkColl)
{
//Move back
mPosX -= mVelX;
mCollider.x = mPosX;
}
//Move Pacman up or down
mPosY += mVelY;
mCollider.y = mPosY;
posWall = -1;
checkColl = false;
for (int i = 1; i <= numbers_Wall; i++)
{
checkColl |= checkCollision(mCollider, wall[i]);
if (checkColl == 1)
{
posWall = i;
break;
}
}
if (posWall != -1) // If Pacman is blocked by walls[posWall]
{
for (int movePixel = 1; movePixel <= 15; movePixel++) {
// Move Pacman left movePixel pixels;
mCollider.x -= movePixel;
if (checkCollision(mCollider, wall[posWall]) == false) {
mPosY -= mVelY;
mPosX -= movePixel;
return;
}
mCollider.x += movePixel;
// Move Pacman right movePixel pixels;
mCollider.x += movePixel;
if (checkCollision(mCollider, wall[posWall]) == false)
{
mPosY -= mVelY;
mPosX += movePixel;
return;
}
mCollider.x -= movePixel;
}
}
//If Pacman went too far up or down
if( ( mPosY < 0 ) || ( mPosY + PACMAN_HEIGHT > SCREEN_HEIGHT ) || checkColl)
{
//Move back
mPosY -= mVelY;
mCollider.y = mPosY;
}
}
//Pacman's animation
SDL_Rect pacmanAnimation[4][4];
SDL_Rect pacmanDeath[14];
void getPacmanAnimation()
{
//Right
pacmanAnimation[Right][0].x = 850; pacmanAnimation[0][0].y = 0; pacmanAnimation[0][0].h = 50; pacmanAnimation[0][0].w = 50;
pacmanAnimation[Right][1].x = 850; pacmanAnimation[0][1].y = 50; pacmanAnimation[0][1].h = 50; pacmanAnimation[0][1].w = 50;
pacmanAnimation[Right][2].x = 850; pacmanAnimation[0][2].y = 100; pacmanAnimation[0][2].h = 50; pacmanAnimation[0][2].w = 50;
pacmanAnimation[Right][3].x = 850; pacmanAnimation[0][3].y = 150; pacmanAnimation[0][3].h = 50; pacmanAnimation[0][3].w = 50;
//Down
pacmanAnimation[Down][0].x = 850; pacmanAnimation[1][0].y = 150; pacmanAnimation[1][0].h = 50; pacmanAnimation[1][0].w = 50;
pacmanAnimation[Down][1].x = 850; pacmanAnimation[1][1].y = 200; pacmanAnimation[1][1].h = 50; pacmanAnimation[1][1].w = 50;
pacmanAnimation[Down][2].x = 850; pacmanAnimation[1][2].y = 250; pacmanAnimation[1][2].h = 50; pacmanAnimation[1][2].w = 50;
pacmanAnimation[Down][3].x = 850; pacmanAnimation[1][3].y = 300; pacmanAnimation[1][3].h = 50; pacmanAnimation[1][3].w = 50;
//Left
pacmanAnimation[Left][0].x = 850; pacmanAnimation[2][0].y = 300; pacmanAnimation[2][0].h = 50; pacmanAnimation[2][0].w = 50;
pacmanAnimation[Left][1].x = 850; pacmanAnimation[2][1].y = 350; pacmanAnimation[2][1].h = 50; pacmanAnimation[2][1].w = 50;
pacmanAnimation[Left][2].x = 850; pacmanAnimation[2][2].y = 400; pacmanAnimation[2][2].h = 50; pacmanAnimation[2][2].w = 50;
pacmanAnimation[Left][3].x = 850; pacmanAnimation[2][3].y = 450; pacmanAnimation[2][3].h = 50; pacmanAnimation[2][3].w = 50;
//Up
pacmanAnimation[Up][0].x = 850; pacmanAnimation[3][0].y = 450; pacmanAnimation[3][0].h = 50; pacmanAnimation[3][0].w = 50;
pacmanAnimation[Up][1].x = 850; pacmanAnimation[3][1].y = 500; pacmanAnimation[3][1].h = 50; pacmanAnimation[3][1].w = 50;
pacmanAnimation[Up][2].x = 850; pacmanAnimation[3][2].y = 550; pacmanAnimation[3][2].h = 50; pacmanAnimation[3][2].w = 50;
pacmanAnimation[Up][3].x = 850; pacmanAnimation[3][3].y = 450; pacmanAnimation[3][3].h = 50; pacmanAnimation[3][3].w = 50;
//Pacman dies
pacmanDeath[0].x = 350; pacmanDeath[0].y = 0; pacmanDeath[0].w = 50; pacmanDeath[0].h = 50;
for (int i = 1; i <= 11; i++)
{
pacmanDeath[i].x = pacmanDeath[i - 1].x;
pacmanDeath[i].y = pacmanDeath[i - 1].y + 50;
pacmanDeath[i].h = pacmanDeath[i - 1].h;
pacmanDeath[i].w = pacmanDeath[i - 1].w;
}
for (int i = 12; i <= 13; i++)
{
pacmanDeath[i].x = pacmanDeath[i - 1].x;
pacmanDeath[i].y = pacmanDeath[i - 1].y;
pacmanDeath[i].h = pacmanDeath[i - 1].h;
pacmanDeath[i].w = pacmanDeath[i - 1].w;
}
}
void Pacman::render()
{
static int framesDeath = 0;
if (pacman.isDead == true)
{
sprites.render(mPosX, mPosY, &pacmanDeath[framesDeath / 9]);
framesDeath++;
if (framesDeath / 9 >= 14) // Pacman reset
{
Mix_PlayMusic( theme, -1 );
framesDeath = 0;
pacman.Lives -= 1;
pacman.isDead = false;
pacman.reset();
}
}
if (pacman.isDead == false)
{
sprites.render( mPosX, mPosY , &pacmanAnimation[direct][frames / 4]);
}
}