Tetris Game with React
- React
- styled-components
- custom Hooks
- Deploy with surge.sh:
- Deploy with Firebase:
- Initial project
- setup react & tooling
- setup hooks useInterval
- Scaffolding/Initial components
- Cell
- Display
- Stage
- StartButton
- Tetris
- Stage and Tetrominos
createState- Tetrominos
- create shape of each element:
0(default), I, J, L, O, S, T, Z randomTetrominofunction
- create shape of each element:
- Styling with styled-components
- StyledCell
- StyledTetris
- StyledStage
- StyledDisplay
- StyledStartButton
- import style to component
usePlayer: set Position tetromino of user current playinguseStage: '[stage, setStage]';
createStage: when we restart the game, we need to create a clean stage for that
- Stage update and player movement
movePlayerfunction: move tetromino when user usemoveleft/right function- in
src/components/Tetris:startGamefunction:dropfunction:dropPlayerfunction: move tetromino when user usemovedown function dropPlayer();movefunction: tracking keyCode of user typing on keyboard
- in
src/gameHelper- updatePlayerPos: set new position of tetromino
- resetPlayer: reset tetrominos when run
startGamefunction
- in
src/hooks/useStageupdateStage- First flush the stage
- Then draw the tetromino
- in
src/hooks/usePlayer- set tetromino is 0 when initial application
- Collision detection
checkCollisionfunction:- Check that we are on an actual Tetromino Cell
- Check that our move is inside the game areas height (y)
- Check that our move is inside the game areas width (x)
- Check that the cell we are moving is isn't set to clear
- Add checkCollision to
src/components/Tetris
- Player RotationG
rotatefunction:- Make the rows to become cols
- Reverse each row to get a rotated matrix
playerRotatefunction- Check when tetromino rotate in a collied: true
- Clear Rows
sweepRows
- drop with useInterval
- copy
useInterval.jshook created by Dan Abramov tosrc/hooks - useInterval in
src/components/Tetris - Interval off when keyDown = DOWN
- Interval off when keyUp
- useGameStatus
- in
src/hooks/useGameStatus.js- linePoints is [40, 100, 300, 1200]
calcScore- useEffect to tracking when rowsCleared
- Import useGameStatus to
src/components/Tetris - Increase level when player has cleared 10 rows
- Also increase speed
- Optimize
src/components/Cellre-render with (React.memo)
- Cell is re-render 240times when Stage change
- So im optimized with (React.memo)
- Remove
console.log() - Deploy
- Write paper
README.md
.
├── .gitignore
├── package.json
├── README.md
├── public
└── src
├── assets
├── fonts
└── img
├── components
├── styles
├── StyledCell.js
├── StyledDisplay.js
├── StyledStage.js
├── StyledStartButton.js
└── StyledTetris.js
├── Display
├── Stage
├── StartButton
├── Cell
└── Tetris
├── hooks
├── useGameStatus.js
├── useInterval.js
├── usePlayer.js
└── useStage.js
├── gameHelper.js
├── tetrominos.js
├── App.js
├── App.css
├── index.css
└── index.js
Use the cmd line to clone repo to your computer
git clone https://github.com/tinspham209/tetris-game-react
Use the cmd line to install dependencies.
npm install
Run in cmd for start the dependencies server
npm start
