Re: [pygame] sprite groups and tilesheets

2008-11-19 Thread Patrick Mullen
On Wed, Nov 19, 2008 at 7:59 PM, Charlie Nolan <[EMAIL PROTECTED]> wrote:
>> A good thing to do in a tile based game, is to blit all of the tiles
>> to a single surface, so you aren't doing a bunch of blits every frame,
>> and then just redraw the whole screen each frame using the rendered
>> map.  If you have animated tiles of course, depending on how many
>> tiles on screen are likely to be animated, this optimization will not
>> be helpful.
>
> It's also not going to be helpful if your map is large.  Assuming SDL
> is willing to create a big enough surface (I've hit the limit with a
> few bugs), you'll be using a ton of memory.
>
> -FM
>

In this case I hope you aren't drawing all of the tiles at once either
:)  Either way you will have to set up a region, but setting up a
region will of course be much simpler with raw tiles.

Off topic, what's with SDL surface sizes being so restrictive?  That
has bitten me in the past with loading animation strips.  It's not
just a memory limit, but a dimensions limit.  I had to convert my
animations to grids to get around it, even though the amount of memory
for the load is the same.  I feel like I should be able to load the
maximum sized image that will fit in available memory.


Re: [pygame] sprite groups and tilesheets

2008-11-19 Thread Charlie Nolan
> A good thing to do in a tile based game, is to blit all of the tiles
> to a single surface, so you aren't doing a bunch of blits every frame,
> and then just redraw the whole screen each frame using the rendered
> map.  If you have animated tiles of course, depending on how many
> tiles on screen are likely to be animated, this optimization will not
> be helpful.

It's also not going to be helpful if your map is large.  Assuming SDL
is willing to create a big enough surface (I've hit the limit with a
few bugs), you'll be using a ton of memory.

-FM


Re: [pygame] sprite groups and tilesheets

2008-11-19 Thread Patrick Mullen
On Sat, Nov 15, 2008 at 3:27 PM, Greg Ewing <[EMAIL PROTECTED]> wrote:
> Michael Fiano wrote:
>>
>> i want to have a faster
>> framerate when there is no scrolling by only updating the player and the
>> background and foreground contained in it's rect area.

Generally in a scrolling game, depending on how you deal with the
camera, the player is scrolling more often than not.  In this case
there is no gain for a faster framerate when not scrolling.  In any
case, if there is important gameplay that occurs while the screen may
be scrolling, such as dodging an enemy, then you have to make sure
everything works at that slower framerate.  If it's more like zelda
(the first one), where you get a non-scrolling screen and then there
is a short scrolling transition when you move off the screen, then
your case would make sense.

A good thing to do in a tile based game, is to blit all of the tiles
to a single surface, so you aren't doing a bunch of blits every frame,
and then just redraw the whole screen each frame using the rendered
map.  If you have animated tiles of course, depending on how many
tiles on screen are likely to be animated, this optimization will not
be helpful.


Re: [pygame] sprite groups and tilesheets

2008-11-15 Thread Greg Ewing

Michael Fiano wrote:

i want to have a faster
framerate when there is no scrolling by only updating the player and the
background and foreground contained in it's rect area.


You can do that by drawing the tiles overlapping that
rectangle. There's no need to pre-draw all the foreground
and background tiles into big surfaces first.

--
Greg


Re: [pygame] sprite groups and tilesheets

2008-11-15 Thread Jake b
Is this actually a bottleneck where you need performace?

Don't waste time optimizing unless you need it. And if you do
optimize, profile first. That way you know exactly what needs to be
optimized.
--
Jake


Re: [pygame] sprite groups and tilesheets

2008-11-15 Thread Andy Brown
This doesn't answer your question directly, but you could have a look
at my gamelet, "Scroll, Jump and Beguile" that does scrolling and only
redraws the whole screen when necessary.

On Sat, Nov 15, 2008 at 1:53 AM, Michael Fiano <[EMAIL PROTECTED]> wrote:
> On Fri, 14 Nov 2008 08:10:53 -0800
> James Paige <[EMAIL PROTECTED]> wrote:
>
>> If you are making a game with scrolling, you should probably get used
>> to the idea that you will be re-drawing everything every time.
>>
>> If I understand correctly, you are asking about how to redraw the
>> background and forgeground but NOT the player when you scroll, right?
>> But any scrolling implies that the character is going to be moving
>> too.
>
> The map isn't always scrolling. there is a 1/4 margin around the screen
> that activates the scrolling; it doesn't scroll unless the player tries
> to move more than 75% in any direction. i want to have a faster
> framerate when there is no scrolling by only updating the player and the
> background and foreground contained in it's rect area. 32x32 x3,
> compared to about 2000x2000 x3. i'm thinking theres a big difference,
> or am i not getting something?
>


Re: [pygame] sprite groups and tilesheets

2008-11-14 Thread Greg Ewing

James Paige wrote:

If you are making a game with scrolling, you should probably get used to 
the idea that you will be re-drawing everything every time.


Even if you're not doing scrolling, redrawing everything each
frame is usually the simplest thing to do.

