
""" 简易单击爆炸效果.py 本程序演示许多橙色的方块从上往下移,单击它们会爆炸! 这是一个演示矩形碰撞的小游戏,rect模块是自己编写的。 它里面有overlap、bounce_on_edge、collidepoint等方法。 这个程序只有两个海龟对象,一个用于不断地重画所有的橙色矩形, 另一个海龟对象用来显示延时效果,注意本程序用了ontimer模拟异步执行。 本程序编写日期2020年11月6号,预备作为11月13号的课堂讲解程序。 """ import rect import time import turtle import random def draw_rect(r,color): """ 画矩形函数 r:一个矩形 color:颜色 """ turtle.fillcolor(color) turtle.goto(r.left,r.top) turtle.pendown() turtle.begin_fill() for _ in range(2): turtle.fd(r.width) turtle.rt(90) turtle.fd(r.height) turtle.rt(90) turtle.end_fill() turtle.penup() width = 800 height = 640 turtle.bgcolor('black') turtle.setup(width,height) turtle.title('简易单击爆炸效果by李兴球') turtle.tracer(0,0) turtle.speed(0) turtle.penup() turtle.ht() es = [] for _ in range(40): x = random.randint(-width/2,width/2) y = random.randint(height/2,height*2) e = rect.Rect(x,y,40,40) e.dy = -5 e.draw=True es.append(e) def explode(x,y): boom.goto(x,y) boom.showturtle() t = 0 def wait(): nonlocal t t = t + 1 if t<10: screen.ontimer(wait,10) else: boom.hideturtle() wait() def hide(x,y): for e in es: if e.draw and e.collidepoint(x,y): e.draw=False explode(x,y) screen = turtle.getscreen() screen.addshape('boom.gif') boom = turtle.Turtle(shape='boom.gif',visible=False) boom.speed(0) boom.penup() screen.onclick(hide) while True: for e in es: e.move() if e.top < -height/2: # 到了最下面 e.left = random.randint(-width/2,width/2) e.top=random.randint(height/2,height*2) turtle.clear() for e in es: if e.draw: draw_rect(e,'orange') turtle.update() time.sleep(0.01)
需要完整项目的源代码和素材,请扫码付款后自行下载,
下载后需要提供技术支持请联系李兴球,相信你一定能找到我的微信号或者QQ号的。
本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 简易单击爆炸效果
李兴球的博客_Python创意编程技术前沿_pygame » 简易单击爆炸效果