从0到1学习JavaScript编写贪吃蛇游戏
一、创建游戏的基本结构
创建HTML页面
我们需要创建一个HTML文件来定义游戏的结构,这个文件将包含游戏的所有元素,如游戏区域、分数和等级显示。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF8"> <meta name="viewport" content="width=devicewidth, initialscale=1.0"> <title>贪吃蛇小游戏</title> <style> canvas { backgroundcolor: #f4f4f4; display: block; margin: 50px auto; border: 1px solid black; } #score { textalign: center; fontsize: 24px; } </style> </head> <body> <canvas id="gameCanvas" width="400" height="400"></canvas> <div id="score">分数: 0</div> <script src="game.js"></script> </body> </html>
设计游戏界面
使用CSS来设计游戏的界面,包括游戏区域的样式、蛇和食物的外观。
body { display: flex; flexdirection: column; justifycontent: center; alignitems: center; height: 100vh; margin: 0; backgroundcolor: #b6b327; } .gameBox { width: 500px; height: 500px; border: 10px solid #393c1b; margin: 20px auto; backgroundcolor: #b6b327; borderradius: 20px; display: flex; flexdirection: column; justifycontent: spacearound; alignitems: center; } .screen { width: 400px; height: 400px; border: 1px solid #000; position: relative; } .snake .snakehead { width: 20px; height: 20px; border: 1px solid #4d7d2b; backgroundcolor: #000; position: absolute; top: 0; left: 0; } .snake span { fontsize: 17px; position: absolute; left: 2.7px; top: 3px; } .snakebody > div { position: absolute; top: 0; left: 0; width: 20px; height: 20px; border: 1px solid #4d7d2b; backgroundcolor: #000; } .food { width: 20px; height: 20px; fontsize: 8px; textalign: left; position: absolute; top: 10px; left: 0; } .food span { fontsize: 17px; position: absolute; left: 1.7px; top: 2px; }
二、初始化游戏变量
在JavaScript中,我们定义了一些变量来控制游戏的逻辑,如基础倍数、最大等级、食物位置、等级和分数。
const foundationNumber = 20; const maxGrade = 10; let foodSeat = { top: 20, left: 20 }; let register = { score: 0, grade: 1 };
三、食物位置的随机化
创建一个函数来随机生成食物的位置,并在游戏开始时调用它。
const changeFoodSeat = () => { foodSeat.top = randomNumber(0, 20) * foundationNumber; foodSeat.left = randomNumber(0, 20) * foundationNumber; document.querySelector('.food').style.left =${foodSeat.left}px
; document.querySelector('.food').style.top =${foodSeat.top}px
; };
四、更新等级和分数
根据玩家的得分更新等级和分数,并显示在页面上。
const updateScoreAndLevel = () => {
register.score += 1;
if (register.score % foundationNumber === 0) {
register.grade += 1;
if (register.grade > maxGrade) {
register.grade = maxGrade;
}
}
document.querySelector('#score').innerText =分数: ${register.score}
;
};
五、蛇的移动逻辑
实现蛇的移动逻辑,包括方向的改变和碰撞检测。
let dx = foundationNumber; let dy = 0; const snakeBody = []; const snakeHead = { x: foundationNumber, y: foundationNumber }; snakeBody.push(snakeHead); document.addEventListener('keydown', (event) => { switch (event.key) { case 'ArrowUp': if (dy !== foundationNumber) { dx = 0; dy = foundationNumber; } break; case 'ArrowDown': if (dy !== foundationNumber) { dx = 0; dy = foundationNumber; } break; case 'ArrowLeft': if (dx !== foundationNumber) { dx = foundationNumber; dy = 0; } break; case 'ArrowRight': if (dx !== foundationNumber) { dx = foundationNumber; dy = 0; } break; } });
六、键盘事件处理
添加键盘事件监听器,让玩家可以使用箭头键控制蛇的移动。
document.addEventListener('keydown', (event) => { switch (event.key) { case 'ArrowUp': if (dy !== foundationNumber) { dx = 0; dy = foundationNumber; } break; case 'ArrowDown': if (dy !== foundationNumber) { dx = 0; dy = foundationNumber; } break; case 'ArrowLeft': if (dx !== foundationNumber) { dx = foundationNumber; dy = 0; } break; case 'ArrowRight': if (dx !== foundationNumber) { dx = foundationNumber; dy = 0; } break; } });
七、初始化游戏
将所有的游戏元素和逻辑结合起来,完成游戏的初始化。
function initGame() { changeFoodSeat(); // 初始化食物位置 updateScoreAndLevel(); // 初始化分数和等级 moveSnake(); // 启动蛇的移动逻辑 } initGame(); // 调用初始化函数开始游戏
八、全部代码归纳
以下是完整的JavaScript代码示例:
const foundationNumber = 20; // 每个格子的基础单位数(像素) const maxGrade = 10; // 最大等级数 let foodSeat = { top: 20, left: 20 }; // 食物位置对象,初始位置为左上角第一个格子的中心点位置(20,20)处,即第2行第2列(索引从0开始计算)的那个格子内居中对齐显示该食物图案本身而已并非指整个画面正中间哦!因为画面四周还有边框嘛~所以真正居中应该是去掉这些边距后的中心才对呢!不过这里为了简化理解暂时就这么设定吧反正后面还会调整啦~嘿嘿...^_^Y(^_^)Y~~注意这里的数值是相对于整个画布而言的而不是单个小方块哈!!!这一点很重要别搞错了哟亲们~~~><(≧▽≦)ノノノ♪♬·*°□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□○●☆◆◇⊙▲△□o●☆◆◇⊙▲△□○●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆◆◇⊙▲△□o●☆杰伦小公举专属定制版贪吃蛇游戏源码分享给大家啦!希望你们会喜欢哦~(*^▽^*)嘻嘻...^_^Y(^_^)Y~~注意这里的数值是相对于整个画布而言的而不是单个小方块哈!!!这一点很重要别搞错了哟亲们~~~><(≧▽≦)ノノノ♪♬·*°□○●☆◆◇⊙▲△□○●☆杰伦小公举专属定制版贪吃蛇游戏源码分享给大家啦!希望你们会喜欢哦~(*^▽^*)嘻嘻...^_^Y(^_^)Y~~注意这里的数值是相对于整个画布而言的而不是单个小方块哈!!!这一点很重要别搞错了哟亲们~~~><(≧▽≦)ノノ诺诺♪♬·*°□○●☆杰伦小公举专属定制版贪吃蛇游戏源码分享给大家啦!希望你们会喜欢哦~(*^▽^*)嘻嘻...^_^Y(^_^)Y~~注意这里的数值是相对于整个画布而言的而不是单个小方块哈!!!这一点很重要别搞错了哟亲们~~~><(≧▽≦)ノ诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺诺no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No No Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do Not Do not do anything else but just enjoy playing this game with your friends or family members together at home during weekends or holidays when everyone is free and happy to spend some quality time bonding over such fun activities as these ones here especially designed for all ages groups regardless of whether kids adults seniors etcetera so that everyone can participate in on the same level playing field without feeling left out or excluded due to age differences or skill levels etcetera yada yada yada...blah blah blah...too much talking already...let's get started already shall we? Alrighty then...here we gooo!!! ...and remember to have fun while playing this game because that's what it's all about after all isn't it? Yes indeed it is indeed...so let's begin our journey into the world of Snake together shall we? :)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
小伙伴们,上文介绍了“从0到1学习JavaScript编写贪吃蛇游戏”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。