You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Processing游戏触发GameOver后如何停止mouseX鼠标跟踪响应

问题描述

开发Processing平台的赛车小游戏时,触发碰撞弹出GameOver界面后,移动鼠标会导致程序直接退回游戏运行状态,需要实现GameOver触发后,鼠标移动不再影响游戏逻辑。
原有实现代码如下:

int score = 0;
float x_2 = 300;        
float y_0 = 250;  
float y_2 = 650;   
int distanz = 0;
int currentXValue = 0;
int savedXValue = currentXValue;
PImage LilaAuto, GruenAuto, SchwarzAuto, LilaAuto_Main;


void setup() {
    size(450,700);
  
    LilaAuto =loadImage("https://raw.githubusercontent.com/Tf187/Slot-Game-Processing/main/LilaAuto.png");
    LilaAuto_Main =loadImage("https://raw.githubusercontent.com/Tf187/Slot-Game-Processing/main/LilaAuto.png");
}

void draw() {              
            
    background(204);
            
    image(LilaAuto,50,y_0, 100,100);   
    y_0--;
    if(y_0<1) {
        y_0= 650;
    }
         
    fill(204);
    image(LilaAuto_Main,mouseX,y_2, 100,100);  //Auto vom Spieler 
    y_2--;
    if(y_2<1) {
        y_2= 650;
    }
            
    image(LilaAuto,x_2,y_0, 100,100);  
    y_0--;
   
    fill(1);
    textSize(25);
    text("Score:", 10,25);
    text(score,75,25);
           
    float d= dist(mouseX,y_0,x_2,y_2);  //Messung der Distanz
    if(d < 100) {
        score = score+1;
    }  
                     
    if(d < 60) {
        gameOver();
    }
}

void gameOver() {
    score = 0;
    background(120);

    textAlign(CENTER);
    text("GameOver", 250,250); 
}
问题原因
  • Processing的draw()函数是按帧自动循环执行的,现有代码的gameOver()仅会在碰撞触发的单帧内绘制一次结束文字,下一帧会自动回到正常游戏逻辑流程,GameOver界面实际没有持续停留
  • mouseX是Processing内置的全局变量,本身会始终实时同步鼠标横坐标,不需要刻意禁用它的更新,通过状态标记阻断游戏运行阶段的逻辑执行即可达到目的
修复方案
  • 新增全局布尔变量作为游戏状态标记,区分「游戏运行中」和「游戏结束」两个状态
  • 在draw()入口处做状态判断:如果处于结束状态,仅渲染GameOver界面,直接终止当前帧后续逻辑,不执行玩家控制、物体移动、碰撞检测相关代码
  • 碰撞判定触发时,先将状态标记设为结束状态,再渲染结束界面
  • 补充鼠标点击重启逻辑,优化交互体验

修改后的完整可运行代码:

int score = 0;
float x_2 = 300;        
float y_0 = 250;  
float y_2 = 650;   
int distanz = 0;
// 游戏状态标记
boolean isGameOver = false;
PImage LilaAuto, GruenAuto, SchwarzAuto, LilaAuto_Main;


void setup() {
    size(450,700);
  
    LilaAuto =loadImage("https://raw.githubusercontent.com/Tf187/Slot-Game-Processing/main/LilaAuto.png");
    LilaAuto_Main =loadImage("https://raw.githubusercontent.com/Tf187/Slot-Game-Processing/main/LilaAuto.png");
}

void draw() {   
    // 游戏结束状态下仅渲染结束界面,跳过后续游戏逻辑
    if (isGameOver) {
        background(120);
        textAlign(CENTER);
        textSize(32);
        text("GameOver", 250, 250);
        textSize(16);
        text("点击鼠标重新开始", 250, 300);
        return;
    }
          
    background(204);
            
    image(LilaAuto,50,y_0, 100,100);   
    y_0--;
    if(y_0<1) {
        y_0= 650;
    }
         
    fill(204);
    image(LilaAuto_Main,mouseX,y_2, 100,100);  // 玩家控制车辆
    y_2--;
    if(y_2<1) {
        y_2= 650;
    }
            
    image(LilaAuto,x_2,y_0, 100,100);  
    y_0--;
   
    fill(1);
    textSize(25);
    text("Score:", 10,25);
    text(score,75,25);
           
    float d= dist(mouseX,y_2,x_2,y_0);  // 计算两车实际距离
    if(d < 100) {
        score = score+1;
    }  
                     
    if(d < 60) {
        // 碰撞后标记游戏状态为结束
        isGameOver = true;
        score = 0;
    }
}

// 点击重启逻辑
void mousePressed() {
    if (isGameOver) {
        // 重置所有游戏参数
        score = 0;
        y_0 = 250;
        y_2 = 650;
        x_2 = 300;
        isGameOver = false;
    }
}

注:原代码距离计算的参数顺序有误,修复版中已调整为两个车辆的正确坐标,避免计分和碰撞判定不准。


内容的提问来源于stack exchange,提问作者18769

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 16:51:27