猫史档案馆


【Python作品分享】猫和老鼠

用户:S号舰长竹子S号舰长竹子查看:0 回复:2 评论:0 创建时间:2021-02-03T10:21:39


【作品展示】

center_image

 

【作品介绍】

上下左右

 

【作品源代码】

import turtle
import time

s = turtle.Screen()
s.setup(1000,1000)
s.title("猫和老鼠")
s.bgpic("背景.gif")
s.addshape("猫.gif")
s.addshape("老鼠.gif")
s.tracer(0)

cat = turtle.Pen()
cat.shape("猫.gif")
cat.penup()

mouse = turtle.Pen()
mouse.shape("老鼠.gif")
mouse.penup()
mouse.goto(200,-200)

def up():
    mouse.setheading(90)
    mouse.forward(50)

def down():
    mouse.setheading(270)
    mouse.forward(50)

def left():
    mouse.setheading(180)
    mouse.forward(50)

def right():
    mouse.setheading(0)
    mouse.forward(50)

s.listen()
s.onkeypress(up,"Up")
s.onkeypress(down,"Down")
s.onkeypress(left,"Left")
s.onkeypress(right,"Right")

e = turtle.Pen()
e.hideturtle()

game = 0
playing = True
while playing:
    angle = cat.towards(mouse)
    cat.setheading(angle)
    cat.forward(1)
    if cat.distance(mouse.xcor(),mouse.ycor()) < 5:
        playing = False
    game += 1
    e.write(f"Score:{game}",font = ("思源黑体",30,"normal"),align = "center")
    s.update()
    time.sleep(0.01)
    e.clear()

e.pencolor("red")
e.write(f"Game Over,Score:{game}",font = ("思源黑体", 30, "normal"), align = "center")

turtle.done()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
BZIClawLYNEYBZIClawLYNEY

加油!

emotion_编程猫_加油

点赞0


评论


小邓学编程小邓学编程

我见过用turtle写游戏的,你是第一个

点赞0


评论