fac19 / snake Goto Github PK
View Code? Open in Web Editor NEWWho's the bigger snake?
Home Page: https://sssssnake.netlify.app
Who's the bigger snake?
Home Page: https://sssssnake.netlify.app
Your repo is currently private, which means no one outside the fac19 GitHub organisation can see it. This will be a problem when a graduate tries to code review the project tomorrow/Friday.
Could you make it public please? :)
CSS: The hover on the snakes looks really cool. You could add a :focus pseudo-class so it stays highlighted after you've selected it. Same with the levels.
hahaa love the levels. Perhaps show difficulty levels for those less politically inclined (easy, medium, impossible)
Hey frens, this is such a cool take on Snake!!! The decrementing score counter is funny, and the reward of seeing a new GIF each time you get a new vote is a nice incentive to keep playing 💯
My favourite detail is the way each block of the snake animates to make it look stretchy and slithery. And the way the tail is slightly more opaque than the head. It's a really nice touch!
As for the code/logic, I will take a look and leave you some feedback in separate issues
(update: it's not allowing me to add labels to these issues, feel free to add the code review label yourselves)
I learnt a load of stuff reading this, including:
in snake.js instead of a big if-else you could just do a ternary inside the {}, or alternately do a small if-else before the return statement that assigns the data to a variable, then put the variable inside the {}. Then you could keep your code DRY by not repeating the HTML tags.
This is an accessibility thing: people with certain vestibular disorders struggle with UIs containing lots of motion. There's also the obvious problem for people with photosensitive epilepsy—some of the gif backgrounds flash really fast and could be dangerous.
Modern browsers support a new media query called prefers-reduced-motion
that will tell you if the user has opted out of animations etc in their operating system settings. You can detect this and hide gifs/disable CSS animations
There is a brief blue flash that occurs when you eat food and a new GIF loads (I don't think it shows in the GIF below though :( ):
It looks like there is a brief moment when the backgroundUrl
of the game-area
div is set to nothing before the new one loads. One way to stop the flash from happening is to set the GIF as the src
on an <img />
tag inside the if (gameplay) { ...
block starting on line 136 of App.js:
if (gamePlay) {
return (
<div
className="game-area"
>
<img
src={
`https://media.giphy.com/media/${
backgroundURL ? backgroundURL : initialState.id
}/giphy.gif`
}
alt="alt-text-goes-here"
/>
...
...
...
}
Just doing the above seemed to fix issue locally for me 💥. Apparently the so-called 'Flash Of Unstyled Content' (or FOUC) is a common problem in React apps. Here's a thread on how to handle it in other situations, like when there are slow loading images on page load
Cannot see div
with 'how to play' information as not able to scroll down page. When I resize the page this means I cannot select the 'GO' button.
This is a minor comment, I noticed you've capitalised the name of the top level App
component file in your application, and that the other component file names (characterSelector.js
, food.js
, score.js
, snake.js
) aren't capitalised. I'd recommend sticking to one naming convention - the one that is commonly recommended for React apps is to capitalise all component names (which you've quite rightly done 👍 ) and the names of the files and folders that contain those components (Food.js
, Score.js
, etc.) :)
The fetch request doesn't work for me, either locally or on the deployed site, but do work for James. Not sure if this is a code issue or a my laptop issue?
At the top of App.js:
import React, { useState, useEffect } from 'react'
import Score from './components/score'
import Snake from './components/snake'
import Food from './components/food'
import CharacterSelector from './components/characterSelector'
import getRandomCoordinates from './utils/getRandomCoordinates'
import initialState from './utils/initialState'
import onKeydown from './utils/keyDown'
import getGif from './utils/getSnake'
import useInterval from './utils/useInterval'
one thing you might like to try as your app increases in complexity is to import all of the components and utils that you need from an index.js
file (instead of importing them from the individual component/util files). When you start having more components/utils, using index files can help keep things easy to read in App.js
.
E.g. if you were to use this file structure to organise your components, you'd create an index.js
file in your components
folder, from which you'd export an object containing all your components from it:
components/index.js
import CharacterSelector from './characterSelector';
import Food from './food';
import Score from './score';
import Snake from './snake';
export { CharacterSelector, Food, Score, Snake };
You'd import this index file in App.js
, and use ES6 object destructuring to unpack the components from it (exactly the same as you've done to access the useState
and useEffect
methods from the React
object):
import React, { useState, useEffect } from 'react'
import { CharacterSelector, Food, Score, Snake } from './components/index'
(hope this reads clearly, but if it doesn't than do give me a shout!)
Nice job creating components
and utils
folders for your components and general purpose application logic!
The useInterval function is really interesting, it might be a good one to talk about in the presentation.
You can separate your CSS out based on the specific component. For example for the food.js component file create a style sheet called food.css and in food.js import it with import ./food.css
The onKeyDown function is really interesting - I notice you plan to mention it in the presentation, which I'm looking forward to because tbh I don't get it. It might also be good to include an explanatory comment for yourself six months down the line.
In case you don't know, React provides a component called Fragment
that you can use in place of a div
to group together JSX elements inside a return block. You can import it from React and use it exactly like you would use a div, the advantage is that it Fragments don't appear in the rendered HTML page (in other words, it doesn't clutter the web page with semantically meaningless divs) :
import React, { Fragment } from 'react'
export default ({ startGame, topScore, chooseCharacter, chooseDifficulty }) => {
return (
<Fragment>
...
...
...
...
...
</Fragment>
)
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.