문제

I fear that this question is way too localized, but I hope you'll still offer some advice with this situation.

I'll get straight to the point, a "grass" is continually being displayed in the upper left hand corner, with the occasional "cube" tile flashing there as well. I cannot find where my code is doing this, and it's really puzzling me. Please take a look at the following code, and post your thoughts on why my code would do this. I have attached a video for further clarification.

EDIT: Removed irrelevant code.

    class Grass(pygame.sprite.Sprite):
            def __init__(self, tileX, tileY):
                pygame.sprite.Sprite.__init__(self)
                self.image = tile_dict[0]
                self.rect = self.image.get_rect()
                self.tileX = tileX
                self.tileY = tileY
            def update(self):
                screen.blit(self.image, (self.tileX, self.tileY))

    class Rock(pygame.sprite.Sprite):
            def __init__(self, tileX, tileY):
                pygame.sprite.Sprite.__init__(self)
                self.image = tile_dict[1]
                self.rect = self.image.get_rect()
                self.tileX = tileX
                self.tileY = tileY
            def update(self):
                screen.blit(self.image, (self.tileX, self.tileY))


    class Cube(pygame.sprite.Sprite):
            def __init__(self, tileX, tileY):
                pygame.sprite.Sprite.__init__(self)
                self.image = tile_dict[2]
                self.rect = self.image.get_rect()
                self.tileX = tileX
                self.tileY = tileY
            def update(self):
                screen.blit(self.image, (self.tileX, self.tileY))


    tile_set = pygame.sprite.Group()


    class MapControl(object):
        def __init__(self, cameraX, cameraY):
            self.tileX = cameraX
            self.tileY = cameraY
            self.length = 0
            self.count = 0
            self.map_array = [] 
            self.file = open("C:\Users\Software Development\Desktop\Map00L0.txt")
        def LoadMap(self, map, cameraX, cameraY):
            self.tileX = cameraX    
            self.tileY = cameraY
            for x in map:
                for tile in x:
                    if tile == '0':
                        grass = Grass(self.tileX, self.tileY)
                        tile_set.add(grass)
                        self.tileX = self.tileX+32
                    if tile == '1':
                        rock = Rock(self.tileX, self.tileY)
                        tile_set.add(rock)
                        self.tileX = self.tileX+32

                    if tile == '2':
                        cube = Cube(self.tileX, self.tileY)
                        tile_set.add(cube)
                        self.tileX = self.tileX+32  
                    if tile == '\n':
                        self.tileX = cameraX
                        self.tileY += 32
                self.tileX = cameraX
                self.tileY = cameraY

        def CalcMapBorders(self, map):
            for ba in map:
                for line in self.file:
                    self.length += 1
                    self.count = len(list(line.strip('\n')))




    file = open('C:\Users\Software Development\Desktop\Map00L0.txt', 'r')   
    map00ly0 = list(file.read())    
    map = [map00ly0]

    def Loop(screen, map, cameraX, cameraY):
        cameraX, cameraY = 0,0
        player = Player(cameraX, cameraY)
        mapcontrol = MapControl(cameraX, cameraY)

        while gamestate.state == 'in_progress':
            sprite_list.add(player) 
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()



            screen.fill((0,0,0))

            mapcontrol.LoadMap(map, cameraX, cameraY)

            mapcontrol.CalcMapBorders(map)


            key = pygame.key.get_pressed()

            if key[pygame.K_DOWN] and not(key[pygame.K_LEFT] or key[pygame.K_RIGHT]):
                time.sleep(0.0)
                player.rect.y += 2
                player.index += 1
                player.list_index = 1
                player.mov = True
            if key[pygame.K_UP] and not(key[pygame.K_LEFT] or key[pygame.K_RIGHT]):
                time.sleep(0.0)
                player.rect.y -= 2
                player.index += 1
                player.list_index = 0
                player.mov = True
            if key[pygame.K_RIGHT]:
                time.sleep(0.0)
                player.rect.x += 2
                player.index += 1
                player.list_index = 3
                player.mov = True
            if key[pygame.K_LEFT]:
                time.sleep(0.0)
                player.rect.x -= 2
                player.index += 1
                player.list_index = 2
                player.mov = True       
            if not key[pygame.K_UP] and not key[pygame.K_DOWN] and not key[pygame.K_LEFT] and not key[pygame.K_RIGHT]:
                player.index = 0    



            if player.rect.y > height - 25:
                player.rect.y -= 2
                player.index += 1
                cameraY -= 3
            if player.rect.y < 0:
                player.rect.y += 2
                player.index += 1
                cameraY += 3
            if player.rect.x > width - 15:
                player.rect.x -= 2
                player.index += 1
                cameraX -= 3
            if player.rect.x < 0:
                player.rect.x += 2
                player.index += 1
                cameraX += 3
            if player.rect.y < cameraY:
                cameraY -= 3
            if player.rect.x < cameraX:
                cameraX -= 3
            if player.rect.y > (cameraY + (mapcontrol.length*32) - 15):
                cameraY += 3
            if player.rect.x > (cameraX + (mapcontrol.count*32) - 15):
                cameraX += 3

            tile_set.update()
            tile_set.draw(screen)
            tile_set.empty()
            sprite_list.update()        
            sprite_list.draw(screen)
            pygame.display.update()
            clock.tick(30)
            gamestate.state_eval()
    Loop(screen, map, cameraX, cameraY)

Youtube link to video here

도움이 되었습니까?

해결책

You have two problems:

  1. You are drawing in Sprite.update(), then you are calling tile_set.draw(screen), so you are drawing twice every tile.

  2. You are not setting the rect attribute for your tiles, so when you call draw, every tile is drawn in the 0,0 position according to it's rect attribute.

To solve the problem, implement the Sprite sub classes like this:

class Grass(pygame.sprite.Sprite):
    def __init__(self, tileX, tileY):
        pygame.sprite.Sprite.__init__(self)
        self.image = tile_dict[0]
        self.rect = self.image.get_rect(topleft=(tileX,tileY))
  1. eliminate the update method (if you need it in the future, don't draw in it).
  2. set coordinates in the get_rect method.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top