Skip to content

Commit

Permalink
support eliminated liens and score
Browse files Browse the repository at this point in the history
  • Loading branch information
huoyijie committed Nov 7, 2023
1 parent c3d8445 commit c6a1244
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/components/Info.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ import Context from './Context';
import { I } from '@/lib/TetrominoType';

export default function () {
const { gameOver, nextTetromino } = useContext(Context)
const { gameOver, nextTetromino, score, eliminatedLines } = useContext(Context)

if (gameOver) return <></>

return (
<div className='flex flex-col gap-8'>
<div className='flex gap-6 justify-between items-center'>
<span>得分</span>
<span className='text-2xl font-bold text-red-600'>100</span>
<span className='text-2xl font-bold text-red-600'>{score}</span>
</div>

<div className='flex gap-6 justify-between items-center'>
<span>消除行</span>
<span className='text-2xl font-bold text-red-600'>20</span>
<span className='text-2xl font-bold text-red-600'>{eliminatedLines}</span>
</div>

{nextTetromino && (
<div className='flex gap-6 justify-between items-center'>
<div>下一个</div>
Expand Down
10 changes: 6 additions & 4 deletions src/lib/tetris.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ export function moveDownTetromino(tetromino, tetrominoes, onCollise, onGameOver,
return moved
}

if (eliminateLines(tetromino, tetrominoes, setTetrominoes) == 0 && reachTop) {
const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes)
if (eliminatedLines == 0 && reachTop) {
onGameOver()
} else {
onCollise()
onCollise(eliminatedLines)
}

return tetromino
Expand Down Expand Up @@ -245,9 +246,10 @@ export function fallDownTetromino(tetromino, tetrominoes, onCollise, onGameOver,

tetromino.y = moved.y - 1

if (eliminateLines(tetromino, tetrominoes, setTetrominoes) == 0 && result.reachTop) {
const eliminatedLines = eliminateLines(tetromino, tetrominoes, setTetrominoes)
if (eliminatedLines == 0 && result.reachTop) {
onGameOver()
} else {
onCollise()
onCollise(eliminatedLines)
}
}
16 changes: 13 additions & 3 deletions src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default function () {
const [tetrominoes, setTetrominoes] = useState([])
const [currentTetromino, setCurrentTetromino] = useState()
const [nextTetromino, setNextTetromino] = useState()
const [score, setScore] = useState(0)
const [eliminatedLines, setEliminatedLines] = useState(0)
const [gameOver, setGameOver] = useState(true)
const mainRef = useRef()

Expand Down Expand Up @@ -55,12 +57,20 @@ export default function () {
setCurrentTetromino(rotateTetromino(currentTetromino, tetrominoes))
}

const onCollise = (elimiLines) => {
if (elimiLines > 0) {
setEliminatedLines(eliminatedLines + elimiLines)
setScore(score + 10 * Math.pow(2, elimiLines))
}
queueMicrotask(next)
}

const down = () => {
setCurrentTetromino(
moveDownTetromino(
currentTetromino,
tetrominoes,
() => queueMicrotask(next),
onCollise,
() => setGameOver(true),
setTetrominoes)
)
Expand All @@ -79,7 +89,7 @@ export default function () {
fallDownTetromino(
currentTetromino,
tetrominoes,
() => queueMicrotask(next),
onCollise,
() => setGameOver(true),
setTetrominoes)
}
Expand Down Expand Up @@ -117,7 +127,7 @@ export default function () {
}

return (
<Context.Provider value={{ currentTetromino, nextTetromino, tetrominoes, newGame, fallDown, rotate, down, left, right, next, gameOver, setGameOver }}>
<Context.Provider value={{ currentTetromino, nextTetromino, tetrominoes, newGame, fallDown, rotate, down, left, right, next, gameOver, setGameOver, score, eliminatedLines }}>
<main ref={mainRef} className='w-full h-screen focus:outline-none flex gap-8 items-center justify-center' tabIndex={1} onKeyDown={onKeyDown}>
<Operation />
<Board />
Expand Down

0 comments on commit c6a1244

Please sign in to comment.