Skip to content

Commit 0af61fe

Browse files
committed
Integrate new game graphics
1 parent 9c61163 commit 0af61fe

File tree

7 files changed

+31
-24
lines changed

7 files changed

+31
-24
lines changed

Diff for: 16_canvas.txt

+23-17
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,10 @@ copied, and the sixth to ninth argument give the rectangle (on the
533533
canvas) into which it should be copied.
534534

535535
This can be used to pack multiple _sprites_ (image elements) into a
536-
single image file, and then copy out the part you need. For example,
536+
single image file, and then only draw the part you need. For example,
537537
we have this picture containing a game character in multiple poses.
538538

539-
// FIXME update with new art
540-
541-
image::img/player.png[alt="Various poses of a game character"]
539+
image::img/player_big.png[alt="Various poses of a game character"]
542540

543541
If we alternate which pose we draw, we can show an animation that
544542
looks like a walking character.
@@ -547,7 +545,7 @@ To animate the picture on a canvas, the `clearRect` method is useful.
547545
It resembles `fillRect`, but instead of coloring the rectangle, it
548546
resets it back to transparent.
549547

550-
We know that each _sprite_, each sub-picture, is 16 pixels wide and 30
548+
We know that each _sprite_, each sub-picture, is 24 pixels wide and 30
551549
pixels high. The code below loads the image, and then sets up an
552550
interval (repeated timer) to draw the next frame.
553551

@@ -558,7 +556,7 @@ interval (repeated timer) to draw the next frame.
558556
var cx = document.querySelector("canvas").getContext("2d");
559557
var img = document.createElement("img");
560558
img.src = "img/player.png";
561-
var spriteW = 16, spriteH = 30;
559+
var spriteW = 24, spriteH = 30;
562560
img.addEventListener("load", function() {
563561
var cycle = 0;
564562
setInterval(function() {
@@ -684,7 +682,7 @@ the world around the character's vertical center:
684682
var cx = document.querySelector("canvas").getContext("2d");
685683
var img = document.createElement("img");
686684
img.src = "img/player.png";
687-
var spriteW = 16, spriteH = 30;
685+
var spriteW = 24, spriteH = 30;
688686
img.addEventListener("load", function() {
689687
flipHorizontally(cx, 100 + spriteW / 2);
690688
cx.drawImage(img, 0, 0, spriteW, spriteH,
@@ -932,7 +930,7 @@ Tiles that are not empty (null) are drawn with `drawImage`. The
932930
are not the player. It contains, from left to right, the wall tile,
933931
the lava tile, and then the sprite for a coin.
934932

935-
image::img/sprites_background.png[alt="Sprites for our game"]
933+
image::img/sprites_big.png[alt="Sprites for our game"]
936934

937935
Background tiles are 20 by 20 pixels, since we will use the same scale
938936
that we used in `DOMDisplay`. Thus, the offset for lava tiles is 20
@@ -947,13 +945,18 @@ scene will appear as soon as the loading finishes.
947945

948946
The walking character we used before will be used to represent the
949947
player. The code that draws it needs to pick the right sprite and
950-
direction based on the player's current motion. When the player is
951-
standing still, we draw the leftmost sprite. During jumps (when the
952-
vertical speed is not zero), we use the rightmost sprite. When
953-
walking, we cycle through the first 8 sprites based on the display's
954-
`animationTime` property. This is measured in seconds, and we want to
955-
switch frames twelve times per second, so the time is multiplied by 12
956-
first.
948+
direction based on the player's current motion. The first 8 sprites
949+
contain a walking animation. When the player is moving along a floor,
950+
we cycle through them based on the display's `animationTime` property.
951+
This is measured in seconds, and we want to switch frames twelve times
952+
per second, so the time is multiplied by 12 first. When the player is
953+
standing still, we draw the ninth sprite. During jumps (when the
954+
vertical speed is not zero), we use the tenth, rightmost sprite.
955+
956+
Because the sprites are slightly wider than the player object—24
957+
instead of 16 pixels, to allow some space for feet and arms—the method
958+
has to adjust the x coordinate and width by a given amount
959+
(`playerXOverlap`).
957960

958961
// include_code
959962

@@ -962,15 +965,18 @@ first.
962965
----
963966
var playerSprites = document.createElement("img");
964967
playerSprites.src = "img/player.png";
968+
var playerXOverlap = 4;
965969

966970
CanvasDisplay.prototype.drawPlayer = function(x, y, width,
967971
height) {
968-
var sprite = 0, player = this.level.player;
972+
var sprite = 8, player = this.level.player;
973+
width += playerXOverlap * 2;
974+
x -= playerXOverlap;
969975
if (player.speed.x)
970976
this.flipPlayer = player.speed.x < 0;
971977

972978
if (player.speed.y)
973-
sprite = 8;
979+
sprite = 9;
974980
else if (player.speed.x)
975981
sprite = Math.floor(this.animationTime * 12) % 8;
976982

Diff for: html/index.html

+8-7
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ <h1>Eloquent JavaScript<div style="font-size: 70%">second edition</div></h2>
2121
an <a href="http://opensource.org/licenses/MIT">MIT license</a>.</p>
2222

2323
<p>Illustrations by various artists: Cover by Wasif Hyder. Computer
24-
(introduction) by Max Xiantu. Sea of bits (chapter 1) and
25-
weresquirrel (chapter 4) by Margarita Martínez and José Menor.
26-
Octopuses (chapter 2 and 4) by Jim Tierney. Object with on/off
27-
switch (chapter 6) by Dyle MacGregor. Regular expression diagrams in
28-
chapter 9 generated
24+
(introduction) by Max Xiantu. Sea of bits (Chapter 1) and
25+
weresquirrel (Chapter 4) by Margarita Martínez and José Menor.
26+
Octopuses (Chapter 2 and 4) by Jim Tierney. Object with on/off
27+
switch (Chapter 6) by Dyle MacGregor. Regular expression diagrams in
28+
Chapter 9 generated
2929
with <a href="http://regexper.com">regexper.com</a> by Jeff
30-
Avallone. Game concept for chapter 15
31-
by <a href="http://lessmilk.com">Thomas Palef</a>.</p>
30+
Avallone. Game concept for Chapter 15
31+
by <a href="http://lessmilk.com">Thomas Palef</a>. Pixel art in
32+
Chapter 16 by Antonio Perdomo Pastor.</p>
3233

3334
<h2>Contents</h2>
3435

Diff for: img/player.png

-6.74 KB
Loading

Diff for: img/player_big.png

2.63 KB
Loading

Diff for: img/sprites.png

-1.15 KB
Loading

Diff for: img/sprites_background.png

-2.47 KB
Binary file not shown.

Diff for: img/sprites_big.png

1.14 KB
Loading

0 commit comments

Comments
 (0)