python - Pygame 2d tile scrolling edges don't load -


im trying make 2d game in pygame kinda works pokemon. ive gotten stuck on problem dont know how solve.

when move character scroll map instead player stays centered. i've created "animation" offsetting distance 2 pixels @ time instead of moving full tile size smooth movemen. problem have when move, screen doesn't load new tiles in edges i'm moving towards edges end white space until i've completed full animation. i'll link code , can me :)

tilesize = 32 map_width = 25 map_height = 25  class player(pygame.sprite.sprite):     def __init__(self, color, width, height):         # call parent class (sprite) constructor         super().__init__()         self.name = "player"         self.width = width         self.height = height         self.image = pygame.surface([width, height])         self.image.fill(color)         self.rect = self.image.get_rect()         self.rect.x = int(tilesize * (map_width / 2)) - tilesize / 2         self.rect.y = int(tilesize * (map_height / 2)) - tilesize / 2  class world:     def __init__(self):         self.shiftx = 0         self.shifty = 0         self.tile_map = [ [dirt w in range(map_width)] h in range(map_height)]         row in range(map_height):             column in range(map_width):                 try:                     if real_map[row + self.shifty][column + self.shiftx] == 0:                         tile = dirt                     elif real_map[row + self.shifty][column + self.shiftx] == 1:                         tile = grass                     elif real_map[row + self.shifty][column + self.shiftx] == 2:                         tile = water                     else:                         tile = dirt                     self.tile_map[row][column] = tile                 except:                     self.tile_map[row][column] = water      def shiftworld(self):          row in range(map_height):             column in range(map_width):                 try:                     if real_map[row + self.shifty][column + self.shiftx] == 0:                         tile = dirt                     elif real_map[row + self.shifty][column + self.shiftx] == 1:                         tile = grass                     elif real_map[row + self.shifty][column + self.shiftx] == 2:                         tile = water                     else:                         tile = dirt                     self.tile_map[row][column] = tile                 except:                     self.tile_map[row][column] = water      def oktomove(self, key):         if key[k_w]:             if self.tile_map[int(map_width/2 - 1)][int(map_height/2)] != 2:                 return true         elif key[k_s]:             if self.tile_map[int(map_width/2 + 1)][int(map_height/2)] != 2:                 return true         elif key[k_a]:             if self.tile_map[int(map_width/2)][int(map_height/2) - 1] != 2:                 return true         elif key[k_d]:             if self.tile_map[int(map_width/2)][int(map_height/2) + 1] != 2:                 return true  def start_game():     pygame.init()     clock = pygame.time.clock()     #hÄr kan vi mÅla upp mer     #screen = pygame.display.set_mode((map_width*tilesize, map_height*tilesize))     world = world()     screen = pygame.display.set_mode((tilesize * (map_width-2), tilesize * (map_height-4)))     running = true     player = player(black, 32, 32)     sprites = pygame.sprite.group()     sprites.add(player)     movement = 0     offsety = 0     offsetx = 0     animation_north = false     animation_south = false     animation_west = false     animation_east = false      while running:         event in pygame.event.get():             if event.type==quit:                 pygame.quit()                 sys.exit()             elif event.type == pygame.keydown:                 #get keyinput , whatever needs done                 key = pygame.key.get_pressed()                 if key[k_escape]:                     pygame.quit()                     sys.exit()                  if animation_east or animation_north or animation_south or animation_west:                     pass                 else:                     if key[k_w]:                         oktomove = world.oktomove(key)                         if oktomove == true:                             animation_north = true                         else:                             pass                     elif key[k_a]:                         oktomove = world.oktomove(key)                         if oktomove == true:                             animation_west = true                      elif key[k_s]:                         oktomove = world.oktomove(key)                         if oktomove == true:                             animation_south = true                      elif key[k_d]:                         oktomove = world.oktomove(key)                         if oktomove == true:                             animation_east = true             if animation_north == true:             if movement == 32:                 movement = 0                 world.shifty -= 1                 world.shiftworld()                 offsety = 0                 animation_north = false              else:                 offsety += 4                 movement += 4          if animation_south == true:             if movement == 32:                 movement = 0                 world.shifty += 1                 world.shiftworld()                 offsety = 0                 animation_south = false                 inty = 0             else:                 offsety -= 4                 movement += 4          if animation_west == true:             if movement == 32:                 movement = 0                 world.shiftx -= 1                 world.shiftworld()                 offsetx = 0                 animation_west = false             else:                 offsetx += 4                 movement += 4          if animation_east == true:             if movement == 32:                 world.shiftx += 1                 world.shiftworld()                 movement = 0                 offsetx = 0                 animation_east = false             else:                 offsetx -= 4                 movement += 4           screen.fill(white)          row in range(map_height):             column in range(map_width):                 screen.blit(textures[world.tile_map[row][column]], (column*tilesize + offsetx, row*tilesize + offsety))           sprites.draw(screen)         pygame.display.update()         pygame.display.flip()         clock.tick(60)  start_game() 

i writing similar game , share logic you. blocks 32x32 each block 32 pixesl.

enter image description here

the outer border sprites screen , inner square monitor. have 1 sprite on sides of screen. if count pixel movement on side of screen it's easy keep track of when need draw next row or column of sprites not drawn off screen. if pixel movement -8 (left movement) draw column of sprites on right side on border os screen outside visible area. same goes other side.

here code program. sprite adding code.

    def add_sprites(self):      """sprites added group appear on screen right. column number value in coldrawn. selct columns list according value. once end of column list reached start again first one. cycle thru list depending on numcycle[0] value."""      if self.coldrawn < self.columns_in_dungeon - 1:         self.coldrawn += 1      else:  # columns drawn increment flag         self.coldrawn = 0         self.numcycle[1] += 1      if self.numcycle[1] >= self.numcycle[0]:  # if flag equal number of cycles screen scrolled set numcycle[2] true         self.numcycle[2] = true      else:  # screen can scrolled         spritecol = self.all_wall_sprite_columns_list[self.coldrawn]         self.wallspritegroup.add(spritecol)  # add column of sprites sprite group     return 

and here sprite removing code.

    def remove_sprites(self):      """sprites removed group exit screen left."""      sprt in self.wallspritegroup:  # remove_basic sprites move_basic off screen on left          if sprt.rect.x <= -48:             sprt.rect.x = self.screenw # reset x position ,             sprt.kill()             #spritegrp.remove_basic(sprt)  # remove_basic sprite sprite group      return 

the code quite easy follow have commented them. hope helps.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -