Saturday, September 8, 2012

Scrolling

I've been trying to figure out how to scroll the background scene from left to right and up and down.

I initially started using the pygame Surface.scroll feature for image surfaces.  Although I was able to quickly get the image to scroll with the cursor keys, I'm having a problem with redrawing the image properly.

As I was searching for a solution, I found this link to Parallax scrolling, which seems interesting.  Just I was about to try parallax scrolling, I opened up the scrolling example that ships with pygame and realized that there is a cool method for rectangles called, Rect.move_ip.  Although I had seen this in the documentation before, I always assumed that ip dealt with the networking.  Well, it turns out that ip stands for In Place, not Internet Protocol.  Wow, move in place.  With this, I was off and scrolling in no time.





I'm using a single image in this short example and simply controlling movement and sight with the joystick hats.  Here's a lengthy way to pan the image around.

    h_axis = joy.get_axis(0)

    v_axis = joy.get_axis(1)
    x = int(h_axis * scroll_speed)
 
    y = int(v_axis * scroll_speed)
 
    if scenery_rect.left < 0 and x < 0:
        scenery_rect.move_ip(-x, 0)
    elif scenery_rect.left > 0 and x < 0:
        scenery_rect.right = WIDTH
    elif x >= 0 and scenery_rect.right >= WIDTH:
        scenery_rect.move_ip(-x, 0)
    elif scenery_rect.right <= WIDTH and x > 0:
        scenery_rect.left = 0
    if scenery_rect.top < 0 and y < 0:
        scenery_rect.move_ip (0, -y)
    elif scenery_rect.bottom > HEIGHT and y >=0:
        scenery_rect.move_ip(0, -y)

Due to my sports injury, I've been spending more time with my student.  He's making great progress with his shooter game.  He's up to one jet, one stealth fighter and one pistol.    He also reduced the target area of the crosshairs to around 7 pixels.

No comments:

Post a Comment