Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create a Simple Two Player Game using Turtle in Python
Next article icon

Turtle is a built-in Python module that provides a simple way to draw and create graphics using a virtual turtle on the screen. You can control the turtle using commands like forward() and right() to move it around and draw shapes. In this article, we'll use Turtle to create a fun animation where multiple turtles race on a track. Let's understand the steps to achieve this.

Below is the implementation:

Python
fromturtleimport*fromrandomimportrandint# Draw the racing trackspeed(0)penup()goto(-140,140)forstepinrange(15):write(step,align='center')right(90)fordashinrange(8):penup()forward(10)pendown()forward(10)penup()backward(160)left(90)forward(20)# Create turtle racerscolors=['red','blue','green','orange']y_positions=[100,70,40,10]players=[]foriinrange(4):racer=Turtle()racer.color(colors[i])racer.shape('turtle')racer.penup()racer.goto(-160,y_positions[i])racer.pendown()# Little spin before raceforturninrange(36):racer.right(10)players.append(racer)# Start the raceformoveinrange(100):forturtleinplayers:turtle.forward(randint(1,5))

Output

Output
Output

Explanation:

  • Set the turtle's speed to the fastest so the race track draws quickly.
  • Move the turtle to the top-left corner of the screen to start drawing.
  • Draw 15 vertical lines to create the race track, numbering each one.
  • Each line has dashed marks to look like lanes on a track.
  • After drawing each line, the turtle shifts right to draw the next one.
  • Make a list of four different colors for the racing turtles.
  • Set different vertical positions so each turtle has its own track lane.
  • Create four uniquely colored turtles, position them in separate lanes at the start and spin each once before the race.
  • Start the race with 100 loops, moving each turtle forward randomly to make it unpredictable.

Related Articles:


Improve
Article Tags :
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