海龟画完红色的正方形后,再单击,在画布上的项目们都会移动.
""" 获取填充块的左右上下坐标 单击后移动字和海龟,填充块会移动并且碰到边缘会反弹 """ import time import turtle from random import randint turtle.shape('turtle') turtle.penup() turtle.speed(1) turtle.fillcolor('red') turtle.begin_fill() for _ in range(4): turtle.fd(100) turtle.rt(90) turtle.end_fill() hg = turtle.getturtle() sc = turtle.getscreen() cv = turtle.getcanvas() sw = sc.window_width() # 屏宽 sh = sc.window_height() # 屏高 def move(x,y): sc.onclick(None) dx = randint(-5,5) dy = randint(-5,5) while True: cv.move(text,1,0) turtle.setx(turtle.xcor()-1) cv.move(redsquare,dx,dy) cv.update() xy = cv.coords(redsquare) left = min([xy[i] for i in range(len(xy)) if i%2==0]) right = max([xy[i] for i in range(len(xy)) if i%2==0]) top = max([xy[i] for i in range(len(xy)) if i%2==1]) bottom = min([xy[i] for i in range(len(xy)) if i%2==1]) if left<= -sw//2 or right>= sw//2:dx = -dx if top>= sh//2 or bottom<= -sh//2:dy = -dy time.sleep(0.01) redsquare = hg.items[-1] zt = ('',32,'underline') turtle.sety(turtle.ycor() + 100) turtle.write('请单击',align='center',font=zt) text = hg.items[-1] sc.onclick(move) sc.mainloop()
本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 获取填充块的左右上下坐标
李兴球的博客_Python创意编程技术前沿_pygame » 获取填充块的左右上下坐标