Skip to content Skip to sidebar Skip to footer

Using Class To Draw Shapes In Turtle

I am supposed to develop classes for line segments, circles, and rectangles in which each shape should contain a Turtle object and color to allow it to be drawn in a Turtle graphic

Solution 1:

Well there are relatively simple solutions to each of these but since it is an assignment I won't give you the exact code just a general idea of what you need to do.

For the circle:

Turtle has a circle function that looks like turtle.circle(n) where n is the radius

For the rectangle:

You have 2 good options:

  1. You could use 90 degree turns, such as right or left, and forwards with your lengths to draw the rectangle.
  2. You can calculate the four corners' x and y coordinates and use the goto or setpos functions to go to those corners in order

For the line:

Use the goto or setpos to go to a location making a line or use forward to draw a straight line in the direction that you are facing.

Solution 2:

I'm working on the exact same problem now: Under shape make a subclass for each of your different kinds of shapes. So when using the class in other code it would look like:

shape.rectangle(width,height)<<<(depending on how you're going to have it draw the thing)

Answer :

classShape(self):#thendefrectangle(self, width, height):
#instructions for making a rectangle with width and height in this case

Post a Comment for "Using Class To Draw Shapes In Turtle"