I built a Snake Game in React

i-built-a-snake-game-in-react

TL;DR: I built this classic “Snake Game” using React, HTML Canvas, and TypeScript. No third-party libraries are used.

Snakes Game Demo

Background Story

While I was bored and lying on the swing the other day, a random thought just went into my head. I remember back in the days that I used to play this Snakes game on a Nokia phone. So I wondered how could I build a game like this🤔.

Web development is my thing, I’m not a game dev and I never built a game before, so this would be a challenge.

But tackling a challenge is also my thing.

So I sat there for a couple of hours conceptualizing this possibly ‘new project’ to see if I could pull this up. I wrote down my ideas and the structure of the project on my phone, as well as some pseudocode.

After pondering about it for a while, I think I can build this thing and I have a rough concept in my phone, with all the game logic ready to be implemented and tested if this idea works.

Development Time

I was excited to start this project. So I brought my phone with my concept in it, went to my computer, and opened VS Code.

I’ve thought of what tech should I use. Welp, I don’t think I have lots of choices, I only know React (as I said, I’m just a web developer 😅). But since the game will be implemented primarily using HTML Canvas, I thought React would be optional as I can build this without a framework. But still, I pushed on as I hadn’t built a React project before with this complex Canvas Element logic, so I gave it a try.

Of course, I used TypeScript😉.

And ohh, no CSS libraries. A plain ‘ol CSS would do the trick since the UI won’t be too complex and the components that I will style won’t be that much.

After several hours of coding (and tons of debugging), I finally managed to implement the game logic and my idea came to fruition. It’s rough to the edges but the game is already working.

This is the first game I made and the joy of making it work is priceless. It was just an idea moments ago and now I’m already playing it.

Anyway, I’m also a bit tired at this moment so I continued the other day.

So on the next day, I spent most of my hours fixing several bugs in the code. I also added some new features, like the scoring, high score, and pause/play.

Then it’s finished. I was so ecstatic. I showed it to my brothers and let them play for a while (and also to check if they might encounter some bugs, which they actually did).

After fixing those minor bugs and making the game fully working, I hosted it on Vercel and wrote this blog.

Demo

Snakes Game Demo

Features

  • Classic Snake gameplay
  • Score tracking
  • Saves HighScore
  • Game over screen with the option to restart
  • Keyboard controls for navigation

Tech Stack

  • React.js and HTML Canvas
  • TypeScript for type safety
  • No third-party libraries used

Game Controls

Use the arrow keys or W,A,S,D keys on your keyboard to control the snake’s direction:

↑ (Up) or W – Move Up
↓ (Down) or S – Move Down
← (Left) or A – Move Left
→ (Right) or D – Move Right

Others:

  • To Pause the game – Press esc or click anywhere on the screen

Run it locally

To run the game locally, follow these steps:

1.) Clone this repository:

git clone git@github.com:/snakes-game.git

2.) Navigate to the project directory:

cd snake-game

3.) Install the required dependencies. Yarn is recommended:

yarn

4.) Run the build script:

yarn build

5.) Start the game by serving the build output:

yarn preview

NOTE: The development environment causes the components to re-render, causing the game logic for the canvas to be duplicated and appear buggy. That’s why it’s recommended to build the app and run the build output to avoid the re-renders.

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
[beginner-friendly]-transform-your-web-app-with-formbricks-surveys:-a-must-know-guide

[Beginner Friendly] Transform Your Web App with Formbricks Surveys: A Must-Know Guide🔥🔥🔥

Next Post
what’s-new-in-stateadapt-20.0:-adapt-rework

What’s New in StateAdapt 2.0.0: adapt rework

Related Posts