这基本上是一个简单的游戏,当鼠标指针在圆圈内时,它会给分数加1。我是一个新手,尝试着创造这个简单的游戏。
代码如下:
float dist;
float score;
float x;
float y;
float ran;
float a;
float b;
void setup(){
size(800,600);
background(0);
score = 0;
}
void draw(){
background(0);
text("score: "+score,600,20);
x = random(800);
y = random(600);
circle_(x,y);
if ( (abs(mouseX - x) <= 200) && (abs(mouseY - y) <= 200 )) { // algorithm for checking whether mouse inside the circle or not
score = score + 1;
}
}
void circle_(float x,float y){
delay(1000);
circle(x,y,50);
}
这里有两个问题:首先,你在应该是25的地方检查了200像素(因为25是圆直径的一半)。但这个比较简单。真正的问题是delay(1000);
行。
这才是真正的痛苦。
这意味着你的程序"休眠"了。大约1000/1001秒。虽然鼠标可以在屏幕上移动,但这只鼠标就是不听。这是等待。
您可以使用millis()
方法跟踪Processing中的时间。它会告诉你从你开始运行草图到现在已经过去了多少毫秒。我忍者给你写了几行代码来记录时间每1秒传送一个圆圈。下面是代码:
float score;
float x;
float y;
int respawnCircle;
void setup() {
size(800, 600);
background(0);
score = 0;
}
void draw() {
background(0);
text("score: "+score, 600, 20);
if (millis() > respawnCircle) {
changeCircleCoordinates();
}
circle_(x, y);
if ((abs(mouseX - x) <= 25) && (abs(mouseY - y) <= 25 )) { // using 25 as it's half the circle's diameter
score = score + 1;
// if you want the circle to change place right when you get one point uncomment the next line
// changeCircleCoordinates();
}
}
void changeCircleCoordinates() {
respawnCircle = millis() + 1000;
x = random(800);
y = random(600);
}
void circle_(float x, float y) {
//delay(1000); // this is why your collision detection wasn't working: the program is sleeping for about 1000/1001 milliseconds... you have to be very lucky to be on target at just the right time
circle(x, y, 50);
}
希望有帮助。玩得开心!