0

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

kryger
  • 12,906
  • 8
  • 44
  • 65
FrigidDev
  • 222
  • 1
  • 2
  • 13
  • tl;dr. Please post the _relevant_ code, where you think the problem is or atleast a simplified version of your problem. No one is going to sift through all that code. – sshashank124 May 10 '14 at 01:52
  • Working on it...and I don't think that this question deserves a downvote, I mean, COME ON. – FrigidDev May 10 '14 at 02:20
  • 1
    There are a bunch of issues with the code, but I have no idea which one might be causing your issue. I'd start with moving the map loading stuff out of the loop. Its silly to repeat that every frame. I'd also suggest only reading the file one time in one place, rather than having two different copies of it open (once as the global `file`, and once as `self.file` in `MapControl`). Your `CalcMapBorders` method doesn't really do what you'd expect, as it only counts the number of lines in the file once (and the length of the last line), despite an extra loop and getting called repeatedly. – Blckknght May 10 '14 at 02:40
  • As for CalcMapBorders, it does exactly what I want it to do. And as for moving the "map loading stuff" out of my loop, if I don't update the map every loop, how will I "move" it? Any alternatives? – FrigidDev May 10 '14 at 03:03

1 Answers1

1

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.
pmoleri
  • 4,238
  • 1
  • 15
  • 25
  • Ah, I didn't know that I could do that within rect, you find new things about python every day! Thanks, I'll check it out. – FrigidDev May 10 '14 at 05:15