{"id":85,"date":"2025-10-12T05:02:05","date_gmt":"2025-10-12T05:02:05","guid":{"rendered":"https:\/\/blackhatseomaster.com\/?p=85"},"modified":"2025-10-12T05:58:39","modified_gmt":"2025-10-12T05:58:39","slug":"building-your-first-2d-game-with-javascript-and-canvas","status":"publish","type":"post","link":"https:\/\/blackhatseomaster.com\/en-in\/building-your-first-2d-game-with-javascript-and-canvas\/","title":{"rendered":"Building Your First 2D Game with JavaScript and Canvas"},"content":{"rendered":"<p>Learn how to build a simple but fun 2D game using only HTML, CSS, and JavaScript Canvas. Perfect for beginners who want hands-on experience.<\/p>\n\n\n\n<p>Game development may sound intimidating at first, but with modern web technologies, you can build interactive games with just a few lines of code. In this tutorial, we\u2019ll create a simple 2D game where a player controls a square that must dodge falling objects.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1: Setting Up the HTML Structure<\/h4>\n\n\n\n<p>Start with a simple HTML file. We need a <code>&lt;canvas&gt;<\/code> element where the game will be rendered.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;head&gt;\n  &lt;title&gt;2D Game&lt;\/title&gt;\n  &lt;style&gt;\n    canvas {\n      background: #000;\n      display: block;\n      margin: auto;\n    }\n  &lt;\/style&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n  &lt;canvas id=&quot;gameCanvas&quot; width=&quot;400&quot; height=&quot;600&quot;&gt;&lt;\/canvas&gt;\n  &lt;script src=&quot;game.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2: Drawing on Canvas<\/h4>\n\n\n\n<p>In <code>game.js<\/code>, we grab the canvas and draw the player:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const canvas = document.getElementById('gameCanvas');\nconst ctx = canvas.getContext('2d');\n\nlet player = { x: 180, y: 550, width: 40, height: 40, color: 'lime' };\n\nfunction drawPlayer() {\n  ctx.fillStyle = player.color;\n  ctx.fillRect(player.x, player.y, player.width, player.height);\n}\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3: Movement and Controls<\/h4>\n\n\n\n<p>We want the player to move left and right using arrow keys.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>document.addEventListener('keydown', (e) =&gt; {\n  if (e.key === 'ArrowLeft' &amp;&amp; player.x &gt; 0) player.x -= 20;\n  if (e.key === 'ArrowRight' &amp;&amp; player.x &lt; canvas.width - player.width) player.x += 20;\n});\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4: Adding Falling Objects<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>let obstacles = &#91;];\n\nfunction createObstacle() {\n  obstacles.push({\n    x: Math.random() * (canvas.width - 40),\n    y: 0,\n    width: 40,\n    height: 40,\n    color: 'red'\n  });\n}\nsetInterval(createObstacle, 1000);\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5: Game Loop and Collision<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>function update() {\n  ctx.clearRect(0, 0, canvas.width, canvas.height);\n  drawPlayer();\n\n  obstacles.forEach(ob =&gt; {\n    ob.y += 5;\n    ctx.fillStyle = ob.color;\n    ctx.fillRect(ob.x, ob.y, ob.width, ob.height);\n\n    if (\n      ob.x &lt; player.x + player.width &amp;&amp;\n      ob.x + ob.width &gt; player.x &amp;&amp;\n      ob.y &lt; player.y + player.height &amp;&amp;\n      ob.y + ob.height &gt; player.y\n    ) {\n      alert('Game Over!');\n      document.location.reload();\n    }\n  });\n\n  requestAnimationFrame(update);\n}\nupdate();\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Step 6: Adding Style and Enhancements<\/h4>\n\n\n\n<p>You can make the game more engaging by adding background music, increasing difficulty, or showing a score.<\/p>\n\n\n\n<p>\u2705 <strong>Key Takeaways:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>JavaScript Canvas makes real-time game rendering easy.<\/li>\n\n\n\n<li>Simple collision detection can create fun, interactive mechanics.<\/li>\n\n\n\n<li>You can build and expand this project into more complex games.<\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Learn how to build a simple but fun 2D game using only HTML, CSS, and JavaScript Canvas. Perfect for beginners who want hands-on experience. Game development may sound intimidating at first, but with modern web technologies, you can build interactive games with just a few lines of code. In this tutorial, we\u2019ll create a simple &#8230; <a title=\"Building Your First 2D Game with JavaScript and Canvas\" class=\"read-more\" href=\"https:\/\/blackhatseomaster.com\/en-in\/building-your-first-2d-game-with-javascript-and-canvas\/\" aria-label=\"Read more about Building Your First 2D Game with JavaScript and Canvas\">Read more<\/a><\/p>","protected":false},"author":1,"featured_media":93,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-game-codes"],"_links":{"self":[{"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":2,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":95,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/posts\/85\/revisions\/95"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/media\/93"}],"wp:attachment":[{"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blackhatseomaster.com\/en-in\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}