Alternate Controller Project: Using a joystick to play borderless snake

     This project was based around the game of snake, where to goal is to eat enough apples to make the snake fill the entire screen of the game. Once this is done, you have won. But if you run into yourself, then you die and have to restart. The gameplay is through the joystick, which is irregular for the game on snake, usually played on a keyboard using the arrow keys or aswd.

I would usually include here a CSV snapshot that shows all the parts of the project, but since TinkerCAD didn't have most of the parts required, I will just list them out here.

  1. 1 Arduino Uno
  2. 1 Breadboard
  3. 5 male to male Arduino wires
  4. 4 male to female Arduino wires
  5. 1 Arduino 8x8 compatible LED display
  6. 1 Arduino compatible Joystick
Both the 8x8 LED and the Joystick had to be bought separately from the "base" Arduino kit. As such, they required research into them to make sure I got the right parts, as well as how to use an implement them properly. One of the big mistakes I made involved the 8x8 LED, where unlike the joystick, I did not buy one with pins already soldered in, so I had to do that myself. Luckily this was not to big of an issue as I have soldering tools readily available, but this could have been more of an issue for someone that dosen't have the tools. So make sure to double check the parts before you buy.




The circuit for the project is super simple, and way less complicated than the coding side. I string the 5v power and ground to the bread board, both of which the LED array and the joystick connect to. The joystick has 2 extra output lines I use, the X and Y output, which are connected to the analog 3 and 4 pins respectively. These pins relay the current X and Y values of the joystick to the Arduino. The 8x8 LED has 3 input pins, being the DIN, CS, and CLK, being connected to the 12, 11, and 10 digital pins respectively. The circuit runs from the joystick output, into the Arduino, and then being outputted through the 8x8 LED array.


The code for this program is super complicated, so I will try to break it down as best as possible, while not diving to deep and still showing off the project. The code starts by creating a struct for both the snake and apple, as well as setting up the pins for the joystick and LED array, as well as set time variables. Once we get to setup(), we set LED display settings, as well as determine setup pin functions for the Joystick X and Y. Into loop, we move update the time, as well as read the current values of the joystick. These values are fed into the if else block below, which then changes the snakes current direction based on the joystick value. We then hit the update, then the render functions. We then come to the border handler (which can be turned off and on to the players liking), the self-collision handle, and the eat apple handler. The last bit of update adds the snakes new head and apple. Render renders the current snake and apple on the LED array. The removeFirst function removes the last section of the snake.


This project was super fun and super complicated. Having the code and components all come together for this fun game was a super satisfying conclusion to it all, and this is by far the most fun Arduino project I have done. It even gave me a chance to practice soldering skills for this class, which is something I didn't think I would have to do, but it is nice that it came up. This will most likely be the project I keep my Arduino as when this class is over.


Credits:


Comments

Popular posts from this blog

P1 Completed Project Post

Final Project: Reusing the Snake board design to create Tetris with the Arduino