about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/broughlike-tutorial/stage1.html
blob: 32c2ce84096ba718fcb98877ead1c5e4c8e0cd8d (plain) (blame)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
<!DOCTYPE html>
<html>
<head>
    <title>Broughlike tutorial - Stage 0</title>
    <meta charset="utf-8">
</head>
<body>
    <div id="outer">
        <div class="header">
            <a href="index.html">JavaScript Broughlike Tutorial</a>
            <a href="stage0.html">Previously: Introduction</a>
        </div>
        <h1>Stage 1 - Drawing to the screen</h1>
        You should already have everything you need installed: a browser, a text editor, and an image editor. The apps that I'm going to use and that I recommend are Google Chrome, Sublime Text, and <a href="https://www.gimp.org/" target="_blank">GIMP</a>.
        <h2>Getting started</h2>
        First open your text editor and create a file called <em>index.html</em>, type in
        <div class="code-container-inline inline"><pre><code class="html inline">Hello World!</code></pre></div>,
        and save the file.
        <br><br>
        Open the file in your chosen browser. Be aware that this isn't valid HTML but the browser will render it just fine anyway.
        <img class="screen" src="screens/helloworld.png">
        <br><br>
        Let's replace our "Hello World!" text with the next 6 lines, which are all you need to draw to the screen:

        
        <div class="filename">index.html</div>
        <div class="code-container"><pre><code id="contentA" class="html"></code></pre></div>
        The 
        <div class="code-container-inline inline"><pre><code class="html inline">canvas</code></pre></div> is an HTML tag on which we can draw shapes, images, and text. Our
        <div class="code-container-inline inline"><pre><code class="html inline">script</code></pre></div>
        tag is where our JavaScript code goes. Here, we're grabbing a reference to the
        <div class="code-container-inline inline"><pre><code class="html inline">canvas</code></pre></div>
        tag, getting the 2D context which is what we'll use to draw, and then drawing a rectangle.

        <br><br>
        Three lines might seem excessive for a single draw operation, but the first two lines are boilerplate. We'll only need to write them once for the whole project.
        <br><br>
        Our little black rectangle might not look like much yet, but that's our player character!  Sadly the player doesn't move. We just need a little bit more code to handle the player's current position (I'll be highlighting removed lines with red and added lines with green).

        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentB" class="html"></code></pre>
                <pre><code id="contentC" class="javascript remove"></code></pre>
                <pre><code id="contentD" class="javascript add"></code></pre>
                <pre><code id="contentE" class="html"></code></pre>
        </div>
        
        We're managing our player's position with
        <div class="code-container-inline inline"><pre><code class="javascript inline">x</code></pre></div>
        and
        <div class="code-container-inline inline"><pre><code class="javascript inline">y</code></pre></div>
        coordinates (counted in grid tiles since our game will be played on a grid). 
        <br><br>
        Then the 
        <div class="code-container-inline inline"><pre><code class="javascript inline">onkeypress</code></pre></div>
        event will fire whenever the user presses a key (specifically lowercase "wasd" keys). We'll move the player horizontally or vertically by changing 
        <div class="code-container-inline inline"><pre><code class="javascript inline">x</code></pre></div>
        or
        <div class="code-container-inline inline"><pre><code class="javascript inline">y</code></pre></div>
        as appropriate. 
        <br><br>
        Finally, the 
        <div class="code-container-inline inline"><pre><code class="javascript inline">fillRect</code></pre></div>
        call is updated to use our new coordinate variables, moved into a function, and triggered with
        <div class="code-container-inline inline"><pre><code class="javascript inline">setInterval</code></pre></div>
        to make sure the screen is drawn every 15 milliseconds (more than 60 frames per second).
        
        <img src="screens/snake.png">
        It works but... there's no code to clear the character aftering rendering though. Let's do that at the beginning of every draw call.

        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentF" class="javascript"></code></pre>
                <pre><code id="contentG" class="javascript add"></code></pre>
                <pre><code id="contentH" class="javascript"></code></pre>
        </div>

        In 20 lines of code, we have a moving player. We're 90% of the way to having a functioning game. 😉
        <br><br>
        One problem is the canvas is not aligned properly, it's very small, and we don't know where it ends. Let's add an outline and center the canvas. To do this, we're going to add a 
        <div class="code-container-inline inline"><pre><code class="html inline">style</code></pre></div>
        tag to the top of our file and put CSS in it. If we had a lot of CSS, it would be better to put it in a separate file but the small amount we need fits well in the document.

        <div class="filename">index.html</div>
        <div class="code-container">
            <pre><code id="contentSTYLE" class="html add"></code></pre>
            <pre><code id="contentSTYLEAFTER" class="html"></code></pre>
        </div>
        <br>
        A couple more lines at the very top to make this truly valid HTML. The 
        <div class="code-container-inline inline"><pre><code class="html inline">doctype</code></pre></div>
        tells the browser we're an HTML page, duh. And a
        <div class="code-container-inline inline"><pre><code class="html inline">title</code></pre></div>
        is required for... reasons.

        <div class="filename">index.html</div>
        <div class="code-container"><pre><code id="contentDOCTYPE" class="html add"></code></pre></div>

        By the way, you might be used to 
        <div class="code-container-inline inline"><pre><code class="html inline">html</code></pre></div>
        /
        <div class="code-container-inline inline"><pre><code class="html inline">head</code></pre></div>
        /
        <div class="code-container-inline inline"><pre><code class="html inline">body</code></pre></div>
        tags but they're all optional! Seriously. Include them if you'd like.

        <h2>Getting organized</h2>
        Let's lay the groundwork for the rest of the project by creating a couple folders and empty files. Keep in mind, this is only to help us stay organized. There's no requirement to have a specific directory layout. If we keep all of our code in one file, it'll get really hard to dig through. But it's certainly not impossible. I know a major roguelike project that consists of a single 80,000 line python file.
        <br><br>
        Create a folder called <strong>js</strong> where all our script files will go and one called <strong>sounds</strong> where our sounds will go.
        <br><br>
        Go ahead and create six empty JavaScript files in the new <strong>js</strong> folder:

        <ul>
            <li>game.js</li>
            <li>map.js</li>
            <li>tile.js</li>
            <li>monster.js</li>
            <li>util.js</li>
            <li>spell.js</li>
        </ul>
        Now we'll include those JavaScript files using
        <div class="code-container-inline inline"><pre><code class="html inline">script</code></pre></div>
        tags.

        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentI" class="html"></code></pre>
                <pre><code id="contentJ" class="html add"></code></pre>
                <pre><code id="contentK" class="javascript"></code></pre>
        </div>
        <br>
        I'll be defining everything in these files globally and without using any sort of namespacing or modules. This makes the code shorter, but potentially harder to track down. One easy alternative is defining the entire file as an object like this:
        <div class="filename">game.js</div>
        <div class="code-container"><pre><code id="contentNAMESPACE" class="javascript"></code></pre></div>
        Then when you read
        <div class="code-container-inline inline"><pre><code class="javascript inline">game.someFunction()</code></pre></div>
        you'll know exactly where to find it (in the file game.js). For now, I'm going to stick to global functions.
        <br><br>
        Let's <em>move</em> our
        <div class="code-container-inline inline"><pre><code class="javascript inline">canvas</code></pre></div>
        and
        <div class="code-container-inline inline"><pre><code class="javascript inline">ctx</code></pre></div>
        definitions to a new function in game.js and call that new function from index.html.
        <div class="filename">index.html</div>

        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentL" class="html"></code></pre>
                <pre><code id="contentM" class="html remove"></code></pre>
                <pre><code id="contentN" class="javascript"></code></pre>
                <pre><code id="contentO" class="javascript add"></code></pre>
                <pre><code id="contentP" class="html"></code></pre>
        </div>



        <div class="filename">game.js</div>
        <div class="code-container">
                <pre><code id="contentQ" class="javascript add"></code></pre>
        </div>

        If you test the game, nothing should have changed. But we have taken the first step towards organizing our code.
        <br><br>
        We want to dynamically size the canvas based on some constants. This will make it really easy to tweak the design of the game later, making the grid size tiny or huge instantly.

        <div class="filename">game.js</div>
        <div class="code-container">
                <pre><code id="contentR" class="javascript"></code></pre>
                <pre><code id="contentS" class="javascript add"></code></pre>
                <pre><code id="contentT" class="javascript"></code></pre>
        </div>
        Run the code.
        <br><br>
        <img class="screen" src="screens/tileSizeUndefined.png">
        You should get an error. Uh oh. We forgot to define a few variables. Let's do that now.



        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentU" class="html"></code></pre>
                <pre><code id="contentV" class="javascript add"></code></pre>
                <pre><code id="contentW" class="javascript"></code></pre>
        </div>

        These 3 variables tell us respectively: how big a tile should be in on screen pixels, how wide/tall our map will be measured in tiles, and how much space we should reserve for the game UI.
        <br><br>
        That should fix the error we saw. Now we can also utilize some of these variables in the draw call.
        <br><br>

        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentX" class="javascript"></code></pre>
                <pre><code id="contentY" class="javascript remove"></code></pre>
                <pre><code id="contentZ" class="javascript add"></code></pre>
                <pre><code id="contentAA" class="javascript"></code></pre>
        </div>
        You'll notice that the "player" rectangle gets bigger and when you move to the bottom/edge of the screen, it lines up nicely.
        
        <h2>Drawing sprites</h2>  

        <div class="drawing-section">
            Finally, let's replace our little rectangle with a real sprite. You could make the whole game in rectangles if you wanted (it worked for Mike Bithell), but I'm not talented enough for that. 
            <br><br>
            Open up your favorite image editor. I'm going to use <a href="https://www.gimp.org/" target="_blank">GIMP</a>.
            <br><br>
            Here are some setup tips specific to GIMP:
            <ul>
                <li>
                    Select <strong>File > New</strong> and create a new file that's <strong>512 by 16 pixels</strong>. Make sure your starting background is transparent (under <strong>Advanced Options</strong> choose <strong>Fill with: Transparency</strong>). Normally, a spritesheet is arranged in big square but it's a little simpler to make it a long line of sprites.
                </li>
                <li>
                    A visible grid will help us distinguish our sprites. Select <strong>Image > Configure Grid</strong>, and configure the spacing to be <strong>16 pixels</strong> in both directions. Then select <strong>View > Show Grid</strong>.
                </li>
                <li>
                    Configure your <strong>Eraser Tool</strong> to use <strong>Hard edge</strong>. Otherwise, you won't fully erase things.
                </li>
                <li>
                    Select the <strong>Pencil Tool</strong> and make sure your <strong>Brush</strong> setting is configured to be <strong>Size: 1</strong>.
                </li>
                <li>
                    One extra, optional tip: sprites can be hard to see on the checkerboard pattern of transparent layers, so try adding an extra background layer filled with a darkish grey color.
                </li>
            </ul>
            With the <strong>Pencil Tool</strong> in hand, you're ready. We're going to use a simple 3 step process to draw our sprites:
            <ol>
                <li>Fill in a basic shape with 1 or 2 colors</li>
                <li>Add some shading</li>
                <li>Finish details and highlights</li>
            </ol>

            I'm going to show you each of my 3 steps, but only as a hint. Feel free to really make this art your own.
            <br><br>
            Draw your character sprite in the first 16 by 16 pixel tile.
            <img src="art/player.png">
            Draw your a corpse sprite in the next spot over. I cheated a bit by rotating the a copy of the first sprite 90 degrees.
            <img src="art/player-corpse.png">
            The last step after drawing each sprite is exporting your spritesheet by selecting <strong>File > Export</strong> and exporting to a file name <strong>spritesheet.png</strong> in your game's directory.
            <br><br>
            As a sanity check, here's what your spritesheet should look like at this point.
            <img src="screens/first-draw.png">


        </div>
        <br>
        Let's use the sprite we just drew. This code will load the PNG file.
        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentAB" class="html"></code></pre>
                <pre><code id="contentAC" class="javascript add"></code></pre>
        </div>
        <br>
        We're going to draw a sprite from that spritesheet now instead of drawing a rectangle. Let's try the simplest way to call 
        <div class="code-container-inline inline"><pre><code class="javascript inline">drawImage</code></pre></div>
        with 3 arguments.
        <div class="filename">index.html</div>
        <div class="code-container">
            <pre><code id="contentAD" class="javascript"></code></pre>
            <pre><code id="contentAE" class="javascript remove"></code></pre>
            <pre><code id="contentAF" class="javascript add"></code></pre>
            <pre><code id="contentAG" class="javascript"></code></pre>
        </div>

        <img src="screens/tinysprite.png">
        It works, but there's a few problem. The sprites are too small for one. The bigger problem though is that this displays the <em>whole</em> spritesheet. We need to call <div class="code-container-inline inline"><pre><code class="javascript inline">drawImage</code></pre></div> with <a href="https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage" target="_blank">9 arguments</a>.
        <br><br>
        It sounds bad, but it's pretty simple once you see the purpose. The first argument is the name of the image. The last four arguments are totally identical to the ones we used in 
        <div class="code-container-inline inline"><pre><code class="javascript inline">drawRect</code></pre></div>.
        Those specify how to draw to the <em>destination</em> on screen. But we're using a spritesheet which consists of many sprites. We can't use the whole image, so we need 4 more arguments to specify how to pull from the <em>source</em>.
        <br><br>
        One image, 4 source variables, and 4 destination variables.
        <br><br>
        <div class="code-container-inline inline"><pre><code class="javascript inline">ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);</code></pre></div>
        <br><br>
        Since we're going to be making this same call repeatedly, let's move this code out to a new function called
        <div class="code-container-inline inline"><pre><code class="javascript inline">drawSprite</code></pre></div>
        and move/update our
        <div class="code-container-inline inline"><pre><code class="javascript inline">draw</code></pre></div>
        function too. And let's put each argument on a different line to make it clearer.
        <div class="filename">index.html</div>
        <div class="code-container">
                <pre><code id="contentAH" class="javascript remove"></code></pre>
        </div>

        <div class="filename">game.js</div>
        <div class="code-container">
            <pre><code id="contentAI" class="javascript"></code></pre>
            <pre><code id="contentAJ" class="javascript add"></code></pre>
        </div>

        <img src="screens/blurry.png">

        Now one more hiccup. Our sprite looks like poop. The reason is the way browser scales images by default. It makes sene for photos but not for pixel art. For years, displaying scaled pixel art in browsers was basically impossible. Luckily we only need one line to correct it.
        <div class="filename">game.js</div>
        <div class="code-container">
            <pre><code id="contentAK" class="javascript"></code></pre>
            <pre><code id="contentAL" class="javascript add"></code></pre>
            <pre><code id="contentAM" class="javascript"></code></pre>
        </div>
        And we're done! If you're feeling a little overwhelmed by all these arcane references to interacting with the DOM (the way the browser connect HTML and JS together), I understand.
        <br><br>Take heart that we're pretty much past all of that. Our entry point into drawing stuff on the screen is to load images, grab a canvas element, get its context, and start calling draw operations on it. A lot of boilerplate right? But the good news is that we don't need any more hooks into the DOM and the rest of the code we write will be fairly self contained.
        <img src="screens/stage1-complete.png">
        <br><br>
        In the <a href="stage2.html">next section</a>, we'll generate a map.

    </div>

    <script>
        let content = {
            A: `
<canvas></canvas>
<script>
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");
    ctx.fillRect(0,0,20,20,20);
<\/script>
            `,
            STYLE: `
<style>
    canvas{
        outline: 1px solid white;
    }

    body{
        background-color: indigo;
        text-align: center;
        margin-top: 50px;
    }
<\/style>
            `,
            B: `
<script>
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");
            `,
            C: `
    ctx.fillRect(0,0,20,20,20);
            `,
            D: `
    x = y = 0;

    document.querySelector("html").onkeypress = function(e){
        if(e.key=="w") y--;
        if(e.key=="s") y++;
        if(e.key=="a") x--;
        if(e.key=="d") x++;
    };

    function draw(){
        ctx.fillRect (x*20,y*20,20,20);
    }

    setInterval(draw, 15);
            `,
            E: `
<\/script>
            `,
            F: `
function draw(){
            `,
            G: `
    ctx.clearRect(0,0,1000,1000);
            `,
            H: `
    ctx.fillRect (x*20,y*20,20,20);
}
            `,
            DOCTYPE: `
<!DOCTYPE html>
<title>AWESOME BROUGHLIKE</title>
            `,
            I: `
<canvas></canvas>
            `,
            J: `
<script src="https://nluqo.github.io/broughlike-tutorial/js/game.js"><\/script>
<script src="https://nluqo.github.io/broughlike-tutorial/js/map.js"><\/script>
<script src="https://nluqo.github.io/broughlike-tutorial/js/tile.js"><\/script>
<script src="https://nluqo.github.io/broughlike-tutorial/js/monster.js"><\/script>
<script src="https://nluqo.github.io/broughlike-tutorial/js/util.js"><\/script>
<script src="https://nluqo.github.io/broughlike-tutorial/js/spell.js"><\/script>
            `,
            K: `
<script>
    canvas = document.querySelector("canvas");
            `,
            NAMESPACE: `
game = {
    someFunction: function(){
        ...
    },
    anotherFunction: function(){
        ...
    },
    andAnother: function(){
        ...
    }
}
            `,
            L: `
<script>
            `,
            M: `
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");
          `,
            N: `
    x = y = 0;

    document.querySelector("html").onkeypress = function(e){
        if(e.key=="w") y--;
        if(e.key=="s") y++;
        if(e.key=="a") x--;
        if(e.key=="d") x++;
    };

    function draw(){
        ctx.fillRect (x*20,y*20,20,20);
    }

    setInterval(draw, 15);
            `,
            O: `

    setupCanvas();
            `,
            P: `
<\/script>
            `,
            Q: `
function setupCanvas(){
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");
}
            `,
            R: `
function setupCanvas(){
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");

            `,
            S: `
    canvas.width = tileSize*(numTiles+uiWidth);
    canvas.height = tileSize*numTiles;
    canvas.style.width = canvas.width + 'px';
    canvas.style.height = canvas.height + 'px';
            `,
            T: `
}
            `,
            U: `
<script>
            `,
            V: `
    tileSize = 64;
    numTiles = 9;
    uiWidth = 4;
            `,
            W: `

    x = y = 0;
            `,
            X: `
    function draw(){
            `,
            Y: `
        ctx.clearRect(0,0,1000,1000);
        ctx.fillRect (x*20,y*20,20,20);
            `,
            Z: `
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.fillRect (x*tileSize,y*tileSize,tileSize,tileSize);
            `,
            AA: `
    }
            `,
            AB: `
<script>
    tileSize = 64;
    numTiles = 9;
    uiWidth = 4;

    x = y = 0;
            `,
            AC: `
    spritesheet = new Image();
    spritesheet.src = 'spritesheet.png';
            `,
            AD: `
    function draw(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
            `,
            AE: `
        ctx.fillRect (x*tileSize,y*tileSize,tileSize,tileSize);
            `,
            AF: `
        ctx.drawImage(spritesheet, x*tileSize, y*tileSize);
            `,
            AG: `
    }
            `,
            AH: `
    function draw(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.drawImage(spritesheet, x*tileSize, y*tileSize);
    }
            `,
            AI: `
function setupCanvas(){
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");

    canvas.width = tileSize*(numTiles+uiWidth);
    canvas.height = tileSize*numTiles;
    canvas.style.width = canvas.width + 'px';
    canvas.style.height = canvas.height + 'px';
}

            `,
            AJ: `
function drawSprite(sprite, x, y){
    ctx.drawImage(
        spritesheet,
        sprite*16,
        0,
        16,
        16,
        x*tileSize,
        y*tileSize,
        tileSize,
        tileSize
    );
}

function draw(){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    drawSprite(0, x, y);
}
            `,
            AK: `
function setupCanvas(){
    canvas = document.querySelector("canvas");
    ctx = canvas.getContext("2d");

    canvas.width = tileSize*(numTiles+uiWidth);
    canvas.height = tileSize*numTiles;
    canvas.style.width = canvas.width + 'px';
    canvas.style.height = canvas.height + 'px';
            `,
            AL: `
    ctx.imageSmoothingEnabled = false;
            `,
            AM: `
}
            `,
            STYLEAFTER: `

<canvas></canvas>
<script>
    canvas = document.querySelector("canvas");
            `
        };

    </script>

    <link rel="stylesheet" href="highlight.min.css">
    <link rel="stylesheet" href="style.css">
    <script src="highlight.min.js"></script>
    <script src="diff.js"></script>
</body>
</html>