In a tile-based game, you end up blitting just as many pixels
either way, and that's usually where the bottleneck is.

If optimization is needed, use dirty rects or something like
that.

--
Greg


Re: [pygame] sprite groups and tilesheets

2008-11-14 Thread Michael Fiano
On Fri, 14 Nov 2008 08:10:53 -0800
James Paige <[EMAIL PROTECTED]> wrote:

> If you are making a game with scrolling, you should probably get used
> to the idea that you will be re-drawing everything every time.
> 
> If I understand correctly, you are asking about how to redraw the 
> background and forgeground but NOT the player when you scroll, right? 
> But any scrolling implies that the character is going to be moving
> too.

The map isn't always scrolling. there is a 1/4 margin around the screen
that activates the scrolling; it doesn't scroll unless the player tries
to move more than 75% in any direction. i want to have a faster
framerate when there is no scrolling by only updating the player and the
background and foreground contained in it's rect area. 32x32 x3,
compared to about 2000x2000 x3. i'm thinking theres a big difference,
or am i not getting something?


Re: [pygame] sprite groups and tilesheets

2008-11-14 Thread Aaron Maupin

Michael Fiano wrote:


What must I do exactly to get scrolling to work?


if scrolling:
draw background
if scrolling will stop:
keep a copy of the background surface
draw player
else:
if player moves:
cover player old position with portion of background
draw player in new position

Since you've got foreground elements you'll have to add those in as 
well, depending on whether they're alpha blended and how you're storing 
them you may or may not be able to just redraw them over themselves.


Re: [pygame] sprite groups and tilesheets

2008-11-14 Thread James Paige
On Fri, Nov 14, 2008 at 10:10:56AM -0500, Michael Fiano wrote:
> I managed to get my 3 layers (bg, player, fg) rendered with
> LayeredDirty sprite groups. They all draw correctly and even in the
> right order. The problem I'm having now is getting the map to scroll
> without blitting manually. What must I do exactly to get scrolling to
> work? Basically what I want to do is redraw the player only when it's
> moving, and redraw the bg and fg only when it's scrolling. I included a
> gzipped tar of my project if anyone can give me some pointers.

If you are making a game with scrolling, you should probably get used to 
the idea that you will be re-drawing everything every time.

If I understand correctly, you are asking about how to redraw the 
background and forgeground but NOT the player when you scroll, right? 
But any scrolling implies that the character is going to be moving too.

...or maybe I misunderstand what you are trying to do.

---
James Paige


Re: [pygame] sprite groups and tilesheets

2008-11-13 Thread Andy Brown
For scrolling games, keep a "logical" rect that represents where the
character is in the world and either:

1) keep a "display" rect that represents where the character is on the
screen.  update it on every movement
2) every frame, look at the character's logical rect and the current
logical offset of the screen and calculate (taking into consideration
the edges of the world) where the character should be drawn.

On Thu, Nov 13, 2008 at 3:20 AM, Michael Fiano <[EMAIL PROTECTED]> wrote:
> On Tue, 11 Nov 2008 13:27:08 -0800 (PST)
> Devon Scott-Tunkin <[EMAIL PROTECTED]> wrote:
>
>> Here's a basic sprite class with a looping animation:
>>
>> class Bob(pygame.sprite.Sprite):
>>   def __init__(self):
>> self.frame = 0
>> self.images = load_sprite('bob.png')
>> self.image = self.images[self.frame]
>> self.rect = self.image.get_rect()
>> self.animcounter = 0
>> self.animspeed = 7
>>   def update(self):
>> self.animcounter = (self.animcounter + 1)%self.animspeed
>> if self.animcounter == 0:
>>   self.frame = (self.frame + 1)%len(self.images)
>> self.image = self.images[self.frame]
>>
>>
>>
>> bob = Bob()
>> where bob.update() #would be called in the main loop
>
> Thanks, Devon. I finally created a real sprite and i'm updating its
> position with self.rect. Only problem is this bit of pseudo-code:
>
> if moving down
>if self.rect.bottom > 3/4 of screen
>scrollpos = 0,-1
>else
>self.rect.move_ip(0,1)
>
> What this does is scrolls the map if the rect is more than 75% down the
> screen. But its not keeping track of the rect position when the screen
> is scrolling. The player can run past the edge of map and avoid the
> collisions because the rect isnt in the player's position since the
> background moved. How can I move the rect AND the map like this?
>


Re: [pygame] sprite groups and tilesheets

2008-11-13 Thread Michael Fiano
On Tue, 11 Nov 2008 13:27:08 -0800 (PST)
Devon Scott-Tunkin <[EMAIL PROTECTED]> wrote:

> Here's a basic sprite class with a looping animation:
> 
> class Bob(pygame.sprite.Sprite):
>   def __init__(self):
> self.frame = 0
> self.images = load_sprite('bob.png')
> self.image = self.images[self.frame]
> self.rect = self.image.get_rect()
> self.animcounter = 0
> self.animspeed = 7
>   def update(self):
> self.animcounter = (self.animcounter + 1)%self.animspeed
> if self.animcounter == 0:
>   self.frame = (self.frame + 1)%len(self.images)
> self.image = self.images[self.frame]
> 
> 
> 
> bob = Bob()
> where bob.update() #would be called in the main loop

