~kkissling/python-snippets/testzweig

« back to all changes in this revision

Viewing changes to pyturtle/tdemo_colormixer.py

  • Committer: Grant Bowman
  • Date: 2010-04-19 11:22:17 UTC
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: grantbow@gmail.com-20100419112217-rw0i2xlr622wc3ag
rough conversion

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
#
 
3
# [SNIPPET_NAME: Turtle Color Mixer]
 
4
# [SNIPPET_CATEGORIES: PyTurtle]
 
5
# [SNIPPET_DESCRIPTION: Use turtle to show a color mixing screen]
 
6
# [SNIPPET_DOCS: http://docs.python.org/library/turtle.html]
 
7
# [SNIPPET_AUTHOR: Grant Bowman <grantbow@ubuntu.com>]
 
8
# [SNIPPET_LICENSE: PSF]
 
9
 
 
10
 
 
11
from turtle import Screen, Turtle, mainloop
 
12
 
 
13
class ColorTurtle(Turtle):
 
14
 
 
15
    def __init__(self, x, y):
 
16
        Turtle.__init__(self)
 
17
        self.shape("turtle")
 
18
        self.resizemode("user")
 
19
        self.shapesize(3,3,5)
 
20
        self.pensize(10)
 
21
        self._color = [0,0,0]
 
22
        self.x = x
 
23
        self._color[x] = y
 
24
        self.color(self._color)
 
25
        self.speed(0)
 
26
        self.left(90)
 
27
        self.pu()
 
28
        self.goto(x,0)
 
29
        self.pd()
 
30
        self.sety(1)
 
31
        self.pu()
 
32
        self.sety(y)
 
33
        self.pencolor("gray25")
 
34
        self.ondrag(self.shift)
 
35
 
 
36
    def shift(self, x, y):
 
37
        self.sety(max(0,min(y,1)))
 
38
        self._color[self.x] = self.ycor()
 
39
        self.fillcolor(self._color)
 
40
        setbgcolor()
 
41
 
 
42
def setbgcolor():
 
43
    screen.bgcolor(red.ycor(), green.ycor(), blue.ycor())
 
44
 
 
45
def main():
 
46
    global screen, red, green, blue
 
47
    screen = Screen()
 
48
    screen.delay(0)
 
49
    screen.setworldcoordinates(-1, -0.3, 3, 1.3)
 
50
 
 
51
    red = ColorTurtle(0, .5)
 
52
    green = ColorTurtle(1, .5)
 
53
    blue = ColorTurtle(2, .5)
 
54
    setbgcolor()
 
55
 
 
56
    writer = Turtle()
 
57
    writer.ht()
 
58
    writer.pu()
 
59
    writer.goto(1,1.15)
 
60
    writer.write("DRAG!",align="center",font=("Arial",30,("bold","italic")))
 
61
    return "EVENTLOOP"
 
62
 
 
63
if __name__ == "__main__":
 
64
    msg = main()
 
65
    print msg
 
66
    mainloop()