2019-03-08
阅读量:
555
python如何将代码转换为库(3)
PART(2/2):模块化在第二部分中,我们将了解面向对象编程的一个基本特征,即模块化。到目前为止,我们还没有介绍任何会使这个难以维护或扩展的内容,至少在我们可以用PyGame做的范围内。如何使其模块化?对此有一个非常简单的测试,让我们尝试导入它!
为此,我们有两个文件。让我们复制Blob类并随机。
import random
class Blob:
def __init__(self, color):
self.x = random.randrange(0, WIDTH)
self.y = random.randrange(0, HEIGHT)
self.size = random.randrange(4,8)
self.color = color
def move(self):
self.move_x = random.randrange(-1,2)
self.move_y = random.randrange(-1,2)
self.x += self.move_x
self.y += self.move_y
if self.x WIDTH: self.x = WIDTH
if self.y HEIGHT: self.y = HEIGHT
Back to our original file, let’s remove the Blob class, and then import Blob from blob.py.
import pygame
import random
from blob import Blob
STARTING_BLUE_BLOBS = 10
...






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
0条评论