Browse Source

日常开发

林旭祥 1 month ago
parent
commit
d0da110ef8
1 changed files with 47 additions and 9 deletions
  1. 47 9
      src/views/game/basketball.vue

+ 47 - 9
src/views/game/basketball.vue

@@ -296,7 +296,7 @@ const gameState = reactive({
   started: false,
   gameOver: false,
   ballX: (640 - 93) / 2, // 篮球初始X坐标(居中)
-  ballY: 880, // 篮球初始Y坐标(底部)
+  ballY: 680, // 篮球初始Y坐标(底部)
   ballVel: 300,
   ballAngleVel: 100,
   ballAngle: 0,
@@ -315,6 +315,7 @@ const gameState = reactive({
   keyLeft: false,
   keyRight: false,
   spaceShoot: false,    // 空格键投篮状态
+  exitTimer: null, // 用于存储退出定时器ID
 });
 
 // 篮筐类
@@ -377,7 +378,7 @@ class Ball {
   }
 
   shoot(power) {
-    this.speed = power + Math.floor(Math.random() * 40);
+    this.speed = power + Math.floor(Math.random() * 40) * 1;
     this.setAngle(270);
   }
 
@@ -719,7 +720,7 @@ const update = (delta) => {
         ball.drawAngle = gameState.ballAngle;
         ball.shoot(1480);
         gameState.balls.push(ball);
-        gameState.ballY = 961;
+        gameState.ballY = clientObj.value.height - 90;
       }
     }
 
@@ -834,7 +835,7 @@ const draw = () => {
 
     // 绘制分数和时间
     // 更靠左的得分
-    drawText(ctx, '得分: ' + gameState.score, 100, 70, 50);
+    drawText(ctx, '得分: ' + gameState.score, 150, 70, 50);
     // 更靠右的时间
     drawText(ctx, '时间: ' + gameState.time, clientObj.value.width - 115, 70, 50);
 
@@ -846,9 +847,39 @@ const draw = () => {
   if (gameState.state === 'over') {
     ctx.textAlign = 'center';
     drawText(ctx, 'Game Over', clientObj.value.width / 2, 200, 80);
-    drawText(ctx, 'Score: ' + gameState.score, clientObj.value.width / 2, 400, 50);
-    drawText(ctx, 'Click to Continue', clientObj.value.width / 2, 800, 50);
+    drawText(ctx, '成绩:' + gameState.score, clientObj.value.width / 2, 400, 50);
     ctx.textAlign = 'center';
+
+  // 游戏结束后2秒自动退出
+  if (!gameState.exitTimer) {
+    gameState.exitTimer = setTimeout(() => {
+      // 退出游戏,这里可以根据实际需求调整,比如返回主菜单或跳转到其他页面
+      gameState.gameOver = false;
+      gameState.started = false;
+      gameState.score = 0;
+      gameState.time = 60;
+      gameState.balls = [];
+      gameState.state = 'menu';
+      gameState.click = false;
+      gameState.exitTimer = null;
+      
+      // 如果需要跳转到其他页面,可以使用路由
+      // router.push('/');
+    }, 2000);
+  }
+  
+  // 保留点击退出的功能
+  if (gameState.click) {
+    clearTimeout(gameState.exitTimer);
+    gameState.gameOver = false;
+    gameState.started = false;
+    gameState.score = 0;
+    gameState.time = 60;
+    gameState.balls = [];
+    gameState.state = 'menu';
+    gameState.click = false;
+    gameState.exitTimer = null;
+  }
   }
 };
 
@@ -866,13 +897,14 @@ const initGame = async () => {
 
   // 添加篮筐
   gameState.hoops = [
-    new Hoop(110, 520),
-    new Hoop(clientObj.value.width - 148 - 110, 520),
-    new Hoop(clientObj.value.width / 2 - (148 / 2), 260)
+    new Hoop(110, 300),
+    new Hoop(clientObj.value.width / 2 - (148 / 2), 120),
+    new Hoop(clientObj.value.width - 148 - 110, 300),
   ];
 
   // 开始游戏循环
   gameState.animationFrameId = requestAnimationFrame(gameLoop);
+  gameState.ballY = clientObj.value.height - 90;
 };
 
 
@@ -895,7 +927,13 @@ onBeforeUnmount(() => {
   if (gameState.animationFrameId) {
     cancelAnimationFrame(gameState.animationFrameId);
   }
+  // 清除退出定时器
+  if (gameState.exitTimer) {
+    clearTimeout(gameState.exitTimer);
+  }
   window.removeEventListener('resize', resizeToWindow);
+  window.removeEventListener('keydown', handleKeyDown);
+  window.removeEventListener('keyup', handleKeyUp);
 });
 </script>