<?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>Forem: Baruch Hen</title>
    <description>The latest articles on Forem by Baruch Hen (@_baruchadi).</description>
    <link>https://forem.com/_baruchadi</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%2F157299%2F8d8a1ea9-1d1f-45af-be8d-54250989724d.jpg</url>
      <title>Forem: Baruch Hen</title>
      <link>https://forem.com/_baruchadi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/_baruchadi"/>
    <language>en</language>
    <item>
      <title>Learn like a baby</title>
      <dc:creator>Baruch Hen</dc:creator>
      <pubDate>Sun, 21 Apr 2019 17:30:00 +0000</pubDate>
      <link>https://forem.com/_baruchadi/compare-yourself-to-a-baby-377k</link>
      <guid>https://forem.com/_baruchadi/compare-yourself-to-a-baby-377k</guid>
      <description>

&lt;h6&gt;
  
  
  &lt;em&gt;Originally posted on &lt;a href="http://www.baruchadi.com/self-improvement/goals/career/2019/04/21/compare-yourself-to-a-baby.html"&gt;baruchadi.com&lt;/a&gt;&lt;/em&gt;
&lt;/h6&gt;

&lt;p&gt;Learning takes time. Babies know that, so should you.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This article will focus on software engineering, but it can apply to anything in your life.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Baby steps
&lt;/h2&gt;

&lt;p&gt;Everything takes time. Just like a baby who’s learning how to talk or walk for the first time, you will need time to learn &amp;amp; master what it is you are currently working on.&lt;/p&gt;

&lt;p&gt;You need to understand that everything has a learning curve. Some are steeper than others, and you need to overcome that. You shouldn’t jump into the most complicated part of the skill you are attempting to learn, but instead, you should learn the simpler parts that together compose that complex skill.&lt;/p&gt;

&lt;p&gt;A baby first learns to crawl, then walk, then run. It’s all about pacing, you need to go through the basics before you jump to the final goal.&lt;/p&gt;

&lt;p&gt;You need to remember that &lt;strong&gt;No one is just born with that knowledge.&lt;/strong&gt; Knowledge is earned, not given.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dedication
&lt;/h2&gt;

&lt;p&gt;When a baby first learns to walk, there are stages. In the beginning, the baby can’t crawl, or even support itself. Given that, it’s not expected from the baby to be able to run yet! You should have the same perspective about your skills as a programmer. If you just picked up programming, and you are just getting started, it is obvious that you won’t be able to do what the “pros” are doing yet, it takes time! Babies can take ~6 months to be able to crawl, and it’s different for each baby. Everybody is different. It doesn’t mean that you should take it easy and give yourself all the time in the world. Babies put effort every day to learn those things. Attempting to crawl, attempting to talk. This shows that you need put in effort and dedication if you want to achieve your target. If you keep delaying and procrastinating you’ll get nowhere. So get up, and do the thing you kept telling yourself you would!&lt;/p&gt;

&lt;h2&gt;
  
  
  Admiration &amp;amp; Motivation, Not Jealousy
&lt;/h2&gt;

&lt;p&gt;Sometimes when we see someone successful, we tend to immediately jump to jealousy.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Wow, look at that guy. He got that amazing job, doing these amazing things, and I can barely write “hello world”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Well, let’s take a different perspective on that situation. This time, from a child’s point of view. When children see someone doing an amazing thing, take for example a soccer player, they would say something different. You wouldn’t hear them saying&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;wow look at that soccer player making tons of money and playing like a pro. I can’t even dribble.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;No, they would take that person and make them their idol! They would instead of being jealous of that amazing soccer player, would make that player their inspiration! They will start playing more soccer on their free times and try to imitate their most amazing plays and moves. whether or not that child would take soccer to the next level and become a pro is not important, the important part is that the child will improve! No, the improvement wouldn’t be immediate, but by dedicating time and trying to be more like their idol they will eventually be good!&lt;/p&gt;

&lt;p&gt;That is the same perspective I would recommend to use when you see someone who is a “pro”. Learn from them. If they are one of your peers or your coworkers, befriend them! Instead of making them an unhealthy rival, make them your friend and learn from them. If it’s a person that you just read about online and can’t befriend, learn from what they do!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;em&gt;EOF&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;Hopefully, this article helped you understand how to assess your growth and realize that it takes time to perfect a skill. Remember that different people learn at different paces and that knowledge is earned, not given. We are all capable of something great, we just need to put in the time and effort to enhance ourselves.&lt;/p&gt;

