-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrog.cpp
More file actions
121 lines (106 loc) · 2.81 KB
/
Frog.cpp
File metadata and controls
121 lines (106 loc) · 2.81 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
#include "Frog.h"
#define STARTING_POSITION_X 288
#define STARTING_POSITION_Y 384
Frog::Frog(int l) {
positionX = STARTING_POSITION_X;
positionY = STARTING_POSITION_Y;
width = 32;
height = 32;
speed = 32;
lives = l;
int base[6] = { 0 };
if ((image = SDL_LoadBMP("images/frog2.bmp")) == NULL) {
printf("image = SDL_LoadBMP(\"images/frog2.bmp\") error: %s\n", SDL_GetError());
}
}
Frog::~Frog() {
}
void Frog::draw(SDL_Surface* screen) {
SDL_Rect destination;
destination.x = positionX;
destination.y = positionY;
SDL_BlitSurface(image, NULL, screen, &destination);
}
void Frog::drawBase(SDL_Surface* screen) {
for (int i = 0; i < 6; i++) {
if (base[i]) {
SDL_Rect destination;
destination.x = 64 + (i * 96);
destination.y = 64;
SDL_BlitSurface(image, NULL, screen, &destination);
}
}
}
void Frog::move() {
SDL_Event event;
const Uint8* state = SDL_GetKeyboardState(NULL);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
if (state[SDL_SCANCODE_UP] || state[SDL_SCANCODE_W]) {
if (positionY - speed >= 64) positionY -= speed;
}
if (state[SDL_SCANCODE_DOWN] || state[SDL_SCANCODE_S]) {
if (positionY + speed <= 384) positionY += speed;
}
if (state[SDL_SCANCODE_LEFT] || state[SDL_SCANCODE_A]) {
if (positionX - speed >= 0) positionX -= speed;
}
if (state[SDL_SCANCODE_RIGHT] || state[SDL_SCANCODE_D]) {
if (positionX + speed <= 624) positionX += speed;
}
}
}
}
void Frog::move(float s) {
if (positionX < -width || positionX > 640) reset();
else positionX += s;
}
bool Frog::onRiver() {
return (positionY >= 96 && positionY <= 192);
}
void Frog::reset() {
positionX = STARTING_POSITION_X;
positionY = STARTING_POSITION_Y;
lives--;
}
bool Frog::isAlive() {
return (lives > 0);
}
int Frog::getLives() {
return lives;
}
bool Frog::collision(Obstacle* other) {
return !(positionX + width <= other->getX() || positionX >= other->getX() + other->getWidth() || positionY + 32 <= other->getY() || positionY >= other->getY() + 32);
}
void Frog::touchdown() {
if (positionY == 64) {
for (int i = 0; i < 6; i++) {
if (positionX >= 64 + (i * 96) - 8 && positionX <= 96 + (i * 96) + 8 && !base[i]) {
base[i] = 1;
positionX = STARTING_POSITION_X;
positionY = STARTING_POSITION_Y;
return;
}
}
for (int i = 0; i < 6; i++) {
if (positionX >= 64 + (i * 96) - 8 && positionX <= 96 + (i * 96) + 8 && !base[i]) {
base[i] = 1;
positionX = STARTING_POSITION_X;
positionY = STARTING_POSITION_Y;
return;
}
else {
reset();
return;
}
}
}
}
bool Frog::endGame() {
int counter = 0;
for (int i = 0; i < 6; i++) {
if (base[i]) counter++;
}
if (counter == 6) return true;
else return false;
}