@@ -2,6 +2,7 @@ import React, { EventHandler, FC, KeyboardEvent, useCallback, useEffect, useRedu
22
33import * as actionTypes from '@actions/actionTypes' ;
44import MatrixCanvas from '@components/MatrixCanvas' ;
5+ import Stats from '@components/Stats' ;
56import Queue from '@components/Queue' ;
67import { BLOCK_SIZE , BOARD_HEIGHT , BOARD_WIDTH } from '@constants' ;
78import { initialState , reducer } from '@reducers' ;
@@ -17,11 +18,11 @@ const Tetris: FC = () => {
1718 const requestRef = useRef ( 0 ) ;
1819 const lastTick = useRef ( 0 ) ;
1920 const [ frameCount , setFrameCount ] = useState ( 0 ) ;
20- const [ gameStart , setGameStart ] = useState ( false ) ;
21+ const [ gameStart , setGameStart ] = useState ( true ) ;
2122 const [ isPaused , setIsPaused ] = useState ( false ) ;
2223 const [ gameOver , setGameOver ] = useState ( false ) ;
2324
24- const { gameBoard, player, queue } = state ;
25+ const { gameBoard, player, queue, stats } = state ;
2526
2627 const _movePlayerDown = useCallback ( ( ) => {
2728 if ( player . y === player . placeholder . y ) {
@@ -47,8 +48,9 @@ const Tetris: FC = () => {
4748 if ( gameStart && ! isPaused && ! gameOver ) {
4849 requestRef . current = requestAnimationFrame ( ( ) => {
4950 const now = performance . now ( ) ;
51+ const tickTime = 250 - 25 * stats . level ;
5052
51- if ( now - 500 >= lastTick . current ) {
53+ if ( now - tickTime >= lastTick . current ) {
5254 _movePlayerDown ( ) ;
5355
5456 lastTick . current = now ;
@@ -62,7 +64,7 @@ const Tetris: FC = () => {
6264 } else {
6365 return ;
6466 }
65- } , [ _movePlayerDown , frameCount , gameOver , gameStart , isPaused ] ) ;
67+ } , [ _movePlayerDown , frameCount , gameOver , gameStart , isPaused , stats . level ] ) ;
6668
6769 useEffect ( ( ) => {
6870 // Re-draw the game board and player position
@@ -72,7 +74,7 @@ const Tetris: FC = () => {
7274 drawInitialScreen ( gameBoardCtxRef . current ) ;
7375 } else if ( gameOver ) {
7476 console . log ( 'game over' ) ;
75- drawGameOverScreen ( gameBoardCtxRef . current ) ;
77+ drawGameOverScreen ( gameBoardCtxRef . current , stats . score ) ;
7678 } else if ( isPaused ) {
7779 console . log ( 'paused' ) ;
7880 drawPauseScreen ( gameBoardCtxRef . current ) ;
@@ -194,6 +196,7 @@ const Tetris: FC = () => {
194196 width = { BOARD_WIDTH * BLOCK_SIZE }
195197 />
196198 < Queue matrices = { queue } />
199+ < Stats { ...stats } />
197200 </ div >
198201 </ div >
199202 ) ;
0 commit comments