-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetrisindex.html
408 lines (325 loc) · 11.7 KB
/
Tetrisindex.html
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
<!DOCTYPE HTML>
<!--
Phantom by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>Tetris</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
<link rel="icon" href="images/logo.svg" type="image/x-icon">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/styles/default.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.4.0/highlight.min.js"></script>
</head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-QL94LWVWCN"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-QL94LWVWCN');
</script>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<div class="inner">
<!-- Logo -->
<a href="index.html" class="logo">
<span class="symbol"><img src="images/logo.svg" alt="" /></span><span class="title">Fernanda Robles</span>
</a>
</div>
</header>
<!-- Main -->
<div id="main">
<div class="inner">
<h1>Stacking Success: Introduction to Game Development Using Tetris</h1>
<p>
You can find this code's C++ implementation in my GitHub:
</p>
<ul class="actions">
<li><a href="https://github.com/FernandaRobles1234/Tetris" class="button">C++</a></li>
</ul>
<p>
Fueled by my passion for C++ and the urge to learn more, I've stepped into the vibrant world of video game development.
What intrigues me the most is the diversity of problems we can tackle due to the virtual nature of video games.
This field truly offers a playground for anyone looking to deepen their understanding of software development,
since various approaches at different levels stand out more here than in other domains.
</p>
<p>
With a surge of enthusiasm, I took on my first game project, which, as you can discern, is Tetris!
</p>
<p>
And my aim is to bring you along on my journey 😀 and convey the approachability I experienced during this introduction.
</p>
<h2>Tetris</h2>
<p>
Coming soon.
</p>
<h2>Lets get started</h2>
<p>
In this tutorial, I'll be walking you through the process of creating an interactive application using Visual Studio
in object-oriented programming. To facilitate the development, we'll use
the Simple DirectMedia Layer (SDL), a library to
give developers intimate, low-level access to elements such as audio, keyboard, mouse, joystick, and graphics.
But what's development without robust testing? Hence, we'll adopt a debugging-oriented approach throughout.
Dive in and let's begin this exciting journey!
</p>
<p>
I won't be talking about how to install SLD, but <a href="https://www.youtube.com/watch?v=13AEnd8XB70&ab_channel=TheInteractiveStudio">here</a> is the video where I learned!
</p>
<h2>Implementation design</h2>
<p>
Designing our game is the foundational step. It sets the stage because all the programming
will revolve around it. Given that Tetris is a widely-analyzed challenge,
many solutions have been proposed over the years. One of the most straightforward methods to
tackle it is by writing the code in-line, leading to a single file for our entire project.
</p>
<p>
We won't be taking that route. Instead, we'll prepare for an industry that's
predominantly Object-Oriented. It's a complex programming subject; however,
I believe this application does a good job of keeping it simple.
</p>
<p>
And, once you get the hang of it, you'll find there's
a comprehensive methodology underlying Object-Oriented-Programming (OOP) that can truly make our life easier as programmers.
</p>
<section>
<h3>Steps</h3>
<p>
The following list outlines some of the most basic tasks a game can entail. Organized sequentially, it ensures that our
progression through each task remains independent of subsequent ones. This structure guarantees that even if we pause at any stage,
we'll have a somewhat complete exercise to reflect upon and showcase 🙂
</p>
<div class="row">
<div class="col-6 col-12-medium">
<div class="col-6 col-12-medium">
<ol>
<li>Set up your development</li>
<li>Game Initialization.</li>
<li>Basic Gameboard</li>
<li>Tetromino implementation</li>
<li>Game logic</li>
<li>User input</li>
<li>Game flow</li>
</ol>
</div>
</div>
</div>
<p>
There are also some steps that could lead to a more polished result, but I haven't explored them myself. Some examples:
</p>
<div class="col-6 col-12-medium">
<ul>
<li>Polishing</li>
<li>User interface</li>
<li>Testing iteration</li>
</ul>
</div>
</section>
<h2>Implementation</h2>
<h3>1. Set up your environment.</h3>
<p>
After we install SDL as seen <a href="https://www.youtube.com/watch?v=13AEnd8XB70&ab_channel=TheInteractiveStudio">here</a>, we can confirm with the following snippet that we can use it.
</p>
<pre><code class='cpp'>
////////////////// Main.cpp /////////////////////
#include <SDL.h>
int main(int argc, char* argv[]) {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 1;
}
return 0;
}
</code></pre>
<h3>2. Game Initialization</h3>
<h4>SDL Usage</h4>
<p>
At this point, SDL functions should be completely accesible 🙂, but as SDL usage depends on other components, first we need to initialize it.
This way, we make sure every subsystem (video, audio, events, etc.) is operationg properly. If anything turns out, you can handle errors gracefully instead of
encountering unpredictable behavior later. Also, we set up the necessary ressources for the library to work properly. It's a crucial step in using the library correctly and efficiently.
</p>
<p>
Please note, we are also creating and initializing the window where we will see tangible proof of our advancements.
</p>
<pre><code class='cpp'>
////////////////// Main.cpp /////////////////////
bool initializeSDL(SDL_Window* &window, SDL_Renderer* &renderer) {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
}
// Create window
window = SDL_CreateWindow("Tetris",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (!window) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return false;
}
// Initialize renderer
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return false;
}
//For opacity
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
return true;
}
</code></pre>
<p>
To mirror this, cleaning up after using SDL is equally crucial.
</p>
<pre><code class='cpp'>
////////////////// Main.cpp /////////////////////
bool cleanupSDL(SDL_Window*& window, SDL_Renderer*& renderer) {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return true;
}
</code></pre>
<h4>Game Class</h4>
<p>
Here, we introduce the concept of the "main loop" or "game loop." Essentially, it's a continuous loop responsible for calling the core mechanics of our Tetris game.
Some comon mechanics are:
</p>
<div class="col-6 col-12-medium">
<ul>
<li>Event handling: This method is responsible for handling user input or other external events </li>
<li>Update: Updates the game state. This can include moving characters, checking for collisions, updating timers, etc.</li>
<li>Render phases: This method is responsible for drawing the game's content on the screen.</li>
</ul>
</div>
<p>
As I mentioned before, we will introduce this mechanichs as characteristics of an object "Game".
</p>
<pre><code class='cpp'>
////////////////// Game.h /////////////////////
class Game {
public:
Game(SDL_Renderer* sdlRenderer); // This makes the renderer accesible to the game mechanics
const bool isRunning() const; // The main loop continues as long as the game is "running".
void handleEvents();
void update();
void render();
private:
SDL_Renderer* mSdlRenderer;
bool mIsRunning;
void renderWindow(); //specialized rendering function
};
</code></pre>
<p>
After defining our "Game"" class, we've implemented it's member functions and initialization.
The code is quite extensive, so I encourage you to review the "Game.cpp" file. Please focus on
the functions introduced so far and disregard the rest for now.
</p>
<p>
Then we can call our new code to manage correctly our game setup.
</p>
<pre><code class='cpp'>
////////////////// Main.cpp /////////////////////
#include <SDL.h>
#include "Game.h"
int main(int argc, char* argv[]) {
SDL_Window* window = nullptr;
SDL_Renderer* renderer = nullptr;
if (!initializeSDL(window, renderer)) {
return 1;
}
//Initialize game renderer
Game game(renderer);
//Game loop
while (game.isRunning()) {
game.handleEvents();
game.update();
game.render();
}
//clean up and free resources.
cleanupSDL(window, renderer);
return 0;
}
</code></pre>
<p>
Now, we have some logic going on.
So far, your window should look like this: Picture coming soon!
TODO: Check if this code would actually not end uo in an infinite loop
</p>
<h3>3. Basic Gameboard</h3>
<p>
Coming soon!
</p>
<h3>4. Tetromino Implementation</h3>
<p>
Coming soon!
</p>
<h3>5. Game Logic</h3>
<p>
Coming soon!
</p>
<h3>6. User Input</h3>
<p>
Coming soon!
</p>
<h3>7. Game Flow</h3>
<p>
Coming soon!
</p>
<h2>Next Steps</h2>
<p>
Coming soon!
</p>
<h2>Aknowledgements</h2>
<p>
Coming soon!
</p>
</div>
</div>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<section>
<h2>Follow me!</h2>
<ul class="icons">
<ul class="icons">
<li><a href="https://www.linkedin.com/in/fernandarobles1234/" class="icon brands style2 fa-linkedin"><span class="label">LinkedIn</span></a></li>
<li><a href="https://github.com/FernandaRobles1234" class="icon brands style2 fa-github"><span class="label">GitHub</span></a></li>
</ul>
</ul>
</section>
<ul class="copyright">
<li>© Fernanda Robles. All rights reserved</li>
<li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
<script>
document.addEventListener('DOMContentLoaded', (event) => {
document.querySelectorAll('pre code').forEach((block) => {
hljs.highlightElement(block);
});
});
</script>
</body>
</html>