/Demo/turtle/tdemo_colormixer.py

http://unladen-swallow.googlecode.com/ · Python · 58 lines · 48 code · 9 blank · 1 comment · 1 complexity · 50a3c11aaaba691e6936674c7188c853 MD5 · raw file

  1. # colormixer
  2. from turtle import Screen, Turtle, mainloop
  3. class ColorTurtle(Turtle):
  4. def __init__(self, x, y):
  5. Turtle.__init__(self)
  6. self.shape("turtle")
  7. self.resizemode("user")
  8. self.shapesize(3,3,5)
  9. self.pensize(10)
  10. self._color = [0,0,0]
  11. self.x = x
  12. self._color[x] = y
  13. self.color(self._color)
  14. self.speed(0)
  15. self.left(90)
  16. self.pu()
  17. self.goto(x,0)
  18. self.pd()
  19. self.sety(1)
  20. self.pu()
  21. self.sety(y)
  22. self.pencolor("gray25")
  23. self.ondrag(self.shift)
  24. def shift(self, x, y):
  25. self.sety(max(0,min(y,1)))
  26. self._color[self.x] = self.ycor()
  27. self.fillcolor(self._color)
  28. setbgcolor()
  29. def setbgcolor():
  30. screen.bgcolor(red.ycor(), green.ycor(), blue.ycor())
  31. def main():
  32. global screen, red, green, blue
  33. screen = Screen()
  34. screen.delay(0)
  35. screen.setworldcoordinates(-1, -0.3, 3, 1.3)
  36. red = ColorTurtle(0, .5)
  37. green = ColorTurtle(1, .5)
  38. blue = ColorTurtle(2, .5)
  39. setbgcolor()
  40. writer = Turtle()
  41. writer.ht()
  42. writer.pu()
  43. writer.goto(1,1.15)
  44. writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic")))
  45. return "EVENTLOOP"
  46. if __name__ == "__main__":
  47. msg = main()
  48. print msg
  49. mainloop()