<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.3">Jekyll</generator><link href="https://ikzer.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://ikzer.github.io/" rel="alternate" type="text/html" /><updated>2023-09-15T14:38:31+02:00</updated><id>https://ikzer.github.io/feed.xml</id><title type="html">Ikzer’s Blog</title><subtitle>Dev blog that nobody will read but eh.
</subtitle><author><name>Fernando Delgado</name></author><entry><title type="html">Manga List design and requirements</title><link href="https://ikzer.github.io/2023/09/12/manga-list-design.html" rel="alternate" type="text/html" title="Manga List design and requirements" /><published>2023-09-12T20:30:00+02:00</published><updated>2023-09-12T20:30:00+02:00</updated><id>https://ikzer.github.io/2023/09/12/manga-list-design</id><content type="html" xml:base="https://ikzer.github.io/2023/09/12/manga-list-design.html"></content><author><name>Fernando Delgado</name></author><category term="Web Projects" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Initialize Next JS Project for the Manga List project</title><link href="https://ikzer.github.io/2023/09/12/initialize-next-js-project.html" rel="alternate" type="text/html" title="Initialize Next JS Project for the Manga List project" /><published>2023-09-12T16:30:00+02:00</published><updated>2023-09-12T16:30:00+02:00</updated><id>https://ikzer.github.io/2023/09/12/initialize-next-js-project</id><content type="html" xml:base="https://ikzer.github.io/2023/09/12/initialize-next-js-project.html"></content><author><name>Fernando Delgado</name></author><category term="NextJS" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Breakout V1 Part III: The Blocks</title><link href="https://ikzer.github.io/2022/11/19/breakout-v1-part-iii.html" rel="alternate" type="text/html" title="Breakout V1 Part III: The Blocks" /><published>2022-11-19T19:30:00+01:00</published><updated>2022-11-19T19:30:00+01:00</updated><id>https://ikzer.github.io/2022/11/19/breakout-v1-part-iii</id><content type="html" xml:base="https://ikzer.github.io/2022/11/19/breakout-v1-part-iii.html">&lt;p&gt;&lt;img style=&quot;float: left; width: 25%;&quot; src=&quot;/images/breakout1.png&quot; /&gt;
Let’s continue building the Breakout game, the game in which you destroy blocks by bouncing a ball against them. We left the game in a basic state, with a ball bouncing endlessly on the walls of the canvas, and a minimal library of funtions to help refactor code. This time we will talk about the blocks.&lt;/p&gt;

&lt;h2 id=&quot;the-paddle&quot;&gt;The Paddle&lt;/h2&gt;

&lt;p&gt;First o all we have to add the paddle that we will control, where the ball should bounce to not lose the game. First we define its dimensions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var paddlex;
var paddleh;
var paddlew;
 
function init_paddle() {
 paddlex = WIDTH / 2;
 paddleh = 10;
 paddlew = 75;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;paddlex&lt;/code&gt; is the center point of the bar, it will be useful later. Then we modify the &lt;code&gt;paint()&lt;/code&gt; to draw the bar and make the ball bounce on the superior edge of it (not below), and that it only bounces below if it’s touching the paddle:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 clear();
 circle(x, y, 10);
 
// Draw the paddle
 rect(paddlex, HEIGHT-paddleh, paddlew, paddleh);
 
 // Check boundaries to bounce
 if (x + dx &amp;gt; WIDTH || x + dx &amp;lt; 0)
 dx = -dx;
 
if (y + dy &amp;lt; 0)
 dy = -dy;
 else if (y + dy &amp;gt; HEIGHT) {
 if (x &amp;gt; paddlex &amp;amp;&amp;amp; x &amp;lt; paddlex + paddlew)
 
// If it's inside the paddle we change direction
 dy = -dy;
 else
 // If its outside the bar, we stop the animation and end the game
 clearInterval(game_loop);
 }
 
 x += dx;
 y += dy;
}
 
var game_loop = init();
init_paddle();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;FOr starters, on line 6 we draw the rectangle from the middle point &lt;code&gt;paddlex&lt;/code&gt; with the dimensions we defined previously.&lt;/p&gt;

&lt;p&gt;Between lines 11 and 21 we introduce the “magic” so the ball bounce on the paddle and stops if it falls outside. On line 8 we see the condition to bounce on the left and right walls:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (x + dx &amp;gt; WIDTH || x + dx &amp;lt; 0)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If the new &lt;code&gt;x&lt;/code&gt; position is bigger than the &lt;code&gt;WIDTH&lt;/code&gt; or less than 0 we change the direction.&lt;/p&gt;

&lt;p&gt;We did the same on &lt;code&gt;y&lt;/code&gt; before:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (y + dy &amp;gt; HEIGHT || y + dy &amp;lt; 0)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;[breakoutv1-2] But this time we only want it to bounce if it touches te top wall, so we remove the condition for &lt;code&gt;(y + dy &amp;gt; HEIGHT)&lt;/code&gt; on line 9, leaving just &lt;code&gt;(y + dy &amp;lt; 0)&lt;/code&gt; to make it bounce, and introducing another condition for the bottom boundaries since we need to make more checks before.&lt;/p&gt;

&lt;p&gt;If the ball its about to fall from the bottom wall, we check it its position on &lt;code&gt;x&lt;/code&gt; its inside our paddle &lt;code&gt;(x &amp;lt; paddlex + paddlew)&lt;/code&gt;. If its inside that interval, we make it bounce changing the direction of &lt;code&gt;dy&lt;/code&gt;, and if it’s not it means it’s outside the paddle so we lost the game and we have to stop the animation.&lt;/p&gt;

&lt;p&gt;To stop the animation we need two things. First, with &lt;code&gt;clearInterval(game_loop)&lt;/code&gt; we stop the &lt;code&gt;setInterval()&lt;/code&gt; that we create on &lt;code&gt;init()&lt;/code&gt;. But to clear that particular interval we need to pass it to &lt;code&gt;clearInterval&lt;/code&gt;, in this case using the &lt;code&gt;game_loop&lt;/code&gt; variable. As we set up &lt;code&gt;init()&lt;/code&gt; to return the &lt;code&gt;setInterval()&lt;/code&gt;, we store it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var game_loop = init()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And that’s what we pass to &lt;code&gt;clearInterval()&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;movement&quot;&gt;Movement&lt;/h2&gt;

&lt;p&gt;We have two of the main elements on stage. Let’s make them interactive. As I said, we will use the keyboard as well as the mouse to move the paddle, which moves only in one dimension (on the X axis), which makes things a lot easier.&lt;/p&gt;

&lt;p&gt;This time the movement needs to be constant, unlike last time, so it’s not enough if a key has been pressed: we need to know if it’s still pressed to keep the paddle moving. To do that we have to follow a few stpes, and we will use a little jQuery magic to ease things out:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Know if a key has been pressed&lt;/li&gt;
  &lt;li&gt;Move the paddle while it’s pressed&lt;/li&gt;
  &lt;li&gt;Know if a key stopped being pressed&lt;/li&gt;
  &lt;li&gt;Stop the paddle when it stops being pressed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First of all, we store the state of the pulsation in two variables (left and right), assuming they are not pressed:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;rightDown = false; 