&lt;h6&gt;
  
  
  &lt;em&gt;Photo by &lt;a href="https://unsplash.com/photos/AVGc87j_vNA?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Valeria Zoncoll&lt;/a&gt; on &lt;a href="https://unsplash.com/search/photos/baby?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;
&lt;/h6&gt;


</description>
      <category>selfimprovement</category>
      <category>goals</category>
      <category>career</category>
      <category>learning</category>
    </item>
    <item>
      <title>What's your best failure?</title>
      <dc:creator>Baruch Hen</dc:creator>
      <pubDate>Sat, 20 Apr 2019 13:25:02 +0000</pubDate>
      <link>https://forem.com/_baruchadi/what-s-your-best-failure-6ho</link>
      <guid>https://forem.com/_baruchadi/what-s-your-best-failure-6ho</guid>
      <description>&lt;p&gt;We love talking about our successes, but failure can be also seen as a positive thing rather than just a negative thing. &lt;/p&gt;

&lt;p&gt;We learn often from our failures and that's what helps us build ourselves. I know that without all of my mistakes, I wouldn't be where I am today.&lt;/p&gt;

&lt;p&gt;So let's talk about our &lt;strong&gt;favorite failures&lt;/strong&gt; &amp;amp; what we learned from it! It could be anything! &lt;/p&gt;

&lt;h3&gt;
  
  
  My Failure!
&lt;/h3&gt;

&lt;p&gt;In my case, I'll talk about a passion project. &lt;br&gt;
I learned how to program mainly to develop video games. My work from that time can be seen on &lt;a href="http://isolatedpixel.com/"&gt;http://isolatedpixel.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At the time I was really bad at estimating what it really takes to create a full-blown project, and I was a strictly solo developer. It was hard for me to delegate work down to other people, but I slowly learned from those experiences that I can trust others to deliver as well. I shouldn't rely only on myself. I also ignored a big important factor that would determine the project's success, Marketing. I thought at the time that if I simply make something, people would line up to buy it! (Spoiler alert, that doesn't really happen... 😅)&lt;/p&gt;

