当前位置:   article > 正文

Python+turtle实现一个乌龟逃跑小游戏(可以和孩子一起完成)_turtle好玩的游戏

turtle好玩的游戏

直接上演示视频

请添加图片描述

这个代码也是之前当老师的时候,给孩子们写的一个小游戏,那么我们一起看一下这个小游戏是如何让完成的

1、首先完成代码的前期准备

1、这里我们t = turtle.Pen() # 海龟—表示我们操作的小海龟
2、enemy = turtle.Pen() # 敌龟—表示追击我们的小海龟
3、enemy.shape(“triangle”) # 敌龟,这个是小海归的形状
4、‘arrow’, ‘turtle’, ‘circle’, ‘square’, ‘triangle’, ‘classic’,这是小海归常见的形状,箭头形,海龟形,圆形,正方形、三角形、 经典形

enemy.color(“red”) # 敌龟颜色为红色

import turtle, random


window = turtle.Screen() # 创建屏幕对象
t = turtle.Pen()  # 海龟

enemy = turtle.Pen()  # 敌龟
enemy.pensize(8)  # 敌龟
enemy.color("red")  # 敌龟
enemy.shape("triangle")  # 敌龟

t.shape("turtle")
t.penup()  # 将海龟的画笔提起,使不会出现轨迹
t.goto(100, 100)  # 海龟的初始地址放置在坐标(100,100)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

2、设置一个边框,防止小海龟跑出窗口之外

def checkbound():
    boxsize = 300
    if t.xcor() > boxsize:
        t.goto(boxsize, t.ycor())

    if t.xcor() < -boxsize:
        t.goto(-boxsize, t.ycor())

    if t.ycor() > boxsize:
        t.goto(t.xcor(), boxsize)

    if t.ycor() < -boxsize:
        t.goto(t.xcor(), boxsize)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

3、写4个函数

up()和back().分别是前进和后退
left()和right(),分别是左转和右转,角度均为45°

# 前进
def up():
    t.forward(10)
    checkbound()


# 左转
def left():
    t.left(45)


# 右转
def right():
    t.right(45)


# 后退
def back():
    t.bk(10)
    checkbound()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

瞬移,这是我加上去的

# 瞬移
def teleport():
    t.goto(random.randint(-200, 200), random.randint(-200, 200))
  • 1
  • 2
  • 3

4、窗口对象绑定键盘按键

window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(teleport, "space")
# 窗体监听按键
window.listen()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

5、最后写上主循环

1、enemy.seth(enemy.towards(t)),求出小海龟与敌龟的角度
2、enemy.distance(t) < 5,小海龟与敌龟的距离小于5则表示 输掉游戏
3、t.write("死", align="center", font=(r"C:\Windows\Fonts\STHUPO.TTF", 50, "bold")),小海龟将会写一个“死”字
上一行的参数:t.write("这里是描述的文本", align="center"表示居中, font=(r"C:\Windows\Fonts\STHUPO.TTF"这是字体样式, 50字号, "bold"加粗))
caught = False
while caught == False:
    enemy.seth(enemy.towards(t))
    enemy.fd(1)
    if enemy.distance(t) < 5:
        caught = True
        print("你被敌人抓住了!!!!")
        t.pencolor("red")
        t.write("死", align="center", font=(r"C:\Windows\Fonts\STHUPO.TTF", 50, "bold"))
    if enemy.distance(t) < 20:
        t.fillcolor("red")
    elif enemy.distance(t) < 50:
        t.fillcolor("yellow")
    else:
        t.fillcolor("green")
turtle.done()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

完整版代码

import turtle, random


window = turtle.Screen()
t = turtle.Pen()  # 海龟

enemy = turtle.Pen()  # 敌龟
enemy.pensize(8)  # 敌龟
enemy.color("red")  # 敌龟
enemy.shape("triangle")  # 敌龟

t.shape("turtle")
t.penup()  # 将海龟的画笔提起,使不会出现轨迹
t.goto(100, 100)  # 海龟的初始地址放置在坐标(100,100)


# 按动方向键Up则执行函数up
def checkbound():
    boxsize = 300
    if t.xcor() > boxsize:
        t.goto(boxsize, t.ycor())

    if t.xcor() < -boxsize:
        t.goto(-boxsize, t.ycor())

    if t.ycor() > boxsize:
        t.goto(t.xcor(), boxsize)

    if t.ycor() < -boxsize:
        t.goto(t.xcor(), boxsize)


# 老鼠前进
def up():
    t.forward(10)
    checkbound()


# 老鼠左转
def left():
    t.left(45)


# 老鼠右转
def right():
    t.right(45)


# 老鼠后退
def back():
    t.bk(10)
    checkbound()


# 瞬移
def teleport():
    t.goto(random.randint(-200, 200), random.randint(-200, 200))


window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(teleport, "space")
# 窗体监听按键
window.listen()

caught = False
while caught == False:
    enemy.seth(enemy.towards(t))
    enemy.fd(1)
    if enemy.distance(t) < 5:
        caught = True
        print("你被敌人抓住了!!!!")
        t.pencolor("red")
        t.write("死", align="center", font=(r"C:\Windows\Fonts\STHUPO.TTF", 50, "bold"))
    if enemy.distance(t) < 20:
        t.fillcolor("red")
    elif enemy.distance(t) < 50:
        t.fillcolor("yellow")
    else:
        t.fillcolor("green")
turtle.done()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84

上一届内容:python+turtle实现夜空小星星

非常简单的小代码,爸爸妈妈们可以教给孩子们,可以一起共同完成

希望能够帮助到你

拜拜!!声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】

推荐阅读
相关标签