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

Re: [pygame] scrolling maps



I've been implementing scrolling by drawing the entire map onto a surface
larger than the display window, and blitting only the visible portion of the
full map surface to the display window. Are there any disadvantages to doing
it this way? I could well be wrong, but at a guess it seems to me that this
would be faster than nested for loops.


----- Original Message -----
From: "Pete Shinners" <shredwheat@attbi.com>
To: <pygame-users@seul.org>
Sent: Monday, October 21, 2002 3:25 AM
Subject: 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
>

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