-1

creating an image of a cube for minecraft. Trying to get the colors inside the box to change by themselves. Is there some kind of integer algorithm I can use to achieve this besides using random? Because right now, it creates random colors, but I want the little boxes to change colors by themselves. Any Ideas?

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)
cdlane
  • 40,441
  • 5
  • 32
  • 81
pewpew
  • 700
  • 2
  • 9
  • 32
  • Turtle speed numbers are a bit weird. Speed 0 is fastest, but then they go from 1 being slowest, 2 is a little faster, 6 is normal speed, etc up to 10 being fast. Any numbers higher than 10 or lower than 0.5 get converted to 0. See `help(turtle.speed)`. – PM 2Ring Feb 10 '15 at 03:51

2 Answers2

0

Based on your description of the problem, this is what I believe you're asking for -- a grid of randomly colored boxes that appear to individually change color at random:

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()

The key to this, like many turtle problems, is to stamp, not draw. Stamped images can do a number of things that drawn images can't -- in this case they can be individually removed and replace by other stamps.

Also, never let your turtle code run forever, nor any anywere close to it -- use an ontimer() event instead so that other turtle events (like properly closing the window) can trigger correctly.

cdlane
  • 40,441
  • 5
  • 32
  • 81
-1

random.random() create a random number falls between 0 and 1. So when you need a random number generate between 0 and MAX, just simply multiply with it, in your case, like this way:

int(random.random()*256)

BTW, I checked the turtle docs again. turtle.color(*args) expects two color args, "Return or set pencolor and fillcolor". That means you need to pass it turtle.color((40, 80, 120), (160, 200, 240)) this way.

John Hua
  • 1,400
  • 9
  • 15
  • keeps saying bad color sequence. : (xxx,xxx,xxx) – pewpew Feb 10 '15 at 03:46
  • This seems more a comment than an answer. As an answer it doesn't address the OP's central question. As a comment, it's generally wrong. The default `turtle.colormode()` is 1.0 which means `random.random()` is a valid way of generating colors as you see if you run the code. Also, `turtle.color()` can take just one argument which is treated as both the pencolor and fillcolor -- you don't need to supply both. – cdlane Dec 08 '16 at 08:07