Introduction & Concept:
I switched from my initial plan of making a simple dino jump game to more colourful and fun game called “SHINOBI SPRINT”. The game is a side-scrolling endless runner game based on the famous ninja from the Naruto anime series, as he runs horizontally and avoids obstacles vertically to score points. The game provides a challenging experience where players need to demonstrate agility and reflexes to achieve high scores.
The main objective of the game is to control Naruto and survive as long as possible while avoiding obstacles coming from right towards Naruto. Players need to control Naruto’s vertical movements to avoid obstacles by moving the ultrasonic distance sensor. The plan initially was to move the hand in front of the sensor but after doing some test runs, I realised that the sensor wasn’t picking the values correctly. So, now the player has to carry the sensor in his/her hand and move it away or towards any surface to the control the vertical movement of Naruto.
The Obstacles in the game appear at regular intervals and move towards Naruto and consist of fire images that are randomly placed on the canvas. Players must navigate Naruto vertically to avoid colliding with the obstacles. Colliding with an obstacle ends the game. The game includes a score system that tracks the player’s survival time. The longer the player survives, the higher the score.
Implementation:
The implementation is again simple, player carries the sensor in his/her hand move it up and down on some surface (preferably table) to make Naruto go up or down. The sensor values start from 0 and for the sake of this project the values only range till 460 to make Naruto go up and down within the canvas.
The goal is to navigate Naruto through the gaps between obstacles and avoid collisions. The game ends when Naruto collides with an obstacle, and the player’s score is displayed.
Arduino:
Using serial communication between p5.JS and arduino, I was able to send distance values to p5.js to control Naruto in the game. The Arduino code is responsible for reading sensor data, processing it, and sending it to the p5.js program. Here’s the Arduino Code:
// defines pins numbers const int trigPin = 9; const int echoPin = 10; // const int ledPin = 3; // int duty = 10; // defines variables long duration; int distance; void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output // pinMode(ledPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); // Starts the serial communication } void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance = duration * 0.034 / 2; // Prints the distance on the Serial Monitor * 0.034 / 2 // analogWrite(ledPin, duty); Serial.println(distance*4); delay(150); }
P5.JS:
The p5.js code handles the game’s main logic and rendering. It includes functions to load images, control the movement of the landscape, manage game states (main screen, gameplay, game over), and handle collisions between objects. The code uses the sprite concept to represent Naruto, obstacles, and other elements in the game. It also includes functions for drawing the main screen and game over screen and the mousePressed function attached to clicking on Play before the start and to restart the game each time it’s over. I’ll be submitting the project below so you can all take a look over the p5.js code.
Communication between Arduino and p5.js
The communication between Arduino and p5.js is established through a serial connection. The p5.js program reads the distance values sent by the Arduino and uses them to control Naruto’s vertical position. The Arduino continuously sends distance data, which is received by the p5.js program. This communication allows the player to control Naruto’s movement in real-time.
Cool Things in the game:
Here are some of game prospects that I am most proud of:
Parallax Landscape: The game features a visually appealing parallax landscape, where the background scrolls at a slower speed than the foreground, creating depth and immersion.
Sprite Animation: The use of sprite animation brings Naruto and other elements to life, enhancing the game’s visual experience.
Intuitive Controls: The game utilizes an Arduino device to control Naruto’s movement, providing a unique and engaging gameplay experience.
Here’s the p5.js code for this project:
Future Improvement
Game Difficulty: Implementing adjustable difficulty levels or introducing new obstacles or power-ups could enhance the game’s replay value.
Audio: Adding sound effects and background music would contribute to the game’s overall atmosphere.
Changing the values received from arduino when communicating data with it: As the sensor values increase from down to up, the movement of Naruto on the screen is opposite the way it should be. I would like to fix this feature in future.
User Testing:
Here’s the video of one of my friends trying to play the game.
I think the controls were not really difficult for the players to understand. My friend was able to figure out the controls on his own and was able to score good score in his 3rd try.
Again, the only problem here is that the movement of Naruto is opposite than the user movement of his/her hands. For example, when going up with the sensor, Naruto goes down. This is one of the main issue I faced and couldn’t find a way to fix it. Hopefully, when I make changes to this project in Future, I’ll be able to figure out the solution for this problem.