2017-08-15 50 views
0

我試圖在每次按下某個鍵時生成隨機顏色。我拿這個例子中的代碼:http://learningprocessing.com/examples/chp14/example-14-18-solar-system-OOP處理:在類中爲橢圓添加隨機顏色

我已經調整過我自己的項目,但是我每次按下按鍵時都很難改變填充。 (我想在這裏把它粘貼但格式不停地搞亂了,所以我把它放在引擎收錄)

類文件:https://pastebin.com/HiBXdA4A

主文件:

boolean colorChange = false; 

// An array of 8 planet objects 
Planet[] planets = new Planet[30]; 
ArrayList<Planet> newPlanets = new ArrayList<Planet>() ; 

void setup() { 
    //size(900, 900); 
    fullScreen(); 

    // The planet objects are initialized using the counter variable 
    for (int i = 0; i < planets.length; i++) { 

    planets[i] = new Planet(185 + i*5, 8); 

    } 

} 

void draw() { 
    background(0); 

    /* Stars */ 
    randomSeed(103); 

    for (int i = 0; i < 300; i++) { 
    float x = random(0, width); 
    float y = random(0, height); 

    ellipse(x, y, 2, 2); 
    fill(255); 
    } 

    // Drawing the Earth 
    pushMatrix(); 
    translate(width/2, height/2); 
    stroke(0); 
    fill(0, 191, 255); 
    ellipse(0,0,350,350); 
    noFill() ; 
    // Drawing all Planets 
    for (int i = 0; i < planets.length; i++) { 
    planets[i].update(); 
    planets[i].display(); 
    } 

    if(newPlanets.size() > 0) { 
     for(int i = 0 ; i < newPlanets.size() ;i++) { 
     println("newPlanets should be drawing") ; 
     Planet p = newPlanets.get(i) ; 
     p.update() ; 
     p.display() ; 
     } 
    } 


    popMatrix(); 

    fill(255, 0, 0); 
    text("[Press E for Air Pollution]", width/9, height - (height/8)); 
    fill(255, 255, 0); 
    text("[Press W for Ground Level Pollution]", width/9, height - (height/8 + 15)); 
    fill(0, 255, 0); 
    text("[Press Q for Greenhouse Gasses]", width/9, height - (height/8 + 30)); 
} 

void keyPressed() { 
    if(key == 'q' || key == 'Q') { 
     for(int i = 0 ; i < planets.length ; i++) { 
     newPlanets.add(new Planet(185 + i*5, 8)); 
     } 
    } 

    if(key == 'w' || key == 'W') { 
     for(int i = 0 ; i < planets.length ; i++) { 
     newPlanets.add(new Planet(185 + i*5, 8)) ; 
     } 
    } 

    if(key == 'e' || key == 'E') { 
     for(int i = 0 ; i < planets.length ; i++) { 
     newPlanets.add(new Planet(185 + i*5, 8)); 
     } 
    } 

} 

類文件:

// Example 14-18: Object-oriented solar system 

class Planet { 
    // Each planet object keeps track of its own angle of rotation. 
    float theta;  // Rotation around sun 
    float diameter; // Size of planet 
    float distance; // Distance from sun 
    float orbitspeed; // Orbit speed 
    float resetingDistance ; 
    color planetColor; 
    boolean colorChange = false; 


    Planet(float distance_, float diameter_) { 
    distance = distance_; 
    resetingDistance = distance_ ; 
    diameter = diameter_; 
    theta = 0; 
    orbitspeed = random(0.01, 0.03); 
    //planetColor = color(random(255), random(255), random(255), random(255)); 
    } 

    void update() { 
    // Increment the angle to rotate 
    theta += orbitspeed; 
    } 

    void display() { 
    // Before rotation and translation, the state of the matrix is saved with pushMatrix(). 
    pushMatrix(); 
    // Rotate orbit 
    rotate(theta); 
    // Translate out distance 
    translate(distance, 0); 
    stroke(0); 
    fill(175); 

    if (colorChange == true) { 
     //fill(random(255), random(255), random(255), random(255)); 
     planetColor = color(random(255), random(255), random(255), random(255)); 
    } 

    ellipse(0, 0, diameter, diameter); 
    // Once the planet is drawn, the matrix is restored with popMatrix() so that the next planet is not affected. 
    popMatrix(); 
    } 
} 
+0

所以你在某些情況下改變了「planetColor」,colorChange,但是你似乎沒有改變colorChange變量,並且你沒有在任何地方使用「planetColor」變量 - 除非我失去了一些東西。 – Stefan

+0

當您按下按鍵時已經發現如何設置colorChange,請記住在planetColor獲得其新值時重置該變量。 – Stefan

回答

0

你不應該複製粘貼在互聯網上找到的代碼,然後嘗試去摧毀它。你會這樣給自己一大堆頭痛。相反,您確實需要準確理解每條線的功能,然後創建一個新草圖並採取這些經驗教訓來實現您的目標。

您需要break your problem down into smaller steps,然後逐個採取這些步驟。例如,你可以創建一個簡單的草圖,只顯示一個隨機的顏色?好的,現在您可以在用戶輸入密鑰時更改顏色了嗎?

你需要採取的基本做法是這樣的:

  • 存放在一組變量的顏色。
  • 使用這些變量繪製場景。
  • 當用戶採取某些操作時更改這些變量。

是一般的做法,並在你的情況下,你可能會想保存在您的Planet類的變量,這樣每個行星都可以有自己的顏色。

下面是遵循這一做法表現出隨機顏色每當用戶點擊鼠標的一個小例子:

<script src="https://cdnjs.cloudflare.com/ajax/libs/processing.js/1.6.6/processing.js"></script> 
 
<script type="application/processing"> 
 

 
float r = 128; 
 
float g = 128; 
 
float b = 128; 
 

 
void setup(){ 
 
    size(300, 200); 
 
} 
 

 
void draw(){ 
 
    background(r, g, b); 
 
} 
 

 
void mousePressed(){ 
 
    r = random(256); 
 
    g = random(256); 
 
    b = random(256); 
 
} 
 
</script> 
 
<canvas> </canvas>

該代碼僅僅是一個例子,但它說明了如何當用戶採取行動時,使用上述方法來更改內容。我強烈建議你從這個簡單的草圖開始,如果你遇到困難,請發帖MCVE。祝你好運。