leftDown = false;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And to not repeat code everywhere, we create two functions, &lt;code&gt;onKeyDown()&lt;/code&gt; and &lt;code&gt;onKeyUp()&lt;/code&gt; to change the state of said variables when needed (toggle true and false), and then bind the functions to the browser events via jQuery:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Activate leftDown or rightDown if hte left or right arrow keys are pressed
function onKeyDown(evt) {
 if (evt.keyCode == 39) rightDown = true;
 else if (evt.keyCode == 37) leftDown = true;
}
// And deactivate them when not
function onKeyUp(evt) {
 if (evt.keyCode == 39) rightDown = false;
 else if (evt.keyCode == 37) leftDown = false;
}
 
// Bind these functions to their corresponding events
// that will trigger them when they are pressed
$(document).keydown(onKeyDown);
$(document).keyup(onKeyUp);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once we have detected the key pulsation, we need to move the paddle accordingly (on the &lt;code&gt;paint()&lt;/code&gt; function):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 clear();
 circle(x, y, 10);
 
 // Move the paddle if left or right are pressed
 if (rightDown) paddlex += 5;
 else if (leftDown) paddlex -= 5;
 
// Draw the paddle
 rect(paddlex, HEIGHT-paddleh, paddlew, paddleh);
 
 if (x + dx &amp;gt; WIDTH || x + dx &amp;lt; 0)
 dx = -dx;
 
if (y + dy &amp;lt; 0)
 dy = -dy;
 else if (y + dy &amp;gt; HEIGHT) {
 if (x &amp;gt; paddlex &amp;amp;&amp;amp; x &amp;lt; paddlex + paddlew)
 dy = -dy;
 else
 clearInterval(game_loop);
 }
 
 x += dx;
 y += dy;
}
 
var game_loop = init();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here in the lines 5 to 7 we update the &lt;code&gt;x&lt;/code&gt; position of the paddle by checking the right and left movements variables before drawing its rectangle, then draw it normally.&lt;/p&gt;

&lt;p&gt;I’m refactoring some code now to introduce the &lt;code&gt;init_paddle()&lt;/code&gt; inside of &lt;code&gt;init()&lt;/code&gt;, since we just need to initialize the position of &lt;code&gt;paddlex&lt;/code&gt; once, as the others are constants and can be defined as globals.&lt;/p&gt;

