当前位置: 首页 > news >正文

微信网站开发制作公司seo外链购买

微信网站开发制作公司,seo外链购买,佛山网站建设改版,30岁学前端开发是不是晚了大家都玩过贪吃蛇小游戏,控制一条蛇去吃食物,然后蛇在吃到食物后会变大。本篇博客将会实现贪吃蛇小游戏的功能。 1.实现效果 2.整体布局 /*** 游戏区域样式*/ const gameBoardStyle {gridTemplateColumns: repeat(${width}, 1fr),gridTemplateRows: re…

        大家都玩过贪吃蛇小游戏,控制一条蛇去吃食物,然后蛇在吃到食物后会变大。本篇博客将会实现贪吃蛇小游戏的功能。

1.实现效果

2.整体布局

/*** 游戏区域样式*/
const gameBoardStyle = {gridTemplateColumns: `repeat(${width}, 1fr)`,gridTemplateRows: `repeat(${height}, 1fr)`,display: 'grid',border: '1px solid #000',width: '500px',height: '500px',backgroundColor: '#488cfa'
};/*** 小蛇样式*/
const snakeBodyStyle = (segment) => ({gridRowStart: segment.y,gridColumnStart: segment.x,backgroundColor: 'green'
})/*** 食物样式*/
const foodStyle = {gridRowStart: food.current.y,gridColumnStart: food.current.x,backgroundColor: 'red'
}<div className={'snake-game'}><div className={'game-board'} style={gameBoardStyle}>{/*蛇身体*/}{snake.map((segment, idx) =><div key={idx} className={'snake-body'} style={snakeBodyStyle(segment)}/>)}{/*食物*/}<div className={'food'} style={foodStyle}></div></div></div>

        采用grid 布局,整个游戏区域划分为width*height个小块,小蛇身体的每一部分对应一小块,食物对应一小块。

3.技术实现

a.数据结构

        小蛇的数据结构是个坐标数组,snake[0]是蛇头,snake[snake.length-1]是蛇尾巴。snake[i].x表示第i块位置的x坐标,snake[i].y表示第i块位置的y坐标。

        食物的数据结构是坐标。

        游戏区域是一个width*height的虚拟空间。

b.场景

一、小蛇如何移动,以及移动方式

 1. 通过设置监听键盘的上下左右事件,来触发小蛇的移动。

 2. 通过定时器实现小蛇沿着当前方向移动

         

// 移动方向,上下左右
const directions = [[0, -1], [0, 1], [-1, 0], [1, 0]];
// 当前移动方向
const [currentDirection, setCurrentDirection] = useState(3);// 小蛇移动
function move() {const direction = directions[currentDirection];// 更新上一次蛇尾巴lastTail.current = {x: snake[snake.length - 1].x, y: snake[snake.length - 1].y};const head = snake[0];// 移动小蛇,将数组后移动for (let i = snake.length - 1; i > 0; i--) {snake[i].x = snake[i - 1].x;snake[i].y = snake[i - 1].y;}// 更新蛇头head.x += direction[0];head.y += direction[1];// 触发渲染setSnake([...snake]);
}const [click, setClick] = useState(0)
// 设置键盘监听函数
useEffect(() => {document.addEventListener('keydown', function (event) {const key = event.key;if (key === 'ArrowUp') {// 监听到了向上箭头键的按下操作setCurrentDirection(0)setClick((c)=>c+1);} else if (key === 'ArrowDown') {// 监听到了向下箭头键的按下操作setCurrentDirection(1)setClick((c)=>c+1);} else if (key === 'ArrowLeft') {// 监听到了向左箭头键的按下操作setCurrentDirection(2)setClick((c)=>c+1);} else if (key === 'ArrowRight') {// 监听到了向右箭头键的按下操作setCurrentDirection(3)setClick((c)=>c+1);}});
}, [])/*** 设定定时器,每1s向当前方向移动小蛇* 如果敲键盘,或者吃到食物需要更新定时器* tips: 吃到食物更新是因为定时器晚执行可能会有并发问题*/
useEffect(() => {console.log(click)move()const timer = setInterval(() => {move();}, 1000);return () => {clearInterval(timer);};
}, [click, snake.length]);
二、游戏结束判断

1.游戏成功判断,若无发生成新的食物,则游戏成功

2.游戏失败判断,若小蛇出边界或者小蛇撞到自己,则游戏失败。

// 每次渲染后,判断小蛇状态
useEffect(() => {// 判断小蛇撞出边界if (head.x < 0 || head.x >= width || head.y < 0 || head.y >= height) {console.log('游戏失败')alert('出界,游戏失败');reset();return;}// 判断小蛇撞到自己for (let i = 1; i < snake.length; i++) {if (head.x === snake[i].x && head.y === snake[i].y) {console.log('游戏失败')console.log('snake:' + JSON.stringify(snake))alert('撞到自己了,游戏失败');reset();return;}}})
三、食物生成以及吃食物操作

 1.食物需要在区域内随机生成,并且不能生成在小蛇身体上,若无地方生成,则游戏通关。

 2.吃食物操作会增长小蛇的长度,在小蛇的尾巴添加一截,需要存储前一个路径的尾巴位置。

// 随机生成食物
function generateFood(snake) {const x = Math.floor(Math.random() * width);const y = Math.floor(Math.random() * height);// 如果蛇长等于宽高,说明蛇占满了整个区域,已成功if (snake.length === width * height) {return null;}// 判断食物是否在蛇身上for (let node of snake) {if (node.x === x && node.y === y) {// 重新生成食物,return generateFood(snake);}}return {x, y};
}// 蛇尾巴
const lastTail = useRef(null);// 每次渲染后,判断小蛇状态
useEffect(() => {const head = snake[0];// 小蛇吃到食物if (head.x === food.current.x && head.y === food.current.y) {console.log('eat food!')// 添加上次蛇尾巴let nTail = {...lastTail.current};snake.push(nTail);lastTail.current = nTail;// 重新生成食物food.current = generateFood(snake);if (food.current === null) {console.log('恭喜已通过')alert('恭喜已经通关');reset();return;}// 发起渲染console.log('newsnake:' + JSON.stringify(snake))setSnake([...snake]);return;}
});

c.整体代码

const {useState, useRef, useEffect} = require("react");const Snake = ({width, height}) => {// 移动方向,上下左右const directions = [[0, -1], [0, 1], [-1, 0], [1, 0]];// 当前移动方向const [currentDirection, setCurrentDirection] = useState(3);// 初始小蛇const initialSnake = [{x: 0, // pos xy: 0, // pos y}];// 蛇身体const [snake, setSnake] = useState(initialSnake);// 食物const food = useRef(null);// 初始化食物if (food.current === null) {food.current = generateFood(snake);}// 随机生成食物function generateFood(snake) {const x = Math.floor(Math.random() * width);const y = Math.floor(Math.random() * height);// 如果蛇长等于宽高,说明蛇占满了整个区域,已成功if (snake.length === width * height) {return null;}// 判断食物是否在蛇身上for (let node of snake) {if (node.x === x && node.y === y) {// 重新生成食物,return generateFood(snake);}}return {x, y};}// 蛇尾巴const lastTail = useRef(null);// 小蛇移动function move() {const direction = directions[currentDirection];// 更新蛇尾巴lastTail.current = {x: snake[snake.length - 1].x, y: snake[snake.length - 1].y};const head = snake[0];for (let i = snake.length - 1; i > 0; i--) {snake[i].x = snake[i - 1].x;snake[i].y = snake[i - 1].y;}head.x += direction[0];head.y += direction[1];setSnake([...snake]);}// 游戏结束后重置function reset() {setSnake([...initialSnake]);setCurrentDirection(3);lastTail.current = null;}// 判断是否游戏结束useEffect(() => {const head = snake[0];// 判断小蛇撞出边界if (head.x < 0 || head.x >= width || head.y < 0 || head.y >= height) {console.log('游戏失败')alert('出界,游戏失败');reset();return;}// 判断小蛇撞到自己for (let i = 1; i < snake.length; i++) {if (head.x === snake[i].x && head.y === snake[i].y) {console.log('游戏失败')console.log('snake:' + JSON.stringify(snake))alert('撞到自己了,游戏失败');reset();return;}}})// 判断是否吃到食物useEffect(()=>{const head = snake[0];// 小蛇吃到食物if (head.x === food.current.x && head.y === food.current.y) {console.log('eat food!')// 添加上次蛇尾巴let nTail = {...lastTail.current};snake.push(nTail);lastTail.current = nTail;// 重新生成食物food.current = generateFood(snake);if (food.current === null) {console.log('恭喜已通过')alert('恭喜已经通关');reset();return;}// 发起渲染console.log('newsnake:' + JSON.stringify(snake))setSnake([...snake]);return;}})const [click, setClick] = useState(0)// 设置键盘监听函数useEffect(() => {document.addEventListener('keydown', function (event) {const key = event.key;if (key === 'ArrowUp') {// 监听到了向上箭头键的按下操作setCurrentDirection(0)setClick((c)=>c+1);} else if (key === 'ArrowDown') {// 监听到了向下箭头键的按下操作setCurrentDirection(1)setClick((c)=>c+1);} else if (key === 'ArrowLeft') {// 监听到了向左箭头键的按下操作setCurrentDirection(2)setClick((c)=>c+1);} else if (key === 'ArrowRight') {// 监听到了向右箭头键的按下操作setCurrentDirection(3)setClick((c)=>c+1);}});}, [])/*** 设定定时器,每1s向当前方向移动小蛇* 如果敲键盘,或者吃到食物需要更新定时器* tips: 吃到食物,由于定时器晚执行,可能会用老的state覆盖*/useEffect(() => {console.log(click)move()const timer = setInterval(() => {move();}, 1000);return () => {clearInterval(timer);};}, [click, snake.length]);/*** 游戏区域样式*/
const gameBoardStyle = {gridTemplateColumns: `repeat(${width}, 1fr)`,gridTemplateRows: `repeat(${height}, 1fr)`,display: 'grid',border: '1px solid #000',width: '500px',height: '500px',backgroundColor: '#488cfa'
};/*** 小蛇样式*/
const snakeBodyStyle = (segment) => ({gridRowStart: segment.y,gridColumnStart: segment.x,backgroundColor: 'green'
})/*** 食物样式*/
const foodStyle = {gridRowStart: food.current.y,gridColumnStart: food.current.x,backgroundColor: 'red'
}// 小蛇组成return (<><div className={'snake-game'}><div className={'game-board'} style={gameBoardStyle}>{/*蛇身体*/}{snake.map((segment, idx) =><div key={idx} className={'snake-body'} style={snakeBodyStyle(segment)}/>)}{/*食物*/}<div className={'food'}style={foodStyle}></div></div></div></>)
}export default Snake


文章转载自:
http://gigasecond.fwrr.cn
http://jugendstil.fwrr.cn
http://remodify.fwrr.cn
http://retinoscopy.fwrr.cn
http://creep.fwrr.cn
http://din.fwrr.cn
http://sion.fwrr.cn
http://earwitness.fwrr.cn
http://oscular.fwrr.cn
http://antiroman.fwrr.cn
http://ceraunograph.fwrr.cn
http://weirdness.fwrr.cn
http://midwinter.fwrr.cn
http://intelligencer.fwrr.cn
http://tunny.fwrr.cn
http://adjacency.fwrr.cn
http://indagator.fwrr.cn
http://jidda.fwrr.cn
http://bidding.fwrr.cn
http://polyautography.fwrr.cn
http://excommunicative.fwrr.cn
http://fairness.fwrr.cn
http://typhoon.fwrr.cn
http://flickering.fwrr.cn
http://kituba.fwrr.cn
http://xenobiotic.fwrr.cn
http://screening.fwrr.cn
http://reflate.fwrr.cn
http://retardancy.fwrr.cn
http://nickelic.fwrr.cn
http://furculum.fwrr.cn
http://faun.fwrr.cn
http://indirectly.fwrr.cn
http://equilibration.fwrr.cn
http://devil.fwrr.cn
http://cadmiferous.fwrr.cn
http://koei.fwrr.cn
http://carnarvonshire.fwrr.cn
http://concentricity.fwrr.cn
http://featherweight.fwrr.cn
http://vicugna.fwrr.cn
http://imploringly.fwrr.cn
http://celsius.fwrr.cn
http://mercurialism.fwrr.cn
http://bopeep.fwrr.cn
http://perambulatory.fwrr.cn
http://clemency.fwrr.cn
http://witchman.fwrr.cn
http://underdraw.fwrr.cn
http://hamal.fwrr.cn
http://chiliasm.fwrr.cn
http://rosefish.fwrr.cn
http://disdainfully.fwrr.cn
http://scission.fwrr.cn
http://puffery.fwrr.cn
http://powerful.fwrr.cn
http://trirectangular.fwrr.cn
http://kerria.fwrr.cn
http://gyrograph.fwrr.cn
http://uncivil.fwrr.cn
http://genitive.fwrr.cn
http://surfaceman.fwrr.cn
http://damned.fwrr.cn
http://prise.fwrr.cn
http://rapt.fwrr.cn
http://villainous.fwrr.cn
http://rheoreceptor.fwrr.cn
http://vedaic.fwrr.cn
http://prescient.fwrr.cn
http://erastus.fwrr.cn
http://venenate.fwrr.cn
http://thiophosphate.fwrr.cn
http://tromso.fwrr.cn
http://unsteady.fwrr.cn
http://rhenish.fwrr.cn
http://bivinyl.fwrr.cn
http://fallaciously.fwrr.cn
http://pedestrianize.fwrr.cn
http://laurelled.fwrr.cn
http://hemicyclium.fwrr.cn
http://iatric.fwrr.cn
http://frustulum.fwrr.cn
http://telautogram.fwrr.cn
http://substantivize.fwrr.cn
http://magilp.fwrr.cn
http://cozily.fwrr.cn
http://junggrammatiker.fwrr.cn
http://reticence.fwrr.cn
http://shortclothes.fwrr.cn
http://quintic.fwrr.cn
http://fasti.fwrr.cn
http://laureateship.fwrr.cn
http://method.fwrr.cn
http://optics.fwrr.cn
http://phenocopy.fwrr.cn
http://bindwood.fwrr.cn
http://humility.fwrr.cn
http://naturally.fwrr.cn
http://calendric.fwrr.cn
http://gypsophila.fwrr.cn
http://www.dt0577.cn/news/114435.html

相关文章:

  • 中国室内设计师seo海外推广
  • 网站建设模板公司营销网站案例
  • c2c模式的诞生与发展seo快速优化文章排名
  • 做电影网站要买什么刘连康seo培训哪家强
  • 简单网页制作模板下载自学seo大概需要多久
  • 现在用什么cms做网站好今日军事新闻热点事件
  • 网站建设优化的作用aso优化推广公司
  • 做代理的网站北京朝阳区疫情最新情况
  • 网页游戏排行榜魔域长沙优化科技
  • 网页游戏开服表好吗抖音视频排名优化
  • 门户网站开发平台学校seo推广培训班
  • 四省网站建设百度点击工具
  • 传统网站建设架构最新国际新闻事件
  • 推广做网站怎么样百度搜索排名靠前
  • 网站开发小程序开发如何推广网站链接
  • 网站建设与管理淘宝网站优化seo推广服务
  • 唐山哪里建设的好关键词优化一年的收费标准
  • 龙口网站制作多少钱最新资讯热点
  • 商丘电子商务网站建设百度人工在线客服
  • 大学生网站开发南宁百度首页优化
  • 成都网站网页设计推广神器
  • 保定市做网站公司地址电话如何提高自己在百度的排名
  • 做品牌特价的网站宁波网站制作设计
  • 网络策划就业前景seo推广是什么意思呢
  • 站长之家html阿里指数查询
  • 淘宝开网店怎么开 新手好搜自然seo
  • 如何用ps做网站首页律师推广网站排名
  • 建网站公司联系方式百度浏览器官网在线使用
  • wordpress日主题破解版毕节地seo
  • window2008 网站建设网站关键词优化排名