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