Review
I wasn't able to figure out a way to make the code more compact and in my opinion the code is pretty clear. However I do have some other feedback.
Use a proper main
Instead of:
turtle.setworldcoordinates(0, 0, 4, 255)
turtle.colormode(255)
turtle.tracer(False)
turtle.bgcolor(0,0,0)
...
def main():
spawningPainters()
assign_painters()
listening_input()
main()
turtle.listen()
turtle.tracer(True)
turtle.mainloop()
Do:
...
if __name__ == "__main__":
turtle.setworldcoordinates(0, 0, 4, 255)
turtle.colormode(255)
turtle.tracer(False)
turtle.bgcolor(0,0,0)
spawningPainters()
assign_painters()
listening_input()
turtle.listen()
turtle.tracer(True)
turtle.mainloop()
For more information you can look at this post: https://stackoverflow.com/questions/419163/what-does-if-name-main-do
Prevent the turtle from going outside of the window
With the current code it is possible to drag the turtle outside of the window. If you then stop dragging you cannot get it back unless you reset the position. To prevent this from happening we can put boundaries around the y value inside of the draw functions.
def draw_blue(x, y):
bluePainter.ondrag(None)
x = blue_x
y = min(y, 255)
y = max(y, 0)
bluePainter.goto(x, y)
bluePainter.ondrag(draw_blue)
update_screen_color()
Update screen color when going back to the origin
When the position is put back to the origin the screen color is not redrawn. Unless this is intended it would be good to call the update_screen_color
function at the end of the back_to_origin
function.
Few tips
The global painter
variable is not used anywhere outside of the spawningPainters
function, so it can be removed.
Move the colors
list to the spawningPainters
function as it is not used anywhere else.
You might want to rename spawningPainters
to spawn_painters
.
Extra
Add typing to the painters
list
When an empty list is declared the intellisense of your IDE may have trouble figuring out what type of elements the list will contain. By adding type annotations you can help the intellisense better understand your code.
Instead of:
painters = []
Do:
from typing import List
...
painters: List[turtle.Turtle] = []
```