Python gui 乌龟赛跑代码
时间: 2023-08-28 09:05:22 浏览: 176
龟兔赛跑代码
好的,下面是一个简单的Python GUI乌龟赛跑的代码示例,您可以参考一下:
```python
import turtle
import random
# 创建赛道
def create_track():
track = turtle.Turtle()
track.penup()
track.goto(-200, 100)
track.pendown()
track.goto(200, 100)
track.penup()
track.goto(-200, 50)
track.pendown()
track.goto(200, 50)
track.penup()
track.goto(-200, 0)
track.pendown()
track.goto(200, 0)
track.penup()
track.goto(-200, -50)
track.pendown()
track.goto(200, -50)
track.penup()
track.goto(-200, -100)
track.pendown()
track.goto(200, -100)
# 创建乌龟
def create_turtle(color, y):
turtle = turtle.Turtle()
turtle.color(color)
turtle.shape("turtle")
turtle.penup()
turtle.goto(-200, y)
turtle.pendown()
return turtle
# 运行比赛
def run_race():
track = turtle.Screen()
track.title("Turtle Race")
# 创建赛道
create_track()
# 创建乌龟
turtle1 = create_turtle("red", 75)
turtle2 = create_turtle("blue", 25)
turtle3 = create_turtle("green", -25)
turtle4 = create_turtle("purple", -75)
# 让乌龟开始跑
for i in range(150):
turtle1.forward(random.randint(1,5))
turtle2.forward(random.randint(1,5))
turtle3.forward(random.randint(1,5))
turtle4.forward(random.randint(1,5))
# 退出程序
track.exitonclick()
# 运行游戏
run_race()
```
这是一个简单的乌龟赛跑游戏,它创建了一个赛道和四只乌龟,然后让它们开始竞争。您可以根据自己的需求来修改和定制这个游戏,例如调整乌龟的数量、速度和颜色等。
阅读全文