Python程序 Fauna
Python程序 Fauna
Rutherford1895 发表于6个月前
Python程序 Fauna
  • 发表于 6个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0
import turtle
import time

boxsize=200
caught=False
score=0

#functions that are called on keypresses
def up():
    mouse.forward(10)
    checkbound()

def left():
    mouse.left(45)

def right():
    mouse.right(45)

def down():
    mouse.backward(10)
    checkbound()

def quitTurtles():
    window.bye()

#stop the mouse from leaving window
def checkbound():
    global boxsize
    if mouse.xcor() > boxsize:
        mouse.goto(boxsize,mouse.ycor())
    if mouse.xcor() < -boxsize:
        mouse.goto(-boxsize,mouse.ycor())
    if mouse.ycor() > boxsize:
        mouse.goto(mouse.xcor(),boxsize)
    if mouse.ycor() < -boxsize:
        mouse.goto(mouse.xcor(),-boxsize)

#setup screen
window = turtle.Screen()
mouse = turtle.Turtle()
cat= turtle.Turtle()
me=turtle.Turtle()
mouse.goto(100,100)
me.goto(-100,-100)

#add key listeners
window.onkeypress(up,"Up")
window.onkeypress(left,"Left")
window.onkeypress(right,"Right")
window.onkeypress(down,"Down")
window.onkeypress(quitTurtles, "Escape")

difficulty=window.numinput("Difficulty",
    "Enter a difficulty from 1 - 5",
    minval=1, maxval=5)

window.listen()
#mainloop
#note how it changes with difficulty
while not caught:
    cat.setheading(cat.towards(mouse))
    cat.forward(8+difficulty)
    me.setheading(me.towards(cat))
    me.forward(10+difficulty)
    score = score+1
    if cat.distance(mouse)<5:
        caught=True
    elif me.distance(cat)<5:
        caught=True
    time.sleep(0.2-(0.01*difficulty))
window.textinput("GG","Welldone. You scored:"
        + str(score*difficulty))
window.bye()

 

标签: Python
共有 人打赏支持
粉丝 0
博文 2
码字总数 234
×
Rutherford1895
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: