簡體   English   中英

我的基於滾動圖塊的平台游戲的圖塊沒有加載到屏幕上

[英]The tiles of my scrolling tile-based platformer aren't loading onto the screen

我已經在這個平台游戲上工作了幾個月,但遇到了一個問題:當我嘗試加載關卡的圖塊時,它們沒有出現。 另外,當我嘗試.blit()瓷磚時,游戲會像地獄一樣滯后。 使用.convert().convert_alpha()有所幫助,但並沒有完全刪除它,或者至少不足以使游戲可玩。 我試圖制作一個單獨的 for 循環,以便在更新它們的位置后一個接一個地加載瓷磚,但它沒有改變任何東西。 我嘗試在.blit()函數之后使用.flip()pygame.display.update()但徒勞無功。

這是瓷磚類:


class Tile(pygame.sprite.Sprite):

                def __init__(self,pos,size):
                                super().__init__()
                                self.image = pygame.Surface((size,size))
                                self.rect = self.image.get_rect(topleft = pos)
                                
                                
                def update(self,x_shift,y_shift,png,screen):
                                self.rect.x += x_shift
                                self.rect.y += round(y_shift)
                                screen.blit(png, (self.rect.x, self.rect.y))

游戲循環(不要介意凌亂的代碼):

def run(self):
                                player = self.player.sprite
                                
                                if self.innit == "false":
                                                self.innit = "true"
                                                self.scroll_y_position = player.rect.y
                                if self.menu == "false":
                                                self.menu = "true"
                                                self.button = Button((200,200),"start")
                                                self.go = "no"
                                if self.go == "no":
                                                self.go = self.button.update_start()
                                                self.button.update(self.display_surface)

#Everything above this comment is just for the menu and the innit function, the real game loop is below this comment
                                
                                else:
                                                self.player.update()
                                                self.tiles.update(self.world_shiftx, self.world_shifty,self.tile_image,self.display_surface)

                                                self.dangers.update(self.world_shiftx, self.world_shifty)
                                                self.dangers.draw(self.display_surface)

                                                self.portals.update(self.world_shiftx, self.world_shifty)
                                                self.portals.draw(self.display_surface)
                        
                                                self.scroll_x()
                                                player.rect.x += player.direction.x * player.speed  
                                                self.horizontal_movement_collision()

                                                self.scroll_y()
                                                player.rect.y += player.direction.y + self.world_shifty
                                                self.vertical_movement_collision()

                                                self.player.draw(self.display_surface)
                                                self.scroll_y_position += player.direction.y - 0.8

                                                self.player_events()

和 tile_image 變量:

self.tile_image = pygame.image.load("/home/yzaques/Platformer/tile.png")

提前致謝!

您正在使用pygame.sprite.Sprite 您的Sprite可能是通過pygame.sprite.Group.draw繪制的。 此方法使用Spriterectimage屬性來繪制它們。 因此,您需要更改image屬性而不是嘗試對新位圖進行位圖blit

class Tile(pygame.sprite.Sprite):
    def __init__(self,pos,size):
        self.image = pygame.Surface((size,size))
        self.rect = self.image.get_rect(topleft = pos)

    def update(self,x_shift,y_shift,png,screen):
        self.rect.x += x_shift
        self.rect.y += round(y_shift)
        self.image = png
        self.rect = self.image.get_rect(center = self.rect.center)       

暫無
暫無

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

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