Skip to content

Instantly share code, notes, and snippets.

@roshnet
Created October 30, 2021 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roshnet/86475b81b1688cf254c2c0ed4d97e6b3 to your computer and use it in GitHub Desktop.
Save roshnet/86475b81b1688cf254c2c0ed4d97e6b3 to your computer and use it in GitHub Desktop.
I was bored...
import { useLayoutEffect, useState } from 'react'
const SECONDS = 0.6
export default function Game() {
const [x, setX] = useState(0)
const [y, setY] = useState(0)
const [score, setScore] = useState(0)
// Change coordinates every N seconds
useLayoutEffect(() => {
const timer = setInterval(() => {
setX(Math.floor(Math.random() * 500))
setY(Math.floor(Math.random() * 500))
}, SECONDS * 1000)
return () => {
clearInterval(timer)
}
}, [])
return (
<>
<div style={{ textAlign: 'center' }}>
<h1>{score} points</h1>
</div>
<div
id="root"
style={{
height: '100vh',
width: '100vw',
backgroundColor: '#000',
}}
>
<div
style={{
position: 'absolute',
height: 40,
width: 40,
backgroundColor: 'red',
left: x,
top: y,
}}
onClick={() => setScore(score + 1)}
></div>
</div>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment