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

Re: [pygame] how to bind surface rects without subsurface (need help with bug)

On Mon, Mar 9, 2009 at 10:31 PM, Michael Fiano <michael.fiano@xxxxxxxxx> wrote:
> I realize my code isn't the best code there is, but it's also my first
> programming project and I've been teaching myself all sorts of things
> as I go. But, could someone please take a look at code and
> tell me why the foreground isn't aligned when scrolling? To reproduce,
> you'll have to move the player toward the right side of the screen
> until it scrolls the map, and while it does pay attention to the huge
> trees and other foreground objects such as the tall grass.

Sure.  I love looking at other peoples code for problems.  (Honestly,
it's a lot more fun than trying to find problems in code I wrote)

This was hard, because while your code is not bad per say, there is a
lot of indirection, and much overengineering going on.  There are too
many strong connections between classes (like the player having to
know about the background and the foreground), and this coupling makes
it very inflexible.

In this case, the problem is simply a matter of ordering.  The
background is updated, then the player, then the foreground.  When you
start scrolling the screen (from the player update), the background
has already run it's update for that frame, while the foreground gets
to run its update with the move command.  So it is a frame ahead of
the background.

In scene.py, if you change the order that objects are updated:


Then the player will set up the scrolling for the next frame, which
fixes the problem.  A better way to do cameras, instead of having it
happening in the player movement function, is to have a separate
camera update that scrolls the right direction at a maximum speed
until whatever it is focused on is within some region of the screen.
That way if you start the player offscreen, the camera will still be
able to find him.  Also, for cutscenes, you can have the camera focus
on a different character than the player fairly easily.

Another thing that bugged me: It seems weird that you have limited
yourself to only two layers in the code, when any number of layers
would actually be easier.  A map.move() function, which moves all of
its layers by a set amount, means one less line all of the places you
have to write "background.move... foreground.move", and would also
solve the above problem (because the movement happens instantly
instead of waiting for some update).  And in the map, instead of
having background, and foreground, you could simply have layers[].  In
the future, when you want to add a paralax treeshadow layer to display
on top of the player and npc's, you will be hitting yourself at all of
the places you will have to add the new layer.

Good luck with the game, the sprites/tiles look really nice, and the
code isn't all bad :)