&lt;p&gt;(of course, some people do that and get away with it! It's still a lot of work though. My favorite example of someone who did it is the creator of Stardew Valley &lt;a href="https://twitter.com/ConcernedApe"&gt;@ConcernedApe&lt;/a&gt; )&lt;/p&gt;

&lt;p&gt;Those projects on that site were really fun for me to work on, and I'm hoping to one day revive the site. &lt;/p&gt;

&lt;p&gt;I am extremely proud of the following failures from that site:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;grid/chunk system I designed for &lt;a href="http://isolatedpixel.com/?cat=18"&gt;"Project Biosphere"&lt;/a&gt; which is a Terraria clone in a way. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Inventory/stat system I designed for &lt;a href="http://isolatedpixel.com/?cat=70"&gt;"Project DG"&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A game I actually published on the Android Play Store, which I later took down b/c I didn't have time to support and patch. The game was called &lt;a href="http://isolatedpixel.com/?p=109"&gt;"Tomchick's Adventures"&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;None of my projects really got traction or sold, therefore they can be seen as failures. However, I learned a lot from making them, and I know that my next project would be better! &lt;/p&gt;

&lt;p&gt;I am looking forward to my next project, failure or success, as long as I'm learning from it &amp;amp; enjoying the journey!   &lt;/p&gt;

&lt;p&gt;This post is inspired by a recent #AMA by &lt;a class="comment-mentioned-user" href="https://dev.to/joelgriffith"&gt;@joelgriffith&lt;/a&gt;
&lt;br&gt;
(You can see our full discussion &lt;a href="https://dev.to/joelgriffith/i-m-the-creator-of-browserless-io-let-s-talk-about-starting-your-own-company-2ej3"&gt;here&lt;/a&gt; )&lt;/p&gt;

&lt;h4&gt;
  
  
  So now it's your turn, what are &lt;em&gt;your&lt;/em&gt; best failures? we all got some!
&lt;/h4&gt;

</description>
      <category>discuss</category>
      <category>showdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>How do you stay motivated for the entirety of your project?</title>
      <dc:creator>Baruch Hen</dc:creator>
      <pubDate>Thu, 18 Apr 2019 19:38:36 +0000</pubDate>
      <link>https://forem.com/_baruchadi/how-do-you-stay-motivated-for-the-entirety-of-your-project-2i6f</link>
      <guid>https://forem.com/_baruchadi/how-do-you-stay-motivated-for-the-entirety-of-your-project-2i6f</guid>
      <description>&lt;p&gt;When starting a new project I find myself full of energy and motivation. I can spend days working on it non-stop. &lt;/p&gt;

&lt;p&gt;However, once I get to a certain point where the project is taking shape, I find it harder to dedicate time and stay focused/motivated.&lt;/p&gt;

&lt;p&gt;Basically when I get to the polishing, modifying, or adding features portion of the journey; I simply start losing interest. How do you prevent this or push through this plateau? &lt;/p&gt;

&lt;p&gt;Any &amp;amp; all advice are welcome! &lt;/p&gt;

</description>
      <category>productivity</category>
      <category>help</category>
      <category>advice</category>
    </item>
    <item>
      <title>How to make a browser game with p5.js</title>
      <dc:creator>Baruch Hen</dc:creator>
      <pubDate>Tue, 16 Apr 2019 18:00:00 +0000</pubDate>
      <link>https://forem.com/_baruchadi/how-to-make-a-browser-game-with-p5-js-4da6</link>
      <guid>https://forem.com/_baruchadi/how-to-make-a-browser-game-with-p5-js-4da6</guid>
      <description>

&lt;p&gt;Learn how to make your own browser game with p5.js! It's way easier than you think!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Tools
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://p5js.org/"&gt;p5.js&lt;/a&gt; is a simple graphics library. It’s easy to get started and the documentation is concise and to the point! I was introduced to p5.js through a YouTube channel by &lt;a href="https://www.youtube.com/user/shiffman"&gt;Daniel Shiffman&lt;/a&gt;, I highly recommend you check out his YouTube channel!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Goal
&lt;/h3&gt;

&lt;p&gt;The final product is going to be a simple square walking around a grid (see &lt;a href="https://squares-grid-blog.now.sh/"&gt;here&lt;/a&gt;, this version is actually multiplayer. We’ll get to server/client in a different blog post 😛), but that’s nowhere near the limit of what you can do!&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting started
&lt;/h3&gt;

&lt;p&gt;Alright, we are going to hop right in! First thing first, download the &lt;a href="https://p5js.org/download/"&gt;p5.js complete library&lt;/a&gt; from the download page and extract it. We are going to work in &lt;code&gt;p5/empty-example/sketch.js&lt;/code&gt;. Alright, now all you have to do is open your favorite editor (I’ll be using VS Code) and follow along!&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawing a Square
&lt;/h3&gt;

&lt;p&gt;Once you open your &lt;code&gt;sketch.js&lt;/code&gt;, you’ll notice two functions already set up for you. In &lt;code&gt;setup&lt;/code&gt; we will initialize our environment, and in &lt;code&gt;draw&lt;/code&gt; we will describe what should be drawn per frame.&lt;/p&gt;

&lt;p&gt;Before we can draw our square, we will need to create something for us to draw it on! For this, we will use &lt;a href="https://p5js.org/reference/#/p5/createcanvas"&gt;&lt;code&gt;createCanvas&lt;/code&gt;&lt;/a&gt;, which will create a virtual canvas🖼 for us to draw on 🎨🖌&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setup() {
  createCanvas(100, 50); // 100 -&amp;gt; width, 50 -&amp;gt; height
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if you’ll open up the adjacent index.html file in your choice of a browser, you’ll see… well, nothing yet 😅. We have created the canvas, but we didn’t draw anything on it yet!&lt;/p&gt;

&lt;p&gt;To draw our square, we will use the &lt;a href="https://p5js.org/reference/#/p5/rect"&gt;&lt;code&gt;rect&lt;/code&gt;&lt;/a&gt; function. We will give it the position for where to draw it (x,y) as well as how large it should be (width, height). We will do all of this in the draw function since we want to draw the square for each frame and not just once.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function draw() {
  // x, y, width, height
  rect(10, 10, 10, 10);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YnhGc9PN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/first_square.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YnhGc9PN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/first_square.png" alt="our first square"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now if you open up your browser, you should see a square!&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining Our World
&lt;/h3&gt;

&lt;p&gt;Time to add some structure to our game-world! Since we are drawing squares, let’s add a grid. To define the grid, I will define a new variable called &lt;code&gt;UNIT&lt;/code&gt; which will be in charge of the scale of the world and the consistency. after defining our world measurement unit, we need to modify our canvas and square to use that unit. For this game, I’ll say that our world is going to be a 10x10 grid. For that, I will also define a &lt;code&gt;WIDTH&lt;/code&gt; and &lt;code&gt;HEIGHT&lt;/code&gt;, which will define our world size in terms of our world’s unit.&lt;/p&gt;

&lt;p&gt;Lastly, we will modify our canvas to use those variables, and our square to draw itself as one unit wide and tall. We will also relocate our square to be positioned at (1,1) in world units.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const UNIT = 32; // each unit in the world is 32 pixels.
const WIDTH = 10; // width, in world units.
const HEIGHT = 10; // height, in world units.

function setup() {
  // width, height
  createCanvas(WIDTH * UNIT, HEIGHT * UNIT);
}

function draw() {
  // x, y, width, height
  rect(1 * UNIT, 1 * UNIT, UNIT, UNIT);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MYJapVOo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/unit_square.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MYJapVOo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/unit_square.png" alt="unit based square"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our square is now one unit wide and tall, and our world has more consistency to it as everything would use that one unit of measurement!&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawing the Grid
&lt;/h3&gt;

&lt;p&gt;To draw our world’s grid, we simply need to draw a line horizontally, as well as vertically, per unit. To do so, we will use the &lt;a href="https://p5js.org/reference/#/p5/line"&gt;&lt;code&gt;line&lt;/code&gt;&lt;/a&gt; function. The line function requires a start position (x1,y1) and an end position (x2,y2).&lt;/p&gt;

&lt;p&gt;For a horizontal line, we keep the &lt;code&gt;y&lt;/code&gt; value the same (y1==y2), but the &lt;code&gt;x&lt;/code&gt; values will be different (x1 != x2). In our case, the x position will start from the beginning of the canvas (&lt;code&gt;x1=0&lt;/code&gt;), and end at the end of our canvas (&lt;code&gt;x2=WIDTH*UNIT&lt;/code&gt;). We’ll set &lt;code&gt;y1&lt;/code&gt; and &lt;code&gt;y2&lt;/code&gt; to &lt;code&gt;1*UNIT&lt;/code&gt; for now.&lt;/p&gt;

&lt;p&gt;We will do the opposite, keeping the x’s the same and y’s different (&lt;code&gt;x1==x2&lt;/code&gt;, &lt;code&gt;y1!=y2&lt;/code&gt;) for a vertical line. We’ll set &lt;code&gt;x1&lt;/code&gt; and &lt;code&gt;x2&lt;/code&gt; to &lt;code&gt;1*UNIT&lt;/code&gt; for now as well.&lt;/p&gt;

&lt;p&gt;Congrats! now we have two lines ready to be drawn, one vertical and one horizontal.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ... constants &amp;amp; setup function
function draw() {
  // ... rest of drawing code

  //(x1,y2,x2,y2)
  // Horizontal line
  line(0, UNIT * 1, WIDTH * UNIT, UNIT * 1);

  // Vertical line
  line(UNIT * 1, 0, UNIT * 1, HEIGHT * UNIT);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bHGbyijF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/lines.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bHGbyijF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/lines.png" alt="two lines and a square drawn"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you got one line of each drawn, it’s time to draw the rest of the grid! We will use a &lt;a href="https://mdn.io/for"&gt;&lt;code&gt;for&lt;/code&gt; loop&lt;/a&gt; to do that. we will have two for loops (though because our grid is a square, this could be achieved with a single loop since &lt;code&gt;WIDTH == HEIGHT&lt;/code&gt;). We will need to do some minimal alteration to our original line drawing code: simply change the value of &lt;code&gt;1&lt;/code&gt; that is being multiplied by &lt;code&gt;UNIT&lt;/code&gt;, with the value that the loop is in charge of. We will loop over the number of units on our &lt;code&gt;y&lt;/code&gt; axis (&lt;code&gt;HEIGHT&lt;/code&gt;) to draw the horizontal lines, and loop over the number of units we have on our &lt;code&gt;x&lt;/code&gt; axis (&lt;code&gt;WIDTH&lt;/code&gt;) to draw the vertical lines.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ... constants &amp;amp; setup function
function draw() {
  // ... rest of drawing code

  // Horizontal lines
  for (y = 0; y &amp;lt; HEIGHT; y++) {
    //(x1,y2,x2,y2)
    line(0, UNIT * y, WIDTH * UNIT, UNIT * y);
  }

  // Vertical lines
  for (x = 0; x &amp;lt; WIDTH; x++) {
    //(x1,y2,x2,y2)
    line(UNIT * x, 0, UNIT * x, HEIGHT * UNIT);
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Lo6yS-lN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/grid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Lo6yS-lN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/grid.png" alt="grid"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You should now have a grid drawn, as well as your square!… But, where is the square..? Let’s change the square’s color so it’s easier to distinguish it from the rest of the grid. We will need to use the function &lt;a href="https://p5js.org/reference/#/p5/fill"&gt;fill&lt;/a&gt; and give it RGB values to fill our rectangle with.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ... constants &amp;amp; setup function
function draw() {
  // r, g, b
  fill(0, 100, 255); //fill w/ blue

  // ... rest of drawing code
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QfoR1fll--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/colored_square.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QfoR1fll--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/colored_square.png" alt="blue square on canvas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Phew, there is our square! Time to make the square move 😁&lt;/p&gt;

&lt;h3&gt;
  
  
  Moving Your Square
&lt;/h3&gt;

&lt;p&gt;Time to make that square move! Let’s start off by using variables for the position of the square (&lt;code&gt;x,y&lt;/code&gt;) instead of just numbers. To do that, let’s create an object called &lt;code&gt;pos&lt;/code&gt; to represent our position in the external scope. After we create that variable, we will then replace the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; coordinates of the square to use the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; components of the position object.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ...constants

const pos = {x:1,y:1}

// ...setup function

function draw(){
  // ... fill function

  rect(pos.x * UNIT, pos.y * UNIT, UNIT, UNIT)

  // ... grid draw loop
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we need to update the &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; components of our &lt;code&gt;pos&lt;/code&gt; according to the keys pressed. To do so, we will use the &lt;a href="https://p5js.org/reference/#/p5/keyIsDown"&gt;&lt;code&gt;keyIsDown&lt;/code&gt;&lt;/a&gt; function and the provided Arrow &lt;a href="https://p5js.org/reference/#/p5/keyCode"&gt;keyCodes&lt;/a&gt;. Since (0,0) is our top left corner, we would want to increment &lt;code&gt;x&lt;/code&gt; when the right arrow is pressed, and decrement when the left arrow is pressed. As for &lt;code&gt;y&lt;/code&gt;, we would want to increment when the down arrow is pressed, and decrement when the up arrow is pressed.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ...constants &amp;amp; setup function
function draw() {
  // ... rest of draw function

  if (keyIsDown(LEFT_ARROW)) {
    pos.x--;
  }

  if (keyIsDown(RIGHT_ARROW)) {
    pos.x++;
  }

  if (keyIsDown(UP_ARROW)) {
    pos.y--;
  }

  if (keyIsDown(DOWN_ARROW)) {
    pos.y++;
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Time to see what we came up with! Let’s try to move around now… and… uhhh well, that’s not what we wanted now, is it?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8R-gQ2HD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/smearing_square.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8R-gQ2HD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/smearing_square.gif" alt="square smearing on canvas"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the reason this is happening is that the canvas is never getting cleared between frames! If we keep on drawing on that canvas every frame, and never cleaning it, the drawing from the previous frame remain!&lt;/p&gt;

&lt;p&gt;Not to worry, there is an easy solution, the function &lt;a href="https://p5js.org/reference/#/p5/clear"&gt;&lt;code&gt;clear&lt;/code&gt;&lt;/a&gt;. We will run it as the first thing in our drawing loop, so it will clear our canvas before each frame and get it ready for a re-draw!&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ...constants &amp;amp; setup function
function draw() {
  clear();
  // ... rest of draw function
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DtERjhSR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/moving_square.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DtERjhSR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/http://www.baruchadi.com/assets/2019-04-15/moving_square.gif" alt="square moving with arrow keys"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that’s more like it! Our square is moving according to whatever key we pressed!&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;em&gt;EOF&lt;/em&gt;
&lt;/h1&gt;

&lt;p&gt;There are many directions you can take it from here! This is just the tip of the iceberg. Personally, I took this little challenge and turned it to a multiplayer game that I am actively working on and will document my progress on 😁&lt;/p&gt;

&lt;p&gt;This is my first post that makes it to dev.to! Feel free to reach out to me if you have any questions in the comments or directly on Twitter 🙌&lt;/p&gt;


</description>
      <category>gamedev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
