Is it possible to rotate a shape with a negative degree in python?

Viewed 21
import turtle
import time


class Square:
    def __init__(self, color='red', pos=(0, 0)):
        self.__pen = turtle.Turtle('square')
        self.__set_pen(pos, color)

    def __set_pen(self, pos, color):
        self.__pen.shapesize(5, 5) 
        self.__pen.up()
        self.__pen.goto(pos)
        self.__pen.color(color)
        wn.update()

    def rotate(self, angle):  # CLOCKWISE
        for i in range(angle):  
            self.__pen.right(1)
            wn.update()
            time.sleep(0.01)
wn = turtle.Screen()
wn.tracer(False)
s01 = Square(color='green', pos=(100, 100))
s01.rotate(-60)
wn.mainloop()

This is my code that im working on but how do I make it work with a negative input in s01.rotate() and rotate counter clockwise. I tired inputing a negative integer but it wouldn't rotate at all.

1 Answers

I think this should work

import turtle
import time


class Square:
    def __init__(self, color='red', pos=(0, 0)):
        self.__pen = turtle.Turtle('square')
        self.__set_pen(pos, color)

    def __set_pen(self, pos, color):
        self.__pen.shapesize(5, 5) 
        self.__pen.up()
        self.__pen.goto(pos)
        self.__pen.color(color)
        wn.update()

    def rotate(self, angle):  
        negative = True if angle < 0 else False
        for i in range(abs(angle)): 
            if negative:
                self.__pen.left(1)
            else: 
                self.__pen.right(1)
            wn.update()
            time.sleep(0.01)
Related