以下是部分代码预览:
""" 多彩弹球三.py。 本程序新建Ball类,它继承自Turtle。""" from glob import glob from turtle import Screen,Turtle from random import randint,choice class Ball(Turtle): def __init__(self,image): """image:已注册的gif图""" Turtle.__init__(self,shape=image) self.penup() self.xspeed = choice(speeds) # 此处用到了全局变量speeds self.yspeed = choice(speeds) self.sw = self.screen.window_width() # 屏幕宽度属性 self.sh = self.screen.window_height() # 屏幕高度属性 def move(self): """移动小球方法""" x = self.xcor() + self.xspeed # 新的x坐标是原坐标加xspeed y = self.ycor() + self.yspeed # 新的y坐标是原坐标加yspeed if __name__ == "__main__": width,height = 800,600 gif_images = glob("images/*.gif") speeds = [x for x in range(-3,3) if x!=0] # 如果x不是0则加到列表中 screen = Screen() screen.delay(0) # 屏幕延时为0毫秒 screen.bgcolor("black") # 设定屏幕背景色 screen.setup(width,height) # 设定屏幕宽高 screen.title("多彩弹球三") [screen.addshape(image) for image in gif_images] # 注册所有gif到屏幕 while running: balls[index].move() # 移动小球 index = index + 1 # 索引号加1 index = index % amounts # 索引对总数量求余 screen.bye() # 关闭窗口
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)