8

I am trying to create a game using livewires in which there are multiple levels. In each level, the screen will need to be a different size, so either I can create a new screen, or I can resize it. When I tried a new screen, like this (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()

I get the error:

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.width and height cannot be set (you can only get them), so I cannot do it like that, and when I change add line in livewires.games to reset the screen count to 0, I get an error in pygame instead.

Does anyone know of a way to resize, or else destroy and recreate the screen in livewires?


Note: I'm using Michael Dawsons edited version. Download for pygame and livewires used.

4

2 回答 2

3

您可以通过调用pygame中的方法来探索一种方法display.set_mode,例如在类构造函数中(您提供的包中的 game.py 中的 l.165)Screen

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

此解决方案的一个缺点是,如果您在屏幕更改期间保留一些活动精灵,它们将不会在显示时正确更新擦除方法,这可能会在绘制过程中留下一些线条。为避免这种情况,您可以调用,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)

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

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) )
...
于 2018-06-16T15:20:54.810 回答
0

您需要做的就是致电:

    games.screen.quit()

这将关闭屏幕,然后只需调用 games.init() 函数以使用新尺寸重新创建屏幕。您可能希望创建一个单独的值,每次通过一个级别时都会增加 - 例如

    height = (original value) + (level * increase)
    width = (original value) + (level * increase)
    games.init(screen_height = height, screen.width = width, fps = 50)

或者其他的东西。

希望这可以帮助。

于 2018-06-09T17:56:21.567 回答