10秒钟,chatgpt帮你生成简单贪吃蛇游戏

编程知识 行业动态 更新时间:2024-06-13 00:20:35

场景:

制作一个简单html贪吃蛇游戏


方法


 

<!DOCTYPE html>
<html>
<head>
	<title>贪吃蛇</title>
	<style>
		body {
			margin: 0;
			padding: 0;
		}
		canvas {
			border: 1px solid black;
		}
	</style>
</head>
<body>
	<canvas id="canvas" width="400" height="400"></canvas>
	<script>
		// 获取画布
		var canvas = document.getElementById("canvas");
		var ctx = canvas.getContext("2d");

		// 定义变量
		var snake = [];
		var snakeLength = 5;
		var snakeSize = 20;
		var food = {};
		var score = 0;
		var direction = "right";

		// 初始化
		function init() {
			createSnake();
			createFood();
			drawSnake();
			drawFood();
			document.addEventListener("keydown", changeDirection);
			setInterval(gameLoop, 100);
		}

		// 创建蛇
		function createSnake() {
			for (var i = snakeLength - 1; i >= 0; i--) {
				snake.push({ x: i, y: 0 });
			}
		}

		// 创建食物
		function createFood() {
			food.x = Math.floor(Math.random() * (canvas.width / snakeSize));
			food.y = Math.floor(Math.random() * (canvas.height / snakeSize));
		}

		// 绘制蛇
		function drawSnake() {
			for (var i = 0; i < snake.length; i++) {
				ctx.fillStyle = "green";
				ctx.fillRect(snake[i].x * snakeSize, snake[i].y * snakeSize, snakeSize, snakeSize);
			}
		}

		// 绘制食物
		function drawFood() {
			ctx.fillStyle = "red";
			ctx.fillRect(food.x * snakeSize, food.y * snakeSize, snakeSize, snakeSize);
		}

		// 改变方向
		function changeDirection(event) {
			var key = event.keyCode;
			if (key == 37 && direction != "right") {
				direction = "left";
			} else if (key == 38 && direction != "down") {
				direction = "up";
			} else if (key == 39 && direction != "left") {
				direction = "right";
			} else if (key == 40 && direction != "up") {
				direction = "down";
			}
		}

		// 游戏循环
		function gameLoop() {
			ctx.clearRect(0, 0, canvas.width, canvas.height);
			drawSnake();
			drawFood();

			// 移动蛇
			var headX = snake[0].x;
			var headY = snake[0].y;
			if (direction == "right") {
				headX++;
			} else if (direction == "left") {
				headX--;
			} else if (direction == "up") {
				headY--;
			} else if (direction == "down") {
				headY++;
			}
			var tail = snake.pop();
			tail.x = headX;
			tail.y = headY;
			snake.unshift(tail);

			// 检测碰撞
			if (headX == food.x && headY == food.y) {
				snake.push({ x: 0, y: 0 });
				score++;
				createFood();
			}
			if (headX < 0 || headX >= canvas.width / snakeSize || headY < 0 || headY >= canvas.height / snakeSize) {
				alert("游戏结束,得分:" + score);
				window.location.reload();
			}
			for (var i = 1; i < snake.length; i++) {
				if (headX == snake[i].x && headY == snake[i].y) {
					alert("游戏结束,得分:" + score);
					window.location.reload();
				}
			}
		}

		// 开始游戏
		init();
	</script>
</body>
</html>

保存为html文件,直接打开


更多推荐

10秒钟,chatgpt帮你生成简单贪吃蛇游戏

本文发布于:2023-03-30 04:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/72995bed5df123dec1e29d0cca564120.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帮你   贪吃蛇   简单   游戏   chatgpt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!