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

20260508(2)

重写一下那个联机射击的网页小游戏吧,这次逻辑和运算都要写在后端。
今晚只完成了一小部分后端,前端还没开始写:

app.js

const express = require('express');
const WebSocket = require('ws');
const cors = require('cors');
const app = express();app.use(express.json());
app.use(cors());const server = app.listen(5000, () => {console.log('服务器运行在 http://localhost:5000');
});const wss = new WebSocket.Server({server});let players = []
let bullets = []let config = {FPS: 1000 / 60,shootGap: 1000,
}wss.on('connection', ws => {console.log('有玩家连接服务器');ws.on('message', message => {const msg = JSON.parse(message);const type = msg.type;const data = msg.data;const tsp = Date.now();let index;switch (type) {//玩家加入case 'pJoin':players.push(new Player(data))console.log(data.name + " 加入游戏")break;//玩家移动case 'pMove':index = players.findIndex(player => player.id === data.id)if (index > -1) {players[index].up = data.up;players[index].down = data.down;players[index].left = data.left;players[index].right = data.right;}break;//玩家射击case 'pShoot':index = players.findIndex(player => player.id === data.id)if ((index > -1) && (tsp - players[index].latestShoot > config.shootGap)) {//新建子弹bullets.push(new Bullet({id: bullets.length,from: players[index].name,color: players[index].color,at: players[index].at,x: players[index].x,y: players[index].y,w: 10,h: 10,a: 0,v: 5,angle: players[index].angle}));}break;//玩家死亡case 'pDelete':index = players.findIndex(player => player.id === data.id)if (index > -1) {players.splice(index, 1)console.log(data.name + " 离开游戏")}break;}})
})
//广播定时器
setInterval(() => {players.forEach(player => {//更新玩家位置player.playerMove();})bullets.forEach(bullet => {//更新子弹位置bullet.bulletMove();})//碰撞检测//广播位置}, config.FPS)//玩家类
class Player {constructor({name,id,color,maxHp,hp,at,latestShoot,latestHurt,x,y,w,h,a,v,up,down,left,right,angle}) {this.name = name;this.id = id;this.color = color;this.maxHp = maxHp;this.hp = hp;this.at = at;this.latestShoot = latestShoot;this.latestHurt = latestHurt;this.x = x;this.y = y;this.w = w;this.h = h;this.a = a;this.v = v;this.up = up;this.down = down;this.left = left;this.right = right;this.angle = angle;}playerMove() {if (this.up) {this.y -= this.v;}if (this.down) {this.y += this.v;}if (this.left) {this.x -= this.v;}if (this.right) {this.x += this.v;}}
}//子弹类
class Bullet {constructor({id, from, color, at, x, y, w, h, a, v, angle}) {this.id = id;this.from = from;this.color = color;this.at = at;this.x = x;this.y = y;this.w = w;this.h = h;this.a = a;this.v = v;this.angle = angle;}bulletMove() {const rad = (this.angle * Math.PI) / 180;this.x += Math.cos(rad) * this.v;this.y += Math.sin(rad) * this.v;}
}
http://www.jsqmd.com/news/779600/

相关文章:

  • 3DThinker:几何直觉与视觉语言模型的融合创新
  • ArmForge并行程序Profile工具
  • Youtu-VL:统一自回归框架的视觉语言模型解析
  • 前端实战:从设计稿到高性能网页的全链路开发指南
  • 如何用AI生成Logo?我对比了7个AI Logo生成器,简单、高效、专业 - 企业数字化观察家
  • 自建LinkVault:打造私有化链接管理系统的技术架构与部署实践
  • Skill 学习篇(九)| 编排框架 · OpenSpec 专篇(1→10 阶段)
  • V-Bridge:视频生成先验驱动的少样本图像修复技术
  • 对比直接使用官方API通过聚合平台管理成本的优势体验
  • QOwnNotes:基于Markdown文件与脚本的本地知识管理方案解析
  • Awesome MCP Hub:AI应用开发者的MCP服务器资源导航与实战指南
  • Mac/Windows系统下Jupyter Notebook报500错误的终极排查指南(附conda环境解决方案)
  • Matsumiko/runbook:代码化运维手册,实现故障处理自动化与知识沉淀
  • 从图像到数据:如何用WebPlotDigitizer解锁科研图表中的隐藏信息宝库
  • 【LSF集群搭建】7-为集群打补丁
  • iGRPO:基于自反馈机制的大语言模型推理优化方法
  • 别再被AUTOSAR通信协议栈搞懵了!手把手教你从DBC导入到无错配置(CAN/CANIF/PDUR/COM全流程)
  • Robert Griesemer 亲述:只解决 90% 问题的“箭头函数”该长什么样?
  • 2026 年教育培训行业 GEO 服务商排行榜,五大实力机构深度盘点 - GEO优化
  • iGRPO框架:大语言模型推理效率的动态优化方案
  • V-Bridge:基于视频先验的少样本图像修复技术
  • MCP协议应用商店:awesome-mcp-hub资源索引库实战指南
  • LangChain框架解析:从RAG到Agent的AI应用开发实践
  • 2026届学术党必备的十大AI辅助论文神器实际效果
  • 告别繁琐调参!基于ESO的PMSM无差拍预测控制Simulink仿真建模全流程(附模型文件)
  • Pilot Shell:基于规格驱动开发的Claude Code工程化框架实践
  • GPT Academic:模块化AI助手在学术研究中的深度应用与配置指南
  • 基于eBPF与规则引擎的SnoutGuard开源安全工具实战解析
  • 2026 年生活服务行业 GEO 服务商排行榜,五大实力机构深度盘点 - GEO优化
  • AI智能体技能库:OpenClaw生态下的工具复用与集成实战