Commit 0dea1e58 by BellCodeEditor

save project

parent b4e39faf
Showing with 74 additions and 0 deletions
#导入turtlt库
import turtle as x
#创建海龟画图窗口
x.TurtleScreen._RUNNING = True
#设置窗口大小
x.setup(width=960,height=720)
#设置窗口颜色
x.bgcolor("white")
#创建海龟
turtle_1=x.Turtle()
turtle_2=x.Turtle()
turtle_1.shape("turtle")
turtle_2.shape("turtle")
turtle_1.goto (100,100)
#改变画笔图标大小
turtle_1.shapesize(2)
turtle_2.shapesize(2)
#改变颜色
turtle_1.color('gray')
turtle_2.color('pink')
#乌龟前进
#限制活动范围
def front():
turtle_1.forward(10)
if turtle_1.xcor()>200:
turtle_1.goto(200,turtle_1.ycor())
if turtle_1.xcor()<-200:
turtle_1.goto (-200,turtle_1.ycor())
if turtle_1.ycor()>200:
turtle_1.goto(turtle_1.xcor(),200)
if turtle_1.ycor()<-200:
turtle_1.goto(turtle_1.xcor(),-200)
def back():
turtle_1.backward(10)
def left():
turtle_1.left(45)
def right():
turtle_1.right(45)
#按下w键前进
x.onkeypress(front,'w')
x.onkeypress(back,'s')
x.onkeypress(left,'a')
x.onkeypress(right,'d')
#窗口监听
x.listen()
#抬起画笔
turtle_1.penup()
#向着乌龟方向
while True:
turtle_2.setheading(turtle_2.towards(turtle_1))
#乌龟2移动
turtle_2.forward(5)
#判断两只乌龟的距离
if turtle_2.distance(turtle_1)<50:
x.bye()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment