2015-02-10 28 views
-1

爲我的世界創建一個立方體的圖像。 試圖讓盒子裏的顏色自己改變。有什麼樣的整數算法,我可以用來實現這一點,除了使用隨機?因爲現在它會產生隨機顏色,但我希望小盒子能夠自己改變顏色。有任何想法嗎?Python龜,如何讓顏色自己改變?

import turtle 
import random 


minecraft = turtle.Turtle() 

minecraft.ht() 
minecraft.speed(9999999999999) #I guess there is a max speed??? wanted it to make the mini cubes a lot faster. 
#centers the box 
minecraft.up() 
minecraft.goto(-50,50) 
minecraft.down() 
#end of center box 
for i in range(4): #Creates the box 
minecraft.forward(100) 
minecraft.right(90) 
for i in range(1000): #Repeats all this code over and over 
for i in range(10): #makes the 10 cubes going down, then it comes back up  and repeates making cubes until it gets to the last cube. 
    for i in range(10): #initiate the random colors 
     red = random.random() 
     blue = random.random() 
     yellow = random.random() 
     minecraft.color(red, blue, yellow) 
     for i in range(1): #the little boxes 
      minecraft.begin_fill()  
      minecraft.forward(10) 
      minecraft.right(90) 
      minecraft.forward(10) 
      minecraft.right(90) 
      minecraft.forward(10) 
      minecraft.right(90) 
      minecraft.forward(10) 
      minecraft.right(90) 
      minecraft.end_fill() 

     minecraft.right(90) #little boxes changing directions 
     minecraft.forward(10) 
     minecraft.right(-90) 

    minecraft.forward(10) #little boxes changing directions...again 
    minecraft.right(-90) 
    minecraft.forward(100) 
    minecraft.right(90) 

minecraft.right(180) #and again... 
minecraft.forward(100) 
minecraft.right(180) 
+0

海龜的速度數字有點奇怪。速度0是最快的,但是他們從1最慢,2更快一點,6是正常速度,等到10快。任何高於10或低於0.5的數字都將轉換爲0.請參閱'help(turtle.speed)'。 – 2015-02-10 03:51:57

回答

-1

random.random()創建一個隨機數落入0和1之間。所以,當你需要一個隨機數0和MAX之間產生,只是簡單的用它大量繁殖,在你的情況下,像這樣:

int(random.random()*256) 

順便說一句,我再次檢查了烏龜文檔。 turtle.color(* args)需要兩個顏色參數,「返回或設置pencolor和fillcolor」。這意味着你需要以這種方式傳遞它turtle.color((40,80,120),(160,200,240))。

+0

一直說不好的顏色順序。 :(xxx,xxx,xxx) – pewpew 2015-02-10 03:46:32

+0

這似乎更多的評論比答案。作爲答案,它沒有解決OP的核心問題。作爲評論,這通常是錯誤的。默認的'turtle.colormode()'是1.0,意思是'random.random()'是一種生成顏色的有效方法,就像你看到的那樣,如果你運行代碼。此外,'turtle.color()'可以只接受一個參數,它被視爲pencolor和fillcolor--你不需要同時提供這兩個參數。 – cdlane 2016-12-08 08:07:07

0

根據您的問題描述,這是什麼,我相信你問 - 這顯得隨意單獨改變顏色隨機彩色框的網格:

from turtle import Turtle, Screen 
import random 

BOX_SIZE = 100 
SQUARE_SIZE = 10 
DELAY = 100 # milliseconds 

minecraft = Turtle(shape="square", visible=False) 
minecraft.shapesize(SQUARE_SIZE/20) 
minecraft.speed("fastest") 

# Center the box 
minecraft.up() 
minecraft.goto(-BOX_SIZE//2, BOX_SIZE//2) 
minecraft.down() 

# Create the box 
for _ in range(4): 
    minecraft.forward(BOX_SIZE) 
    minecraft.right(90) 
minecraft.up() 

# Move turtle inside box 
minecraft.forward(SQUARE_SIZE//2) 
minecraft.right(90) 
minecraft.forward(SQUARE_SIZE//2) 
minecraft.left(90) 

squares = [] 

for i in range(BOX_SIZE // SQUARE_SIZE): 
    # makes the 10 cubes going across, then it backs up and 
    # repeats making cubes until it gets to the last cube. 

    for j in range(BOX_SIZE // SQUARE_SIZE): 

     # initiate the random colors 
     red = random.random() 
     blue = random.random() 
     yellow = random.random() 

     minecraft.color(red, blue, yellow) 
     squares.append((minecraft.position(), minecraft.stamp())) 

     minecraft.forward(SQUARE_SIZE) 

    minecraft.backward(SQUARE_SIZE) 
    minecraft.sety(minecraft.ycor() - SQUARE_SIZE) 
    minecraft.right(180) # little boxes changing directions 

def change(): 
    random_choice = random.choice(squares) 
    squares.remove(random_choice) 
    position, stamp = random_choice 
    minecraft.goto(position) 

    red = random.random() 
    blue = random.random() 
    yellow = random.random() 

    minecraft.color(red, blue, yellow) 
    minecraft.clearstamp(stamp) 
    squares.append((minecraft.position(), minecraft.stamp())) 

    screen.ontimer(change, DELAY) 

screen = Screen() 

screen.ontimer(change, DELAY) 

screen.exitonclick() 

關鍵就在這,就像很多烏龜問題,是郵票,而不是。衝壓圖像可以完成許多繪製圖像無法做到的事情 - 在這種情況下,它們可以單獨移除並由其他印記代替。

此外,永遠不要讓你的海龜代碼永遠運行,也不要讓它接近它 - 使用事件代替ontimer(),以便其他海龜事件(如正確關閉窗口)可以正確觸發。