python龟兔赛跑小游戏代码
时间: 2023-12-11 12:33:59 浏览: 102
Python推箱子小游戏源代码
5星 · 资源好评率100%
以下是一个简单的Python龟兔赛跑小游戏代码,使用了turtle库和random库:
```python
import turtle
import random
# 设置窗口大小和背景颜色
turtle.setup(800, 600)
turtle.bgcolor('white')
# 画出终点线
turtle.penup()
turtle.goto(300, 200)
turtle.pendown()
turtle.goto(300, -200)
# 画出起点线
turtle.penup()
turtle.goto(-300, 200)
turtle.pendown()
turtle.goto(-300, -200)
# 画出乌龟和兔子
turtle.penup()
turtle.goto(-250, 0)
turtle.pendown()
turtle.shape('turtle')
turtle.color('green')
turtle.penup()
turtle.goto(-250, -50)
turtle.pendown()
turtle.shape('turtle')
turtle.color('blue')
turtle.penup()
turtle.goto(-250, -100)
turtle.pendown()
turtle.shape('turtle')
turtle.color('red')
turtle.penup()
turtle.goto(-250, -150)
turtle.pendown()
turtle.shape('turtle')
turtle.color('purple')
turtle.penup()
turtle.goto(-250, -200)
turtle.pendown()
turtle.shape('turtle')
turtle.color('orange')
turtle.penup()
turtle.goto(-250, 50)
turtle.pendown()
turtle.shape('turtle')
turtle.color('brown')
turtle.penup()
turtle.goto(-250, 100)
turtle.pendown()
turtle.shape('turtle')
turtle.color('black')
turtle.penup()
turtle.goto(-250, 150)
turtle.pendown()
turtle.shape('turtle')
turtle.color('gray')
turtle.penup()
turtle.goto(-250, 200)
turtle.pendown()
turtle.shape('turtle')
turtle.color('pink')
# 开始比赛
while True:
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, -50)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, -100)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, -150)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, -200)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, 50)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, 100)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, 150)
turtle.pendown()
turtle.forward(random.randint(1, 5))
turtle.penup()
turtle.goto(-250, 200)
turtle.pendown()
turtle.forward(random.randint(1, 5))
# 判断是否有乌龟或兔子到达终点
if turtle.xcor() >= 300:
turtle.penup()
turtle.goto(0, 0)
turtle.write('乌龟赢了!', font=('Arial', 30, 'normal'), align='center')
break
elif turtle2.xcor() >= 300:
turtle.penup()
turtle.goto(0, 0)
turtle.write('兔子赢了!', font=('Arial', 30, 'normal'), align='center')
break
# 相关问题:
--相关问题--:
阅读全文