微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有 pygame 视差背景的随机滞后尖峰

如何解决具有 pygame 视差背景的随机滞后尖峰

我正在尝试制作具有视差背景的平台游戏。我管理了代码并确保添加.convert。 它在大多数情况下运行良好,但时不时会出现周期性的滞后峰值。

# Imports
from vars import *


# Background class
class Background(pygame.sprite.Sprite):
    def __init__(self,image,x):
        super(Background,self).__init__()
        self.surf = pygame.transform.smoothscale(pygame.image.load(image).convert_alpha(),(s_width,s_height))
        self.rect = self.surf.get_rect(center=(x,s_height/2))


# Cave Background
class BgSurface(pygame.sprite.Sprite):
    def __init__(self):
        super(BgSurface,self).__init__()
        self.surf = pygame.Surface((s_width,s_height))
        self.rect = self.surf.get_rect(center=(s_width/2,s_height/2))
        self.surf.fill((144,156,156))


# Background stuff
cave_air = BgSurface()
l1 = Background("layer1.png",s_width/2)
l2 = Background("layer2.png",s_width/2)
layer_list = [pygame.sprite.Group(l1),pygame.sprite.Group(l2)]
head_list = [pygame.sprite.Group(l1),pygame.sprite.Group(l2)]
bg_img_list = ["layer1.png","layer2.png"]


def parallax(s_xdir,s_ydir):
    for i in range(len(layer_list)):
        ind = 0
        for x in layer_list[i]:
            ind += 1
            x.rect.move_ip(s_xdir * vel_list[i],s_ydir * vel_list[2])
            # Adding to left
            if x.rect.left > 0 and ind == len(layer_list[i]) and not x.rect.centerx > s_width:
                new_bg = Background(bg_img_list[i],x.rect.centerx - s_width)
                new_bg.rect.centery = x.rect.centery
                layer_list[i].add(new_bg)
            # Memory optimization
            if x.rect.left > s_width or x.rect.right < 1:
                if x in head_list[i]:
                    x.kill()
                    for a in layer_list[i]:
                        head_list[i] = pygame.sprite.Group(a)
                x.kill()

        # Adding to right side
        for a in head_list[i]:
            if a.rect.right < s_width:
                new_head = Background(bg_img_list[i],a.rect.centerx + s_width)
                new_head.rect.centery = a.rect.centery
                layer_list[i].add(new_head)
                head_list[i] = pygame.sprite.Group(new_head)

        # Experimental deletion of common centers
        for p in layer_list[i]:
            for q in layer_list[i]:
                if p.rect.centerx == q.rect.centerx and p != q:
                    p.kill()

(s_width 和 s_height 之类的东西在我导入的 vars 模块中定义) 我将最后一个终止循环移出函数并检查每秒后台中有多少精灵,但它返回在滞后峰值期间没有添加不必要的精灵。 尖峰甚至与添加新表面以填充屏幕的时刻不一致。你们中有人知道为什么会这样吗?

解决方法

不要在应用程序循环中加载图像。加载图像非常耗时,因为必须读取和解释图像文件。在应用程序开始时加载一次图像:

# Background class
class Background(pygame.sprite.Sprite):
    def __init__(self,image,x):
        super(Background,self).__init__()
        self.surf = pygame.transform.smoothscale(image,(s_width,s_height))
        self.rect = self.surf.get_rect(center=(x,s_height/2))
bg_img_filelist = ["layer1.png","layer2.png"]
bg_img_list = [pygame.image.load(f).convert_alpha() for f in bg_img_filelist ]
l1 = Background(bg_img_list[0],s_width/2)
l2 = Background(bg_img_list[1],s_width/2)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。