<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hieu Le</title>
    <description>The latest articles on DEV Community by Hieu Le (@hieule).</description>
    <link>https://dev.to/hieule</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1063181%2F9435cfd7-7e0e-41c3-83bf-93c0f4c0fe1a.png</url>
      <title>DEV Community: Hieu Le</title>
      <link>https://dev.to/hieule</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hieule"/>
    <language>en</language>
    <item>
      <title>🦖3 minutes to create a game using JavaScript</title>
      <dc:creator>Hieu Le</dc:creator>
      <pubDate>Tue, 11 Apr 2023 15:29:02 +0000</pubDate>
      <link>https://dev.to/hieule/3-minutes-to-create-a-game-using-javascript-1jgn</link>
      <guid>https://dev.to/hieule/3-minutes-to-create-a-game-using-javascript-1jgn</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;You can see the full code here &lt;a href="https://github.com/phocaplaptrinh/simple-chrome-dinosaur-game"&gt;https://github.com/phocaplaptrinh/simple-chrome-dinosaur-game&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Firstly, in order to draw graphics on a webpage, HTML provides a tag called &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt;. Therefore, I will use the two.js library, which helps me to manipulate the canvas more easily and quickly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6PSQzwvu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f31d2avzw9qlrht0oow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6PSQzwvu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7f31d2avzw9qlrht0oow.png" alt="Image description" width="512" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will embed this library into my index.html file. I also create a game.js file where I will write all of my code and then embed it into the webpage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;Dinosaur Game&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;

    &amp;lt;script src="https://cdnjs.cloudflare.com/ajax/libs/two.js/0.8.10/two.min.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script src="game.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, I create a game screen that is 500 pixels wide and 300 pixels high, and I specify that my game will be displayed using the canvas. After that, I attach this screen to an element on the webpage, specifically to the body element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let screen = new Two({
    width: 500,
    height: 300,
    type: Two.Types.canvas
});
screen.appendTo(document.body);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, I run the webpage using the Live Server extension on Visual Studio Code. You can see that it has already created a canvas for me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dvMvW3gI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxt1eo1sorrbmwox7f7r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dvMvW3gI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxt1eo1sorrbmwox7f7r.png" alt="Image description" width="559" height="248"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh, I almost forgot to mention that I will create a mini-game featuring a jumping dinosaur, similar to the one that appears on Google Chrome when there is no internet connection 😊.&lt;br&gt;
Next, I will draw a dinosaur on my game screen 🦖. I have prepared an image of a dinosaur with a size of 140 x 140 pixels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2TVv8RgJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbyzmre0i8f5mnmnklk6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2TVv8RgJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbyzmre0i8f5mnmnklk6.png" alt="Image description" width="140" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I will place the dinosaur at the position with x-coordinate of 50 and y-coordinate of 200.&lt;br&gt;
To play the game, we need to press the play button, right? 😁&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;screen.play();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, I will return to the website, and you can see that the dinosaur has appeared, but it is a bit too large.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xPpsE53R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur0pxy4619adv4n30pax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xPpsE53R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ur0pxy4619adv4n30pax.png" alt="Image description" width="636" height="382"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let dinosaur = screen.makeSprite('dinosaur.png', 50, 200);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Please note that the canvas has its coordinate origin located at the top left corner, where the x-value increases towards the right and the y-value increases towards the bottom.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eGHrPFsn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k35yy1omfcj0nz338nnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eGHrPFsn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k35yy1omfcj0nz338nnl.png" alt="Image description" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will reduce its size by half.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dinosaur.scale = 0.5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it looks better now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YEMBouW2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxzk2y5s6xv2xtkyp1af.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YEMBouW2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lxzk2y5s6xv2xtkyp1af.png" alt="Image description" width="549" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I will draw the ground to make the dinosaur stand on instead of hovering in the air. I draw a straight line with the starting point at (0, 235) and the ending point at (500, 235).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let ground = screen.makeLine(0, 235, 500, 235);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can refer to the image below to understand why I chose these values.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qcT2XREn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3thnusealksdqq9fh6j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qcT2XREn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o3thnusealksdqq9fh6j.png" alt="Image description" width="741" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, I will draw the enemy of the dinosaur, which is a cactus 🌵. I also have an image of a cactus with a size of 60 x 140 pixels.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SORFdSbv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzjy6fv2d8jx2prc79n6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SORFdSbv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzjy6fv2d8jx2prc79n6.png" alt="Image description" width="60" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And I draw it in the same way as I drew the dinosaur.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let cactus = screen.makeSprite('cactus.png', 400, 200);
cactus.scale = 0.5;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks beautiful now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F5OfyT_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugzoxa7i8qyn48jtaz4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F5OfyT_K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugzoxa7i8qyn48jtaz4g.png" alt="Image description" width="519" height="358"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, I need to make the cactus move to the left. I will create an update function and call it continuously.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function update() {

}

