首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在按下按钮的过程中停止和重新启动游戏

在按下按钮的过程中停止和重新启动游戏
EN

Stack Overflow用户
提问于 2018-09-19 23:32:03
回答 2查看 2.5K关注 0票数 0

我目前正在开发一个学习处理的小游戏。

我希望我的游戏在我按下“停止”时停止,并希望我的游戏在“停止”更改为“开始”时重置/重新启动。

(当我点击按钮时,它会将Stop更改为Start,back更改为Stop等。(所以基本上,我有一个‘按钮’)

我正在苦苦挣扎,在internet / stackoverflow上找不到解决方案,所以也许有人可以帮助我?

(@ mousePressed's,否则我需要'stop and restart function')

代码语言:javascript
复制
float x = width/2;
float speed = 2;

boolean textHasBeenClicked = false;

int aantalRaak = 0; 
int aantalMis = 0;
int positieText = 20;

void setup() {
  background(0);
  size(600,500);
}

void draw() {
  clear();
  move();
  display();  
  smooth();
  //Scoreboard bovenaan
  fill(255);
  textSize(20);
  textAlign(LEFT);
  text("Aantal geraakt: " + aantalRaak,0, positieText); text("Aantal gemist: " + aantalMis, width/2, positieText);

  //button onderaan
      fill(0,255,0);
      rect(width/2-40, height-40, 100, 50);// draw anyway...

} 

void mousePressed() {
    // toggle 
    textHasBeenClicked = ! textHasBeenClicked;
    fill(0);
    if (textHasBeenClicked) {
        // display text 2
        textSize(30);
        textAlign(CENTER);
        text("Stop" , width/2,height-10); 

    }
    else  {
        // display text 1
        textSize(30);
        textAlign(CENTER);
        text("Start" , width/2,height-10); 
        }
}



void move() {
  x = x + speed;
  if (x > width) {
    x = 0;
  }
}

void display(){
  //schietschijf
  float y = height/2;
  noStroke();
  fill(255, 0, 0);
  ellipse(x, y, 40, 40);   

  fill(255);
  ellipse(x, y, 30, 30);

  fill(255, 0, 0);
  ellipse(x, y, 20, 20);  

  fill(255);
  ellipse(x, y, 10, 10);  
}
EN

回答 2

Stack Overflow用户

发布于 2018-09-20 00:42:40

您应该尝试break your problem down into smaller steps,并一次一个地执行这些步骤。你真的问了两个问题:

  • 如何显示变成开始按钮的停止按钮?
  • 如何重置草图?

对于第一个问题,您可以创建一个布尔变量。在draw()函数中使用该变量,然后在mousePressed()函数中修改该变量。

代码语言:javascript
复制
boolean running = false;

void draw() {
  fill(0);
  if (running) {
    background(255, 0, 0);
    text("Stop", 25, 25);
  } else {
    background(0, 255, 0);
    text("Start", 25, 25);
  }
}

void mousePressed() {
  running = !running;
}

然后,为了重置草图,您可以创建一个函数,将所有变量更改回它们的默认值。下面是一个简单的例子:

代码语言:javascript
复制
float circleY;

void setup() {
  size(100, 500);
}

void draw() {
  background(0);
  circleY++;

  ellipse(width/2, circleY, 20, 20);
}

void reset() {
  circleY = 0;
}

void mousePressed() {
  reset();
}

试着从像这样的小例子开始工作,而不是你的整个程序,如果你被卡住了,就发布一个MCVE。祝好运。

票数 0
EN

Stack Overflow用户

发布于 2018-09-19 23:40:49

您可以考虑实现一个while循环。我不知道您使用哪个库进行输入,所以我不能确切地告诉您要做什么。但大致是这样的:

代码语言:javascript
复制
while(!InputReceived) {
    if(CheckForMouseInput()) // Assuming CheckForMouseInput returns true if input was detected
        break // Input was detected, now do stuff based on that.
    else {
        // Must #include <thread> and #include <chrono>
        // Wait a bit...

        continue; // Jump back to the top of the loop, effectively restarting it.
    }

可能会满足你的需要。至少我会这么做。循环中断后,游戏将有效地重新启动,您可以基于此执行所需的任何操作。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52409455

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档