[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Re: question about scrolling the world
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] Re: question about scrolling the world
- From: "Jake b" <ninmonkeys@xxxxxxxxx>
- Date: Wed, 2 Apr 2008 10:45:45 -0500
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Wed, 02 Apr 2008 11:45:54 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; bh=8Fb8NBi+hETmILGtcGkBROi3aalXkeVwQOVv9d8hPww=; b=jzJiNSdslp+O/c+Yqha1h4oitN1jVjnNZkA4Evxv3wPB+NNuWdq1XwiStkI5OzLuUOSIQLdeYLX47T3wbptmsS7mZL87UHZZHB7mQq6i2zYznnCg/0al3KmJfcAQSuQhheGhzMc9pbJ9uWTOBOlGPugU6Z5zddVEJjO51X5VfVk=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=lXs4IQNyB2cYWnAa1ahT8N0oT1bsWAnoDnGWDc317EtEjAN5/P8tlfzWbhXcIAYXnD2NCXioeSr9rBK1wr81D56nmCbR96ynvGCZ3/L92pnswfTza9sUeudkjKIEiMVDejQLLOBFtjar/v6Xz0BEI0jgJmmatH74sxBzwoIzn/Q=
- In-reply-to: <20080321234830.4da9a559@xxxxxxxxxxxxx>
- References: <20080320132903.7042c386@xxxxxxxxxxxxx> <20080321234830.4da9a559@xxxxxxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
( 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.