Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create a simple Animation using Turtle in Python
Next article icon

Prerequisite:Python Turtle Basics

Turtleis an inbuilt module in python. It provides drawing using a screen (cardboard) and turtle (pen). To draw something on the screen, we need to move the turtle. To move turtle, there are some functions i.e forward(), backward(), etc.

1.)Move the Object (ball)  :

Following steps are used:

  • Import Turtle package.
  • Set screen with dimensions and color.
  • Form turtle object with color.
  • Form the object (ball - made of colored circle).
  • Call the function for making object again and again and clear the screen.

Below is the implementation  :

Python3
# import turtle packageimportturtle# function for movement of an objectdefmoving_object(move):# to fill the color in ballmove.fillcolor('orange')# start color fillingmove.begin_fill()# draw circlemove.circle(20)# end color fillingmove.end_fill()# Driver Codeif__name__=="__main__":# create a screen objectscreen=turtle.Screen()# set screen sizescreen.setup(600,600)# screen background colorscreen.bgcolor('green')# screen updaionscreen.tracer(0)# create a turtle object objectmove=turtle.Turtle()# set a turtle object colormove.color('orange')# set turtle object speedmove.speed(0)# set turtle object widthmove.width(2)# hide turtle objectmove.hideturtle()# turtle object in airmove.penup()# set initial positionmove.goto(-250,0)# move turtle object to surfacemove.pendown()# infinite loopwhileTrue:# clear turtle workmove.clear()# call function to draw ballmoving_object(move)# update screenscreen.update()# forward motion by turtle objectmove.forward(0.5)

Output :

Example 2:  Move the Object (box)

Following steps are used:

  • Import Turtle package.
  • Set screen with dimensions and color.
  • Form turtle object with color.
  • Form the object (box - made of colored square).
  • Call the function for making object again and again and clear the screen.

Below is the implementation:-

Python3
importturtlescreen=turtle.Screen()screen.setup(500,500)screen.bgcolor('Green')# tell screen to not# show automaticallyscreen.tracer(0)t=turtle.Turtle()t.speed(0)t.width(3)# hide donatello, we# only want to see the drawingt.hideturtle()defdraw_square():t.fillcolor("Orange")t.begin_fill()forsideinrange(4):t.forward(100)t.left(90)t.end_fill()t.penup()t.goto(-350,0)t.pendown()whileTrue:t.clear()draw_square()# only now show the screen,# as one of the framesscreen.update()t.forward(0.02)# This code is contributed by pulkitagarwal03pulkit

Output:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp