2016-11-06 10 views
0
void setup() { 
    size(800, 500); 
} 

float x=20; 
int y=480; 
int aptSize=20; 
float a, e; 
float currentFloor=0; 
float s; 
float offset; 
//float aptNum; 
void draw() { 

    //draw sky 
    // sky(); 
    float n=0;  

// if(keyPressed){ 
// sky(); 

    //draw a building 
    for (int f=0; f<3 && x<600; f++, x = x + ((n*20)+20)) { 
    n = building(x, y, 15); 
    } 
} 
//}  

void sky() { 
    float r= 50; 
    float g= 0; 
    float b=200; 
    background(random(r), random(g), random(b)); 
    r=constrain(r, 0, 140); 
    g=constrain(g, 0, 65); 
    b=constrain(b, 0, 255); 
    }  

float building(float x, int y, float floorNum) { 
    for (int i=0; i<floorNum; i++, y=y-aptSize) { 
    a = random(3, 12); 
    int b = 7; 
    currentFloor= y*aptSize; 
    aptRow(x, y, b); 
    if (currentFloor<floorNum/3) { 
     b=b-1; 
    } 
    } 

    return a; 
}  

void aptRow(float x, int y, float aptNum) { 
    for (int j=0; j<aptNum; j++, x=x+aptSize) { 
    if (x<width) 
     aptUnitA(x, y, aptSize); 
    } 
} 

void aptUnitA(float x, int y, int aptSize) { 
    fill(155); 
    stroke(0); 
    rect(x, y, aptSize, aptSize); 
    noStroke(); 
    fill(242, 235, 53); 
    ellipse(x+5, y+5, aptSize/5, aptSize/5); 
    ellipse(x+15, y+5, aptSize/5, aptSize/5); 
    ellipse(x+10, y+15, aptSize/5, aptSize/2); 
} 

このコードはいくつかの建物です。私はちょうどキーが押されるたびに建物のランダムな新しいセットを描く方法を見つけようとしています、そして、建物の頂部で異なるサイズのアパートの列をいくつか持っていますか?処理の手助けが必要です。

+0

このコードを実行したときの結果は何ですか?間違いはありますか?何が起こるのですか?質問を編集してこの情報を追加することができます。 – Theresa

答えて

0

キーボード入力を取得するには、keyPressed()関数またはkeyPressed変数を使用できます。詳細はthe referenceまたはthis tutorial(免責事項:私はそのチュートリアルを書いています)で入手できます。

建物をさまざまなサイズにするには、random()関数を使用して乱数を取得し、パラメータのaptRow()関数に渡します。

私は一歩前進してより簡単なものを最初に稼働させることをお勧めします。ユーザーがキーを押すたびに1つのランダムなサイズの矩形を表示できますか?あなたの実際の目標に向かってそこからあなたの方法を上げてください。

関連する問題