# 代码和图像最好放在同一个目录下, 同级别
# 图像自己找一张就好,但要和设置的界面尺寸比例一样, 可以自己调尺寸
# 我写的封装,调用略微有点小麻烦
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()