# 1.一个可以关闭的空白窗体 # 2.使用精灵显示背景, 背景的循环展示

# 代码和图像最好放在同一个目录下, 同级别

# 图像自己找一张就好,但要和设置的界面尺寸比例一样, 可以自己调尺寸

# 我写的封装,调用略微有点小麻烦
import pygame
import sys


class BaseSprite(pygame.sprite.Sprite):
        def __init__(self, name):
                super().__init__()
                self.image = pygame.image.load(name)
                self.rect = self.image.get_rect()


class BGSprite(BaseSprite):
        def __init__(self, name, top_left):
                super().__init__(name)
                self.rect.topleft = top_left

        def update(self):
                self.rect.top += 5
                if self.rect.top >= 700:
                self.rect.top = -700


class BGManage:
        def __init__(self, gm):
                super().__init__()
                self.gm = gm
                self.bg_group = pygame.sprite.Group()

                BGSprite("images/background.png", (0, 0)).add(self.bg_group)
                BGSprite("images/background.png", (0, -700)).add(self.bg_group)

        def update(self):
                self.bg_group.update()
                self.bg_group.draw(self.gm.screen)


class GameManage:
        def __init__(self):
                pygame.init()
                self.screen = pygame.display.set_mode((480, 700))
                pygame.display.set_caption("嚯哈哈哈")
                self.clock = pygame.time.Clock()

                self.bg_manage = BGManage(self)

        def check_event(self):
                for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                                pygame.quit()
                                sys.exit()

        def draw_update(self):
                self.bg_manage.update()

        def run(self):
                while True:
                        self.clock.tick(20)
                        self.check_event()
                        self.draw_update()
                        pygame.display.flip()


gm = GameManage()
gm.run()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值