[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] scrolling maps



Gabriele Farina wrote:
> How can I scroll my pygame screen??
> I got a TXT file where I saved a lot of char...any char represents a
> 25x25px image, that I blit on the screen...when The game starts, I see
> only 1/4 o the map...I want the my Hero stay al the centre of the map,
> and when ho moves, pygame has to del the images we don't see and has to
> draw the one we can see...
> 
> Someone have something to send to me or have some link where I can see
> any example?

fullscreen scrolling doesn't mean we "delete" what is seen or anything like
that. the regular way to do things is redraw the entire screen each frame.

of course if the background doesn't always scroll you can find ways to
optimize this, but it gets tricky to create consistent framerates if you
draw the background sometimes and not others.

if you have a dictionary with the proper image at each "x, y" coordinate,
the code would look something like this


tilewidth = 50
tileheight = 50
game_map = {dictionary of images for "x,y" pairs}
map_position = 10, 10

def draw_map(screen):
  screenrect = screen.get_rect()
  for y in range(0, screenrect.height, tileheight):
    for x in range(0, screenrect.width, tilewidth):
      pos = map_position[0]+x, map_position[1]+y
      try: img = game_map[pos]
      except KeyError: continue #off edge of map
      blitpos = x*tilewidth, y*tileheight
      screen.blit(img, blitpos)


anyways, that will do it, (give or take why my email editor didn't catch as
an error). you can move the map by chaning "map_position", which should be
the tile drawn at the topleft corner


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org