首页 > 解决方案 > 如何在基本字母按下游戏中按下正确字母时删除椭圆?

问题描述

我是一个完全的编程初学者,这是一个处理类作业,如果这真的很明显,请道歉。对于这个任务,我正在制作一个简单的游戏,圆圈向顶部浮动,您必须键入屏幕上显示的字母才能一次弹出一个。我完成了程序的大部分部分,除了制作它以便每个正确的字母笔划删除一个圆圈。我该怎么做呢?

int l=97;
int s=0;
int visa = 0;

Bubble[] bubbles = new Bubble[20]; 

void setup(){
    size(640,360,P2D);

    for (int i = 0; i< bubbles.length; i++){ 
        bubbles [i] = new Bubble (random(40,40));
    }
}

void draw(){
    background(255);
    char c=char(l);
    fill(0, 102, 153);
    textSize(22);
    text("Press this letter:  "+c,160, 100);//text(stringdata, x, y, width, height)

    if(millis()< 300000000&&key==c)//30 seconds
    {
        l=int(random(97,122));s++;
    }//here is where the score gets added! s++
    //random(low, high)

    for (int i = 0; i < bubbles.length; i++){

        bubbles[i].display();
        bubbles[i].acsend();
        bubbles[i].top();
    }
}
class Bubble{
    float x;
    float y;
    float diameter;
    float yspeed;

    Bubble(float tempD){
        x =  random(width);
        y = height;
        diameter = tempD;
        yspeed = random(0.5,1.5);
    }

    void display(){
        stroke(255);
        fill(60,120,200,100);
        ellipse(x,y,diameter,diameter);
    }

    void acsend(){
        if (y > 0){
            y = y - yspeed;
            x = x + random(-0.5, 0.5);
        }
    }

    void top(){
        if (y < diameter/2){
            y = height;
            x = random(width);
        }
    }
}

标签: processing

解决方案


将初始气泡数存储到变量中:

Bubble[] bubbles = new Bubble[20]; 
int noOfBubbles = bubbles.length; 

使用keyPressed(), 来评估是否按下了正确的键。
减少气泡数,增加分数并创建一个新的随机字符:

void keyPressed() {
    char c=char(l);
    if (key == c) {

        if (noOfBubbles > 0) {
            noOfBubbles --;
            s ++;
            l=int(random(97,122));
        }
    }
}

只画剩下的气泡数 ( for (int i = 0; i < noOfBubbles; i++)):

void draw(){
    background(255);
    fill(0, 102, 153);

    textSize(22);
    char c=char(l);
    text("Press this letter:  "+c,160, 100);//text(stringdata, x, y, width, height)

    for (int i = 0; i < noOfBubbles; i++){

        bubbles[i].display();
        bubbles[i].acsend();
        bubbles[i].top();
    }
}

推荐阅读