上期在一小时内被我用流量券顶到了20 后面就没啥亮点了
_____________________________________________________________________________
老规矩 先放代码(20行代码以下代码段为准):
import turtle,random;turtle.tracer(0);bg2=turtle.Screen();bg=turtle.Turtle();dg=turtle.Turtle();xg=turtle.Turtle();word=turtle.Turtle();bg2.bgcolor('sky blue')
bg2.setup(600,600);bg.speed(0);bg.ht();word.ht();word.color('red');dg.shape('turtle');dg.shapesize(1.5);dg.pu();dg.speed(0);xg.shape('turtle');xg.pu();xg.speed(0)
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90);word.pu();word.goto(-300,0);word.pu();x,y=random.randint(-14,14),random.randint(-14,14);xg.goto(x*20,y*20)
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
def go(pen,x1,y1):
global xg,dg;pen.goto(x1, y1);xgx,xgy=xg.pos();dgx,dgy=dg.pos();turtle.update()
if abs(xgx)>300 or abs(xgy)>300:xg.reset();xg.pu();xg.speed(0)
if abs(dgx)>300 or abs(dgy)>300:dg.reset();dg.pu();dg.speed(0);dg.shapesize(1.5)
if round(dgx)==round(xgx) and round(dgy)==round(xgy):xg.ht();turtle.update();word.write('小乌龟牺牲',font=('微软雅黑',90));turtle.done();xg.done();dg.done()
def goLeft():x,y=dg.pos();dg.seth(180);go(dg,x-20,y)
def goUp():x,y=dg.pos();dg.seth(90);go(dg,x,y+20)
def goRight():x,y=dg.pos();dg.seth(0);go(dg,x+20,y)
def goDown():x,y=dg.pos();dg.seth(270);go(dg,x,y-20)
def goW():x,y=xg.pos();xg.seth(90);go(xg,x,y+20)
def goS():x,y=xg.pos();xg.seth(270);go(xg,x,y-20)
def goA():x,y=xg.pos();xg.seth(180);go(xg,x-20,y)
def goD():x,y=xg.pos();xg.seth(0);go(xg,x+20,y)
turtle.listen();turtle.onkeypress(goW,'w');turtle.onkeypress(goS,'s');turtle.onkeypress(goD,'d');turtle.onkeypress(goA,'a')
turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')
_____________________________________________________________________________
import turtle,random;turtle.tracer(0);bg2=turtle.Screen();bg=turtle.Turtle();dg=turtle.Turtle();xg=turtle.Turtle();word=turtle.Turtle();bg2.bgcolor('sky blue')
bg2.setup(600,600);bg.speed(0);bg.ht();word.ht();word.color('red');dg.shape('turtle');dg.shapesize(1.5);dg.pu();dg.speed(0);xg.shape('turtle');xg.pu();xg.speed(0)
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90);word.pu();word.goto(-300,0);word.pu();x,y=random.randint(-14,14),random.randint(-14,14);xg.goto(x*20,y*20)
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
def go(pen,x1,y1):
global xg,dg;pen.goto(x1, y1);xgx,xgy=xg.pos();dgx,dgy=dg.pos();turtle.update()
if abs(xgx)>300 or abs(xgy)>300:xg.reset();xg.pu();xg.speed(0)
if abs(dgx)>300 or abs(dgy)>300:dg.reset();dg.pu();dg.speed(0);dg.shapesize(1.5)
if round(dgx)==round(xgx) and round(dgy)==round(xgy):xg.ht();turtle.update();word.write('小乌龟牺牲',font=('微软雅黑',90));turtle.done();xg.done();dg.done()
def goLeft():x,y=dg.pos();dg.seth(180);go(dg,x-20,y)
def goUp():x,y=dg.pos();dg.seth(90);go(dg,x,y+20)
def goRight():x,y=dg.pos();dg.seth(0);go(dg,x+20,y)
def goDown():x,y=dg.pos();dg.seth(270);go(dg,x,y-20)
def goW():x,y=xg.pos();xg.seth(90);go(xg,x,y+20)
def goS():x,y=xg.pos();xg.seth(270);go(xg,x,y-20)
def goA():x,y=xg.pos();xg.seth(180);go(xg,x-20,y)
def goD():x,y=xg.pos();xg.seth(0);go(xg,x+20,y)
turtle.listen();turtle.onkeypress(goW,'w');turtle.onkeypress(goS,'s');turtle.onkeypress(goD,'d');turtle.onkeypress(goA,'a')
turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')
_____________________________________________________________________________
第一步.基础设置
import turtle,random#导入模块
turtle.tracer(0)#拼命加速
#画笔bg2的设置
bg2=turtle.Screen()#创建画布bg2
bg2.bgcolor('sky blue')#把背景颜色设置成天蓝色
bg2.setup(600,600)#把背景大小设置成600像素*600像素
#画笔bg的设置
bg=turtle.Turtle()#创建画笔bg
bg.speed(0)把画笔bg的速度改成最快
bg.ht()#隐藏画笔bg
bg.left(90)#让画笔bg向左转90度
#画笔word的设置
word.turtle.Turtle()#创建画笔word
word.ht()#把画笔word隐藏
word.color('red')#把画笔word画出的颜色改为红色
word.pu()#不让画笔word留下痕迹
word.goto(-300,0)#让画笔word去-300,0的位置
#画笔word的设置
dg.turtle.Turtle()#创建画笔bg
dg.shape('turtle')#把画笔bg的形状设置为海龟
dg.shapesize(1.5)#把画笔bg大小改为1.5倍
dg.pu()#不让画笔dg留下痕迹
dg.speed(0)#把画笔dg的速度设为最快
#画笔xg的设置
xg.turtle.Turtle()#创建画笔xg
xg.shape('turtle')#把画笔xg的形状设置为海龟
xg.pu()#不让画笔xg留下痕迹
xg.speed(0)#把速度设置为最快
x,y=random.randint(-14,14),random.randint(-14,14)#随机横线与纵线交叉点
xg.goto(x*20,y*20)#保证x,y轴坐标都是20的倍数
_____________________________________________________________________________
第二步.画辅助线
for i in range(31):#画31条线
bg.pu()#不让画笔bg留下痕迹
bg.goto(-300,-300+i*20)
bg.pd()#让画笔bg重新留下痕迹
bg.fd(600)#让画笔bg前进600像素
bg.left(90)#让画笔bg左转90度
for j in range(31):
bg.pu()#不让画笔bg重新留下痕迹
bg.goto(-300+j*20,300)
bg.pd()#让画笔bg重新留下痕迹
bg.bk(600)#让画笔bg前进600像素
第三步.跑路模版
def go(pen,x1,y1):#定义跑路函数
global xg,dg#让这个函数里可以使用xg,dg画笔
pen.goto(x1, y1)#去x1,y1处
xgx,xgy=xg.pos()#获取画笔xg位置
dgx,dgy=dg.pos()#获取画笔dg位置
turtle.update()#刷新画面
if abs(xgx)>300 or abs(xgy)>300:#判断画笔xg是否撞墙 abs(number)=number的绝对值(abs(-300)=300 abs(300)=300)
xg.reset()#重新定义画笔xg
xg.pu()#不让画笔xg留下痕迹
xg.speed(0)#把画笔xg的速度设置为最快
if abs(dgx)>300 or abs(dgy)>300:#判断画笔dg是否撞墙 abs(number)=number的绝对值(abs(-300)=300 abs(300)=300)
dg.reset()#重新设置画笔dg
dg.pu()#不让画笔dg留下痕迹
dg.speed(0)#把画笔dg的速度设置为最快
dg.shapesize(1.5)#把dg画笔的大小设置为1.5
if round(dgx)==round(xgx) and round(dgy)==round(xgy):#判断小乌龟是否被大乌龟吃
xg.ht()#隐藏画笔xg
turtle.update()#刷新画面
word.write('小乌龟牺牲',font=('微软雅黑',90))#小乌龟趋势提示
turtle.done()#停止turtle作画
xg.done()#停止画笔xg的画面
dg.done()#停止画笔dg的画面
_____________________________________________________________________________
第四步.大小乌龟跑路函数
def goLeft():#定义函数goLeft
x,y=dg.pos()#获取画笔dg位置
dg.seth(180)#让画笔dg朝向正左边
go(dg,x-20,y)#让画笔dg去左边一格
def goUp():#定义函数goUp
x,y=dg.pos()#获得画笔dg位置
dg.seth(90)#让画笔dg朝向正上方
go(dg,x,y+20)#让画笔dg去上面一格
def goRight():#定义函数goRight
x,y=dg.pos()#获得画笔dg位置
dg.seth(0)#让画笔dg朝向正右边
go(dg,x+20,y)#让画笔dg去右边一格
def goDown():#定义函数goDown
x,y=dg.pos()#获得画笔dg位置
dg.seth(270)#让画笔朝向正下方
go(dg,x,y-20)#让画笔dg去下面一格
def goW():#定义函数goW
x,y=xg.pos()#获得画笔xg位置
xg.seth(90)#让画笔xg朝向正上方
go(xg,x,y+20)#让画笔xg去上面一格
def goS():#定义函数goS
x,y=xg.pos()#获得画笔xg位置
xg.seth(270)#让画笔xg朝向正下方
go(xg,x,y-20)让画笔xg去下面一格
def goA():#定义函数goA
x,y=xg.pos()#获得画笔xg位置
xg.seth(180)#让画笔xg朝向正左边
go(xg,x-20,y)#让画笔xg去左边一格
def goD():#定义函数goD
x,y=xg.pos()#获得画笔xg位置
xg.seth(0)#让画笔xg朝向正右边
go(xg,x+20,y)#让画笔xg去右边一格
_____________________________________________________________________________
第五步.监听键盘
turtle.listen()#开始监听
turtle.onkeypress(goW,'w')
turtle.onkeypress(goS,'s')
turtle.onkeypress(goD,'d')
turtle.onkeypress(goA,'a')
turtle.onkeypress(goLeft,'Left')
turtle.onkeypress(goRight,'Right')
turtle.onkeypress(goDown,'Down')
turtle.onkeypress(goUp,'Up')
MVP结算:
本期字数:6348
本期质量分:79
10赞花絮 15赞Python 25赞c++
标签:turtle,bg,20,画笔,Python,dg,xg From: https://blog.csdn.net/kgxgfrb/article/details/140587467