-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFirstGame.cpp
More file actions
237 lines (143 loc) · 4.76 KB
/
MyFirstGame.cpp
File metadata and controls
237 lines (143 loc) · 4.76 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
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
// How to Play:
// WASD to move, ESC to Quit Game
// You play as yellow Square, your goal is to kill the Red Square by touching it
// Kill red Square by touching on it
// Kill 10 times to win!
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <SDL2/SDL.h>
using namespace std;
int CollisionWallX(int posX, int blockWidth, int screenWidth)
{
// Check collision on X
if(posX < 0)
posX = posX + 10;
if(posX + blockWidth > screenWidth)
posX = posX - 10;
return(posX);
}
int CollisionWallY(int posY, int blockHeight, int screenHeight)
{
// Check collision on Y
if(posY < 0)
posY = posY + 10;
if(posY + blockHeight > screenHeight)
posY = posY - 10;
return(posY);
}
bool CollisionEnemy(SDL_Renderer* render, int x, int y, int enemyX, int enemyY, int blockWidth, int blockHeight)
{
if((x + blockWidth) >= enemyX && x <= (enemyX + blockWidth) && (y + blockHeight) >= enemyY && y <= (enemyY + blockHeight))
return true;
else
return false;
}
void RenderRect(SDL_Renderer* render, int x, int y, int blockWidth, int blockHeight)
{
// Rectangle coordinates and dimention
SDL_Rect rect = {x, y, blockWidth, blockHeight};
SDL_SetRenderDrawColor(render, 255, 255, 0, 255);
SDL_RenderFillRect(render, &rect);
}
void EnemyRenderRect(SDL_Renderer* render, int x, int y, int blockWidth, int blockHeight)
{
// Rectangle coordinates and dimention
SDL_Rect rect = {x, y, blockWidth, blockHeight};
// Enemy will render red
SDL_SetRenderDrawColor(render, 255, 0, 0, 255);
SDL_RenderFillRect(render, &rect);
}
int main(int argc, char* argv[])
{
SDL_Window* window;
SDL_Init(SDL_INIT_VIDEO);
// Window dimentions
int width = 640;
int height = 480;
window = SDL_CreateWindow("My First Game!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
SDL_Renderer* render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
bool running = true;
SDL_Event ev;
// Initial coordinates of block(0, 0)
int playerX, playerY, enemyX, enemyY;
playerX = 80;
playerY = 80;
// Uses time to randomly generate seed
srand(time(0));
// Enemy appears on different coordinates every time
enemyX = rand() % (width - 50);
enemyY = rand() % (height - 50);
// Rectangle dimenstions
int blockWidth, blockHeight;
blockWidth = 50;
blockHeight = 50;
// Declare Variable
bool enemyCollision;
// kill 10 times to win game
int kill;
// Loop of window running
while(running)
{
// Check for collision
enemyCollision = CollisionEnemy(render, playerX, playerY, enemyX, enemyY, blockWidth, blockHeight);
// Events, where everything happens
while(SDL_PollEvent(&ev) != 0)
{
if(ev.type == SDL_QUIT)
running = false;
else if(ev.type == SDL_KEYDOWN)
{
switch( ev.key.keysym.sym )
{
// Moving Player
case SDLK_w:
playerY = playerY - 5;
break;
case SDLK_s:
playerY = playerY + 5;
break;
case SDLK_a:
playerX = playerX - 5;
break;
case SDLK_d:
playerX = playerX + 5;
break;
case SDLK_ESCAPE:
running = false;
break;
}
}
}
if(enemyCollision != false)
{
// Uses time to randomly generate seed
srand(time(0));
// Enemy appears on different coordinates every time
enemyX = rand() % (width - 50);
enemyY = rand() % (height - 50);
enemyCollision = 0;
// Kill count;
kill = kill + 1;
}
if(kill == 10)
running = false;
// Update window
SDL_SetRenderDrawColor(render, 211, 211, 211, 255); // Background color
SDL_RenderClear(render);
// Check Collision
playerX = CollisionWallX(playerX, blockWidth, width);
playerY = CollisionWallY(playerY, blockHeight, height);
// Render Rectangle
RenderRect(render, playerX, playerY, blockWidth, blockHeight);
// Render Enemy Rectangle
EnemyRenderRect(render, enemyX, enemyY, blockWidth, blockHeight);
// Render what's left
SDL_RenderPresent(render);
SDL_UpdateWindowSurface(window);
}
// End/Destroy everything
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}