簡體   English   中英

Python livewires調整大小屏幕

[英]Python livewires resize screen

我正在嘗試使用具有多個級別的帶電電線來創建游戲。 在每個級別中,屏幕都需要具有不同的大小,因此我可以創建一個新屏幕,也可以調整其大小。 當我嘗試新屏幕時,如下所示(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中的add行以將屏幕數重置為0時,pygame中出現錯誤代替。

有誰知道一種調整大小的方法,或者銷毀並重新創建帶電屏幕?


注意:我使用的是Michael Dawsons的編輯版本。 下載使用的pygame和livewires。

您可以通過調用pygame中的 display.set_mode方法來探索的方式,例如在Screen類的構造函數中(您提供的包中game.py中的165)

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) )
...

您需要做的只是致電:

    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)

或者其他的東西。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM