I would like to know if possible a suggested manor of making it so, a character sprite, in a 2d game when walks toward off screen that the screen follows him. so if i had a map that was 1000x1000 that the 640x480 windows was always semi-focused on the character, but that if you got soo close to the edge of the screen that the screen would move up if you continued in that direction? any ideas on how i could do this or how i could achieve something simular.

Last edited by Yoriz on Sun Aug 04, 2013 1:00 pm, edited 1 time in total.
Reason:changed title

Here is an example using a solid image map (as opposed to a tiled map). The map stays centered on the player unless he is near an edge, in which case he moves off center. Example uses pixel perfect collision techniques (masks).

(Code below loads the images from my photobucket so it might take a second.)

class Player(object): """Our user controllable character.""" def __init__(self,location,speed): """The location is an (x,y) coordinate; speed is in pixels per frame. The location of the player is with respect to the map he is in, not the display screen.""" self.speed = speed self.image = PLAY_IMG self.mask = pg.mask.from_surface(self.image) self.rect = self.image.get_rect(center=location)

class Level(object): """A class for our map. Maps in this implementation are one image; not tile based. This makes collision detection simpler but can have performance implications.""" def __init__(self,map_image,viewport,player): """Requires an image from which to make a mask, and a player instance.""" self.image = map_image self.mask = pg.mask.from_surface(self.image) self.rect = self.image.get_rect() self.player = player self.player.rect.center = self.rect.center #Start at center of map. self.viewport = viewport

def update(self,keys): """Updates the player and then adjust the viewport with respect to the player's new position.""" self.player.update(self.mask,keys) self.update_viewport()

spamynator_1 wrote:Also i cannot read code i didnt write so well, would it be possible for you to demonstrate what happens here in non class format? as in like loops and if statements so i can read it.

I'm afraid learning classes is something you will have to do to move forward with game programming. If I were to rewrite that last program without classes it would be a mess. Also, all of the objects we deal with in pygame are classes so knowing how they work is not something one can avoid. Basically the program works like this:

while our program is running: Check events on the event queue. Run the current levels update function: Run the player's update method and check for collisions. Based on the player's confirmed position, update the level's viewport. Draw the updated level to the screen. Update the display. Limit fps using clock. Loop.

I think you should start with some basic movement and collision detection on a non-scrolling map before trying to move on.