"爷爷 您关注的主播终于更新了"一位不太年轻的年轻人对一位九旬老大爷说。
"孙子 我从你这么大就开始关注他了 现在终于更新了"九旬老大爷说。
_____________________________________________________________________________
最近断网 所以停更了一段时间
由于最近看到有许多人做坦克大战都要用图片或者Pygame
因为想想那么多天没更新了 所以一不做二不休憋了波大的
玩法介绍:
红色坦克移动: W键向上 S键向下 A键向左 D键向右
蓝色坦克移动:上键向上 下键(无特殊含义)向下 左键向左 右键向右
红色坦克发射键: F键
蓝色坦克发射键: L键
老规矩:天龙八步走
PS:本文除了带注释的代码段以外 都有未登录的复制渠道
40行代码以下为准(完全展开的已放进结尾):
import turtle,random,time;turtle.tracer(0);bg2=turtle.Screen();bg2.bgcolor('green');bg2.setup(600,600);bg=turtle.Turtle();bg.ht();tk1f=0;tk2f=0
def score(tf):
if tf:global word;word.ht();word.clear()
word=turtle.Turtle();word.color('red');word.pu();word.goto(-40,0);word.write(str(tk2f),font=('微软雅黑',20));word.color('black');word.goto(0,0);word.write(':',font=('微软雅黑',20));word.color('cyan');word.goto(40,0);word.write(str(tk1f),font=('微软雅黑',20));word.ht()
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90);score(0);tk1=turtle.Turtle();tk1.color('cyan');tk1x=random.randint(-10,10);tk1y=random.randint(-10,10);tk1jd=random.randint(0,3);tk1.seth(tk1jd*90);tk1.pu();tk1.goto(tk1x*20,tk1y*20);tk1.pd();tk2=turtle.Turtle()
tk2.color('red');tk2x=random.randint(-10,10);tk2y=random.randint(-10,10);tk2jd=random.randint(0,3);tk2.seth(tk2jd*90);tk2.pu();tk2.goto(tk2x*20,tk2y*20);tk2.pd();turtle.update();word=turtle.Turtle();word.ht()
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
def tk1fs():
global tk1,tk2,tk1f;zd=turtle.Turtle();zd.shapesize(0.8);zd.pu();x,y=tk1.pos();zd.goto(x,y);zd.seth(tk1.heading())
for i in range(30):
x,y=tk2.pos();x1,y1=zd.pos()
if x1==x and y1==y:tk2.ht();tk2.clear();del tk2;tk2=turtle.Turtle();tk2.color('red');tk2x=random.randint(-10,10);tk2y=random.randint(-10,10);tk2jd=random.randint(0,3);tk2.seth(tk2jd*90);tk2.pu();tk2.goto(tk2x*20,tk2y*20);tk2.pd();tk1f+=1;score(1)
zd.fd(20);time.sleep(0.01);turtle.update()
zd.ht();turtle.update()
def tk2fs():
global tk1,tk2,tk2f;zd=turtle.Turtle();zd.shapesize(0.8);zd.pu();x,y=tk2.pos();zd.goto(x,y);zd.seth(tk2.heading())
for i in range(30):
x,y=tk1.pos();x1,y1=zd.pos()
if x1==x and y1==y:tk1.ht();tk1.clear();del tk1;tk1=turtle.Turtle();tk1.color('cyan');tk1x=random.randint(-10,10);tk1y=random.randint(-10,10);tk1jd=random.randint(0,3);tk1.seth(tk1jd*90);tk1.pu();tk1.goto(tk1x*20,tk1y*20);tk1.pd();tk2f+=1;score(1)
zd.fd(20);time.sleep(0.01);turtle.update()
zd.ht();turtle.update()
def go(x1,y1):
global tk1;tk1.goto(x1,y1)
if round(abs(tk1.xcor()))>300 or round(abs(tk1.ycor()))>300:tk1.clear();tk1.ht();del tk1;tk1=turtle.Turtle();tk1.color('cyan')
turtle.update()
def went(x1,y1):
global tk2;tk2.goto(x1,y1)
if round(abs(tk2.xcor()))>300 or round(abs(tk2.ycor()))>300:tk2.clear();tk2.ht();del tk2;tk2=turtle.Turtle();tk2.color('red')
turtle.update()
def goLeft():x,y=tk1.pos();tk1.seth(180);go(x-20,y)
def goUp():x,y=tk1.pos();tk1.seth(90);go(x,y+20)
def goRight():x,y=tk1.pos();tk1.seth(0);go(x+20,y)
def goDown():x,y=tk1.pos();tk1.seth(270);go(x,y-20)
def goW():x,y=tk2.pos();tk2.seth(90);went(x,y+20)
def goS():x,y=tk2.pos();tk2.seth(270);went(x,y-20)
def goA():x,y=tk2.pos();tk2.seth(180);went(x-20,y)
def goD():x,y=tk2.pos();tk2.seth(0);went(x+20,y)
turtle.listen();turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')
turtle.onkeypress(goW,'w');turtle.onkeypress(goS,'s');turtle.onkeypress(goD,'d');turtle.onkeypress(goA,'a');turtle.onkeypress(tk1fs,'l');turtle.onkeypress(tk2fs,'f')
_____________________________________________________________________________
未登录的复制渠道:
import turtle,random,time;turtle.tracer(0);bg2=turtle.Screen();bg2.bgcolor('green');bg2.setup(600,600);bg=turtle.Turtle();bg.ht();tk1f=0;tk2f=0
def score(tf):
if tf:global word;word.ht();word.clear()
word=turtle.Turtle();word.color('red');word.pu();word.goto(-40,0);word.write(str(tk2f),font=('微软雅黑',20));word.color('black');word.goto(0,0);word.write(':',font=('微软雅黑',20));word.color('cyan');word.goto(40,0);word.write(str(tk1f),font=('微软雅黑',20));word.ht()
for i in range(31):bg.pu();bg.goto(-300,-300+i*20);bg.pd();bg.fd(600)
bg.left(90);score(0);tk1=turtle.Turtle();tk1.color('cyan');tk1x=random.randint(-10,10);tk1y=random.randint(-10,10);tk1jd=random.randint(0,3);tk1.seth(tk1jd*90);tk1.pu();tk1.goto(tk1x*20,tk1y*20);tk1.pd();tk2=turtle.Turtle()
tk2.color('red');tk2x=random.randint(-10,10);tk2y=random.randint(-10,10);tk2jd=random.randint(0,3);tk2.seth(tk2jd*90);tk2.pu();tk2.goto(tk2x*20,tk2y*20);tk2.pd();turtle.update();word=turtle.Turtle();word.ht()
for j in range(31):bg.pu();bg.goto(-300+j*20,300);bg.pd();bg.bk(600)
def tk1fs():
global tk1,tk2,tk1f;zd=turtle.Turtle();zd.shapesize(0.8);zd.pu();x,y=tk1.pos();zd.goto(x,y);zd.seth(tk1.heading())
for i in range(30):
x,y=tk2.pos();x1,y1=zd.pos()
if x1==x and y1==y:tk2.ht();tk2.clear();del tk2;tk2=turtle.Turtle();tk2.color('red');tk2x=random.randint(-10,10);tk2y=random.randint(-10,10);tk2jd=random.randint(0,3);tk2.seth(tk2jd*90);tk2.pu();tk2.goto(tk2x*20,tk2y*20);tk2.pd();tk1f+=1;score(1)
zd.fd(20);time.sleep(0.01);turtle.update()
zd.ht();turtle.update()
def tk2fs():
global tk1,tk2,tk2f;zd=turtle.Turtle();zd.shapesize(0.8);zd.pu();x,y=tk2.pos();zd.goto(x,y);zd.seth(tk2.heading())
for i in range(30):
x,y=tk1.pos();x1,y1=zd.pos()
if x1==x and y1==y:tk1.ht();tk1.clear();del tk1;tk1=turtle.Turtle();tk1.color('cyan');tk1x=random.randint(-10,10);tk1y=random.randint(-10,10);tk1jd=random.randint(0,3);tk1.seth(tk1jd*90);tk1.pu();tk1.goto(tk1x*20,tk1y*20);tk1.pd();tk2f+=1;score(1)
zd.fd(20);time.sleep(0.01);turtle.update()
zd.ht();turtle.update()
def go(x1,y1):
global tk1;tk1.goto(x1,y1)
if round(abs(tk1.xcor()))>300 or round(abs(tk1.ycor()))>300:tk1.clear();tk1.ht();del tk1;tk1=turtle.Turtle();tk1.color('cyan')
turtle.update()
def went(x1,y1):
global tk2;tk2.goto(x1,y1)
if round(abs(tk2.xcor()))>300 or round(abs(tk2.ycor()))>300:tk2.clear();tk2.ht();del tk2;tk2=turtle.Turtle();tk2.color('red')
turtle.update()
def goLeft():x,y=tk1.pos();tk1.seth(180);go(x-20,y)
def goUp():x,y=tk1.pos();tk1.seth(90);go(x,y+20)
def goRight():x,y=tk1.pos();tk1.seth(0);go(x+20,y)
def goDown():x,y=tk1.pos();tk1.seth(270);go(x,y-20)
def goW():x,y=tk2.pos();tk2.seth(90);went(x,y+20)
def goS():x,y=tk2.pos();tk2.seth(270);went(x,y-20)
def goA():x,y=tk2.pos();tk2.seth(180);went(x-20,y)
def goD():x,y=tk2.pos();tk2.seth(0);went(x+20,y)
turtle.listen();turtle.onkeypress(goLeft,'Left');turtle.onkeypress(goRight,'Right');turtle.onkeypress(goDown,'Down');turtle.onkeypress(goUp,'Up')
turtle.onkeypress(goW,'w');turtle.onkeypress(goS,'s');turtle.onkeypress(goD,'d');turtle.onkeypress(goA,'a');turtle.onkeypress(tk1fs,'l');turtle.onkeypress(tk2fs,'f')
_____________________________________________________________________________
正文开始(还想看叙事性讲程序的可以去看我的《大乌龟吃小乌龟》):
据说远在银河系猎户臂的老大爷苦瓜想干飞日本早已消失多年
再回来时带来了一种坦克大战的游戏
爱赚钱的记者闻讯而来
_____________________________________________________________________________
第一步.基础设置:
import turtle#导入turtle模块 负责图像显示以及获取图像数据
import random#导入random模块 负责给初始的坦克随机位置以及随机方向
import time#导入time模块 负责让肉眼能看见子弹
turtle.tracer(0)#以最快速度运行
记者发出疑问*1:大爷 我看您往期视频的基础设置都是特别的 今天咋缩水那么多捏?
大爷表示:我融后面了。
_____________________________________________________________________________
第二步.背景设置:
bg2=turtle.Screen()#创建窗口bg2
bg2.bgcolor('green')#把窗口的背景颜色改为绿色
bg2.setup(600,600)#把窗口设置为600像素*600像素
_____________________________________________________________________________
记者偷偷对观众说:"""
大爷今年五升六
最近断网是因为在补山一样多的作业
Such as(比如):
324页的金星数学全解
68页的抄写本
74页的语文阅读
78页的快乐暑假
3张语文试卷
背六年级上册所有的古诗以及要背的课文,文言文
60天的日记
8篇周记
两篇作文
20篇一起作业"""
_____________________________________________________________________________
第三步.辅助线:
bg=turtle.Turtle()#创建画笔bg
bg.ht()#隐藏画笔bg
for i in range(31):#画31条线
bg.pu()#不让画笔bg留下痕迹
bg.goto(-300,-300+i*20)#让画笔bg去x轴为-300 y轴为-300+i*20
bg.pd()#让画笔bg留下痕迹
bg.fd(600)#让画笔bg前进600像素
bg.left(90)#让画笔bg左转90度
for j in range(31):#画31条线
bg.pu()#让画笔bg留下痕迹
bg.goto(-300+j*20,300)#让画笔bg去x轴为-300+j*20 y轴为300
bg.pd()#让画笔bg留下痕迹
bg.bk(600)#让画笔bg后退600像素
_____________________________________________________________________________
第四步.坦克设置:
tk1=turtle.Turtle()#创建画笔tk1
tk1.color('cyan')#把画笔tk1的颜色改为青色 其实主要是蓝色不明显 只能退而求其次了
tk1x=random.randint(-10,10)#随机出蓝色坦克的x轴 保存进变量tk1x
tk1y=random.randint(-10,10)#随机出蓝色坦克的y轴 保存进变量tk1y
tk1jd=random.randint(0,3)#随机出蓝色坦克的方向 保存进变量tk1jd
tk1.seth(tk1jd*90)#让蓝色坦克朝向tk1jd*90方向
tk1.pu()#不让蓝色坦克留下痕迹
tk1.goto(tk1x*20,tk1y*20)#让蓝色坦克去x轴为tk1x*20 y轴为tk1y*20的地方
tk1.pd()#让蓝色坦克留下痕迹
tk2=turtle.Turtle()#创建画笔tk1
tk2.color('red')#把tk1的颜色改为红色
tk2x=random.randint(-10,10)#随机出红色坦克的x轴 保存进变量tk2x
tk2y=random.randint(-10,10)#随机出红色坦克的y轴 保存进变量tk2y
tk2jd=random.randint(0,3)#随机出红色坦克的方向 保存进变量tk2jd
tk2.seth(tk2jd*90)#让红色坦克朝向tk2jd*90的方向
tk2.pu()#不让红色坦克留下痕迹
tk2.goto(tk2x*20,tk2y*20)#让红色坦克去x轴为tk2x*20 y轴为tk2y*20的地方
tk2.pd()#让红色坦克留下痕迹
turtle.update()#刷新画面
_____________________________________________________________________________
记者又发出疑惑*2:大爷 您这个random什么逻辑啊
大爷表示:这是以格子为单位的 乘以20是为了在图像上不出现问题
大爷又表示:后面那个是以四个方向为单位
_____________________________________________________________________________
第五步.分数系统
tk1f=0#创建画笔tk1f(蓝色坦克的分数)
tk2f=0#创建画笔tk2f(红色坦克的分数)
word=turtle.Turtle()#创建画笔word
word.ht()#隐藏画笔word
def score(tf):#定义函数score(分数的英语)
if tf:#如果表达式tf结果不是False运行
global word#把画笔word改为全局变量(可以改变画笔word)
word.ht()#隐藏画笔word
word.clear()#把画笔word造成的痕迹清空
word=turtle.Turtle()#创建画笔word
word.color('red')#把画笔word的颜色改为红色
word.pu()#不让画笔word留下痕迹
word.goto(-40,0)#让画笔word去x轴为-40 y轴为0的地方
word.write(str(tk2f),font=('微软雅黑',20))#写出变量tk2f
word.color('black')#把画笔word颜色改为黑色
word.goto(0,0)#让画笔word去x轴为0 y轴为0的地方
word.write(':',font=('微软雅黑',20))#写出冒号
word.color('cyan')#把画笔word的颜色改为青色
word.goto(40,0)#让画笔word去x轴为40 y轴为0的地方
word.write(str(tk1f),font=('微软雅黑',20))#写出变量tk1f
word.ht()#隐藏画笔word
score(0)#形参tf为0 不运行if语句里的代码
_____________________________________________________________________________
记者双发出疑惑*3:大爷 您这个if里套个tf干嘛
大爷表示:方便后续判断
记者叒发出疑惑*4:大爷 这个tf什么逻辑啊?
大爷表示:在if语句会把后面的表达式看成bool类型
大爷又表示:并且0会直接转换成False 其他不管什么都会转换成True
记者叕发出疑惑*5:大爷 word画笔都pu函数了 咋还能写出字呢?
大爷表示:word是用来定位的 字就像贴纸一样贴上面 和pu函数无关
_____________________________________________________________________________
第六步.坦克发射函数:
def tk1fs():#定义函数tk1fs
global tk1,tk2,tk1f#把tk1,tk2,tk1f改为全局变量
zd=turtle.Turtle()#创建画笔zd
zd.shapesize(0.8)#把画笔zd的大小改为正常的80%
zd.pu()#不让画笔zd留下痕迹
x,y=tk1.pos()#获得蓝色坦克的位置 x轴保存进变量x y轴保存在变量y
zd.goto(x,y)#让画笔zd去蓝色坦克的位置
zd.seth(tk1.heading())#让画笔zd的朝向与蓝色坦克一致
for i in range(30):#运行30次(走30格的距离 刚好是地图的边长)
x,y=tk2.pos()#获得红色坦克的位置 x轴保存进变量x y轴保存在变量y
x1,y1=zd.pos()#获得画笔zd的位置 x轴保存进变量x1 y轴保存在变量y1
if x1==x and y1==y:#如果画笔zd的位置与红色坦克一致时运行
tk2.ht()#隐藏画笔tk2
tk2.clear()#删除画笔tk2留下的痕迹
del tk2#删除画笔tk2
tk2=turtle.Turtle()#创建画笔tk2
tk2.color('red')#把画笔tk2的颜色改为红色
tk2x=random.randint(-10,10)#从以地图中心点为中心点的长度个点20格的线段中随机一个点并保存进变量tk2x
tk2y=random.randint(-10,10)#从以地图中心点为中心点的长度个点20格的线段中随机一个点并保存进变量tk2y
tk2jd=random.randint(0,3)#四个方向选一个方向
tk2.seth(tk2jd*90)#让画笔tk2朝向tk2jd*90的方向
tk2.pu()#让画笔tk2不留下痕迹
tk2.goto(tk2x*20,tk2y*20)#让画笔tk2去x轴为tk2x*20 y轴为tk2y*20的位置
tk2.pd()#让tk2留下痕迹
tk1f+=1#蓝色坦克的分数变量加1
score(1)#重新刷新一遍分数
zd.fd(20)#让画笔zd向前移动20像素
time.sleep(0.01)#停0.01秒
turtle.update()#重新刷新屏幕
zd.ht()#隐藏画笔zd
turtle.update()#刷新一遍画面
'''
同理 我们可以做出红色坦克的发射按钮
'''
def tk2fs():#定义函数tk2fs
global tk1,tk2,tk2f#把tk1,tk2,tk2f改为全局变量
zd=turtle.Turtle()#创建画笔zd
zd.shapesize(0.8)#把画笔zd的大小改为正常的80%
zd.pu()#不让画笔zd留下痕迹
x,y=tk2.pos()#获得红色坦克的位置 x轴保存进变量x y轴保存在变量y
zd.goto(x,y)#让画笔zd去红色坦克的位置
zd.seth(tk2.heading())#让画笔zd的朝向与红色坦克一致
for i in range(30):#运行30次(走30格的距离 刚好是地图的边长)
x,y=tk1.pos()#获得蓝色坦克的位置 x轴保存进变量x y轴保存在变量y
x1,y1=zd.pos()#获得画笔zd的位置 x轴保存进变量x1 y轴保存在变量y1
if x1==x and y1==y:#如果画笔zd的位置与蓝色坦克一致时运行
tk1.ht()#隐藏画笔tk1
tk1.clear()#删除画笔tk1留下的痕迹
del tk1#删除画笔tk1
tk1=turtle.Turtle()#创建画笔tk1
tk1.color('cyan')#把画笔tk1的颜色改为青色
tk1x=random.randint(-10,10)#从以地图中心点为中心点的长度个点20格的线段中随机一个点并保存进变量tk1x
tk1y=random.randint(-10,10)#从以地图中心点为中心点的长度个点20格的线段中随机一个点并保存进变量tk1y
tk1jd=random.randint(0,3)#随机一个方向 保存进变量tk1jd里
tk1.seth(tk1jd*90)#让画笔tk1朝向tk1jd*90的方向
tk1.pu()#不让画笔tk1留下痕迹
tk1.goto(tk1x*20,tk1y*20)#让画笔tk1去x轴为tk1x*20 y轴为tk1y*20的位置
tk1.pd()#让画笔tk1留下痕迹
tk2f+=1#红色坦克的分数变量加1
score(1)#重新刷新一遍分数
zd.fd(20)#让画笔zd向前移动20像素
time.sleep(0.01)#停0.01秒
turtle.update()#刷新屏幕
zd.ht()#隐藏画笔zd
turtle.update()#刷新一遍屏幕
_____________________________________________________________________________
记者又叕发出疑惑*6:大爷 您发射就发射 咋还要一个for循环慢慢发射呀?
大爷表示:前面用了个tracer函数 直接fd到底会看不见画笔zd
大爷又表示:并且这样还方便判断是否撞到另一辆坦克
_____________________________________________________________________________
第七步.跑路函数:
def go(x1,y1):#定义函数go
global tk1#把画笔tk1改为全局变量
tk1.goto(x1,y1)#让画笔tk1去x轴为x1 y轴为y1的地方
if round(abs(tk1.xcor()))>300 or round(abs(tk1.ycor()))>300:#判断蓝色坦克是否撞墙 用三目运算符解释获得绝对值函数abs: abs(number)=(number>0)?number:0-number;
tk1.clear()#删除蓝色坦克留下的痕迹
tk1.ht()#隐藏蓝色坦克
del tk1#删除画笔tk1
tk1=turtle.Turtle()#重新创建蓝色坦克
tk1.color('cyan')#把蓝色坦克的颜色改为青色
turtle.update()#刷新画面
def went(x1,y1):#定义函数went(go的过去式)
global tk2#把画笔tk2改为全局变量
tk2.goto(x1,y1)#让画笔tk2去x轴为x1 y轴为y1的地方
if round(abs(tk2.xcor()))>300 or round(abs(tk2.ycor()))>300:#判断红色坦克是否撞墙
tk2.clear()#删除红色坦克留下的痕迹
tk2.ht()#隐藏红色坦克
del tk2#删除画笔tk2
tk2=turtle.Turtle()#重新创建红色坦克
tk2.color('red')#把红色坦克的颜色改为红色
turtle.update()#刷新画面
def goLeft():#定义函数goLeft 负责让蓝色坦克去左边
x,y=tk1.pos()#获得蓝色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk1.seth(180)#让蓝色坦克朝向左边
go(x-20,y)#让蓝色坦克去左边的格子里
def goUp():#定义函数goUp 负责让蓝色坦克去上方
x,y=tk1.pos()#获得蓝色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk1.seth(90)#让蓝色坦克朝向上方
go(x,y+20)#让蓝色坦克去上方的格子
def goRight():#定义函数goRight 负责让蓝色坦克去又右边
x,y=tk1.pos()#获得蓝色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk1.seth(0)#让蓝色坦克朝向右边
go(x+20,y)#让蓝色坦克去右边的格子
def goDown():#定义函数goDown 负责让蓝色坦克去下方
x,y=tk1.pos()#获得蓝色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk1.seth(270)#让蓝色坦克朝向下方
go(x,y-20)#让蓝色坦克去下方的格子里
def goW():#定义函数goW 负责让红色坦克去上方
x,y=tk2.pos()#获得红色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk2.seth(90)#让红色坦克朝向上方
went(x,y+20)#让红色坦克去上方的格子里
def goS():#定义函数goS 负责让红色坦克去下方
x,y=tk2.pos()#获得红色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk2.seth(270)#让红色坦克朝向下方
went(x,y-20)#让红色坦克去下方的格子里
def goA():#定义函数goA 负责让红色坦克去左边
x,y=tk2.pos()#获得红色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk2.seth(180)#让红色坦克朝向左边
went(x-20,y)#让红色坦克去左边的格子里
def goD():#定义函数goD 负责让红色坦克去右边
x,y=tk2.pos()#获得红色坦克的在x与y轴上的位置 并把x轴坐标保存在变量x里 把y轴坐标保存在变量y里
tk2.seth(0)#让红色坦克朝向右边
went(x+20,y)#让红色坦克去下方格子里
_____________________________________________________________________________
记者双叕发出疑惑*7:大爷 这个abs函数什么意思啊?
大爷表示:算绝对值
记者叒叕发出疑问:绝对值是什么?
大爷表示:在坐标系上离中心点的距离
记者又叒叕发出疑惑:听不懂思密达
大爷表示:是负数就把-号删掉 如果不是就啥也不干+3
记者双叒叕发出疑惑:那么went是什么意思
大爷表示:这个不重要 下辈子注意点就行(其实went是go的过去式)
_____________________________________________________________________________
第八步.监听键盘:
turtle.listen()#开始监听
turtle.onkeypress(goLeft,'Left')#如果用户点击左键就运行函数goLeft
turtle.onkeypress(goRight,'Right')#如果用户点击右键就运行函数goRight
turtle.onkeypress(goDown,'Down')#如果用户点击下键(无特殊含义)就运行函数goDown
turtle.onkeypress(goUp,'Up')#如果用户点击上键就运行函数goUp
turtle.onkeypress(tk1fs,'l')#如果用户点击L键就运行函数tk1fs
turtle.onkeypress(goW,'w')#如果用户点击W键就运行函数goW
turtle.onkeypress(goS,'s')#如果用户点击S键就运行函数goS
turtle.onkeypress(goD,'d')#如果用户点击D键就运行函数goD
turtle.onkeypress(goA,'a')#如果用户点击A键就运行函数goA
turtle.onkeypress(tk2fs,'f')#如果用点击F键就运行函数tk2fs
____________________________________________________________________________
记者又双叒叕发出疑惑:大爷 这为啥还要listen函数
大爷表示:不告诉程序要开始听了 程序就跟你一样 英语听力成绩感人
_____________________________________________________________________________
完全展开的:
import turtle,random,time
turtle.tracer(0)
bg2=turtle.Screen()
bg2.bgcolor('green')
bg2.setup(600,600)
bg=turtle.Turtle()
bg.ht()
for i in range(31):
bg.pu()
bg.goto(-300,-300+i*20)
bg.pd()
bg.fd(600)
bg.left(90)
for j in range(31):
bg.pu()
bg.goto(-300+j*20,300)
bg.pd()
bg.bk(600)
tk1=turtle.Turtle()
tk1.color('cyan')
tk1x=random.randint(-10,10)
tk1y=random.randint(-10,10)
tk1jd=random.randint(0,3)
tk1.seth(tk1jd*90)
tk1.pu()
tk1.goto(tk1x*20,tk1y*20)
tk1.pd()
tk2=turtle.Turtle()
tk2.color('red')
tk2x=random.randint(-10,10)
tk2y=random.randint(-10,10)
tk2jd=random.randint(0,3)
tk2.seth(tk2jd*90)
tk2.pu()
tk2.goto(tk2x*20,tk2y*20)
tk2.pd()
turtle.update()
tk1f=0
tk2f=0
word=turtle.Turtle()
word.ht()
def score(tf):
if tf:
global word
word.ht()
word.clear()
word=turtle.Turtle()
word.color('red')
word.pu()
word.goto(-40,0)
word.write(str(tk2f),font=('微软雅黑',20))
word.color('black')
word.goto(0,0)
word.write(':',font=('微软雅黑',20))
word.color('cyan')
word.goto(40,0)
word.write(str(tk1f),font=('微软雅黑',20))
word.ht()
score(0)
def tk1fs():
global tk1,tk2,tk1f
zd=turtle.Turtle()
zd.shapesize(0.8)
zd.pu()
x,y=tk1.pos()
zd.goto(x,y)
zd.seth(tk1.heading())
for i in range(30):
x,y=tk2.pos()
x1,y1=zd.pos()
if x1==x and y1==y:
tk2.ht()
tk2.clear()
del tk2
tk2=turtle.Turtle()
tk2.color('red')
tk2x=random.randint(-10,10)
tk2y=random.randint(-10,10)
tk2jd=random.randint(0,3)
tk2.seth(tk2jd*90)
tk2.pu()
tk2.goto(tk2x*20,tk2y*20)
tk2.pd()
tk1f+=1
score(1)
zd.fd(20)
time.sleep(0.01)
turtle.update()
zd.ht()
turtle.update()
def tk2fs():
global tk1,tk2,tk2f
zd=turtle.Turtle()
zd.shapesize(0.8)
zd.pu()
x,y=tk2.pos()
zd.goto(x,y)
zd.seth(tk2.heading())
for i in range(30):
x,y=tk1.pos()
x1,y1=zd.pos()
if x1==x and y1==y:
tk1.ht()
tk1.clear()
del tk1
tk1=turtle.Turtle()
tk1.color('cyan')
tk1x=random.randint(-10,10)
tk1y=random.randint(-10,10)
tk1jd=random.randint(0,3)
tk1.seth(tk1jd*90)
tk1.pu()
tk1.goto(tk1x*20,tk1y*20)
tk1.pd()
tk2f+=1
score(1)
zd.fd(20)
time.sleep(0.01)
turtle.update()
zd.ht()
turtle.update()
def go(x1,y1):
global tk1
tk1.goto(x1,y1)
if round(abs(tk1.xcor()))>300 or round(abs(tk1.ycor()))>300:
tk1.clear()
tk1.ht()
del tk1
tk1=turtle.Turtle()
tk1.color('cyan')
turtle.update()
def went(x1,y1):
global tk2
tk2.goto(x1,y1)
if round(abs(tk2.xcor()))>300 or round(abs(tk2.ycor()))>300:
tk2.clear()
tk2.ht()
del tk2
tk2=turtle.Turtle()
tk2.color('red')
turtle.update()
def goLeft():x,y=tk1.pos();tk1.seth(180);go(x-20,y)
def goUp():x,y=tk1.pos();tk1.seth(90);go(x,y+20)
def goRight():x,y=tk1.pos();tk1.seth(0);go(x+20,y)
def goDown():x,y=tk1.pos();tk1.seth(270);go(x,y-20)
def goW():x,y=tk2.pos();tk2.seth(90);went(x,y+20)
def goS():x,y=tk2.pos();tk2.seth(270);went(x,y-20)
def goA():x,y=tk2.pos();tk2.seth(180);went(x-20,y)
def goD():x,y=tk2.pos();tk2.seth(0);went(x+20,y)
turtle.listen()
turtle.onkeypress(goLeft,'Left')
turtle.onkeypress(goRight,'Right')
turtle.onkeypress(goDown,'Down')
turtle.onkeypress(goUp,'Up')
turtle.onkeypress(tk1fs,'l')
turtle.onkeypress(goW,'w')
turtle.onkeypress(goS,'s')
turtle.onkeypress(goD,'d')
turtle.onkeypress(goA,'a')
turtle.onkeypress(tk2fs,'f')
_____________________________________________________________________________
未登录的复制渠道:
import turtle,random,time
turtle.tracer(0)
bg2=turtle.Screen()
bg2.bgcolor('green')
bg2.setup(600,600)
bg=turtle.Turtle()
bg.ht()
for i in range(31):
bg.pu()
bg.goto(-300,-300+i*20)
bg.pd()
bg.fd(600)
bg.left(90)
for j in range(31):
bg.pu()
bg.goto(-300+j*20,300)
bg.pd()
bg.bk(600)
tk1=turtle.Turtle()
tk1.color('cyan')
tk1x=random.randint(-10,10)
tk1y=random.randint(-10,10)
tk1jd=random.randint(0,3)
tk1.seth(tk1jd*90)
tk1.pu()
tk1.goto(tk1x*20,tk1y*20)
tk1.pd()
tk2=turtle.Turtle()
tk2.color('red')
tk2x=random.randint(-10,10)
tk2y=random.randint(-10,10)
tk2jd=random.randint(0,3)
tk2.seth(tk2jd*90)
tk2.pu()
tk2.goto(tk2x*20,tk2y*20)
tk2.pd()
turtle.update()
tk1f=0
tk2f=0
word=turtle.Turtle()
word.ht()
def score(tf):
if tf:
global word
word.ht()
word.clear()
word=turtle.Turtle()
word.color('red')
word.pu()
word.goto(-40,0)
word.write(str(tk2f),font=('微软雅黑',20))
word.color('black')
word.goto(0,0)
word.write(':',font=('微软雅黑',20))
word.color('cyan')
word.goto(40,0)
word.write(str(tk1f),font=('微软雅黑',20))
word.ht()
score(0)
def tk1fs():
global tk1,tk2,tk1f
zd=turtle.Turtle()
zd.shapesize(0.8)
zd.pu()
x,y=tk1.pos()
zd.goto(x,y)
zd.seth(tk1.heading())
for i in range(30):
x,y=tk2.pos()
x1,y1=zd.pos()
if x1==x and y1==y:
tk2.ht()
tk2.clear()
del tk2
tk2=turtle.Turtle()
tk2.color('red')
tk2x=random.randint(-10,10)
tk2y=random.randint(-10,10)
tk2jd=random.randint(0,3)
tk2.seth(tk2jd*90)
tk2.pu()
tk2.goto(tk2x*20,tk2y*20)
tk2.pd()
tk1f+=1
score(1)
zd.fd(20)
time.sleep(0.01)
turtle.update()
zd.ht()
turtle.update()
def tk2fs():
global tk1,tk2,tk2f
zd=turtle.Turtle()
zd.shapesize(0.8)
zd.pu()
x,y=tk2.pos()
zd.goto(x,y)
zd.seth(tk2.heading())
for i in range(30):
x,y=tk1.pos()
x1,y1=zd.pos()
if x1==x and y1==y:
tk1.ht()
tk1.clear()
del tk1
tk1=turtle.Turtle()
tk1.color('cyan')
tk1x=random.randint(-10,10)
tk1y=random.randint(-10,10)
tk1jd=random.randint(0,3)
tk1.seth(tk1jd*90)
tk1.pu()
tk1.goto(tk1x*20,tk1y*20)
tk1.pd()
tk2f+=1
score(1)
zd.fd(20)
time.sleep(0.01)
turtle.update()
zd.ht()
turtle.update()
def go(x1,y1):
global tk1
tk1.goto(x1,y1)
if round(abs(tk1.xcor()))>300 or round(abs(tk1.ycor()))>300:
tk1.clear()
tk1.ht()
del tk1
tk1=turtle.Turtle()
tk1.color('cyan')
turtle.update()
def went(x1,y1):
global tk2
tk2.goto(x1,y1)
if round(abs(tk2.xcor()))>300 or round(abs(tk2.ycor()))>300:
tk2.clear()
tk2.ht()
del tk2
tk2=turtle.Turtle()
tk2.color('red')
turtle.update()
def goLeft():x,y=tk1.pos();tk1.seth(180);go(x-20,y)
def goUp():x,y=tk1.pos();tk1.seth(90);go(x,y+20)
def goRight():x,y=tk1.pos();tk1.seth(0);go(x+20,y)
def goDown():x,y=tk1.pos();tk1.seth(270);go(x,y-20)
def goW():x,y=tk2.pos();tk2.seth(90);went(x,y+20)
def goS():x,y=tk2.pos();tk2.seth(270);went(x,y-20)
def goA():x,y=tk2.pos();tk2.seth(180);went(x-20,y)
def goD():x,y=tk2.pos();tk2.seth(0);went(x+20,y)
turtle.listen()
turtle.onkeypress(goLeft,'Left')
turtle.onkeypress(goRight,'Right')
turtle.onkeypress(goDown,'Down')
turtle.onkeypress(goUp,'Up')
turtle.onkeypress(tk1fs,'l')
turtle.onkeypress(goW,'w')
turtle.onkeypress(goS,'s')
turtle.onkeypress(goD,'d')
turtle.onkeypress(goA,'a')
turtle.onkeypress(tk2fs,'f')
_____________________________________________________________________________
MVP结算:
本次字数:20666
本期质量分:82
本期记者发出了11次疑惑
本期大爷回答了记者14次
老规矩:
24小时内破10赞更新本期花絮(我会做类似隐身的效果)
24小时内破20赞更新Python(更新速度取决于评论区)
24小时内破40赞更新C++(内容取决于评论区)