簡體   English   中英

pygame 表面在中間被切斷,盡管與屏幕尺寸相同

[英]pygame surface is cutting off in the middle despite being the same size as the screen

screen = pygame.display.set_mode((WIDTH,HEIGHT))
canvas = pygame.Surface((WIDTH,HEIGHT))
def makefunnytiles(saice):
    global WIDTH
    global HEIGHT
    global screen
    global roomdata
    global tiledefinitions
    print("Baking Tiles")
    print(roomdata)
    index = 0
    index2 = 0
    for symbolic in saice:
        index2 = 0
        symbolic = symbolic.strip()
        print("sanity")
        for symbol in list(symbolic):
            print(symbol)
            if symbol in tiledefinitions:
                canvas.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2),(32 * index)))
            index2 += 1
        index += 1
    screen.blit(canvas, (0,0))
    pygame.display.flip()
    print("drew screen")
    print(str(canvas.get_width))

我遇到了一個問題,由於某種原因,canvas 在屏幕中間切斷。

切斷的圖像

我遇到的另一個問題是文件末尾的pgzrun.go() ,它使程序因以下錯誤而崩潰:

Traceback (most recent call last):
  File "C:\Users\RushVisor\Documents\Glamore\main.py", line 100, in <module>
    pgzrun.go()
  File "E:\Python39\lib\site-packages\pgzrun.py", line 31, in go
    run_mod(mod)
  File "E:\Python39\lib\site-packages\pgzero\runner.py", line 113, in run_mod
    PGZeroGame(mod).run()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 217, in run
    self.mainloop()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 225, in mainloop
    self.reinit_screen()
  File "E:\Python39\lib\site-packages\pgzero\game.py", line 73, in reinit_screen
    self.mod.screen.surface = self.screen
AttributeError: 'pygame.Surface' object has no attribute 'surface'

我嘗試修改 canvas 和屏幕的分辨率值,甚至修改精靈的位置本身。 我將 canvas 傳送到屏幕上,而不是直接繪制到屏幕上,因為如果我是對的,它應該允許我更輕松地添加滾動。

我感謝任何人可以給我的任何幫助。

編輯:代碼在這里https://paste.pythondiscord.com/tetogequze.py

錯誤

self.mod.screen.surface = self.screen

變量self.mod.screen接縫為 class pygame.Surface 的object 此 class 的對象沒有名為surface的變量,這就是錯誤AttributeError: 'pygame.Surface' object has no attribute 'surface'似乎來自。

如果您不想使用表面屬性實現額外的 class ,這應該可以解決問題: self.mod.screen = self.screen 但不要忘記,這不會復制表面self.screen ,如果你需要這樣的東西: self.mod.screen = self.screen.copy()

滾動

您的想法是實現滾動的一種可能方式。 另一個是您存儲某種來源並基於該來源的瓷磚。 如果你現在想滾動,你只需要改變你之前定義的這個原點。

原產地定義

origin = [10,10]  # or use pygame.math.Vector2 or something else

位代碼

index = 0
index2 = 0
for symbolic in saice:
    index2 = 0
    symbolic = symbolic.strip()
    print("sanity")
    for symbol in list(symbolic):
        print(symbol)
        if symbol in tiledefinitions:
            screen.blit(pygame.image.load("sprites/" + str(roomdata['area']) + "/" + str(tiledefinitions[symbol]) + ".png").convert_alpha(), ((32 * index2)+origin[0],(32 * index)+origin[1]))
        index2 += 1
    index += 1

現在您只需要每幀調用一次 blit 代碼。 如果您現在想要滾動,只需執行以下操作: origin[0] += 10 (將所有符號向右移動)

上面的 blit 代碼不是最快的,因為它需要在每一幀上加載所有圖像,但這只是滾動如何與其他想法一起工作的一個示例

編輯:我對 Pygame-Zero 不是很熟悉,所以這只是基於我對標准 Pygame 的了解。

暫無
暫無

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

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