This is another personal project, continuing to experiment with PokéApi! And for this project, I used React and Tailwind.css because I want to become better at React and creating a memory game seemed to be quite good project to further develop my skills and understand React better.
If you want to clone this project to try it for yourself, I provided instruction at the end of this readme.
This is supposed to be a memory game of one sort. Not your typical one where you match two cards and continue, but you need to click each Pokemon once to win in this game. Sounds easy? Wellll... the catch is, that the cards will be shuffeled after each click. Good luck! Oh, and here is one picture of how the game looks like.
Here's a breakdown of how the game functions from start to end. I also try to explain with my own words what I used and learned:
-
Fetching Pokémon Data:
- When the game starts, it fetches data for a predefined list of Pokemons from PokéApi
- The data includes each Pokémon's ID, name, and image URL / sprite
-
Loading state
- While the data is being fetched, a loading message is displayed. Once it's fetched, the loading message is replaced with game grid.
-
Use state
- I used React's
useStatehook to manage various states in this project. I learned that you can useuseStateto re-render page content without updating the whole page itself. You can also set an initial value touseStateliketruefor loops
- I used React's
-
Rendering the game
- The game renders cards in a grid, that display picture and name of pokemon. This one uses data stored in
pokemonstate. Good stuff!
- The game renders cards in a grid, that display picture and name of pokemon. This one uses data stored in
-
Handling Clicks
- Each Pokemon has an unique ID key, and after each click the game checks
clickedarray to see, if the Pokemon has been already clicked / in the array - If
true, the game resets theclickedarray and setsscoreto 0 - If
false, the game continues and adds the next Pokemon with unique ID to theclickedarray - And finally
bestScorestate is updated if the currentscoreexceeds it
- Each Pokemon has an unique ID key, and after each click the game checks
-
Shuffling the cards
- Cards are shuffled after every click to make memory game more difficult. It is done by creating a new shuffled array of Pokemon data
-
Help Popup
- You can find a help button in the bottom right corner of the page. After clicked, it shows a message to tell you how to play the game. It toggles
helpstate that is a boolean. PHEW!
- You can find a help button in the bottom right corner of the page. After clicked, it shows a message to tell you how to play the game. It toggles
Past week or almost two, I was reading through Mozilla dev docs about Javascript to understand more how it works, and made some small exercises along the journey. I wanted to do it before jumping into deep waters of React, even though it's somewhat familiar already to me. But this way, I feel more confident and understand the possible bugs or getting around a problem, I know what could work and what to Google.
And before moving to React, I was debating whether I should try Vue instead, but React felt more comfortable to me this time. But at least I am using Vite, which is made by the same guy who created Vue! And not gonna lie, it is indeed faster to start a project than your regular npx create-react-app. I am still interested in Vue, but right now I want to focus more on React.
I really like how this project turned out. I added some of my favorite Pokemons to the list, because when I had a function to choose randomly generated 12 pokemons from the list of 1,025, the site would load much longer for making always new API requests. The first 12 Pokemons would be too easy as well, because of the Pokemon evolutions, so I thought this would be the best solution.
I also added later a "Loading..." paragraph / state everytime you boot up the page, so the user actually knows that something is happening. This was overall an educational project but now I need to focus more on writing my thesis. I will try to do some side projects as well though.
And now, the instructions in case you want to run this on your own PC
First things first, make sure you have Node.js installed and npm included
-
Clone the repository:
git clone https://github.com/Vaahtopahvi/memory-game.git
-
Navigate to the project dir
cd memory-game -
Install the dependencies
npm install
-
Run the project
npm run dev
Thank you and enjoy!
