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

20260417

这几天尝试一下写一个能实现联机的网页小游戏,想学习使用一下tcp,udp啥的协议,先把简单框架搭起来吧,明天完善一下然后研究一下咋通信,然后先尝试一下用websoket入门

基本框架如下,但还没做完。。

RectangleMove.vue

  1 <template>
  2   <div class="CanvasContainer">
  3     <canvas width="1000" height="800" ref="backgroundCanvas"></canvas>
  4   </div>
  5 </template>
  6 
  7 <script setup>
  8 import { onMounted, ref } from "vue"
  9 
 10 const backgroundCanvas = ref(null)
 11 let ctx = null
 12 //玩家集合
 13 const players = []
 14 //子弹集合
 15 const bullets = []
 16 //按键字典
 17 const keys = {
 18   w: false,
 19   a: false,
 20   s: false,
 21   d: false
 22 }
 23 //按下按键
 24 function keyDown(e) {
 25   if (keys.hasOwnProperty(e.key)) {
 26     keys[e.key] = true
 27   }
 28 }
 29 //松开按键
 30 function keyUp(e) {
 31   if (keys.hasOwnProperty(e.key)) {
 32     keys[e.key] = false
 33   }
 34 }
 35 //
 36 function draw() {
 37   //清空画布
 38   ctx.clearRect(0, 0, backgroundCanvas.value.width, backgroundCanvas.value.height)
 39 
 40   players.forEach(player => {
 41     player.draw()
 42   })
 43 }
 44 //更新位置
 45 function updatedPosition() {
 46   //自己移动
 47   players.forEach((player) => {
 48     if (player.me) {
 49       if (keys.w) player.y -= player.ySpeed
 50       if (keys.s) player.y += player.ySpeed
 51       if (keys.a) player.x -= player.xSpeed
 52       if (keys.d) player.x += player.xSpeed
 53     }
 54   })
 55 }
 56 //动画
 57 function animate() {
 58   updatedPosition()
 59   draw()
 60   requestAnimationFrame(animate)
 61 }
 62 //初始化
 63 onMounted(() => {
 64   const canvas = backgroundCanvas.value
 65   ctx = canvas.getContext("2d")
 66   
 67   const player1 = new Player(100, 100, 60, 60, "red", 2, 2, true)
 68   players.push(player1)
 69   //绑定按键事件
 70   window.addEventListener('keydown', keyDown)
 71   window.addEventListener('keyup', keyUp)
 72   //启动
 73   animate()
 74 })
 75 //玩家类
 76 class Player {
 77   constructor(x, y, width, height, color, xSpeed, ySpeed, me) {
 78     this.x = x
 79     this.y = y
 80     this.width = width
 81     this.height = height
 82     this.color = color
 83     this.xSpeed = xSpeed
 84     this.ySpeed = ySpeed
 85     this.me = me
 86   }
 87 
 88   draw() {
 89     ctx.fillStyle = this.color
 90     ctx.fillRect(this.x, this.y, this.width, this.height)
 91   }
 92 
 93 }
 94 
 95 class Bullet {
 96   constructor(x, y, width, height, color, xSpeed, ySpeed, from) {
 97     this.x = x
 98     this.y = y
 99     this.width = width
100     this.height = height
101     this.color = color
102     this.xSpeed = xSpeed
103     this.ySpeed = ySpeed
104     this.from = from
105   }
106 }
107 </script>
108 
109 <style scoped>
110 .CanvasContainer {
111   display: flex;
112   width: 100vw;
113   height: 100vh;
114   background-color: #000;
115   align-items: center;
116   justify-content: center;
117 }
118 
119 canvas {
120   background-color: #000333;
121   border: red solid 2px;
122 }
123 </style>

 

http://www.jsqmd.com/news/657221/

相关文章:

  • Unity C#脚本控制平滑移动——MoveTowards()方法的进阶应用与性能优化
  • 装修公司怎么选?2026设计施工一体公司推荐与避坑指南 - 品牌策略主理人
  • 保姆级教程:用C++在PX4飞控上实现无人机航线跟踪(Cross-track Error算法详解)
  • AI应用开发必看:Token、Skill、Agent、RAG四概念辨析,手把手教你打造可测知识问答Agent!
  • 如何5分钟完成DOL游戏汉化美化:终极整合包使用指南
  • Unity物理引擎实战:用GJK+EPA算法搞定2D碰撞后的物体分离(附完整C#源码)
  • WereYouLast
  • 差分式升压逆变器MATLAB仿真模型设计——实现110V/50Hz输出电压与THD<5%
  • OpenEMS开源能源管理系统:构建智能能源解决方案的完整指南
  • 海外短视频竞争升级跨境卖家如何提升内容吸引力
  • Windows 11 Android子系统终极指南:3种方法快速部署跨平台应用生态
  • flask》》多线程并发数据安全问题 threading.local werkzeug.local.Local
  • Android手把手编写儿童手机远程监控App之JAVA基础
  • 新建了一个微信群深圳技术交流群
  • CISSP 域6知识点 安全评估与测试策略
  • 测试深度策略
  • 3个步骤轻松掌握PhotoGIMP:从Photoshop无缝迁移到开源图像编辑的终极方案
  • Python小白该这样入门呢
  • 从‘新建’到‘流转’:手把手教你用JIRA问题单驱动敏捷开发全流程
  • # AI Agent爬虫深度解析:从规则驱动到目标驱动,爬虫技术的终局之战
  • 基于忆阻器的自适应神经形态脑机接口解码系统
  • 象州站计算机联锁工程设计复现
  • 为什么 await 没生效?
  • 免费解锁Cursor AI Pro完整功能:5分钟掌握专业级AI编程助手
  • 可跑在STM32上的EtherCAT主机协议栈
  • 告别编译地狱!用Python的TenSEAL库5分钟上手同态加密实战(CKKS方案)
  • Electron 摄像头打不开/锁死问题排查手册
  • WebDebugx移动端网页调试实用技巧大全
  • 深入解析基4 Booth算法在定点乘法器中的高效实现
  • 告别手动复制粘贴:用Makefile自动化你的Vivado DPU XO文件生成流程