python3D多彩重力弹球

3D多彩重力弹球,这是用Python的海龟画图制作的3D多彩重力弹球
以下是部分代码预览:

"""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()  
            

如需要查看完整代码,请扫码付款:

VIP免费查看

已有0人支付

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。QQ群免费教学群:225792826 (提供真实姓名可入群)
李兴球的博客_Python创意编程技术前沿 » python3D多彩重力弹球

学本领,探索更大的世界!

李兴球博客 风火轮编程主页