用javascript制作网页贪吃蛇游戏

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>贪吃蛇游戏</title>
  </head>
  <body>
    <canvas id="gameCanvas" width="400" height="400"></canvas>
    <script src="snake.js"></script>
  </body>
</html>

        
javascript
复制代码

// 定义画布和上下文
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

// 定义贪吃蛇的初始位置和大小
let snake = [{ x: 10, y: 10 }, { x: 9, y: 10 }];
let food = {};
let score = 0;
let speed = 100;

// 定义绘制贪吃蛇的函数
function drawSnake() {
  for (let i = 0; i < snake.length; i++) {
    ctx.fillStyle = "#00FF00"; // 贪吃蛇的身体颜色为绿色
    ctx.fillRect(snake[i].x * 10, snake[i].y * 10, 10, 10); // 每个方块的大小为10像素
  }
}

// 定义绘制食物的函数
function drawFood() {
  ctx.fillStyle = "#FF0000"; // 食物的颜色为红色
  ctx.fillRect(food.x * 10, food.y * 10, 10, 10); // 每个方块的大小为10像素
}

// 定义移动贪吃蛇的函数
function moveSnake() {
  let head = getHead(); // 获取贪吃蛇的头部位置
  let newHead = { x: head.x + dx, y: head.y + dy }; // 根据方向计算新的头部位置

  if (newHead.x < 0 || newHead.x >= canvas.width || newHead.y < 0 || newHead.y >= canvas.height) return; // 如果新的位置超出了画布范围,则返回

  if (newHead === food) food = generateFood(); // 如果新的位置是食物,则生成新的食物位置并更新食物状态
  else snake.pop(); // 如果新的位置不是食物,则删除最后一个身体块并将头部移动到新的位置上

  snake.unshift(newHead); // 将新的头部位置添加到贪吃蛇的前面,形成新的贪吃蛇身体块序列
}

