Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python to realize Turtle Race Mini Game

Shulou Source: shulou.com Published: 2022-06-02 02:37:17 09月13日 Update

Most people don't understand the knowledge points of this article "How to use python to realize turtle race Mini games," so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and there is certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "How to use python to realize turtle race Mini games."

code illustrates

import package

from turtle import Turtle, Screenimport random

The random function is used to generate distances (random) that are moved by turtles. It's better to give the screen size because it's easy to find the coordinates and change them accordingly.

screen = Screen()screen.setup(width=500, height=400)

There is a function called textinput() that opens a dialog box and asks for input from the user.

user_bet = screen.textinput(title="Place your bet", prompt="Which turtle will win the race? Enter a color: ")

Next, we should give our race turtle color. So we can distinguish them. And then the coordinates that should represent the race.

colors = ["red", "orange", "yellow", "green", "blue", "purple"]y_positions = [-100, -60, -20, 20, 60, 100]

By considering the y coordinates and colors above, the exact coordinates of all turtles are classified using a for loop.

for turtle_index in range(0,6): new_turtle = Turtle(shape="turtle") new_turtle.color(colors[turtle_index]) new_turtle.penup() new_turtle.goto(x=-230, y= y_positions[turtle_index]) all_turtles.append(new_turtle)

Now, the last thing we should do is have our turtles move a random distance at a time. And the turtle that gets to the other end of the screen first wins the race. At first, we bet on the turtle. If the turtle wins, we win. If he loses, we lose.

while is_race_on: for turtle in all_turtles: if turtle.xcor() > 230: is_race_on = False winning_color = turtle.pencolor() if winning_color == user_bet: print(f"You've won!, The {winning_color} turtle is the winner. ") else: print(f"You've lost!, The {winning_color} turtle is the winner. ") rand_distance = random.randint(0, 10) turtle.forward(rand_distance)

The main advantage of setting the screen width and height is that we can easily calculate the start and end coordinates by assuming that the screen is checkered paper.

output image

A. Take "red" as user input.

B. Images of how turtles move.

C. The match ended. That tells us whether we won or lost the game.

The above is about the content of this article "How to use python to realize turtle race Mini games." I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

Tags: Turtles content coordinates turtles screens races Mini Game races movements functions images that is articles users knowledge articles colors input next code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Shulou Information MySQL NVidia