On Thu, 20 Mar 2008 13:29:03 -0400
Michael Fiano <[EMAIL PROTECTED]> 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.



import os, sys, random
import pygame
from pygame.locals import *
import config
from data import *

class Player:

        def __init__(self, screen, background, topx, topy):

                # We need access to the screen
                self.screen = screen

                # We need access to the background surface
                self.background = background

                # Get the screen's dimensions from the config file
                self.SCREENWIDTH = config.screen_size[0]
                self.SCREENHEIGHT = config.screen_size[1]

                # Position the player in the center of the background
                # facing south
                self.x = self.SCREENWIDTH/2
                self.y = self.SCREENHEIGHT/2
                self.rect = pygame.Rect(self.x, self.y, 24, 32)
                self.frame = 3

                # Define some needed variables
                self.topx = topx
                self.topy = topy
                self.move = ""
                self.speed = 1
                self.dirtyrects = []
                self.scrollpos = [ 0, 0 ]

                # Define the graphic containing the player's sprites
                self.sprites_player = SpriteSheet('player.png')

                # Define the various frames within the player's spritesheet
                self.playerimgs = self.sprites_player.imgsat([ \
                        (24, 5, 24, 26),                # Frame 0: North1
                        (0, 6, 24, 26),                 # Frame 1: North2
                        (48, 6, 24, 26),                # Frame 2: North3
                        (24, 69, 24, 26),               # Frame 3: South1
                        (0, 70, 24, 26),                # Frame 4: South2
                        (48, 70, 24, 26),               # Frame 5: South3
                        (24, 101, 24, 26),              # Frame 6: West1
                        (0, 102, 24, 26),               # Frame 7: West2
                        (48, 102, 24, 26),              # Frame 8: West3
                        (24, 37, 24, 26),               # Frame 9: East1
                        (0, 38, 24, 26),                # Frame 10: East2
                        (48, 38, 24, 26)],-1)   # Frame 11: East3

        def draw(self):

                # Define the shadow graphic
                shadow,null = load_graphic("shadow.png")

                # Draw the player's shadow
                self.screen.blit(shadow, (self.x, self.y+13))

                # Draw the player to the screen
                self.screen.blit(self.playerimgs[self.frame], (self.x,self.y))
                self.dirtyrects.append(self.rect)

        def update(self, direction):

                # Move the player in the specified direction
                self.dirtyrects = []
                if direction is "north":
                        self.frame = 0
                        for x in range(24):
                                self.screen.fill((0,0,0))
                                self.screen.blit(self.background, 
self.scrollpos)
                                self.y = self.y-self.speed
                                if self.y < self.SCREENHEIGHT/4:
                                        self.y = self.SCREENHEIGHT/4
                                        if self.y > self.scrollpos[1]:
                                                self.scrollpos[1]+=self.speed
                                                print 
self.background.get_height()
                                self.rect[1] = self.y
                                self.draw()
                                if x % 12 == 0:
                                        self.frame+=1
                                        if self.frame > 2:
                                                self.frame = 1
                                if x == 23:
                                        self.frame = 0
                                pygame.time.delay(5)
                                pygame.display.update()
                if direction is "south":
                        self.frame = 3
                        for x in range(24):
                                self.screen.fill((0,0,0))
                                self.screen.blit(self.background, 
self.scrollpos)
                                self.y = self.y+self.speed
                                if self.y > self.SCREENHEIGHT - 
self.SCREENHEIGHT/4:
                                        self.y = self.SCREENHEIGHT - 
self.SCREENHEIGHT/4
                                        if self.y > self.scrollpos[1]:
                                                self.scrollpos[1]-=self.speed
                                self.rect[1] = self.y
                                self.draw()
                                if x % 12 == 0:
                                        self.frame+=1
                                        if self.frame > 5:
                                                self.frame = 4
                                if x == 23:
                                        self.frame = 3
                                pygame.time.delay(5)
                                pygame.display.update()
                if direction is "west":
                        self.frame = 6
                        for x in range(24):
                                self.screen.fill((0,0,0))
                                self.screen.blit(self.background, 
self.scrollpos)
                                self.x = self.x-self.speed
                                if self.x < self.SCREENWIDTH/4:
                                        self.x = self.SCREENWIDTH/4
                                        if self.x > self.scrollpos[0]:
                                                self.scrollpos[0]+=self.speed
                                self.rect[0] = self.x+self.speed
                                self.draw()
                                if x % 12 == 0:
                                        self.frame+=1
                                        if self.frame > 8:
                                                self.frame = 7
                                if x == 23:
                                        self.frame = 6
                                pygame.time.delay(5)
                                pygame.display.update()
                if direction is "east":
                        self.frame = 9
                        for x in range(24):
                                self.screen.fill((0,0,0))
                                self.screen.blit(self.background, 
self.scrollpos)
                                self.x = self.x+self.speed
                                if self.x >= self.SCREENWIDTH - 
self.SCREENWIDTH/4:
                                        self.x = self.SCREENWIDTH - 
self.SCREENWIDTH/4
                                        if self.x > self.scrollpos[0]:
                                                scroll_temp = self.scrollpos[0]
                                                self.scrollpos[0]-=self.speed
                                self.rect[0] = self.x-self.speed
                                self.draw()
                                if x % 12 == 0:
                                        self.frame+=1
                                        if self.frame > 11:
                                                self.frame = 10
                                if x == 23:
                                        self.frame = 9
                                pygame.time.delay(5)
                                pygame.display.update()

Reply via email to