-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpg.cpp
282 lines (253 loc) · 7.71 KB
/
rpg.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Event event;
void Init() {
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
}
void Close() {
SDL_DestroyWindow(window);
window = NULL;
SDL_Quit();
}
class Character {
protected:
std::string m_name;
int m_velocity, m_directionX, m_directionY, m_randomX, m_randomY;
SDL_Texture *m_character;
SDL_Rect m_characterDescription;
void m_LoadCharacter(const char *file) {
m_character = IMG_LoadTexture(renderer, file);
}
public:
Character(){
m_characterDescription.x = m_characterDescription.y = 70;
m_directionX = m_directionY = 1;
m_velocity = 10;
m_randomX = m_randomY = 0;
}
virtual int m_SetX(int x) { m_characterDescription.x = x; return m_characterDescription.x; }
virtual int m_SetY(int y) { m_characterDescription.y = y; return m_characterDescription.y; }
virtual int m_GetX() { return m_characterDescription.x; }
virtual int m_GetY() { return m_characterDescription.y; }
virtual int m_GetWidth() { return m_characterDescription.w; }
virtual int m_GetHeight() { return m_characterDescription.h; }
virtual std::string m_GetName() { return m_name; }
virtual void m_ChangeDirectionX() { m_directionX *= -1; }
virtual void m_ChangeDirectionY() { m_directionY *= -1; }
virtual void m_Move() {}
virtual void Render() {}
virtual void NewDot() {}
};
class Snake : public Character{
public:
Snake(const char *file, int width, int height) : Character() {
m_name = "snake";
m_LoadCharacter(file);
m_characterDescription.w = width;
m_characterDescription.h = height;
m_characterDescription.x = width;
m_characterDescription.y = height;
}
void m_Move() {
if (event.type == SDL_KEYUP || SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP: m_characterDescription.y = m_characterDescription.y - m_velocity; break;
case SDLK_DOWN: m_characterDescription.y = m_characterDescription.y + m_velocity; break;
case SDLK_RIGHT: m_characterDescription.x += m_velocity; break;
case SDLK_LEFT: m_characterDescription.x -= m_velocity; break;
}
}
SDL_RenderCopy(renderer, m_character, 0, &m_characterDescription);
}
};
class Dot : public Character{
public:
Dot(const char *file, int width, int height) : Character() {
m_name = "dot";
m_LoadCharacter(file);
m_characterDescription.w = width;
m_characterDescription.h = height;
m_characterDescription.x = m_GetX();//SCREEN_WIDTH / 2 - width / 2;
m_characterDescription.y = m_GetY();//SCREEN_HEIGHT - height;
srand(time(0));
}
void Render() {
SDL_RenderCopy(renderer, m_character, 0, &m_characterDescription);
}
void NewDot(){
m_SetX(rand() % 640 + 1);
m_SetY(rand() % 480 + 1);
}
};
/*
class GameCharacter {
protected:
std::string m_name;
int m_velocity, m_directionX, m_directionY;
SDL_Texture *m_character;
SDL_Rect m_characterDescription;
void m_LoadCharacter(const char *file) {
m_character = IMG_LoadTexture(renderer, file);
}
public:
GameCharacter() {
m_characterDescription.x = m_characterDescription.y = 0;
m_directionX = m_directionY = 1;
m_velocity = 25;
}
virtual int m_SetX(int x) { m_characterDescription.x = x; return m_characterDescription.x; }
virtual int m_SetY(int y) { m_characterDescription.y = y; return m_characterDescription.y; }
virtual int m_GetX() { return m_characterDescription.x; }
virtual int m_GetY() { return m_characterDescription.y; }
virtual int m_GetWidth() { return m_characterDescription.w; }
virtual int m_GetHeight() { return m_characterDescription.h; }
virtual std::string m_GetName() { return m_name; }
virtual void m_ChangeDirectionX() { m_directionX *= -1; }
virtual void m_ChangeDirectionY() { m_directionY *= -1; }
virtual void m_Move() {}
};
class Dot : public GameCharacter {
public:
Dot(const char *file, int width, int height) : GameCharacter() {
m_name = "dot";
m_LoadCharacter(file);
m_characterDescription.w = width;
m_characterDescription.h = height;
m_characterDescription.x = width;
m_characterDescription.y = height;
}
void m_Move() {
m_characterDescription.x = m_characterDescription.x + m_velocity * m_directionX;
m_characterDescription.y = m_characterDescription.y + m_velocity * m_directionY;
SDL_RenderCopy(renderer, m_character, 0, &m_characterDescription);
}
};
class Paddle : public GameCharacter {
public:
Paddle(const char *file, int width, int height) : GameCharacter() {
m_name = "paddle";
m_LoadCharacter(file);
m_characterDescription.w = width;
m_characterDescription.h = height;
m_characterDescription.x = SCREEN_WIDTH / 2 - width / 2;
m_characterDescription.y = SCREEN_HEIGHT - height;
}
void m_Move() {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_UP: m_characterDescription.x = m_characterDescription.x + m_velocity; break;
case SDLK_DOWN: m_characterDescription.x -= m_velocity; break;
case SDLK_RIGHT: m_characterDescription.x += m_velocity; break;
case SDLK_LEFT: m_characterDescription.x -= m_velocity; break;
}
}
SDL_RenderCopy(renderer, m_character, 0, &m_characterDescription);
}
};
*/
void OutOfScreen(Character &character) {
int x = character.m_GetX();
int y = character.m_GetY();
int w = character.m_GetWidth(); //20
int h = character.m_GetHeight(); //20
if ((x < 0) || (x + w) > (SCREEN_WIDTH - w)) {
character.m_ChangeDirectionX();
// don't let the paddle to escape from the screen
if (x < 0) {
character.m_SetX(0);
}
else {
character.m_SetX(SCREEN_WIDTH - h);
}
}
if (y < 0 || (y + w) > SCREEN_HEIGHT) {
character.m_ChangeDirectionY();
}
if ((y + w) > SCREEN_HEIGHT && character.m_GetName() == "dot") {
exit(-1);
}
}
bool collisionDetection(Character &snake, Character &dot) {
int c1x = snake.m_GetX();
int c1y = snake.m_GetY();
int c1w = snake.m_GetWidth(); //20
int c1h = snake.m_GetHeight(); //20
int c2x = dot.m_GetX(); //420
int c2y = dot.m_GetY(); //100
int c2w = dot.m_GetWidth(); //20
int c2h = dot.m_GetHeight(); //20
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = c1x;
rightA = c1x + c1w;
topA = c1y;
bottomA = c1y + c1h;
//Calculate the sides of rect B
leftB = c2x;
rightB = c2x + c2w;
topB = c2y;
bottomB = c2y + c2h;
// Checks if there is a space between the x and y axis coordinates between the two points
if (bottomA <= topB){
return false; //return false
}
else if (topA >= bottomB){
return false;
}
else if (rightA <= leftB){
return false;
}
else if (leftA >= rightB){
return false;
}
//If none of the sides from A are outside B
else {
return true;
}
}
int main(int argc, char *argv[])
{
Init();
Character *snake = new Snake("snake2.bmp", 20, 20);
Character *dot = new Dot("dot.bmp", 20, 20);
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT) {
quit = true;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); // select colour for drawing
SDL_RenderClear(renderer); // clear entire screen to our selected colour
snake->m_Move(); // (*snake).Move();
dot->Render();
OutOfScreen(*snake);
collisionDetection(*snake, *dot);
if (collisionDetection(*snake, *dot)) { // checks IF TRUE
dot->NewDot();
dot->Render();
}
SDL_RenderPresent(renderer); /* everything before this was drawn behind the scenes this actually
puts the colour on screen for us */
SDL_Delay(70); // gives us time to see the screen
}
Close();
return 0;
}