Browse Source

日常开发

林旭祥 4 weeks ago
parent
commit
99788d88f9
1 changed files with 33 additions and 3 deletions
  1. 33 3
      src/views/game/basketball.vue

+ 33 - 3
src/views/game/basketball.vue

@@ -351,12 +351,37 @@ class Hoop {
   constructor(x, y) {
     this.x = x;
     this.y = y;
+    this.originalX = x; // 记录初始X位置,用于边界计算
+    this.speed = 100; // 滑动速度(像素/秒)
+    this.direction = 1; // 滑动方向:1向右,-1向左
+    this.range = (clientObj.value.width - 150) / 2; // 滑动范围(像素)
     this.points = [
       { x: x + 7, y: y + 18 },
       { x: x + 141, y: y + 18 }
     ];
   }
 
+  // 添加更新方法处理滑动逻辑
+  update(delta) {
+    // 计算新位置
+    this.x += this.speed * this.direction * delta;
+    
+    // 检测边界,反转方向
+    if (this.x > this.originalX + this.range) {
+      this.x = this.originalX + this.range;
+      this.direction = -1;
+    } else if (this.x < this.originalX - this.range) {
+      this.x = this.originalX - this.range;
+      this.direction = 1;
+    }
+    
+    // 更新碰撞检测点
+    this.points = [
+      { x: this.x + 7, y: this.y + 18 },
+      { x: this.x + 141, y: this.y + 18 }
+    ];
+  }
+
   drawBack(ctx, game) {
     drawImage(
       ctx,
@@ -655,6 +680,11 @@ const update = (delta) => {
     //   gameState.ballX = 0;
     // }
 
+    // 添加篮筐更新逻辑
+    gameState.hoops.forEach(hoop => {
+      hoop.update(delta);
+    });
+
     // 键盘控制篮球移动
     if (gameState.keyLeft) {
       //gameState.ballX -= gameState.ballVel * delta;
@@ -929,9 +959,9 @@ const initGame = async () => {
 
   // 添加篮筐
   gameState.hoops = [
-    new Hoop(110, 300),
-    new Hoop(clientObj.value.width / 2 - (148 / 2), 120),
-    new Hoop(clientObj.value.width - 148 - 110, 300),
+    //new Hoop(110, 300),
+    new Hoop(clientObj.value.width / 2 - (148 / 2), 150),
+    //new Hoop(clientObj.value.width - 148 - 110, 300),
   ];
 
   // 开始游戏循环