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

乐平网站建设咨询前端网页开发

乐平网站建设咨询,前端网页开发,阳江城乡建设部网站首页,建设银行网站招聘官网实现效果 使用方向键移动&#xff0c;将橙色箱子推到绿色目标区域&#xff08;黑色块为墙&#xff0c;白色块为可通过区域&#xff0c;蓝球为小人&#xff09; 实现过程 <!DOCTYPE html> <html> <head><title>推箱子小游戏</title><style&g…

实现效果

使用方向键移动,将橙色箱子推到绿色目标区域(黑色块为墙,白色块为可通过区域,蓝球为小人)

实现过程

<!DOCTYPE html>
<html>
<head><title>推箱子小游戏</title><style>.game-container {display: grid;grid-template-columns: repeat(8, 50px);gap: 2px;background: #333;padding: 10px;}.cell {width: 50px;height: 50px;background: #eee;display: flex;align-items: center;justify-content: center;font-size: 24px;}.wall {background: #444;}.box {background: #ff9900;border-radius: 5px;}.player {background: #2196F3;border-radius: 50%;width: 35px;height: 35px;}.target {background: #4CAF50;}.box-on-target {background: #8BC34A;}</style>
</head>
<body><div class="game-container" id="game"></div><p>使用方向键移动,将橙色箱子推到绿色目标区域(黑色块为墙,白色块为可通过区域,蓝球为小人)</p><script>// 关卡配置const level = [[1, 1, 1, 1, 1, 1, 1, 1],[1, 0, 0, 0, 0, 0, 0, 1],[1, 0, 2, 3, 0, 2, 0, 1],[1, 0, 1, 3, 4, 1, 0, 1],[1, 0, 2, 0, 3, 0, 0, 1],[1, 0, 0, 0, 0, 0, 0, 1],[1, 1, 1, 1, 1, 1, 1, 1]];let playerPosition = { x: 4, y: 3 };let boxes = [];const gameContainer = document.getElementById('game');// 初始化游戏function initGame() {gameContainer.innerHTML = '';boxes = [];level.forEach((row, y) => {row.forEach((cell, x) => {const cellElement = document.createElement('div');cellElement.className = 'cell';switch(cell) {case 1: cellElement.classList.add('wall');break;case 2:cellElement.classList.add('target');break;case 3:boxes.push({ x, y });cellElement.classList.add('box');break;case 4:playerPosition = { x, y };break;}gameContainer.appendChild(cellElement);});});updatePlayerPosition();}// 更新玩家位置function updatePlayerPosition() {document.querySelectorAll('.cell').forEach(cell => {cell.classList.remove('player');});const index = playerPosition.y * 8 + playerPosition.x;gameContainer.children[index].classList.add('player');}// 移动检测function canMove(dx, dy) {const newX = playerPosition.x + dx;const newY = playerPosition.y + dy;// 边界检测if (newX < 0 || newX >= 8 || newY < 0 || newY >= 7) return false;// 获取目标位置的单元格const targetCell = level[newY][newX];const targetIndex = newY * 8 + newX;const targetElement = gameContainer.children[targetIndex];// 如果是墙if (targetCell === 1) return false;// 检查是否有箱子const boxIndex = boxes.findIndex(b => b.x === newX && b.y === newY);if (boxIndex > -1) {// 检查箱子后面的位置const boxNewX = newX + dx;const boxNewY = newY + dy;if (boxNewX < 0 || boxNewX >= 8 || boxNewY < 0 || boxNewY >= 7) return false;const nextCell = level[boxNewY][boxNewX];const nextIndex = boxNewY * 8 + boxNewX;const nextElement = gameContainer.children[nextIndex];// 检查箱子后面的位置是否可移动if (nextCell === 1 || boxes.some(b => b.x === boxNewX && b.y === boxNewY)) {return false;}// 移动箱子boxes[boxIndex].x = boxNewX;boxes[boxIndex].y = boxNewY;// 更新箱子显示targetElement.classList.remove('box');nextElement.classList.toggle('box', true);nextElement.classList.toggle('box-on-target', nextElement.classList.contains('target'));}return true;}// 处理键盘事件document.addEventListener('keydown', (e) => {let dx = 0, dy = 0;switch(e.key) {case 'ArrowUp': dy = -1; break;case 'ArrowDown': dy = 1; break;case 'ArrowLeft': dx = -1; break;case 'ArrowRight': dx = 1; break;default: return;}if (canMove(dx, dy)) {playerPosition.x += dx;playerPosition.y += dy;updatePlayerPosition();checkWin();}});// 胜利检测function checkWin() {const allBoxesOnTarget = boxes.every(box => {const index = box.y * 8 + box.x;return gameContainer.children[index].classList.contains('target');});if (allBoxesOnTarget) {setTimeout(() => alert('恭喜过关!'), 100);}}// 启动游戏initGame();</script>
</body>
</html>

http://www.dtcms.com/wzjs/567694.html

相关文章:

  • 网站名称没有排名国外网站制作
  • 做网站是怎样赚钱搜索引擎推广方法
  • 聊城汽车网站建设公司做网站的费用入账
  • 网站加上视频对seo影响网站建设 收费
  • 电商网站的宣传推广免费建网站家谱系统
  • 企业网站营销典型案例网站建设大概需要多少钱
  • 莱芜手机网站建设报价qq网页版登录入口手机版
  • 南京网站推广营销公司哪家好郑州制作网站的基本流程
  • 建设公司网站模板怎么用ps做简单网站
  • 陕西网站开发公司地址在线网站建设系统
  • 全网网站建设设计上海展览
  • 网站建设流程郑州网站制作十年乐云seo
  • php教育学校网站源码如何创建微信小程序
  • 莱芜网站优化费用互联网舆情研究中心
  • 国外网站建设现状wordpress vul
  • 网站开发 运行及维护苏州住房建设建局官方网站
  • 济南便宜网站设计建设网站注意哪几点
  • dw 做网站的思路适合翻译做兼职的网站
  • 营销型网站开发方案无锡网站服务公司
  • 绍兴网站定制公司儿童网站html模板
  • 提供网站建设报龙岩房地产信息网
  • 浏览器怎么连接网站的wordpress 在线手册
  • 做宠物食品的网站企业免费网站推广公司
  • 网站建设中网站需求分析网页设计html期末考试
  • 品牌排名网站做棋牌网站
  • 株洲新站建设简易h5制作开发
  • 设计网站账号网站的运营与管理
  • 爱站数据官网福建网站开发定制
  • 手机网站商城建设答辩怎样辨别自己网站的好坏
  • 出售域名的网站手机中国第一专业门户