"""
简易单击爆炸效果.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)
需要完整项目的源代码和素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

