贪吃蛇小游戏基本简单布局

代码:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Layui贪吃蛇小游戏</title>

<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/css/layui.css">

</head>

<body>

<div class="layui-container">

<h1 class="layui-header">Layui贪吃蛇小游戏</h1>

<div class="layui-row">

<div class="layui-col-md8">

<canvas id="gameCanvas" width="400" height="400"></canvas>

</div>

<div class="layui-col-md4">

<div class="layui-card">

<div class="layui-card-header">游戏控制</div>

<div class="layui-card-body">

<button id="startBtn" class="layui-btn">开始游戏</button>

<button id="pauseBtn" class="layui-btn">暂停游戏</button>

<button id="resetBtn" class="layui-btn">重置游戏</button>

</div>

</div>

</div>

</div>

</div>

<script src="https://cdn.bootcdn.net/ajax/libs/layui/2.5.7/layui.js"></script>

<script>

layui.use(['layer', 'form'], function(){

var layer = layui.layer;

var form = layui.form;

// 游戏逻辑

var canvas = document.getElementById('gameCanvas');

var ctx = canvas.getContext('2d');

var snakeSize = 10;

var snake = [

{x: 200, y: 200},

{x: 190, y: 200},

{x: 180, y: 200},

{x: 170, y: 200},

{x: 160, y: 200}

];

var food = {x: 0, y: 0};

var dx = 10;

var dy = 0;

function drawSnakePart(snakePart) {

ctx.fillStyle = 'lightgreen';

ctx.strokeStyle = 'darkgreen';

ctx.fillRect(snakePart.x, snakePart.y, snakeSize, snakeSize);

ctx.strokeRect(snakePart.x, snakePart.y, snakeSize, snakeSize);

}

function drawSnake() {

snake.forEach(drawSnakePart);

}

function advanceSnake() {

const head = {x: snake[0].x + dx, y: snake[0].y + dy};

snake.unshift(head);

const didEatFood = snake[0].x === food.x && snake[0].y === food.y;

if (didEatFood) {

createFood();

} else {

snake.pop();

}

}

function clearCanvas() {

ctx.fillStyle = 'white';

ctx.strokeStyle = 'black';

ctx.fillRect(0, 0, canvas.width, canvas.height);

ctx.strokeRect(0, 0, canvas.width, canvas.height);

}

function createFood() {

food.x = Math.floor(Math.random() * canvas.width / 10) * 10;

food.y = Math.floor(Math.random() * canvas.height / 10) * 10;

snake.forEach(function isFoodOnSnake(part) {

const foodIsOnSnake = part.x === food.x && part.y === food.y;

if (foodIsOnSnake) {

createFood();

}

});

}

function drawFood() {

ctx.fillStyle = 'red';

ctx.strokeStyle = 'darkred';

ctx.fillRect(food.x, food.y, snakeSize, snakeSize);

ctx.strokeRect(food.x, food.y, snakeSize, snakeSize);

}

function main() {

if (didGameEnd()) return;

setTimeout(function onTick() {

changingDirection = false;

clearCanvas();

drawFood();

advanceSnake();

drawSnake();

main();

}, 100)

}

function didGameEnd() {

for (let i = 4; i < snake.length; i++) {

if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) return true;

}

const hitLeftWall = snake[0].x < 0;

const hitRightWall = snake[0].x > canvas.width - 10;

const hitTopWall = snake[0].y < 0;

const hitBottomWall = snake[0].y > canvas.height - 10;

return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;

}

function resetGame() {

snake = [

{x: 200, y: 200},

{x: 190, y: 200},

{x: 180, y: 200},

{x: 170, y: 200},

{x: 160, y: 200}

];

dx = 10;

dy = 0;

createFood();

}

createFood();

main();

document.addEventListener('keydown', changeDirection);

function changeDirection(event) {

const LEFT_KEY = 37;

const RIGHT_KEY = 39;

const UP_KEY = 38;

const DOWN_KEY = 40;

if (changingDirection) return;

changingDirection = true;

const keyPressed = event.keyCode;

const goingUp = dy === -10;

const goingDown = dy === 10;

const goingRight = dx === 10;

const goingLeft = dx === -10;

if (keyPressed === LEFT_KEY && !goingRight) {

dx = -10;

dy = 0;

}

if (keyPressed === UP_KEY && !goingDown) {

dx = 0;

dy = -10;

}

if (keyPressed === RIGHT_KEY && !goingLeft) {

dx = 10;

dy = 0;

}

if (keyPressed === DOWN_KEY && !goingUp) {

dx = 0;

dy = 10;

}

}

// 游戏控制

var startBtn = document.getElementById('startBtn');

var pauseBtn = document.getElementById('pauseBtn');

var resetBtn = document.getElementById('resetBtn');

startBtn.onclick = function() {

main();

}

pauseBtn.onclick = function() {

clearTimeout(main);

}

resetBtn.onclick = function() {

resetGame();

}

});

</script>

</body>

</html>

结果图:

相关推荐
耶啵奶膘1 小时前
uniapp-是否删除
linux·前端·uni-app
王哈哈^_^3 小时前
【数据集】【YOLO】【目标检测】交通事故识别数据集 8939 张,YOLO道路事故目标检测实战训练教程!
前端·人工智能·深度学习·yolo·目标检测·计算机视觉·pyqt
cs_dn_Jie3 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic4 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿4 小时前
webWorker基本用法
前端·javascript·vue.js
cy玩具4 小时前
点击评论详情,跳到评论页面,携带对象参数写法:
前端
清灵xmf5 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据5 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161775 小时前
防抖函数--应用场景及示例
前端·javascript
334554326 小时前
element动态表头合并表格
开发语言·javascript·ecmascript