screen.bind('update', update);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I will continuously decrease the x-coordinate of the cactus by 5 units to make the cactus move further to the left.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function update() {
    cactus.position.x -= 5;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice that the cactus moves to the left, but it goes out of the screen and disappears.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cfdbSD7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s56hq6scisqvmwsoz60l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cfdbSD7n--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s56hq6scisqvmwsoz60l.gif" alt="Image description" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Therefore, I want to make sure that when one cactus disappears, a new cactus will appear. So, I will check if the x-coordinate of the cactus is less than 0, meaning it goes out of the screen, I will reset the x-coordinate of the cactus to its initial position.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function update() {
    cactus.position.x -= 5;

    if (cactus.position.x &amp;lt; 0) {
        cactus.position.x = 400;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we can see the result.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TDvQChWR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uetjgsr3t53idcgdfo7u.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TDvQChWR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uetjgsr3t53idcgdfo7u.gif" alt="Image description" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, it looks weird when the cactus appears in the middle of the screen. Therefore, I will place its initial position outside the screen, for example, at 600.&lt;/p&gt;

&lt;p&gt;We still can't play the game. Now, I need to make the dinosaur jump when I press the space bar. I will capture the event of pressing a key, and if the pressed key is the space bar, I make the dinosaur jump about 150 pixels.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.addEventListener('keypress', function(e) {
    if (e.key == ' ') {
        dinosaur.position.y -= 150;
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Returning to the game, when I press the space key, you will see that the dinosaur has jumped up and not fallen down.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OKl58WFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc0lsdhohne712nii2yr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OKl58WFS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tc0lsdhohne712nii2yr.gif" alt="Image description" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I will make the dinosaur fall down by continuously increasing its y-coordinate value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dinosaur.position.y += 3;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the problem now is that it doesn't stop when it touches the ground.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gMrWKFZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tukotrr117nlo8xf2pgb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gMrWKFZm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tukotrr117nlo8xf2pgb.gif" alt="Image description" width="500" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I will set a condition that the dinosaur will only fall when its y-coordinate is lower than the initial coordinate of 200.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (dinosaur.position.y &amp;lt;= 200) {
    dinosaur.position.y += 3;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another problem is that I can press the space key multiple times to make the dinosaur fly too high, so I will add a condition that the dinosaur can only jump up when it is at its initial coordinate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (e.key == ' ' &amp;amp;&amp;amp; dinosaur.position.y &amp;gt;= 200) {
    dinosaur.position.y -= 150;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's okay, while the dinosaur is jumping, pressing the space key has no effect.&lt;/p&gt;

&lt;p&gt;The final step is to write a condition to check that when the dinosaur touches the cactus, the game will stop. You can think about why I wrote it like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (dinosaur.position.x + 35 &amp;gt;
        cactus.position.x - 15 &amp;amp;&amp;amp;
        dinosaur.position.y + 35 &amp;gt;
        cactus.position.y - 35
) {
    screen.pause();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you for reading this article ❤️ and if you are interested in this article, you can buy me a coffee ☕.&lt;br&gt;
&lt;a href="https://www.buymeacoffee.com/lehieu6290"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Oibfu3K--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" width="434" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
