Recreating the Worlds Hardest Game on the Arduino
by John_Rock in Circuits > Arduino
1075 Views, 3 Favorites, 0 Comments
Recreating the Worlds Hardest Game on the Arduino
This project is based off of a computer game that you can find online. Its name is, "The Worlds Hardest Game." I was able to re-create it on the Arduino using a four module LED Matrix. In this instructable I teach you how to construct it yourself. Before we get into to too many details I want to give a quick overview of the steps I went through to create this game.
- I gathered all the parts needed.
- I connected the parts together.
- I wrote a sketch that used the joystick to move the players LED across all the modules.
- I designed the first obstacles and added them to the sketch so they emulated the Worlds Hardest Game.
- I then added code to detect a collision with the obstacles, which will restart the game.
- And then I designed two more levels of obstacles.
All the Parts
Here is a list of all the parts you need:
- An Arduino UNO and usb connector: https://www.banggood.com/Wholesale-Arduino-Compati.
- A four module 8x8: LED matrix https://www.banggood.com/MAX7219-Dot-Matrix-Module..
- A Joystick: https://www.banggood.com/PS2-Game-Joystick-Module-...
- 10 male to female wires: https://www.banggood.com/40pcs-20cm-Male-To-Female...
How to Connect the Parts to the Arduino
How to Connect the LED Matrix to the Arduino
- GND goes to GND
- VCC goes to 5V
- DataIn goes to digital pin 12
- CLK goes to digital pin 11
- CS or LOAD goes to digital pin 10
How to Connect the joystick
- GND goes to GND
- 5V goes to 5V
- VRx goes to analog pin A0
- VRy goes to analog pin A1
- SW is not used
The battery connect to the 9v jack to give the Arduino power
Flow Chart of the Program
The oval indicates the start of the program.
The first step is to define all the obstacles.
The next two steps indicate setting the variables and turning on all of the modules.
The next step, is to set the LED's to the first level and any other variables.
Next display the level that the player is currently on.
The rhombus indicates reading the Joystick to see what direction it is being pushed.
Then move the player in whatever direction the Joystick was pushed.
Check and see if the player collided with an obstacle.
If the player hit an obstacle, go back to the first level. If not check to see if the player reached the end of the level.
If the player is at the end of the level, set to next level then go back to, "Display Current Level.". If they are not at the end, move the obstacles and go back to, "Read Joystick."
Programming the Joystick
Of course in order to move the little dot that is the player, we need a joystick. and in order to allow the joystick to actually move the player we need to code it in the Arduino IDE. You need to include the LedControl library which you can find by going to the sketch menu>Include Libraries>manage libraries, and search for LedControl. Here is what the code for the joystick looks like.
#include "LedControl.h" int DataIn = 12; int CLK = 11; int DIN = 10; LedControl lc=LedControl(DataIn,CLK,DIN,4); //Creates object for four modules int delaytime = 50; //speed at which game runs int joystick_RtLt, joystick_UpDn; int players_x = 0; //players horizontal position from 0 to 31 int players_y = 3; //players vertical position from 0 to 7 int row, column, module; void setup() { initialize_modules(); //turn on and set all four led modules } void loop() { move_player(); //start loop by checking if player moves delay(delaytime); } void initialize_modules(){ lc.shutdown(0,false); // starts up module 0 lc.setIntensity(0,1); lc.shutdown(1,false); // starts up module 1 lc.setIntensity(1,1); lc.shutdown(2,false); // starts up module 2 lc.setIntensity(2,1); lc.shutdown(3,false); // starts up module 3 lc.setIntensity(3,1); lc.clearDisplay(0); // clears module 0 } void move_player(){ module = players_x/8; //defines which module the player is on column = players_x%8; //defines the column where the player is on the module row = players_y; lc.setLed(module,row,column,false); //turn off led at player's current position joystick_RtLt = analogRead(A0)/204 - 2; //read X-joystick and map range from 2 to -2 if(joystick_RtLt > 0) //when joystick is to the right players_x++; //go right else if(joystick_RtLt < 0 && players_x > 0) //if joystick is to the left and not at the beginning players_x--; //move left joystick_UpDn = analogRead(A1)/204 - 2; //read Y-joystick and map range from 2 to -2 if(joystick_UpDn < 0 && players_y > 0) //if joystick is down and player is not at bottom players_y--; //move down else if(joystick_UpDn > 0 && players_y < 7) //if joystick is up and player is not at top players_y++; //go up module = players_x/8; //set module to players new position column = players_x%8; //set column to players new position row = players_y; //set row to players new position lc.setLed(module,row,column,true); //turn on led at player's new position }
Now that you have added the joystick let's work on the first obstacle!
The First Level
Alright, so now it's time to get into the main part of the program, the obstacles! The obstacles are what really make this the, "World's Hardest Game." So if you want to actually play the game then you need to add this to your code:
byte obstacle[LEVELS][SCENES][COLUMNS][ROWS] = { //three levels, 8 scenes, 8 collums, 8 rows {{{1,1,1,1,1,1,0,0}, //first level, first scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,1,1,1,1,1,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{1,1,1,1,1,0,0,1}, {0,0,0,0,0,0,0,0}, //first level, second scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,0,0,1,1,1,1,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{1,1,1,1,0,0,1,1}, {0,0,0,0,0,0,0,0}, //first level, third scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,0,0,1,1,1,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{1,1,1,0,0,1,1,1}, {0,0,0,0,0,0,0,0}, //first level, fourth scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,1,0,0,1,1,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{1,1,0,0,1,1,1,1}, {0,0,0,0,0,0,0,0}, //first level, fifth scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,1,1,0,0,1,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{1,0,0,1,1,1,1,1}, {0,0,0,0,0,0,0,0}, //first level, sixth scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,1,1,1,0,0,1}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{0,0,1,1,1,1,1,1}, //first level, seventh scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}, {{0,1,1,1,1,1,1,1}, //first level, eighth scene {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}}, void setup() { initialize_modules(); //turn on and set all four led modules start_level(0); void loop() { move_player(); //start loop by checking if player moves if (collision()){ //check for collision level = 0; //if collision true, reset level start_level(level); //restart game }else if(delay_count == obstacle_delay){ //if no collision move obstacle every other delay moveobstacle(); delay_count = 0; }else delay_count++; delay(delaytime); //delay by speed of game }
And that's the first obstacle! If you get bored of this as your first level then you can always change the bytes, just remember you need to have eight different scenes! Let's continue on to the next step, collision detection!
Collision Detection
This step is another important part of the game, with out it there would not be much of a challenge! you could just weave your way through the obstacles with out any consequences! that wouldn't be much fun right? so let's make the game more challenging (and a lot more entertaining!) by adding this section of the program:
void loop() { move_player(); //start loop by checking if player moves if (collision()){ //check for collision level = 0; //if collision true, reset level start_level(level); //restart game }else if(delay_count == obstacle_delay){ //if no collision move obstacle every other delay moveobstacle(); delay_count = 0; }else delay_count++; delay(delaytime); //delay by speed of game } int collision(){ //check if player hit an obstacle in scene module = players_x/8; column = players_x%8; row = players_y; if(module > 0) if(obstacle[level][scene][column][row] == 1) //if player's position is same as obstacle return(1); //collision detected return true return(0); //no collision return false }
And there you go! now you can enjoy the game a lot more! Now i'm going to show you how to program the last two levels of the game! your almost done with this instructable, and i'm sure your ready to try it out!
The Last Two Levels
Were coming to the end of the instructable, these last two levels are all you need to finish this game. Then you can try it out for yourself! here is the code:
byte obstacle[LEVELS][SCENES][COLUMNS][ROWS] = { //second and third level, 8 scenes, 8 columns, 8 rows {{{1,0,0,0,0,0,0,1}, //second level, first scene {0,1,0,0,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,0,0,1,1,0,0,0}, {0,0,0,1,1,0,0,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,1,0}, {1,0,0,0,0,0,0,1}}, {{1,1,0,0,0,0,0,1}, //second level, second scene {0,0,1,0,0,0,0,1}, {0,0,0,1,0,0,1,0}, {0,0,0,1,1,1,0,0}, {0,0,1,1,1,0,0,0}, {0,1,0,0,1,0,0,0}, {1,0,0,0,0,1,0,0}, {1,0,0,0,0,0,1,1}}, {{0,1,0,0,0,0,0,0}, //second level, third scene {0,0,1,1,0,0,0,1}, {0,0,0,1,0,0,1,0}, {0,0,0,1,1,1,1,0}, {0,1,1,1,1,0,0,0}, {0,1,0,0,1,0,0,0}, {0,1,0,0,0,1,1,0}, {0,0,0,0,0,0,1,0}}, {{0,0,1,0,0,0,0,0}, //second level fourth scene {0,0,0,1,1,0,0,0}, {0,0,0,0,1,0,0,1}, {0,1,1,1,1,0,1,0}, {0,1,0,1,1,1,1,0}, {1,0,0,1,0,0,0,0}, {0,0,0,1,1,0,0,0}, {0,0,0,0,0,1,0,0}}, {{0,0,0,1,0,0,0,0}, //second level, fifth scene {0,0,0,0,1,0,0,0}, {0,0,1,0,0,1,0,0}, {0,1,0,1,1,0,0,1}, {1,0,0,1,1,0,1,0}, {0,0,0,1,0,0,1,0}, {0,0,0,1,0,0,0,0}, {0,0,0,0,1,0,0,0}}, {{0,0,0,0,1,0,0,0}, //second level, sixth scene {0,0,0,0,0,1,0,0}, {0,1,1,0,0,1,0,0}, {1,0,0,1,1,0,0,0}, {0,0,0,1,1,0,1,1}, {0,0,1,0,0,1,1,0}, {0,0,1,0,0,0,0,0}, {0,0,0,1,0,0,0,0}}, {{0,0,0,0,0,1,0,0}, //second level, seventh scene {0,0,0,0,0,1,0,0}, {1,1,1,0,0,1,0,0}, {0,0,0,1,1,0,0,0}, {0,0,0,1,1,0,0,0}, {0,0,1,0,0,1,1,1}, {0,0,1,0,0,0,0,0}, {0,0,1,0,0,0,0,0}}, {{0,0,0,0,0,0,1,0}, //second level eighth scene {1,1,0,0,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,0,0,1,1,0,0,0}, {0,0,0,1,1,0,0,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,1,1}, {0,1,0,0,0,0,0,0}}}, {{{0,1,0,0,0,0,0,1}, //third level, first scene {0,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,1,0,0}, {1,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {0,0,0,1,0,0,0,0}}, {{0,0,0,0,1,0,0,1}, //third level, second scene {1,0,0,0,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0}, {0,0,1,0,0,1,0,0}, {0,0,0,0,1,0,0,0}, {1,0,0,1,0,0,1,0}}, {{0,1,0,0,1,0,0,1}, //third level, third scene {0,0,0,1,0,0,1,0}, {0,0,0,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,1,0,0,1,0}, {0,0,0,0,0,0,0,0}, {0,1,0,0,0,0,0,1}, {1,0,0,1,0,0,0,0}}, {{0,0,0,0,1,0,0,1}, //third level, fourth scene {1,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,0,0,0,1,0,0,1}, {1,0,0,0,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,0,0}, {1,0,0,1,0,0,1,0}}, {{0,1,0,0,1,0,0,1}, //third level, fifth scene {0,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,0,1}, {0,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,0,0,0,0,0}}, {{0,0,0,0,1,0,0,1}, //third level, sixth scene {1,0,0,1,0,0,1,0}, {0,0,1,0,0,0,0,0}, {0,0,0,0,1,0,0,1}, {1,0,0,1,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,0,0,0,0}, {1,0,0,1,0,0,1,0}}, {{0,1,0,0,1,0,0,1}, //third level, seventh scene {0,0,0,1,0,0,0,0}, {0,0,0,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,1,0,0,1,0}, {0,0,1,0,0,0,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0}}, {{0,0,0,0,1,0,0,0}, //third level, eighth scene {1,0,0,0,0,0,1,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,1,0,0,1}, {1,0,0,1,0,0,0,0}, {0,0,1,0,0,1,0,0}, {0,1,0,0,1,0,0,0}, {1,0,0,1,0,0,1,0}}} };
And that's it! you're all set to attempt to complete the Worlds Hardest Game. Also the full Arduino code is attached below.