2016-11-06 20 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 referencethis tutorial(免責聲明:我寫了該教程)。

要使建築物具有不同的尺寸,可以使用random()函數獲取一個隨機數,然後將其作爲aptRow()函數的參數aptNum傳遞。

我建議退後一步,先找一些簡單的工作。每次用戶按下某個鍵時,您能否顯示一個隨機大小的矩形?從那裏走向實際的目標。