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

代码:

<!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>

结果图:

相关推荐
a栋栋栋2 小时前
apifox
java·前端·javascript
请叫我飞哥@2 小时前
HTML 标签页(Tabs)详细讲解
前端·html
Anlici3 小时前
React18与Vue3组件通信对比学习(详细!建议收藏!!🚀🚀)
前端·vue.js·react.js
m0_748251523 小时前
PDF在线预览实现:如何使用vue-pdf-embed实现前端PDF在线阅读
前端·vue.js·pdf
中生代技术3 小时前
3.从制定标准到持续监控:7个关键阶段提升App用户体验
大数据·运维·服务器·前端·ux
m0_748239333 小时前
从零开始:如何在.NET Core Web API中完美配置Swagger文档
前端·.netcore
m0_748232924 小时前
【前端】Node.js使用教程
前端·node.js·vim
hawleyHuo4 小时前
umi 能适配 taro组件?
前端·前端框架
web130933203984 小时前
[JAVA Web] 02_第二章 HTML&CSS
java·前端·html
黑客呀4 小时前
Go Web开发之Revel - 网页请求处理流程
开发语言·前端·web安全·golang·系统安全