微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何制作改变的圆形碰撞箱?

如何解决我如何制作改变的圆形碰撞箱?

我是编程新手,我正在制作一款瞄准训练游戏,您可以在其中击中不同的圆圈,并且必须击中一个,然后才能切换到下一个圆圈。不过我遇到了一些麻烦。我不知道如何为每个圆圈变化的圆圈制作一个命中框以及如何提高分数。

代码正在pjs中开放处理。

  class Timer {
  int time;
  int duration;
  Timer() {
    duration = 100;
    reset();
  }
  void reset() {
    time = millis() + duration;
  }
  boolean alarm() {
    if ( millis() > time ) {
      time = millis() + duration;
      return true;
    }
    return false;
  }
}


float x,y,s; //size and position of circles
color c; //colour of circles
int score = 0; //score
int miss = 0; //misses

Timer timer = new Timer();

void setup() {
  size(700,500);
  timer.duration = 2000; //changes the circle every 2 seconds
    missed =+ 1;
  newEllipse(); // Make initial circle.
}

void draw() {
  background(100);
  fill(c);
    text("score: " + str(score),620,20);
    text("MISSED: " + str(miss),20,20);
  ellipse(x,s,s);
  if( timer.alarm() ){
    newEllipse();
  }
}

void mousepressed(){
  if( overEllipse() ){
        score =+1;
    newEllipse();
  }
}

boolean overEllipse(){
    if(mouseX > x && mouseX < x+s && mouseY > y && mouseY < y + s);
}

void newEllipse() {
  s = random(5,min(50,50));
  x = random(0,width - s);
  y = random(0,height - s);  
  c = color( random(255),random(255),random(255) );
  timer.reset();
}

解决方法

一旦我尝试了你的代码,我意识到你想要的东西非常简单,所以今天不需要 OOP。我修复并评论了您的代码,以便您可以对其进行调整和测试:

float s; //size of circles
PVector targetPosition; // position of target
color c; //colour of circles
int score = 0; //score
int miss = 0; //misses

Timer timer = new Timer();

void setup() {
  size(700,500);
  timer.duration = 2000; //changes the circle every 2 seconds
  newEllipse(); // Make initial circle.
}

void draw() {
  background(100);
  fill(255);  // easier to read if you don't change the color all the time
  text("SCORE: " + str(score),620,20);
  text("MISSED: " + str(miss),20,20);

  fill(c);
  ellipse(targetPosition.x,targetPosition.y,s,s);
  if ( timer.alarm() ) {
    miss += 1; // added a 'miss' when the user isn't able to click the target in due time
    newEllipse();
  }
}

// notice that I switched this method for 'mouseClicked' instead of 'mousePressed'
// the difference is that this works on a simple click,and run once per click instead of continuously
void mouseClicked() {  
  if (overEllipse()) {
    score += 1;
    newEllipse();
  } else { // added a 'miss' when the user clicks at random
    miss += 1;
  }
}

boolean overEllipse() {
  // these 3 lines are giving you feedback so you know how close to the target you were
  PVector mousePosition = new PVector(mouseX,mouseY);
  println(mousePosition.dist(targetPosition) + " / " + s/2);

  // you only need the next line if you don't care about the feedback
  return mousePosition.dist(targetPosition) < s/2;
}

void newEllipse() {
  s = random(10,50); // I removed the `min(50,50)` because the minimum number between 50 and 50 is... 50. Maybe you were going for something else,but like this it was not needed
  targetPosition = new PVector(random(s/2,width - s/2),random(s/2,height - s/2));
  c = color( random(255),random(255),random(255) );
  timer.reset();
}

class Timer {
  int time;
  int duration;
  
  Timer() {
    duration = 100;
    reset();
  }
  
  void reset() {
    time = millis() + duration;
  }

  boolean alarm() {
    if ( millis() > time ) {
      time = millis() + duration;
      return true;
    }
    return false;
  }
}

这里的主要问题是方法 overEllipse 没有意义,所以我将它点燃并使用 PVector 而不是直线坐标重写它。我这样做主要是因为它比使用数学来确定我们稍后是否在目标内部点击更容易(PVector 类会直接为你做,这是一个很棒的工具,看看我是如何调整的overEllipse 方法以及使用此工具如何轻松!)。我还在控制台 println 中添加了一些反馈,以便您了解您点击的距离目标有多近。

我会在附近,您需要我解释一些事情。玩得开心!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。