Python livewires 调整屏幕大小

标签 python pygame livewires

我正在尝试使用 Livewires 创建一个游戏,其中有多个级别。在每个级别中,屏幕都需要有不同的大小,因此我可以创建一个新屏幕,也可以调整它的大小。当我尝试一个新屏幕时,像这样 (mcve):

from livewires import games
games.init(screen_width = 500, screen_height=100, fps = 50)
#dosomething
games.screen.quit()
games.init(screen_width = 100, screen_height=500, fps = 50)    
games.screen.mainloop()

我得到错误:

Traceback (most recent call last):
  File "C:\Users\Fred\Desktop\game\main.py", line 6, in <module>
    games.screen.mainloop()
  File "C:\Users\Fred\Desktop\game\livewires\games.py", line 308, in mainloop
    object._tick()
  File "C:\Users\Fred\Desktop\game\livewires\games.py", line 503, in _tick
    self.tick()
  File "C:\Users\Fred\Desktop\game\livewires\games.py", line 776, in tick
    self._after_death()
  File "C:\Users\Fred\Desktop\game\main.py", line 5, in <module>
    games.init(screen_width = 100, screen_height=500, fps = 50)
  File "C:\Users\Fred\Desktop\game\livewires\games.py", line 884, in init
    screen = Screen(screen_width, screen_height, fps)
  File "C:\Users\Fred\Desktop\game\livewires\games.py", line 159, in __init__
    raise GamesError("Cannot have more than on Screen object")

livewires.games.GamesError: Cannot have more than on Screen object

games.screen.widthheight 无法设置(你只能获取它们),所以我不能那样做,当我更改添加行时livewires.games 将屏幕计数重置为 0,但我在 pygame 中收到错误。

有谁知道在 Livewire 中调整屏幕大小或破坏并重新创建屏幕的方法?


注意:我使用的是 Michael Dawsons 编辑过的版本。 Download for pygame and livewires used.

最佳答案

有一种方法可以探索,通过从 pygame 调用方法 display.set_mode,就像在 Screen 类构造函数中一样 (l . 165 in game.py 来自你提供的包)

games.init(screen_width = 500, screen_height=100, fps = 50)

#[...] level stuffs...

games.screen._width = 100  # this setup the width property directly in the screen
games.screen._height = 500 # this setup the height property 
games.screen._display = pygame.display.set_mode((100, 500)) # this update the display

此解决方案的一个缺点是,如果您在屏幕更改期间保留一些事件 Sprite ,它们将无法在显示时正确更新删除方法,这可能会在绘画期间留下一些线条。为避免这种情况,您可以调用 games.screen.clear() 或者如果您需要选择性清理,您可以使用覆盖的 Sprite 类,它可以通过以下方式处理正确的方法调用 screen.blit_and_dirty

class MySprite(games.Sprite):
    def __init__(self, screen, *args, **kwargs):
        self.screen = screen
        super().__init__(*args, **kwargs)

    def _erase(self):
        """
        This method overrides the class method by using blit_and_dirty instead of blit_background
        """
        self.screen.blit_and_dirty(self.screen._background, self._rect)

要使用这个类,你只需要继承它,并添加屏幕参数:

class Ball(MySprite):
    def __init__(self, screen):
        super().__init__(screen, games.load_image('ball.png'), x=40, y=40, dx=2, dy=2)

    def update(self): 
        if self.left<=0 or self.right>=self.screen.get_width():
            self.dx*=-1

        if self.top<=0 or self.bottom>=self.screen.get_height():
            self.dy*=-1

你可以像下面这样声明一个实例:

games.init(screen_width = 500, screen_height=100, fps = 50)
games.screen.add(Ball(games.screen) )
...

关于Python livewires 调整屏幕大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50650456/

相关文章:

Python Tkinter/如何让OptionMenus共享一项列表?

python - 为什么默认 pygame.py 中的语法无效?

python - Pygame,livewires : Attribute error: Message object has no attribute 'handle collide'

python - 轮文件安装

python - 打印差异大于某个值的列表元素

python - 使用 python 日志记录在日志中插入空行的最佳方法是什么?

python - 在 Pygame 中两个正在进行的音乐轨道之间淡入淡出

python - 如何使一个功能在按下键时停止工作并使行更粗?

Python、Pygame、Livewires - 如何进行平滑的碰撞?

适合绝对初学者的 Python 编程 - Livewires 不起作用