简体   繁体   English

我的基于滚动图块的平台游戏的图块没有加载到屏幕上

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

I've been working on this platformer for a couple months now and I've encountered a problem: when I try to load the tiles of my level, they don't show up.我已经在这个平台游戏上工作了几个月,但遇到了一个问题:当我尝试加载关卡的图块时,它们没有出现。 Plus, the game lags like hell when I try to .blit() the tiles.另外,当我尝试.blit()瓷砖时,游戏会像地狱一样滞后。 Using .convert() and .convert_alpha() helped but it didn't entirely remove it, or at least not enough to make the game playable.使用.convert().convert_alpha()有所帮助,但并没有完全删除它,或者至少不足以使游戏可玩。 I've tried to make a seperate for loop to load the tiles one after then other right after updating their position, but it didn't change anything.我试图制作一个单独的 for 循环,以便在更新它们的位置后一个接一个地加载瓷砖,但它没有改变任何东西。 I tried to use .flip() and pygame.display.update() after the .blit() funtion but in vain.我尝试在.blit()函数之后使用.flip()pygame.display.update()但徒劳无功。

Here's the Tile class:这是瓷砖类:


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

The game loop (don't mind the messy code):游戏循环(不要介意凌乱的代码):

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

And tile_image variable:和 tile_image 变量:

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

Thanks in advance!提前致谢!

You're using pygame.sprite.Sprite s.您正在使用pygame.sprite.Sprite Likely your Sprites are drawn throughpygame.sprite.Group.draw .您的Sprite可能是通过pygame.sprite.Group.draw绘制的。 This method uses the rect and image attributes of the Sprites to draw them.此方法使用Spriterectimage属性来绘制它们。 Therefore, you need to change the image attribute instead of trying to blit the new bitmap:因此,您需要更改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