Thanks, Devon. I finally created a real sprite and i'm updating its
position with self.rect. Only problem is this bit of pseudo-code:

if moving down
if self.rect.bottom > 3/4 of screen
scrollpos = 0,-1
else
self.rect.move_ip(0,1)

What this does is scrolls the map if the rect is more than 75% down the
screen. But its not keeping track of the rect position when the screen
is scrolling. The player can run past the edge of map and avoid the
collisions because the rect isnt in the player's position since the
background moved. How can I move the rect AND the map like this? 


Re: [pygame] sprite groups and tilesheets

2008-11-11 Thread Devon Scott-Tunkin
its returning a list so you cant have self.image = load_sprite('player.png').

you need something like: 

self.images = load_sprite('player.png')
self.image = self.images[0] #for first frame

or

self.frame = 0
self.image = self.images[self.frame] 
#if you want to update it later with the different animation frames.

Here's a basic sprite class with a looping animation:

class Bob(pygame.sprite.Sprite):
  def __init__(self):
self.frame = 0
self.images = load_sprite('bob.png')
self.image = self.images[self.frame]
self.rect = self.image.get_rect()
self.animcounter = 0
self.animspeed = 7
  def update(self):
self.animcounter = (self.animcounter + 1)%self.animspeed
if self.animcounter == 0:
  self.frame = (self.frame + 1)%len(self.images)
self.image = self.images[self.frame]



bob = Bob()
where bob.update() #would be called in the main loop


Devon

--- On Mon, 11/10/08, Michael Fiano <[EMAIL PROTECTED]> wrote:

> From: Michael Fiano <[EMAIL PROTECTED]>
> Subject: [pygame] sprite groups and tilesheets
> To: pygame-users@seul.org
> Date: Monday, November 10, 2008, 10:52 PM
> I am having problems adding my player sprite to a sprite
> group since
> self.image needs a surface, though it is a list of
> animation frames for the player in my code and I can't
> figure out what I need to do. Can anybody give me some
> pointers? Thanks.
> 
> self.image = load_sprite('player.png')
> 
> class load_sprite:
>   def __init__(self, filename):
>   self.sheet =
> pygame.image.load(os.path.join('data',
> 'graphics', filename))
>   def imgat(self, rect, colorkey = None):
>   rect = Rect(rect)
>   image = pygame.Surface(rect.size).convert()
>   image.blit(self.sheet, (0, 0), rect)
>   if colorkey is not None:
>   if colorkey is -1:
>   colorkey = image.get_at((0, 0))
>   image.set_colorkey(colorkey, RLEACCEL)
>   return image
>   def imgsat(self, rects, colorkey = None):
>   imgs = []
>   for rect in rects:
>   imgs.append(self.imgat(rect, colorkey))
>   return imgs


  


Re: [pygame] sprite groups and tilesheets

2008-11-10 Thread Andy Brown
If your sprite object's .image attribute isn't a Surface, then it's
not really a Sprite.  All Sprite objects need to have a .image
attribute that is a Surface, and a .rect attribute that is a Rect.

just give your load_sprite attribute some other name.

On Mon, Nov 10, 2008 at 8:52 PM, Michael Fiano <[EMAIL PROTECTED]> wrote:
> I am having problems adding my player sprite to a sprite group since
> self.image needs a surface, though it is a list of animation frames for the 
> player in my code and I can't
> figure out what I need to do. Can anybody give me some pointers? Thanks.
>
> self.image = load_sprite('player.png')
>
> class load_sprite:
>def __init__(self, filename):
>self.sheet = pygame.image.load(os.path.join('data', 
> 'graphics', filename))
>def imgat(self, rect, colorkey = None):
>rect = Rect(rect)
>image = pygame.Surface(rect.size).convert()
>image.blit(self.sheet, (0, 0), rect)
>if colorkey is not None:
>if colorkey is -1:
>colorkey = image.get_at((0, 0))
>image.set_colorkey(colorkey, RLEACCEL)
>return image
>def imgsat(self, rects, colorkey = None):
>imgs = []
>for rect in rects:
>imgs.append(self.imgat(rect, colorkey))
>return imgs
>


[pygame] sprite groups and tilesheets

2008-11-10 Thread Michael Fiano
I am having problems adding my player sprite to a sprite group since
self.image needs a surface, though it is a list of animation frames for the 
player in my code and I can't
figure out what I need to do. Can anybody give me some pointers? Thanks.

self.image = load_sprite('player.png')

class load_sprite:
def __init__(self, filename):
self.sheet = pygame.image.load(os.path.join('data', 'graphics', 
filename))
def imgat(self, rect, colorkey = None):
rect = Rect(rect)
image = pygame.Surface(rect.size).convert()
image.blit(self.sheet, (0, 0), rect)
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image
def imgsat(self, rects, colorkey = None):
imgs = []
for rect in rects:
imgs.append(self.imgat(rect, colorkey))
return imgs