// 定义生成随机食物位置的函数
function generateFood() {
  let x = Math.floor(Math.random() * canvas.width); // 在画布上随机生成一个位置作为食物位置
  let y = Math.floor(Math.random() * canvas.height); // 在画布上随机生成一个位置作为食物位置

  while (snake.some((segment) => segment.x === x && segment.y === y)) x = Math.floor(Math.random() * canvas.width); // 如果食物位置已经被贪吃蛇占据,则重新生成一个新的食物位置

    

// 定义画布和上下文
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");

// 定义贪吃蛇的初始位置和大小
let snake = [{ x: 10, y: 10 }, { x: 9, y: 10 }];
let food = {};
let score = 0;
let speed = 100;

// 定义绘制贪吃蛇的函数
function drawSnake() {
  for (let i = 0; i < snake.length; i++) {
    ctx.fillStyle = "#00FF00"; // 贪吃蛇的身体颜色为绿色
    ctx.fillRect(snake[i].x * 10, snake[i].y * 10, 10, 10); // 每个方块的大小为10像素
  }
}

// 定义绘制食物的函数
function drawFood() {
  ctx.fillStyle = "#FF0000"; // 食物的颜色为红色
  ctx.fillRect(food.x * 10, food.y * 10, 10, 10); // 每个方块的大小为10像素
}

// 定义移动贪吃蛇的函数
function moveSnake() {
  let head = getHead(); // 获取贪吃蛇的头部位置
  let newHead = { x: head.x + dx, y: head.y + dy }; // 根据方向计算新的头部位置

  if (newHead.x < 0 || newHead.x >= canvas.width || newHead.y < 0 || newHead.y >= canvas.height) return; // 如果新的位置超出了画布范围,则返回

  if (newHead === food) food = generateFood(); // 如果新的位置是食物,则生成新的食物位置并更新食物状态
  else snake.pop(); // 如果新的位置不是食物,则删除最后一个身体块并将头部移动到新的位置上

  snake.unshift(newHead); // 将新的头部位置添加到贪吃蛇的前面,形成新的贪吃蛇身体块序列
}

// 定义生成随机食物位置的函数
function generateFood() {
  let x = Math.floor(Math.random() * canvas.width); // 在画布上随机生成一个位置作为食物位置
  let y = Math.floor(Math.random() * canvas.height); // 在画布上随机生成一个位置作为食物位置

  while (snake.some((segment) => segment.x === x && segment.y === y)) x = Math.floor(Math.random() * canvas.width); // 如果食物位置已经被贪吃蛇占据,则重新生成一个新的食物位置

  food.x = x; // 将新的食物位置保存下来
  food.y = y;
}

//

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给你提供一个简单的贪吃蛇游戏代码,你可以在网页上运行它。 HTML 代码: ``` <!DOCTYPE html> <html> <head> <title>贪吃蛇游戏</title> <style> canvas { border: 1px solid #000; } </style> </head> <body> <h1>贪吃蛇游戏</h1> <canvas id="gameCanvas" width="400" height="400"></canvas> <script src="snake.js"></script> </body> </html> ``` JavaScript 代码: ```javascript // 获取 canvas 元素 var canvas = document.getElementById("gameCanvas"); var ctx = canvas.getContext("2d"); // 定义游戏参数 var blockSize = 10; // 方块大小 var widthInBlocks = canvas.width / blockSize; var heightInBlocks = canvas.height / blockSize; var score = 0; // 定义绘制方块的函数 var drawBlock = function(ctx, position) { var x = position[0] * blockSize; var y = position[1] * blockSize; ctx.fillRect(x, y, blockSize, blockSize); }; // 定义绘制分数的函数 var drawScore = function() { ctx.font = "20px Arial"; ctx.fillStyle = "Black"; ctx.textAlign = "left"; ctx.textBaseline = "top"; ctx.fillText("Score: " + score, blockSize, blockSize); }; // 定义清除画布的函数 var clearCanvas = function() { ctx.clearRect(0, 0, canvas.width, canvas.height); }; // 定义游戏结束的函数 var gameOver = function() { clearInterval(intervalId); ctx.font = "60px Arial"; ctx.fillStyle = "Black"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText("Game Over", canvas.width / 2, canvas.height / 2); }; // 定义方块的构造函数 var Block = function(col, row) { this.col = col; this.row = row; }; // 定义判断方块是否在画布内的函数 Block.prototype.insideCanvas = function() { return (this.col >= 0 && this.col < widthInBlocks && this.row >= 0 && this.row < heightInBlocks); }; // 定义判断方块是否在同一个位置的函数 Block.prototype.equal = function(otherBlock) { return (this.col === otherBlock.col && this.row === otherBlock.row); }; // 定义蛇的构造函数 var Snake = function() { this.segments = [ new Block(7, 5), new Block(6, 5), new Block(5, 5) ]; this.direction = "right"; this.nextDirection = "right"; }; // 定义绘制蛇的函数 Snake.prototype.draw = function() { for (var i = 0; i < this.segments.length; i++) { drawBlock(ctx, this.segments[i].position()); } }; // 定义移动蛇的函数 Snake.prototype.move = function() { var head = this.segments[0]; var newHead; this.direction = this.nextDirection; if (this.direction === "right") { newHead = new Block(head.col + 1, head.row); } else if (this.direction === "down") { newHead = new Block(head.col, head.row + 1); } else if (this.direction === "left") { newHead = new Block(head.col - 1, head.row); } else if (this.direction === "up") { newHead = new Block(head.col, head.row - 1); } if (this.checkCollision(newHead)) { gameOver(); return; } this.segments.unshift(newHead); if (newHead.equal(apple.position())) { score++; apple.move(); } else { this.segments.pop(); } }; // 定义检查蛇是否碰到边界或自己的函数 Snake.prototype.checkCollision = function(head) { var leftCollision = (head.col === 0); var topCollision = (head.row === 0); var rightCollision = (head.col === widthInBlocks - 1); var bottomCollision = (head.row === heightInBlocks - 1); var wallCollision = leftCollision || topCollision || rightCollision || bottomCollision; var selfCollision = false; for (var i = 0; i < this.segments.length; i++) { if (head.equal(this.segments[i])) { selfCollision = true; } } return wallCollision || selfCollision; }; // 定义改变蛇的方向的函数 Snake.prototype.setDirection = function(newDirection) { if (this.direction === "up" && newDirection === "down") { return; } else if (this.direction === "right" && newDirection === "left") { return; } else if (this.direction === "down" && newDirection === "up") { return; } else if (this.direction === "left" && newDirection === "right") { return; } this.nextDirection = newDirection; }; // 定义苹果的构造函数 var Apple = function() { this.position = function() { var col = Math.floor(Math.random() * (widthInBlocks - 2)) + 1; var row = Math.floor(Math.random() * (heightInBlocks - 2)) + 1; return new Block(col, row); }; this.move = function() { this.block = this.position(); }; this.block = this.position(); }; // 创建蛇和苹果对象 var snake = new Snake(); var apple = new Apple(); // 定义游戏循环 var intervalId = setInterval(function() { clearCanvas(); drawScore(); snake.move(); snake.draw(); apple.draw(); }, 100); // 定义键盘按下事件 var directions = { 37: "left", 38: "up", 39: "right", 40: "down" }; document.addEventListener("keydown", function(event) { var newDirection = directions[event.keyCode]; if (newDirection !== undefined) { snake.setDirection(newDirection); } }); ``` 这个代码实现了一个简单的贪吃蛇游戏,你可以在浏览器中打开 HTML 文件,就可以运行游戏了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值