![]() ![]() G=random.randint(81,200) #random numbers for greenī=random.randint(61,255) #random numbers for blue R=random.randint(1,120) #random numbers for red Trtl.pensize(4) #choosing the size of pen nib Import random # importing random module for generating random numbers Here is the code for adding two numbers and drawing a circle: Please note how the image is centred on the screen, with tpos() method. If you want to practise it interactively, here is the code: Trtl.write(' This is '+shapes, font=("Arial", 16, "bold")) #printing the name of the polygon tpos(-80,180) #moving the turtle to make the animation more centric Trtl.right(360/n) #determining the exterior angle of the polygon Shapes=įor i in range(n): # for loop to minimize the same lines of codes being written Screen.bgpic('bg.gif') #making canvas black Therefore, the code can be revised to produce the same shape with efficiency as follows: Trtl.write('Square - Vivax Solutions', font=("Arial", 16, "bold"))Īlthough, the above code produces a square, it is not good programming practice due to repetition of the code, which could have been tackled by a simple loop. This is the code for the above animation: ![]() The following animation shows how the turtle draws a square on the screen: # sign indicates the comments in Python scripts. Trtl.ht() # hiding the turtle from the screen Trtl.write('Vivax Solutions', font=("Arial", 20, "bold")) # chosing the font Trtl.pencolor('green') # choosin the pen colour as green Trtl.pendown() # bringing the pen down for drawing again tpos(-140,-120) # making the new position of the turtle ![]() Trtl.penup() # preparing for moving pen without drawing Trtl.right(90) #asking turtle to turn 90 degrees Trtl.forward(150) #drawing a line of 200 pixels Trtl.shape('turtle') #choosing the shape of pen nib Trtl.speed(1) #choosing the speed of drawing Trtl.pensize(5) #choosing the size of pen nib ![]() Trtl.pencolor('red') #making colour of the pen red Screen.bgcolor('black') #making canvas black Screen=turtle.Screen() #making a canvas for drawing Trtl = turtle.Turtle() #making a turtle object of Turtle class for drawing The documentation of Python turtle is here. This is the approach adopted in this tutorial. The best way to learn the Python turtle is running set of codes, from the simplest to the more advanced gradually, rather than making an effort to understand the simulator fully at first. Write down the code and make impressive animations.Write down import turtle at the top of the file in order to import the module - classes and methods.Load Python IDLE - Integrated Development and Learning Environment - from Windows.Download the latest version of Python from here.The following animation was created by Python Turtle the code is at the bottom of this tutorial.īefore using Python Turtle for animations, please take the following steps to install the environment: With the aid of Object Oriented Programming approach, we can create an impressive set of animations easily. Python Turtle is something that evolved from Logo programming language, invented in 1966 by Wally Feurzig. Please use Google Chrome or Mozilla FireFox to see the animations properly. ![]()
0 Comments
Leave a Reply. |