找回密码
 中文实名注册
查看: 178|回复: 0

七单元-第4课-编程进阶2上-套环游戏-类思路

[复制链接]

10

主题

21

帖子

424

积分

版主

Rank: 7Rank: 7Rank: 7

积分
424
发表于 2021-8-28 17:38:09 | 显示全部楼层 |阅读模式
类是一种定义方式。它定义一个类以及类的属性和动作函数,我们可以修改类中每个个体的属性,或是调用动作函数,来造成不同效果。
[Python] 纯文本查看 复制代码
class Parts():
    # 初始化函数
    def __init__(self, path):
        self.image = pygame.image.load(path)
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(0, 800), 300)

    # 绘制函数
    def draw(self, screen):
        screen.blit(self.image,self.rect)

    # 移动函数
    def move(self):
        self.rect.centerx += 40
        if self.rect.centerx > 800:
            self.rect.centerx = random.randint(-400, 0)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2024-5-5 11:27 , Processed in 0.048681 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表