当前位置: 首页 > news >正文

wordpress4.4长沙seo公司

wordpress4.4,长沙seo公司,建设工程施工合同通用条款,网站源码怎么做网站chatGPT写的网页版贪吃蛇小游戏 前言网页版贪吃蛇小游戏 前言 之前无聊,让ChatGPT写了一段基于html语言的贪吃蛇小游戏代码 网页版贪吃蛇小游戏 将以下内容复制到记事本,重命名为xxx.html即可打开浏览器游玩 这里是一个使用HTML、CSS和JavaScript编写…

chatGPT写的网页版贪吃蛇小游戏

  • 前言
  • 网页版贪吃蛇小游戏

前言

之前无聊,让ChatGPT写了一段基于html语言的贪吃蛇小游戏代码

网页版贪吃蛇小游戏

将以下内容复制到记事本,重命名为xxx.html即可打开浏览器游玩

这里是一个使用HTML、CSS和JavaScript编写的可以在浏览器中直接运行的贪吃蛇游戏代码:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Snake Game by OpenAI</title><style>canvas {display: block;margin: 0 auto;border: 1px solid black;}</style>
</head>
<body><canvas id="gameCanvas" width="400" height="400"></canvas><script>const canvas = document.getElementById('gameCanvas');const ctx = canvas.getContext('2d');const snakeSize = 10;let snake = [{x: 150, y: 150}, {x: 140, y: 150}, {x: 130, y: 150}, {x: 120, y: 150}];let food = {x: 0, y: 0};let dx = 10;let dy = 0;let changingDirection = false;function drawSnakePart(snakePart) {ctx.fillStyle = 'green';ctx.strokeStyle = 'black';ctx.fillRect(snakePart.x, snakePart.y, snakeSize, snakeSize);ctx.strokeRect(snakePart.x, snakePart.y, snakeSize, snakeSize);}function drawSnake() {snake.forEach(drawSnakePart);}function moveSnake() {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 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;}}function createFood() {food.x = Math.floor(Math.random() * canvas.width / 10) * 10;food.y = Math.floor(Math.random() * canvas.height / 10) * 10;snake.forEach(part => {if (part.x === food.x && part.y === food.y) {createFood();}});}function drawFood() {ctx.fillStyle = 'red';ctx.strokeStyle = 'black';ctx.fillRect(food.x, food.y, snakeSize, snakeSize);ctx.strokeRect(food.x, food.y, snakeSize, snakeSize);}function checkCollision() {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 - snakeSize;const hitTopWall = snake[0].y < 0;const hitBottomWall = snake[0].y > canvas.height - snakeSize;return hitLeftWall || hitRightWall || hitTopWall || hitBottomWall;}function gameLoop() {if (checkCollision()) {ctx.clearRect(0, 0, canvas.width, canvas.height);ctx.fillStyle = 'black';ctx.font = '30px Arial';ctx.fillText('Game Over!', 150, 200);return;}changingDirection = false;setTimeout(() => {ctx.clearRect(0, 0, canvas.width, canvas.height);drawFood();moveSnake();drawSnake();gameLoop();}, 100);}createFood();document.addEventListener('keydown', changeDirection);gameLoop();</script>
</body>
</html>

将上面的代码保存为一个.html文件,然后用浏览器打开这个文件,就可以在浏览器中直接运行这个贪吃蛇游戏了。按方向键控制蛇的移动,吃到食物后蛇会变长。玩得开心!

  • end
http://www.hkea.cn/news/531224/

相关文章:

  • 洛卡博网站谁做的seo权重查询
  • 东莞网络科技公司有哪些山东网站seo
  • 网站建设需要学什么网站模板购买
  • 用html做的游戏网站关键词推广效果分析
  • 做影视网站引流正规推广平台有哪些
  • 免费下载简历模板北京seo排名厂家
  • 西昌市做网站的百度搜索排名靠前
  • 办公室装修实景拍摄图重庆seo俱乐部联系方式
  • 网站建设阶段推广计划书怎么写
  • 代做毕业设计网站现成注册网站平台
  • 电商网站开发工作计划企业网络营销策划
  • 用wps网站栏目做树形结构图网页设计代码案例
  • 多媒体网站设计开发是指什么每日关键词搜索排行
  • 网站 seo正规网络公司关键词排名优化
  • 建立网站赚多少钱seo收录排名
  • 怎么做app网站seo学习网站
  • 广西建设职业技术学院官网免费的seo优化
  • 凡科网电脑版怎么做网站百度知道官网手机版
  • 贵卅省住房和城乡建设厅网站周口seo推广
  • 搭建flv视频网站seo工具查询
  • 企业展示网站 数据库设计模板自助建站
  • 房地产设计师上海seo网络优化
  • wordpress迁移打不开百度seo泛解析代发排名
  • 网站兼容性测试怎么做微信营销软件群发
  • wordpress如何设置内容页seo营销优化
  • 高端大气的网站制作南宁百度seo软件
  • 沙井营销型网站建设成人培训机构
  • 网站没有被百度收录搜索引擎排名优化公司
  • 手机网站转换小程序晋江怎么交换友情链接
  • 专业做网站的公司疫情放开最新消息今天