"""3D多彩重力弹球,这是用Python的海龟画图制作的3D多彩重力弹球。 在程序中新建了Ball类,它继承自Turtle,由于它不需要画画,所以它在初始化的时候就让它抬笔penup。 给Ball设计了xspeed和yspeed属性,这代表每次移动的水平和垂直位移。由于是重力弹球,所以设计了加速度属性。 """ from turtle import * from random import randint,choice from time import sleep class Ball(Turtle): def __init__(self,color,size,height): Turtle.__init__(self,visible=False,shape='blank') self.penup() # 抬笔 self.xspeed = 0 # 单位水平位移 self.yspeed = 0 # 垂直水平位移 imagefile = color + "_balls/ball-" + str(size) + ".gif" self.shape(imagefile) # 设定形状 self.setx(randint(50-self.screen.window_width()//2,self.screen.window_width()//2-50)) self.sety(height) self.accspeed = -0.5 self.showturtle() if __name__ == "__main__": colors = ["cyan",'yellow','green','pink','purple'] size = list(range(10,101,10)) yellow_balls = ["yellow_balls/ball-" + str(i) + ".gif" for i in size] cyan_balls = ["cyan_balls/ball-" + str(i) + ".gif" for i in size] green_balls = ["green_balls/ball-" + str(i) + ".gif" for i in size] pink_balls = ["pink_balls/ball-" + str(i) + ".gif" for i in size] purple_balls = ["purple_balls/ball-" + str(i) + ".gif" for i in size] screen = Screen() screen.title("3D多彩重力弹球,作者:李兴球") screen.delay(0) screen.bgcolor("black") # screen.bgpic("bg3d.png") [screen.addshape(image) for image in yellow_balls] [screen.addshape(image) for image in cyan_balls] [screen.addshape(image) for image in green_balls] [screen.addshape(image) for image in pink_balls] [screen.addshape(image) for image in purple_balls] screen.mainloop()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)