[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Re: question about scrolling the world



( If I don't make sense, just ask. )

I'm not sure what your doing in your update function. It looks like
you are running 24 game loops?

I use a game loop() function that is called every frame. It then calls
update() function which updates keyboard input, the players location,
frame number, etc. And then calls a draw() function which does the
blit'ing. Something like this:

def MainLoop(self):
    """Main game loop. The following is the general idea. You already
have the load_sprites(), draw_background(), etc... functions"""

    # pre-loop init
    load_sprites()

    # main loop
    while True:
        # == (1) handle events ==
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            # input: keydown
            elif event.type == KEYDOWN:
                # exit on 'escape'
                if (event.key == K_ESCAPE): sys.exit()

        # == (2) get input / do math ==
        player.update()
        monsters.update()

        # == (3) draw / render ==

        # clear screen
        self.screen.fill( pygame.color.Color("black") )

        # draw background
        draw_background()

        # render sprites
        player.draw()
        monsters.draw()

        # flip
        pygame.display.flip()

        # 'nice' CPU usage
        # if self.bLimitCPU: pygame.time.wait( self.limitCPU_time )

Take a look at using vectors. (
http://www.partiallydisassembled.net/euclid/vector-classes.html ) You
can simplify your code using vectors like this:

def update(self):
    """location is the Vector2(x,y) location. velocity is the
Vector2(x,y) speed"""
    self.location += self.velocity

Then when keys are pressed, you set the velocity:

def input(self):
    """pseudo-code."""

    # reset because if no keys are pressed, don't move.
    self.velocity = Vector2(0, 0)

    # get current key states.
    if left:
        self.velocity.x = -1
    if right:
        self.velocity.x = 1
    if up:
        self.velocity.y = -1
    if down:
        self.velocity.y = 1

This also lets you move diagonally.

In your player.update() function, check if the new location will be
offscreen. If so, don't move.

def update(self):
    """location is the Vector2(x,y) location. velocity is the
Vector2(x,y) speed.
    now prevents moving offscreen"""
    new_loc = self.location + self.velocity

    if new_loc.x > self.SCREEN_W: return
    if new_loc.x < 0: return
    if new_loc.y > self.SCREEN_H: return
    if new_loc.y < 0: return

    # new_loc is on screen, so move:
    self.location = new_loc

This will prevent the center ( player.location) from going offscreen.
But you probably want the whole sprite to stay onscreen. So to make
sure the whole sprite stays on screen, using location as the center of
the sprite, do:

def update(self):
    """location is the Vector2(x,y) location. velocity is the
Vector2(x,y) speed.
    now prevents moving offscreen
    now prevents any part of sprite moving offscreen"""
    new_loc = self.location + self.velocity

    if new_loc.x > self.SCREEN_W + self.SPRITE_W: return
    if new_loc.x < 0 - self.SPRITE_W: return
    if new_loc.y > self.SCREEN_H + self.SPRITE_H: return
    if new_loc.y < 0 - self.SPRITE_H: return

    # new_loc is on screen, so move:
    self.location = new_loc

On Fri, Mar 21, 2008 at 10:48 PM, Michael Fiano <michael.fiano@xxxxxxxxx> wrote:
> On Thu, 20 Mar 2008 13:29:03 -0400
>
> Michael Fiano <michael.fiano@xxxxxxxxx> wrote:
>
> I got it to scroll finally. The only problem I'm having now is making
> the scrolling stop so the player doesn't fall off the world :) The
> player stops at the north and west edges...but continues to scroll,
> walking off the world in the other 2 directions. Can anyone give me
> some pointers? The relevant code is in the update() function (yes, i
> know my update function is very inefficient...I'm trying to get it to
> work before I rewrite it). Thanks for all the help.