Python一日一练18----用turtle写猫和老鼠小游戏

要求

用turtle写一个猫和老鼠的小游戏,要求通过鼠标控制老鼠的移动,逃出电脑控制的猫的追捕。

源码如下

import time
import turtle

boxsize=200
caught=False
score=0
#老鼠前进
def up():
    mouse.forward(10)
    checkbound()
#老鼠左转
def left():
    mouse.left(45)
#老鼠右转
def right():
    mouse.right(45)
#老鼠后退
def back():
    mouse.backward(10)
    checkbound()
#结束游戏
def quitTurtle():
    window.bye()

#检测老鼠是否跑过界
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)

#
window=turtle.Screen()
mouse=turtle.Turtle()
cat=turtle.Turtle()
mouse.penup()                   #将老鼠的画笔提起,使不会出现轨迹
mouse.goto(100,100)             #老鼠的初始地址放置在坐标(100,100)

window.onkeypress(up,"Up")      #按动方向键Up则执行函数up
window.onkeypress(left,"Left")
window.onkeypress(right,"Right")
window.onkeypress(back,"Down")
window.onkeypress(quitTurtle,"Escape")

#产生一个输入难度的对话框
difficulty=window.numinput("难度","请输入游戏的难度(1~5)",minval=1,maxval=5)
#窗体监听按键
window.listen()

while not caught:
    cat.setheading(cat.towards(mouse))   #猫调整自己方向,使自己正对老鼠
    cat.forward(8+difficulty)
    score+=1
    if cat.distance(mouse)<5:            #老鼠与猫的距离少于5个像素就输了
        caught=True
    time.sleep(0.2-(0.01*difficulty))    #难度越高,运行速度越快
window.textinput("GAME OVER","游戏得分:"+str(score*difficulty))
window.bye()    

源码下载:http://download.csdn.net/detail/a359680405/9501893
本文地址:http://blog.csdn.net/a359680405/article/details/51242212

  • 9
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,让我们开始吧!首先,我们需要导入 `turtle` 库: ```python import turtle ``` 接下来,我们需要设置窗口大小和背景色: ```python turtle.setup(600, 600) turtle.bgcolor("white") ``` 然后,我们可以开始绘制小库洛米了。首先,我们需要定义一个函数来绘制头部: ```python def draw_head(): turtle.penup() turtle.goto(0, 200) turtle.pendown() turtle.begin_fill() turtle.circle(150) turtle.end_fill() ``` 这个函数会绘制一个半径为 150 的圆形,作为小库洛米的头部。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 接下来,我们需要绘制眼睛。我们可以定义一个函数来绘制一个眼睛: ```python def draw_eye(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.begin_fill() turtle.circle(30) turtle.end_fill() ``` 这个函数会绘制一个半径为 30 的圆形,作为小库洛米的眼睛。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 现在,我们可以在头部上绘制两个眼睛了: ```python draw_eye(-60, 280) draw_eye(60, 280) ``` 接下来,我们需要绘制小库洛米的嘴巴。我们可以定义一个函数来绘制一个嘴巴: ```python def draw_mouth(): turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.setheading(-60) turtle.circle(80, 120) ``` 这个函数会绘制一个弧形,作为小库洛米的嘴巴。 现在,我们可以在头部上绘制嘴巴了: ```python draw_mouth() ``` 接下来,我们需要绘制小库洛米的身体。我们可以定义一个函数来绘制一个身体: ```python def draw_body(): turtle.penup() turtle.goto(0, -150) turtle.pendown() turtle.begin_fill() turtle.circle(200) turtle.end_fill() ``` 这个函数会绘制一个半径为 200 的圆形,作为小库洛米的身体。我们使用 `begin_fill()` 和 `end_fill()` 函数来填充内部颜色。 现在,我们可以在窗口中绘制小库洛米了: ```python draw_head() draw_eye(-60, 280) draw_eye(60, 280) draw_mouth() draw_body() ``` 最后,我们可以隐藏笔,表示绘制完成: ```python turtle.hideturtle() ``` 完整代码如下: ```python import turtle def draw_head(): turtle.penup() turtle.goto(0, 200) turtle.pendown() turtle.begin_fill() turtle.circle(150) turtle.end_fill() def draw_eye(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.begin_fill() turtle.circle(30) turtle.end_fill() def draw_mouth(): turtle.penup() turtle.goto(0, 100) turtle.pendown() turtle.setheading(-60) turtle.circle(80, 120) def draw_body(): turtle.penup() turtle.goto(0, -150) turtle.pendown() turtle.begin_fill() turtle.circle(200) turtle.end_fill() turtle.setup(600, 600) turtle.bgcolor("white") draw_head() draw_eye(-60, 280) draw_eye(60, 280) draw_mouth() draw_body() turtle.hideturtle() turtle.done() ``` 运行代码,你会看到一个卡通小库洛米出现在窗口中。祝绘愉快!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值