423,851 Members | 2,762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,851 IT Pros & Developers. It's quick & easy.

Function sety in turtle graphics color mixer

P: 7
The following code is from turtle graphics demos, specifically the color mixer demo. The program allows you to adjust the background color of a window using three sliders corresponding to RGB values.

In the following code there is a statement under the shift function "self.sety(max(0,min(y,1)))". The purpose of this statement is unclear. Any thoughts on the matter?

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

Post your reply

Sign in to post your reply or Sign up for a free account.