直接上演示视频
这个代码也是之前当老师的时候,给孩子们写的一个小游戏,那么我们一起看一下这个小游戏是如何让完成的
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)
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)
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()
瞬移,这是我加上去的
# 瞬移
def teleport():
t.goto(random.randint(-200, 200), random.randint(-200, 200))
4、窗口对象绑定键盘按键
window.onkeypress(up, "Up")
window.onkeypress(left, "Left")
window.onkeypress(right, "Right")
window.onkeypress(back, "Down")
window.onkeypress(teleport, "space")
# 窗体监听按键
window.listen()
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()
完整版代码
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()