&lt;p&gt;For the mouse movement we need to work a little bit. First, we need to know if the cursor is inside the canvas, for which we use the variables &lt;code&gt;canvasMinX&lt;/code&gt; and &lt;code&gt;canvasMaxX&lt;/code&gt;, that gives us the left and right boundaries positions on the screen. We declare them as globals and initialize them on &lt;code&gt;init()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;canvasMinX = $(&quot;#canvas&quot;).offset().left;
canvasMaxX = canvasMinX + WIDTH;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For &lt;code&gt;canvasMinX&lt;/code&gt; we get the pòsition of the left wall of the canvas in relative to the left of the window via the &lt;code&gt;offset()&lt;/code&gt; method of jQuery, that gives us the positoin of an object relative to the document. The position of the right will be left plus the width.&lt;/p&gt;

&lt;p&gt;The, as we did with the keyboard, we create the helper &lt;code&gt;onMouseMove()&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Move the paddle if the mouse is inside the canvas:
function onMouseMove(evt) {
 if (evt.pageX &amp;gt; canvasMinX &amp;amp;&amp;amp; evt.pageX &amp;lt; canvasMaxX) {
 paddlex = evt.pageX - canvasMinX;
 }
}
 
// We bind the mouse movement function to the browser event
$(document).mousemove(onMouseMove);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We pass this function to the event manager &lt;code&gt;evt&lt;/code&gt;, it will check the position of the mouse on X (&lt;code&gt;evt.pageX&lt;/code&gt;) and if it’s inside the canvas we change the position of the bar to where the mouse is (only on X). Finally we bind it via jQuery like before.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;With this we have the basic interaction and a working ball and paddle. Next comes the blocks.&lt;/p&gt;

&lt;h2 id=&quot;index&quot;&gt;Index&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;2022/10/15/breakout-v1-part-i&quot;&gt;Initialization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/11/05/breakout-v1-part-ii&quot;&gt;Refactor and Movement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Paddle&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/12/03/breakout-v1-part-iv&quot;&gt;The Blocks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;&quot;&gt;Finishing Touches&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html">Let’s continue building the Breakout game, the game in which you destroy blocks by bouncing a ball against them. We left the game in a basic state, with a ball bouncing endlessly on the walls of the canvas, and a minimal library of funtions to help refactor code. This time we will talk about the blocks.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Breakout V1 Part II: Refactor and Movement</title><link href="https://ikzer.github.io/2022/11/05/breakout-v1-part-ii.html" rel="alternate" type="text/html" title="Breakout V1 Part II: Refactor and Movement" /><published>2022-11-05T19:30:00+01:00</published><updated>2022-11-05T19:30:00+01:00</updated><id>https://ikzer.github.io/2022/11/05/breakout-v1-part-ii</id><content type="html" xml:base="https://ikzer.github.io/2022/11/05/breakout-v1-part-ii.html">&lt;p&gt;&lt;img style=&quot;float: left; width: 25%;&quot; src=&quot;/images/breakout1.png&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;movement&quot;&gt;Movement&lt;/h2&gt;

&lt;p&gt;Let’s continue. For now we have drawn a static ball.  It’s time to move it. FOr starters we initialize the position and create an &lt;code&gt;init()&lt;/code&gt; function to have the initial state of the game and the update interval that will call the future &lt;code&gt;paint()&lt;/code&gt; function that will draw the content and make the actual movements:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var x = 150;
var y = 150;
var dx = 2;
var dy = 4;
var ctx;
 
function init() {
 ctx = $('#canvas')[0].getContext(&quot;2d&quot;);
 return setInterval(paint, 10);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are the initial position of the ball in the canvas, in this case it’s the middle. &lt;code&gt;dx&lt;/code&gt; and &lt;code&gt;dy&lt;/code&gt; are the velocity in both axes, that will dictate how much the ball will move in both directions.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;paint()&lt;/code&gt; function now has to draw the ball and make it move, so it has to redraw everything in every frame:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 // Clean the canvas
 ctx.clearRect(0,0,300,300);
 // Draw the canvas
 ctx.fillStyle = &quot;white&quot;;
 ctx.fillRect(0, 0, w, h);
 ctx.strokeStyle = &quot;black&quot;;
 ctx.strokeRect(0, 0, w, h);
 
 // Draw the circle
 ctx.beginPath();
 ctx.arc(x, y, 10, 0, Math.PI*2, true);
 ctx.fillStyle = &quot;black&quot;;
 ctx.closePath();
 ctx.fill();
 x += dx;
 y += dy;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Starting on line 3 we see the &lt;code&gt;clearRect()&lt;/code&gt; function with which we delete all that was drawn on the canvas previously. Then we move the canvas initialization and the circle draw instructions inside &lt;code&gt;paint()&lt;/code&gt;, but this time instead of using a fixed position we use the variables we created just before.&lt;/p&gt;

&lt;p&gt;On &lt;code&gt;init()&lt;/code&gt; we defined a &lt;code&gt;setInterval()&lt;/code&gt; that will call this function every 10ms, so in every call everything will be redrawn. To create the movement we created some velocity variables, &lt;code&gt;dx&lt;/code&gt; and &lt;code&gt;dy&lt;/code&gt;, so in every call  we will change de position adding this variables so, for example, if in the first call the middle is on (150,150), on the next call it will be (152,154), then (154.158) and so on. This way every time the frame is drawn the ball will be in a different position, creating the illusion of movement. Trying different values (including negatives) on &lt;code&gt;dx&lt;/code&gt; and &lt;code&gt;dy&lt;/code&gt; will change speed and direction.&lt;/p&gt;

&lt;h2 id=&quot;refactor&quot;&gt;Refactor&lt;/h2&gt;

&lt;p&gt;We will need to repeat a lot of things constantly from now on so we need to tidy up the code now before it’s too late. We will create functions for the most basic stuff: draw the frame, draw the circle, initialize the game and draw a rectangle, and of course define some variables.&lt;/p&gt;

&lt;p&gt;Variables:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var x = 150; // Initial position on X
var y = 150; // Initial position on Y
var dx = 2; // Velocity on X
var dy = 4; // Velocity on Y
var WIDTH; // Width of the frame
var HEIGHT; // Height of the frame
var ctx;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Initialize everything:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function init() {
 ctx = $('#canvas')[0].getContext(&quot;2d&quot;); // Obtain the context from the canvas
 WIDTH = $(&quot;#canvas&quot;).width(); // Assign the size to the variables
 HEIGHT = $(&quot;#canvas&quot;).height();
 return setInterval(paint, 10); // Call paint() every 10 ms
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Draw a circle:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Draw a circle with its center on (x,y), radius r and a color 
 
function circle(x,y,r,color) {
 ctx.beginPath();
 ctx.arc(x, y, r, 0, Math.PI*2, true);
 ctx.fillStyle = color;
 ctx.closePath();
 ctx.fill();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Draw a rectangle:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Draw a rectangle of width w, height h and its top left corner on (x,y)
function rect(x,y,w,h) {
 ctx.beginPath();
 ctx.rect(x,y,w,h);
 ctx.closePath();
 ctx.fill();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Clean the frame:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Clear the canvas
function clear() {
 ctx.clearRect(0, 0, WIDTH, HEIGHT);&amp;lt;/pre&amp;gt;
// Draw the canvas
 ctx.fillStyle = &quot;white&quot;;
 ctx.fillRect(0, 0, WIDTH, HEIGHT);
 ctx.strokeStyle = &quot;black&quot;;
 ctx.strokeRect(0, 0, WIDTH, HEIGHT);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Code in action (draw and initialize):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 clear();
 circle(x, y, 10, &quot;black&quot;);
 
 x += dx;
 y += dy;
}
 
init();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All the code before &lt;code&gt;paint()&lt;/code&gt; shouldn’t be too modified from now on (or at all), it’s kind of a basic library to use when necessary.&lt;/p&gt;

&lt;p&gt;Now that we have those in place and a ball that moves in a certain way and falls in a certain place, we need to contain it inside the boundaries of the canvas:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 clear();
 circle(x, y, 10);
 
 if (x + dx &amp;gt; WIDTH || x + dx &amp;lt; 0)
 dx = -dx;
 if (y + dy &amp;gt; HEIGHT || y + dy &amp;lt; 0)
 dy = -dy;
 
 x += dx;
 y += dy;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;img src=&quot;/images/breakoutv1-1.png&quot; alt=&quot;Breakout V1&quot; /&gt; To prevent if from falling or going out of the boundaries we check if the new position will be outside of said boundaries and change the direction before we actually update it. If its current position plus &lt;code&gt;dx&lt;/code&gt; or &lt;code&gt;dy&lt;/code&gt; is outside (greater than &lt;code&gt;WIDTH&lt;/code&gt; by the right, less than 0 by the left, greater than &lt;code&gt;HEIGHT&lt;/code&gt; on top or less than 0 on bottom), we change the sign of &lt;code&gt;dx&lt;/code&gt; or &lt;code&gt;dy&lt;/code&gt; according so the ball goes in the opposite direction in that axis, creating the illusion of “bouncing”. We can also make it accelerate if we increase &lt;code&gt;dx&lt;/code&gt; or &lt;code&gt;dy&lt;/code&gt;, but that will have to wait until there are levels in the future.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;We have plenty now. We created the canvas and put a ball inside. The ball moves constantly and bounces on all four walls, being this latest the key improvement for now. Next time we will create the bottom paddle, control it and the blocks.&lt;/p&gt;

&lt;h2 id=&quot;index&quot;&gt;Index&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/10/15/breakout-v1-part-i&quot;&gt;Initialization&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Refactor and Movement&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/11/19/breakout-v1-part-iii&quot;&gt;The Blocks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;&quot;&gt;Finishing Touches&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Breakout V1 Part I: Initialization</title><link href="https://ikzer.github.io/2022/10/15/breakout-v1-part-i.html" rel="alternate" type="text/html" title="Breakout V1 Part I: Initialization" /><published>2022-10-15T20:30:00+02:00</published><updated>2022-10-15T20:30:00+02:00</updated><id>https://ikzer.github.io/2022/10/15/breakout-v1-part-i</id><content type="html" xml:base="https://ikzer.github.io/2022/10/15/breakout-v1-part-i.html">&lt;p&gt;&lt;img style=&quot;float: left; width: 25%;&quot; src=&quot;/images/breakout1.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Breakout&lt;/strong&gt; is another classic among classics, it has thousands upon thousands of versions and variants in every platform ever and is great to kill some time. And it’s incredibly simple: using a paddle, like in Pong, and a bouncing ball, we change the direction of said paddle to hit the blocks in the top, eliminating them and earning points. That’s it.&lt;/p&gt;

&lt;h2 id=&quot;description&quot;&gt;Description&lt;/h2&gt;

&lt;p&gt;We have three elements: a paddle at the bottom that moves horizontally, a ball constantly bouncing in the walls and other elements, and some blocks in the upper half that get destroyed when the ball hits them. The goal of the game is eliminate all of the blocks, and the game ends if the ball touches the bottom wall below the paddle before eliminating all the blocks. This time we will be able to use the mouse besides the keyboard to play.&lt;/p&gt;

&lt;p&gt;In this first version, and like I did with Snake, we will be making only the gameplay, without any decorations.&lt;/p&gt;

&lt;h2 id=&quot;requisites&quot;&gt;Requisites&lt;/h2&gt;

&lt;p&gt;Like in the previous game, there isn’t any requisite, graphics, libraries or anything like that, but I’ll be using HTML5 (canvas) and jQuery to simplify things, but it’s not really necessary.&lt;/p&gt;

&lt;h2 id=&quot;development&quot;&gt;Development&lt;/h2&gt;

&lt;p&gt;The first thing is create the HTML skeleton where we will make the game, and call the respective libraries. We will be using a 300x300 canvas:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;!DOCTYPE HTML&amp;gt;
&amp;lt;html&amp;gt;
 &amp;lt;body&amp;gt;
 
 &amp;lt;!-- Initialize the 300x300 canvas --&amp;gt;
 &amp;lt;canvas id=&quot;canvas&quot; width=&quot;300&quot; height=&quot;300&quot;&amp;gt;&amp;lt;/canvas&amp;gt;
 
 &amp;lt;!-- JQuery --&amp;gt;
 &amp;lt;script src=&quot;http://code.jquery.com/jquery-2.0.2.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;script src=&quot;breakout1.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;
 &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the corresponding initializations to the canvas in Javascript:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Get the reference to the canvas
var ctx = $('#canvas')[0].getContext(&quot;2d&quot;);
 
var w = $(&quot;#canvas&quot;).width(); // Store width and height
var h = $(&quot;#canvas&quot;).height();
 
// Draw the canvas
 ctx.fillStyle = &quot;white&quot;;
 ctx.fillRect(0, 0, w, h);
 ctx.strokeStyle = &quot;black&quot;;
 ctx.strokeRect(0, 0, w, h);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In line 2 we save a step by saving the context directly, instead of having an auxiliar variable to store the canvas like in Snake. In lines 6 to 10 we simply draw the border of the canvas and paint the background white.&lt;/p&gt;

&lt;h2 id=&quot;circle&quot;&gt;Circle&lt;/h2&gt;

&lt;p&gt;Nows lets see some new instructions to use with canvas besides filing a square:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Draw a circle
ctx.beginPath();
ctx.arc(75, 75, 10, 0, Math.PI*2, true);
ctx.fillStyle = &quot;black&quot;;
ctx.closePath();
ctx.fill();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In line 2 we see the &lt;code&gt;beginPath()&lt;/code&gt; method that initializes a new figure, and &lt;code&gt;closePath()&lt;/code&gt; on line 5 closes it. The instructions in between will be the ones that draw said figure. In our case we use &lt;code&gt;arc()&lt;/code&gt; to draw a circle as an arc of 2*PI radius, whicih forms a full circle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;arc(x, y, r, sAngle, eAngle, direction)&lt;/code&gt; takes six arguments:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are the coordinates for the &lt;strong&gt;center&lt;/strong&gt; of the circle.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;r&lt;/code&gt; is the **radius.&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;sAngle&lt;/code&gt; is the angle in which the curve starts to draw in radians (0 is the 3 o`clock position).&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;eAngle&lt;/code&gt; is the final angle (2*PI equals to 0).&lt;/li&gt;
  &lt;li&gt;&lt;code&gt;direction&lt;/code&gt; tells it to draw clockwise (0) or counterclockwise (1).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then we define again the color of the figure with &lt;code&gt;fillStyle&lt;/code&gt; y actually fill it with &lt;code&gt;fill()&lt;/code&gt; so it’s painted on the screen. If we use &lt;code&gt;stroke()&lt;/code&gt; instead of &lt;code&gt;fill()&lt;/code&gt;, instead of a full color circle we will obtain only the contour with the designated color.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fillStyle()&lt;/code&gt; and &lt;code&gt;fillStroke()&lt;/code&gt; can receive arguments in many formats. We can use named colors, or pass it hexadecimal colors like HTML (#FFFFFF), or in &lt;code&gt;rgba(red,green,blue,alpha)&lt;/code&gt; format to give it transparency.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;With this we have initialized the project, we have a first view of the game field and the ball that we will be using. Next time we will refactor and make some auxiliary functions, and start moving things!&lt;/p&gt;

&lt;h2 id=&quot;index&quot;&gt;Index&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Initialization&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/11/05/breakout-v1-part-ii&quot;&gt;Refactor and Movement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;/2022/11/19/breakout-v1-part-iii&quot;&gt;The Blocks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;&quot;&gt;Finishing Touches&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">GameRunner: The Minimal JavaScript Game Framework</title><link href="https://ikzer.github.io/2022/10/01/game-runner-part-i.html" rel="alternate" type="text/html" title="GameRunner: The Minimal JavaScript Game Framework" /><published>2022-10-01T20:30:00+02:00</published><updated>2022-10-01T20:30:00+02:00</updated><id>https://ikzer.github.io/2022/10/01/game-runner-part-i</id><content type="html" xml:base="https://ikzer.github.io/2022/10/01/game-runner-part-i.html">&lt;h1 id=&quot;gamerunner-the-minimal-javascript-game-framework&quot;&gt;GameRunner: The Minimal JavaScript Game Framework&lt;/h1&gt;

&lt;p&gt;GameRunner is a minimal JavaScript game framework that provides a simple and easy-to-use API for creating 2D games. It is based on the Phaser game engine, but it is stripped down to its bare essentials.&lt;/p&gt;

&lt;h2 id=&quot;features&quot;&gt;Features&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Simple API:&lt;/strong&gt; GameRunner provides a simple and easy-to-use API that makes it easy to create 2D games.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Efficient:&lt;/strong&gt; GameRunner is highly efficient, making it ideal for games that need to run on mobile devices.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Extendable:&lt;/strong&gt; GameRunner is extensible, allowing you to add your own features and functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h2&gt;

&lt;p&gt;To get started with GameRunner, you will need to install the following dependencies:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install gamerunner&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once you have installed the dependencies, you can create a new game by creating a new HTML file and adding the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;html
&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang=&quot;en&quot;&amp;gt;
&amp;lt;head&amp;gt;
&amp;lt;title&amp;gt;My Game&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;canvas id=&quot;game&quot; width=&quot;640&quot; height=&quot;480&quot;&amp;gt;&amp;lt;/canvas&amp;gt;
&amp;lt;script src=&quot;gamerunner.js&quot;&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
// Create a new game instance
const game = new GameRunner({
canvas: document.querySelector(&quot;#game&quot;),
});

// Start the game
game.start();

// Update the game loop
game.onUpdate(() =&amp;gt; {
  // ...
});

// Render the game
game.onDraw(() =&amp;gt; {
  // ...
});
&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This code will create a new game instance with a canvas width of 640 pixels and a height of 480 pixels. It will also start the game loop, which will run every frame.&lt;/p&gt;

&lt;h2 id=&quot;update-and-draw-callbacks&quot;&gt;Update and Draw Callbacks&lt;/h2&gt;

&lt;p&gt;The game loop is called twice per frame, once for the &lt;code&gt;update()&lt;/code&gt; callback and once for the &lt;code&gt;draw()&lt;/code&gt; callback. The &lt;code&gt;update()&lt;/code&gt; callback is called to update the game state, and the &lt;code&gt;draw()&lt;/code&gt; callback is called to render the game to the canvas.&lt;/p&gt;

&lt;h2 id=&quot;rendering-entities&quot;&gt;Rendering Entities&lt;/h2&gt;

&lt;p&gt;GameRunner uses entities to represent objects in the game world. To create an entity, you can use the createEntity() method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;javascript
// Create a new entity
const entity = game.createEntity();

// Set the entity's position
entity.position.x = 100;
entity.position.y = 100;

// Set the entity's size
entity.size.x = 200;
entity.size.y = 200;

// Set the entity's sprite
entity.sprite = new Sprite(&quot;assets/sprite.png&quot;);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To render an entity, you can use the &lt;code&gt;drawEntity()&lt;/code&gt; method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;javascript
// Render the entity
game.drawEntity(entity);
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;events&quot;&gt;Events&lt;/h2&gt;

&lt;p&gt;GameRunner supports events that can be used to listen for game-related events. To listen for an event, you can use the &lt;code&gt;on()&lt;/code&gt; method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;javascript
// Listen for the &quot;click&quot; event
game.on(&quot;click&quot;, (event) =&amp;gt; {
  // ...
});
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;GameRunner is a simple and easy-to-use JavaScript game framework that makes it easy to create 2D games. It is based on the Phaser game engine, but it is stripped down to its bare essentials.&lt;/p&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html">GameRunner: The Minimal JavaScript Game Framework</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Snake V1 Part III: The Game</title><link href="https://ikzer.github.io/2022/09/17/snake-v1-part-iii.html" rel="alternate" type="text/html" title="Snake V1 Part III: The Game" /><published>2022-09-17T20:30:00+02:00</published><updated>2022-09-17T20:30:00+02:00</updated><id>https://ikzer.github.io/2022/09/17/snake-v1-part-iii</id><content type="html" xml:base="https://ikzer.github.io/2022/09/17/snake-v1-part-iii.html">&lt;p&gt;&lt;img src=&quot;/images/snake1.jpg&quot; alt=&quot;Snake&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We left the &lt;a href=&quot;/2022/09/03/snake-v1-part-ii&quot;&gt;Snake&lt;/a&gt; moving freely with keyboard controls but without any type of boundaries, so we need to add some restrictions: the end of game conditions.&lt;/p&gt;

&lt;p&gt;This is, when the snake hits the walls of the plane or with its own body, the game finishes and restart. We also have to add the gameplay elements, like the food that randomly appears and we have to “eat” in order to earn points. We will also add a little score counter to know how many cells we eat.&lt;/p&gt;

&lt;h2 id=&quot;game-boundaries&quot;&gt;Game Boundaries&lt;/h2&gt;

&lt;p&gt;The end of game conditions are really easy. Basically we need to know if the head of the snake is outside the limits of our game space:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// End game conditions
 // Restart the game if it's outside the plane
 if(nx == -1 || nx == w/cw || ny == -1 || ny == h/cw)
 {
    // Restart game
    init();
 
    return;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If the &lt;code&gt;x&lt;/code&gt; positoin is &lt;code&gt;-1&lt;/code&gt; it means it went off in the left side of the plane, and if it’s &lt;code&gt;w/cw&lt;/code&gt; it means it went off by the right. &lt;code&gt;w&lt;/code&gt; is the width of the plane and &lt;code&gt;cw&lt;/code&gt; is the size in pixels of every cell, as stated in the previous post, so if it is outside the range of cells in the plane, it’s over (&lt;code&gt;w/cw&lt;/code&gt; it’s the range, the quantity of cells that we have in the game space, 50 in our case). For &lt;code&gt;y&lt;/code&gt; it’s the same, &lt;code&gt;-1&lt;/code&gt; it’s outside at the top side and &lt;code&gt;h/cw&lt;/code&gt; it’s outside at the bottom. This is a really basic implementation of what is called &lt;em&gt;collision detection&lt;/em&gt;, being a really old method to it (but enough and efficient for this particular case). We do this checks on the &lt;code&gt;paint()&lt;/code&gt; function, as we have to check every time the snake is painted, and we need to do it before it gets painted too, to avoid bugs.&lt;/p&gt;

&lt;h2 id=&quot;self-collissions&quot;&gt;Self Collissions&lt;/h2&gt;

&lt;p&gt;All it’s left is to check self collisions. We need to check if the snake hits itself, as until now it could pass through its own body. For this we create a function that checks this collisions:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function check_collision(x, y, array)
{
// Check if x and y coordinates exists in a cells array
    for(var i = 0; i &amp;lt; array.length; i++)
    {
        if(array[i].x == x &amp;amp;&amp;amp; array[i].y == y)
        return true;
    }
return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we add this collision condition to the ones we already had:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; // End game conditions
 // Restart the game if it's outside the plane
 if(nx == -1 || nx == w/cw || ny == -1 || ny == h/cw || check_collision(nx,  ny, snake_array))
 {
    // Restart game
    init();
 
    return;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;## Restart the Game&lt;/p&gt;

&lt;p&gt;The problem is that we have yet a way to restart the game: until now it just worked in the same conditions. To do this we create the &lt;code&gt;init()&lt;/code&gt; function that we will call when we restart:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; function init()
{
    d = &quot;right&quot;; // Direction of the movement. By default right
    create_snake();
    
    // To move the snake we use a timer that will call the
    // paint() function every 60ms
    if(typeof game_loop != &quot;undefined&quot;)
        clearInterval(game_loop);
        game_loop = setInterval(paint, 60);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The only thing we do here is to refactor and organize some parts of the code we had earlier, like the initialization of the default direction and the creation of a new snake. The direction needs to be global so we declare it outside &lt;code&gt;ìnit()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The most important thing here is the timer &lt;code&gt;game_loop&lt;/code&gt;. We haven’t defined &lt;code&gt;game_loop&lt;/code&gt; anywhere else so it shouldn’t do anything by default, and the only time we call &lt;code&gt;setInterval()&lt;/code&gt; is here. This is done to coordinate the refresh time. Every time the game is restarted, the refresh rate will be constant and won’t depend on the state the game ended after the last collision. This way if the collision happens in an unfinished frame, it won’t interfere with the next game as we clean the state and start over.&lt;/p&gt;

&lt;h2 id=&quot;the-food&quot;&gt;The Food&lt;/h2&gt;

&lt;p&gt;Now the food. We start by, well, creating it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; function create_food()
 {
 food = {
 x: Math.random()*(w-cw)/cw,
 y: Math.random()*(h-cw)/cw,
 };
 // This will create a cell (food{}) with x and y values
 // and it will create it between 0 and w-cw or h-cw, meaning inside the plane
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We create a new cell called &lt;code&gt;food&lt;/code&gt; with two fields: &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt;, and we fill it with the &lt;code&gt;Math.random()&lt;/code&gt; function to get random positions. To limit the range in which those random number are generated we multiply them by the dimension of the plane: &lt;code&gt;(w-cw)/cw&lt;/code&gt; (the division is necessary because the cells are bigger than 1px).&lt;/p&gt;

&lt;p&gt;We have the cell with its position information. Now it’s time to paint it (inside &lt;code&gt;paint()&lt;/code&gt;). To do that, since we have to paint cells constantly (when creating the snake and creating food), it’s time to refactor and create a new function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint_cell(x, y)
 {
 ctx.fillStyle = &quot;blue&quot;;
 ctx.fillRect(x*cw, y*cw, cw, cw);
 ctx.strokeStyle = &quot;white&quot;;
 ctx.strokeRect(x*cw, y*cw, cw, cw);
 }
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We do this by taking the code we used before in the loop to paint the snake inside &lt;code&gt;paint()&lt;/code&gt;. So now we have to replace that code with a call to this function in the loop:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 
... previous code
 
// Paint the snake
 for(var i = 0; i &amp;lt; snake_array.length; i++)
 {
 var c = snake_array[i];
 paint_cell(c.x, c.y);
 }
 
... next code
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now to paint the food we need to do more things. First we create a global variable (&lt;code&gt;var food()&lt;/code&gt;) to contain it. Then we create a call to &lt;code&gt;create_food()&lt;/code&gt; on &lt;code&gt;init()&lt;/code&gt; to create a cell on start, and finally a call to &lt;code&gt;paint_cell()&lt;/code&gt; on &lt;code&gt;paint()&lt;/code&gt; with the fields of the newly created variable to paint it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function init()
 {
 d = &quot;right&quot;; // Direction of the movement, to the right by default
 create_snake(); //Create the snake
 create_food();
 
 // To move the snake we create a timer that will call
 // the paint() function every 60ms
 if(typeof game_loop != &quot;undefined&quot;) clearInterval(game_loop);
 game_loop = setInterval(paint, 60);
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;paint()&lt;/code&gt; function with the call to &lt;code&gt;paint_cell()&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint() {
 
... previous code
 
// Paint the snake
 for(var i = 0; i &amp;lt; snake_array.length; i++)
 {
 var c = snake_array[i];
 paint_cell(c.x, c.y);
 }
 
 // Paint the food
 paint_cell(food.x, food.y);
 
... next code
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we need no make the snake eat the food, so we have to modify &lt;code&gt;paint()&lt;/code&gt;. Before we had:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; var tail = snake_array.pop(); // Pop and store the tail
 tail.x = nx; tail.y = ny; // Assign the tail the position of the head
 snake_array.unshift(tail); // Insert the tail in the first position
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we change it to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// If the new position of the head it's the same as the food
// We create a new head instead of moving the tail
 if(nx == food.x &amp;amp;&amp;amp; ny == food.y)
 {
 var tail = {x: nx, y: ny};
 // Create new food
 create_food();
 }
 else
 {
 var tail = snake_array.pop(); // Pop and store the tail
 tail.x = nx; tail.y = ny; // Assign the tail the position of the head
 }
 // The snake has &quot;eaten&quot; the new cell
 
 snake_array.unshift(tail); // And now we insert the tail in the first position
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When moving the snake, the first think that is calculated is the position of the head. This time, instead of moving it directly, we check. If it’s the same as the food, instead of removing the tail and putting it to the head, we add the new cell, create a new head instead of moving the previous and, of course, create new food. This means assigning new values to tail, not making the &lt;code&gt;pop()&lt;/code&gt; to remove it from the end and after that we add it to the front with &lt;code&gt;unshift()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To finish this V1 we will show a little score text that counts the food we eat.&lt;/p&gt;

&lt;p&gt;For that we create the variable &lt;code&gt;score&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var score;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Initialize it in &lt;code&gt;init()&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function init()
 {
 d = &quot;right&quot;; // Direction of the movement, to the right by default
 create_snake(); //Create the snake
 create_food();

 score = 0;
 
 // To move the snake we create a timer that will call
 // the paint() function every 60ms
 if(typeof game_loop != &quot;undefined&quot;) clearInterval(game_loop);
 game_loop = setInterval(paint, 60);
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We increment it when we eat:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// If the new position of the head it's the same as the food
// We create a new head instead of moving the tail
 if(nx == food.x &amp;amp;&amp;amp; ny == food.y)
 {
 var tail = {x: nx, y: ny};
 score++;

 // Create new food
 create_food();
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And we add it to &lt;code&gt;paint()&lt;/code&gt; at the end to show the score:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Paint the score
 var score_text = &quot;Score: &quot; + score;
 ctx.fillText(score_text, 5, h-5);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With &lt;code&gt;fillText()&lt;/code&gt; we paint texts on the canvas, passing as arguments a string and the x and y positions of the bottom left corner of the text to show.&lt;/p&gt;

&lt;p&gt;And that’s it. With less than 150 lines of code (it can be much less if we want actually) we have a working classic. For now is ugly and don’t have anything fancy what it have is the essential: a game.&lt;/p&gt;

&lt;p&gt;This first version was about gameplay. In the future we can add menus, score tables, lives, levels, bonuses, pause, game modes and lots and lots of other things. But they are just superficial ornaments built over this core gameplay we have coded now.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed and learnt something useful. See you on next projects!&lt;/p&gt;

&lt;h1 id=&quot;index&quot;&gt;Index&lt;/h1&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/08/20/snake-v1-part-i.html&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/09/03/snake-v1-part-ii.html&quot;&gt;The Snake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Game&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Snake V1 Part II: The Snake</title><link href="https://ikzer.github.io/2022/09/03/snake-v1-part-ii.html" rel="alternate" type="text/html" title="Snake V1 Part II: The Snake" /><published>2022-09-03T12:30:00+02:00</published><updated>2022-09-03T12:30:00+02:00</updated><id>https://ikzer.github.io/2022/09/03/snake-v1-part-ii</id><content type="html" xml:base="https://ikzer.github.io/2022/09/03/snake-v1-part-ii.html">&lt;p&gt;&lt;img src=&quot;/images/snake1.jpg&quot; alt=&quot;Snake&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It took me to the ninth post to actually start posting code here. But let’s begin.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#html&quot;&gt;HTML&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#canvas&quot;&gt;Canvas&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#defining-the-snake&quot;&gt;Defining the Snake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#drawing-the-snake&quot;&gt;Drawing the snake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#moving&quot;&gt;Moving&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#keyboard-inputs&quot;&gt;Keyboard Inputs&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#index&quot;&gt;Index&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1 id=&quot;html&quot;&gt;HTML&lt;/h1&gt;

&lt;p&gt;To start, the first thing we need to do is create the base HTML5 that we will use, call the jQuery library and initialize the &lt;em&gt;canvas&lt;/em&gt; where we will draw the elements. So we create and &lt;code&gt;index.html&lt;/code&gt; with this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;```
!DOCTYPE HTML&amp;gt;
`&amp;lt;html&amp;gt;
`&amp;lt;body&amp;gt;
`
`&amp;lt;!-- Create 500x500 canvas --&amp;gt;
&amp;lt;canvas id=&quot;canvas&quot; width=&quot;500&quot; height=&quot;500&quot;&amp;gt;&amp;lt;/canvas&amp;gt;

&amp;lt;!-- JQuery --&amp;gt;
&amp;lt;script src=&quot;http://code.jquery.com/jquery-2.0.2.min.js&quot; type=&quot;text/javascript&quot;&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
```
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That’s basically all the HTML we will need for this project. The rest will be JavaScript only. The &lt;code&gt;canvas&lt;/code&gt; element while having its dimensions set will be a blank square for now, since we haven’t draw anything inside, so it’s pretty useless right now.&lt;/p&gt;

&lt;h1 id=&quot;canvas&quot;&gt;Canvas&lt;/h1&gt;

&lt;p&gt;So let’s initialize the &lt;code&gt;canvas&lt;/code&gt; blank space. Within the &lt;code&gt;&amp;lt;script&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt; tags we type this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$(document).ready(function(){
 // Initialize Canvas
 var canvas = $(&quot;#canvas&quot;)[0]; // The canvas variable contains our &amp;lt;canvas&amp;gt; element
 var ctx = canvas.getContext(&quot;2d&quot;); // We initialize the 2D context of canvas in ctx
 var w = $(&quot;#canvas&quot;).width(); // We save width and height
 var h = $(&quot;#canvas&quot;).height();
 
 // Draw the canvas
 ctx.fillStyle = &quot;white&quot;;
 ctx.fillRect(0, 0, w, h);
 ctx.strokeStyle = &quot;black&quot;;
 ctx.strokeRect(0, 0, w, h);
 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Line 1 is JQuery initialization, to execute the code when the document is ready to show, thus not having to call it anywhere else. In line 3 we just store the canvas in a variable.&lt;/p&gt;

&lt;p&gt;The first interesting thing comes in line 4: the context. The getContext() method returns an object that provides methods and attributes to draw in the canvas. The canvas itself is that: a canvas, and we need to obtain the tools to work on it. As an argument it needs the context we are requiring: in our case is «2d«. Does this mean we have a “3d” context? Not yet, but there is the “webgl” context that let us work with WebGL APIs, and therefore OpenGL-ES 2.0, but that’s for another moment.&lt;/p&gt;

&lt;p&gt;The context provide lots of methods, to obtain information about it’s contents as well as to draw inside it. In lines 5 and 6 we store the canvas dimensions so we have easy access, and then in lines 9 and 10 we draw the rectangle that will have the game. With &lt;code&gt;fillStyle&lt;/code&gt; we can obtain or define the style (color, gradient or pattern) with which it will be filled and with &lt;code&gt;fillRect&lt;/code&gt; we do the fill with the previously defined style. It receives as arguments the top left (x,y) coordinates and the (w,h) dimensions. In the next two lines we make the same but dor &lt;code&gt;strokeStyle&lt;/code&gt; and &lt;code&gt;strokeRect&lt;/code&gt;, that define the borders of the rectangle.&lt;/p&gt;

&lt;h1 id=&quot;defining-the-snake&quot;&gt;Defining the Snake&lt;/h1&gt;

&lt;p&gt;Now we have to create the snake itself. It will be a cells array:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Create the snake
 var snake_array; // It's a cells array
 
 create_snake();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the function to create it&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Fill the snake
 function create_snake()
 {
  var length = 5; // Size of the snake
  snake_array = []; // Initialize the array
  for(var i = length-1; i&amp;gt;=0; i--)
  {
   // This will create a horizontal snake
   // starting at the top left corner
   snake_array.push({x: i, y:0});
  }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The number of cells of the array will be the size of the snake, and we push new elemnts at the end of the array with two properties each: their x and y positions inside the grid. I decided to initialize at the top left, but that’s not necessary.&lt;/p&gt;

&lt;h1 id=&quot;drawing-the-snake&quot;&gt;Drawing the snake&lt;/h1&gt;

&lt;p&gt;The next step is to start drawing the elements that are part of the game, starting with the snake itself. We call the &lt;code&gt;paint()&lt;/code&gt; function to do so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;paint();&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the actual function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var cw = 10; // The size of the cells will be 10px.
 
function paint()
{
 
// Draw the snake
 for(var i = 0; i &amp;lt; snake_array.length; i++)
 {
  var c = snake_array[i];
  ctx.fillStyle = &quot;red&quot;;
  ctx.fillRect(c.x*cw, c.y*cw, cw, cw);
  ctx.strokeStyle = &quot;white&quot;;
  ctx.strokeRect(c.x*cw, c.y*cw, cw, cw);
 }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We store the size of the cells so we can access it if needed, and ot be able to configure it later. In this function we are loop through all the elements of the snake, storing the in &lt;code&gt;c&lt;/code&gt; for easy access, and we draw it as we did with the canvas, calling &lt;code&gt;fillStyle&lt;/code&gt;, &lt;code&gt;fillRect&lt;/code&gt;, &lt;code&gt;strokeStyle&lt;/code&gt; and &lt;code&gt;strokeRect&lt;/code&gt;, using as parameters the x and y positions of each cell, and multiplying it by the size of the cell, so in each iteration we are drawing a square of 10x10.&lt;/p&gt;

&lt;h1 id=&quot;moving&quot;&gt;Moving&lt;/h1&gt;

&lt;p&gt;Now we need to move the snake. The &lt;code&gt;setInterval(function, ms)&lt;/code&gt; function in JavaScript calls the &lt;code&gt;function&lt;/code&gt; we pass it every &lt;code&gt;ms&lt;/code&gt;, as per the second parameter. So in this case we will call every 60 miliseconds the &lt;code&gt;paint&lt;/code&gt; function. In the future this will allow us to change levels and speed (which we are not going to do in the v1):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;game_loop = setInterval(paint, 60);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The movement of the snake will take place inside &lt;code&gt;paint&lt;/code&gt;, since it will need to be repainted constantly:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint()
{
 // -- Movement of the snake --
 // The logic is simple: remove the tail from the end and bring it to the front
 // Store the position of the head in nx and ny
 var nx = snake_array[0].x;
 var ny = snake_array[0].y;
 
 nx++; // Increment the value to get the new position of the head
 
 var tail = snake_array.pop(); // Take out the tail
 tail.x = nx; // Give the tail the position of the head
 snake_array.unshift(tail); // Insert the tail in the first position of the array
 
 ... code to paint the snake
 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The idea when moving the snake is simple. It has to increase an element in the head and lose one in the tail, so what we do is take the tail to the head. First we obtain the head’s position and increment it, for now on &lt;code&gt;x&lt;/code&gt;, to have the position it should have after the movement. Next we &lt;code&gt;pop&lt;/code&gt; the tail from the array, so it has one item less than before (the last one), but we store it as &lt;code&gt;tail&lt;/code&gt;. The we assign &lt;code&gt;tail&lt;/code&gt; the position we previously stored and insert it in the first position of the array, making it the new head.&lt;/p&gt;

&lt;p&gt;But why replace the head with the tail in the first place instead of removing the tail and draw a new snake in the new position? Well, basically that’s almost what we do: we remove the tail and draw a new head, but we use the &lt;code&gt;tail&lt;/code&gt; element, so we don’t have to create a new object with x and y properties, initialize it and assign it the new position. And also the concept of “moving the tail to the head” makes the snake move as expected, because if we just simply restarted in every frame it will move only to the default direction (since the &lt;code&gt;create_snake&lt;/code&gt; function creates a straight array in only one direction). So we don’t restart the snake every time, we just change the position of one of its elements, leaving the rest as they were.&lt;/p&gt;

&lt;p&gt;This way we can see the snake moving to the right forever… but leaving a trail. The problem is that we are only drawing a new cell in the position of the head, because our loop does just that: draw, but not “undraw” anything, so we keep adding new cells, but not removing the previous. To solve this we will use a little trick: before we draw the snake, we draw the full canvas again:&lt;/p&gt;

&lt;p&gt;Draw blank canvas -&amp;gt; draw snake in new position.&lt;/p&gt;

&lt;p&gt;To do this we get the code to draw the canvas inside &lt;code&gt;paint()&lt;/code&gt; and execute before anything else:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function paint()
{
// Draw the canvas
 ctx.fillStyle = &quot;white&quot;;
 ctx.fillRect(0, 0, w, h);
 ctx.strokeStyle = &quot;black&quot;;
 ctx.strokeRect(0, 0, w, h);
 
 ...all previous code of paint()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;For now it moves, but always to the right. We have to add directions restrictions. For that we will store the current direction in a new variable:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var d = &quot;right&quot;;  // Direction of the movement, by default to right.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And instead of incrementing the head’s position only on &lt;code&gt;x&lt;/code&gt; we consider the direction now:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Modify the values according to direction the snake is facing
 if(d == &quot;right&quot;) nx++;
 else if(d == &quot;left&quot;) nx--;
 else if(d == &quot;up&quot;) ny--;
 else if(d == &quot;down&quot;) ny++;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the head’s new position:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tail.x = nx; tail.y = ny; // The head's position is assigned to the tail&lt;/code&gt;&lt;/p&gt;

&lt;h1 id=&quot;keyboard-inputs&quot;&gt;Keyboard Inputs&lt;/h1&gt;

&lt;p&gt;Now the snake can move in every direction… by itself. We need to get the keyboard inputs to be able to interact with it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Add the keyboard controls
// including a clause that prevents it from going bakcwards
 $(document).keydown(function(e){
var key = e.which;
if(key == &quot;37&quot; &amp;amp;&amp;amp; d != &quot;right&quot;) d = &quot;left&quot;;
else if(key == &quot;38&quot; &amp;amp;&amp;amp; d != &quot;down&quot;) d = &quot;up&quot;;
else if(key == &quot;39&quot; &amp;amp;&amp;amp; d != &quot;left&quot;) d = &quot;right&quot;;
else if(key == &quot;40&quot; &amp;amp;&amp;amp; d != &quot;up&quot;) d = &quot;down&quot;;
 })
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It’s simple: we get the input from the keyboard and depending on which key was pressed we assign the direction to the corresponding variable. We check if we are going backwards so it can’t make any weird movements like going to the left while the direction is right.&lt;/p&gt;

&lt;p&gt;For now we will keep it here. We have a canvas with a snake that moves as we command it, always inside the plane because otherwise it will dissappear. In the next part we will add the food, the endgame conditions and basic scores.&lt;/p&gt;

&lt;h1 id=&quot;index&quot;&gt;Index&lt;/h1&gt;

&lt;p&gt;With this we’re almost done. Next time we will make the fun part, with the eating and points elements of the gameplay. Enjoy!&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/08/20/snake-v1-part-i.html&quot;&gt;Introduction&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;The Snake&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/09/17/snake-v1-part-iii.html&quot;&gt;The Game&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Snake V1 Part I: Stateless Snake Introduction</title><link href="https://ikzer.github.io/2022/08/20/snake-v1-part-i.html" rel="alternate" type="text/html" title="Snake V1 Part I: Stateless Snake Introduction" /><published>2022-08-20T20:30:00+02:00</published><updated>2022-08-20T20:30:00+02:00</updated><id>https://ikzer.github.io/2022/08/20/snake-v1-part-i</id><content type="html" xml:base="https://ikzer.github.io/2022/08/20/snake-v1-part-i.html">&lt;p&gt;&lt;img src=&quot;/images/snake1.jpg&quot; alt=&quot;Snake&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Snake&lt;/strong&gt;, that simple game that we all had on those huge Nokia bricks fifteen years ago and we all used to kill time on is the chosen one to start this series. due to its simplicity.&lt;/p&gt;

&lt;p&gt;It will be two parts on this first version since it will be stateless. It’s simple but it gets pretty long (heh), and I want to document every single step. I want to approach this as “the tutorial I would’ve liked when I was learning” so if it helps someone, there is that.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;#description&quot;&gt;Description&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#requisites&quot;&gt;Requisites&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#index&quot;&gt;Index&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;description&quot;&gt;Description&lt;/h2&gt;

&lt;p&gt;In Snake we control a long entity that reminds us of a snake that is constantly moving inside a closed plane. If it collisions with the plane’s border, we lose and have to start again. We can move the direction of the snake’s head in the four basic directions (up, down, left, right) and, again, if the snake’s head hits it’s own body we lose.&lt;/p&gt;

&lt;p&gt;To grow and win points there will be pieces appearing randomly in the plane that we have to “eat”, thus getting longer. The more you eat, the longer you get, the more points you win. But being longer means it will be more difficult to move safely.&lt;/p&gt;

&lt;p&gt;In this first version I’ll only make the gameplay: no menus, options or extras besides the snake and the points.&lt;/p&gt;

&lt;h2 id=&quot;requisites&quot;&gt;Requisites&lt;/h2&gt;

&lt;p&gt;We don’t need anything to this simple version. Nothing more than basic JavaScript syntax and some understanding of JSON for data. In this case I will use basic HTML5 and jQuery for the input management. All the elements can be rendered with code so we will not need assets either. In more complex versions we could use images, sound and effects, but for now that will be out of scope. Keep it simple for now.&lt;/p&gt;

&lt;h2 id=&quot;index&quot;&gt;Index&lt;/h2&gt;

&lt;p&gt;So with this in mind, the series will consist of three parts:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/09/03/snake-v1-part-ii.html&quot;&gt;The Snake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ikzer.github.io/2022/09/17/snake-v1-part-iii.html&quot;&gt;The Game&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Fernando Delgado</name></author><category term="JavaScript GameDev" /><summary type="html"></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Javascript Games Tutorials Project Part III: The Modern Genres</title><link href="https://ikzer.github.io/2022/08/19/javascript-games-part-iii.html" rel="alternate" type="text/html" title="Javascript Games Tutorials Project Part III: The Modern Genres" /><published>2022-08-19T18:30:00+02:00</published><updated>2022-08-19T18:30:00+02:00</updated><id>https://ikzer.github.io/2022/08/19/javascript-games-part-iii</id><content type="html" xml:base="https://ikzer.github.io/2022/08/19/javascript-games-part-iii.html">&lt;p&gt;After the first basic arcade games it’s time to explore modern genres. These games are much more complex so this will take more time but if everything goes as expected they shouldn’t be too difficult, just time consuming.&lt;/p&gt;

&lt;p&gt;The genres to cover here will be:&lt;/p&gt;

&lt;h2 id=&quot;2d-platform&quot;&gt;2D Platform&lt;/h2&gt;

&lt;p&gt;Platformers are fun games focused on level design to give them variety. From Donkey Kong to Super Mario, Prince of Persia, to more modern like Disney’s Aladdin, Sonic or Megaman they are very different. The idea here is to make the first two, Donkey Kong and Mario, as they are genre defining and iconic.&lt;/p&gt;

&lt;h2 id=&quot;2d-jrpg&quot;&gt;2D JRPG&lt;/h2&gt;

&lt;p&gt;Japanese companies like Square and Enix absolutely dominated a new genre in the 80’s and created lasting sagas that survived to this day with great success. The idea here is to make a 2D RPG like Final Fantasy, not as complex of course but the main mechanics will be there.&lt;/p&gt;

&lt;h2 id=&quot;2d-fighting&quot;&gt;2D Fighting&lt;/h2&gt;

&lt;p&gt;This is one that I currently have zero idea on how to do. It will be a challenge. i’ll try to keep it classic, nothing fancy. Something like the first Street Fighter or even earlier games. We’ll see.&lt;/p&gt;

&lt;h2 id=&quot;2d-racing&quot;&gt;2D Racing&lt;/h2&gt;

&lt;p&gt;One of the firsts “advanced” games that we all played were racing games. But instead of making those NES old games I’ll try something more like F-Zero.&lt;/p&gt;

&lt;h2 id=&quot;roguelike&quot;&gt;Roguelike&lt;/h2&gt;

&lt;p&gt;Nowadays almost all the indie studios make roguelikes. Procedural levels are the hottest thing now. But they were around since the 90’s. I’ll try a dungeon generator to make an infinite roguelike game.&lt;/p&gt;

&lt;h2 id=&quot;tower-platformer&quot;&gt;Tower Platformer&lt;/h2&gt;

&lt;p&gt;The last of this pack will be another kind of platformer that nobody makes anymore: tower platformers, like Donkey Kong or others. To relax after all the previous this one will be the end of the Phase 2.&lt;/p&gt;

&lt;p&gt;I hope to make these in the first quarter of 2023, but only time will tell.&lt;/p&gt;</content><author><name>Fernando Delgado</name></author><category term="Projects" /><summary type="html">After the first basic arcade games it’s time to explore modern genres. These games are much more complex so this will take more time but if everything goes as expected they shouldn’t be too difficult, just time consuming.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" /><media:content medium="image" url="https://s.gravatar.com/avatar/2136e716a089f4a3794f4007328c7bfb?s=800" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>