Пример #1
0
 def testEmptyTile(self):
     baseRect = Rect(7 * TILE_SIZE + 2, 5 * TILE_SIZE + 8, 28, 18)
     # [1]
     baseRect.move_ip(0, 0)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [ ]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [ ]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [ ] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #2
0
 def testEmptyTile(self):
     baseRect = Rect(7 * TILE_SIZE + 2, 5 * TILE_SIZE + 8, 28, 18)
     # [1]
     baseRect.move_ip(0, 0)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [ ]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [ ]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [ ] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual(True, all(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #3
0
def main():
    """メインルーチン"""
    rect = Rect(0, 600, 10, 10)
    velocity = [5, -20]
    accel = (0, 0.5)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()


        SURFACE.fill((0, 0, 0))
        velocity[1] += accel[1];
        rect.move_ip(velocity)


        if rect.x > 600:
            rect.x = 0
        if rect.y > 600:
            velocity[1] = -20


        pygame.draw.rect(SURFACE, (255, 255, 255), rect)

        pygame.display.update()
        FPSCLOCK.tick(10)
Пример #4
0
class Ball(pygame.sprite.Sprite):
    def __init__(self,
                 initial_x,
                 initial_y,
                 radius=DEFAULT_RADIUS,
                 speed=[0, 0],
                 color=(0, 0, 0)):
        pygame.sprite.Sprite.__init__(self)
        self.speed = speed
        self.radius = radius
        self.rect = Rect(initial_x - self.radius, initial_y - self.radius,
                         self.radius * 2, self.radius * 2)
        self.color = color

    def move(self):
        self.rect.move_ip(self.speed[X_SPEED], self.speed[Y_SPEED])
        self.rect.clamp(SCREEN_RECT)

    def draw(self, win):
        pygame.draw.circle(win, self.color, self.rect.center, self.radius, 0)
Пример #5
0
def main():
    """メインルーチン"""
    rect = Rect(0, 0, 10, 10)  # 初期位置、大きさの設定
    velocity = (5, 2)
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.init()
                sys.exit()

        SURFACE.fill((0, 0, 0))
        rect.move_ip(velocity)  # 移動
        if rect.x > 600:
            rect.x = 0
        if rect.y > 600:
            rect.y = 0

        pygame.draw.rect(SURFACE, (255, 255, 255), rect)

        pygame.display.update()
        FPSCLOCK.tick(10)
Пример #6
0
class RpgSprite(pygame.sprite.Sprite):

    def __init__(self, spriteFrames, position = (0, 0)):
        pygame.sprite.Sprite.__init__(self)
        # properties common to all RpgSprites
        self.spriteFrames = spriteFrames
        self.position = [i * SCALAR for i in position]
        self.image, temp = self.spriteFrames.advanceFrame(0)
        # indicates if this sprite stands upright
        self.upright = True
        # indicates if this sprite is currently visible
        self.inView = False
        # indicates if this sprite is currently masked by any map tiles
        self.masked = False
        # indicates if this sprite should be removed on next update
        self.toRemove = False
        
    def setup(self, uid, rpgMap, eventBus):
        self.uid = uid
        self.rpgMap = rpgMap
        self.eventBus = eventBus
        
    def setTilePosition(self, tx, ty, level):
        self.x, self.y = tx, ty
        self.setPixelPosition(tx * TILE_SIZE + self.position[0],
                              ty * TILE_SIZE + self.position[1],
                              level)

    def setPixelPosition(self, px = 0, py = 0, level = None):
        # main rect
        self.rect = self.image.get_rect()
        # other rectangles as required by the game engine
        self.mapRect = self.image.get_rect()
        self.initBaseRect()
        # if required, move to the requested position
        if level:
            self.level = level
        if px > 0 or py > 0:
            self.doMove(px, py)
        
    def initBaseRect(self):
        baseRectWidth = self.mapRect.width 
        baseRectHeight = BASE_RECT_HEIGHT
        if hasattr(self, "baseRectSize"):
            baseRectWidth = self.baseRectSize[0]
            baseRectHeight = self.baseRectSize[1]
        baseRectTop = self.getBaseRectTop(baseRectHeight)
        baseRectLeft = (self.mapRect.width - baseRectWidth) / 2
        self.baseRect = Rect(baseRectLeft, baseRectTop, baseRectWidth, baseRectHeight)
        # print self.uid, self.baseRect.width, self.baseRect.height
        
    def getBaseRectTop(self, baseRectHeight):
        return self.mapRect.bottom - baseRectHeight
        
    def doMove(self, px, py):
        self.mapRect.move_ip(px, py)
        self.baseRect.move_ip(px, py)
        # a pseudo z order is used to test if one sprite is behind another
        self.z = int(self.mapRect.bottom + self.level * TILE_SIZE)

    def clearMasks(self):
        if self.masked:
            self.masked = False
            self.spriteFrames.repairCurrentFrame()
        
    def applyMasks(self):
        # masks is a map of lists, keyed on the associated tile points
        masks = self.rpgMap.getMasks(self)
        if len(masks) > 0:
            self.masked = True
            for tilePoint in masks:
                px = tilePoint[0] * view.TILE_SIZE - self.mapRect.left
                py = tilePoint[1] * view.TILE_SIZE - self.mapRect.top
                [self.image.blit(mask, (px, py)) for mask in masks[tilePoint]]
                
    def advanceFrame(self, increment, metadata):
        self.image, frameIndex = self.spriteFrames.advanceFrame(increment, **metadata)
        self.playSound(frameIndex)
        
    def playSound(self, frameIndex):
        pass
            
    def isIntersecting(self, sprite):
        if self != sprite and self.level == sprite.level and self.baseRect.colliderect(sprite.baseRect):
            return True
        return False;
        
    def processCollision(self, player):
        pass
    
    def processAction(self, player):
        pass
Пример #7
0
class MapCamera:
    def __init__(self, x_init_world, y_init_world, width_view_camera, height_view_camera):

        # rect that represents the surface visible of the camera
        self.rect_view_camera = Rect(x_init_world, y_init_world, width_view_camera, height_view_camera)

        self.pos_dest_screen = (0, 0)  # Position to draw the map captured by this Camera
        self.map_handler = None  # Controller of the map

        self.map_tile_surface = None  # Surface Buffer that contains the map drawn
        self.rect_hidden_camera = None  # Rect of the surface buffer with pos on map

        self.following = None
        self.can_follow = {"left": False, "top": False, "bottom": False, "right": True}

    # Start the camera setting buffers
    def init(self):
        assert self.map_handler is not None, "There's not a " + str(MapHandler.__class__) + " instance "
        assert self.map_handler.has_map_loaded(), "MapHandler has no map loaded yet!"
        self.__create_buffer()

    def __create_buffer(self):

        self.rect_hidden_camera = create_camera_rect(self.rect_view_camera, self.map_handler.get_tile_size())
        self.map_tile_surface = self.map_handler.create_image(self.rect_hidden_camera)

    def set_pos_screen(self, x_screen, y_screen):  # Set position to draw the map on screen
        self.pos_dest_screen = (x_screen, y_screen)

    def set_maphandler(self, map_handler):
        self.map_handler = map_handler

    # Move camera x y pixels
    def move(self, x_world, y_world):

        rect_moved = self.rect_view_camera.move(x_world, y_world)

        if self.map_handler.is_rect_out_of_map_bounds(rect_moved):
            return

        self.rect_view_camera.move_ip(x_world, y_world)

        if not self.rect_hidden_camera.contains(self.rect_view_camera):
            # If new pos is out of the buffer, we create a new one!
            self.__create_buffer()

    def draw(self, screen):

        rect_area_visible = Rect(self.rect_view_camera.x - self.rect_hidden_camera.x,
                                 self.rect_view_camera.y - self.rect_hidden_camera.y,
                                 self.rect_view_camera.width, self.rect_view_camera.height)
        screen.blit(self.map_tile_surface, self.pos_dest_screen, rect_area_visible)

    def get_x(self):
        return self.rect_view_camera.x

    def get_y(self):
        return self.rect_view_camera.y

    def follow(self, player):
        self.following = player

    def update(self):
        if self.following:
            x_p, y_p = self.following.x, \
                       self.following.y

            if self.can_follow['right']:
                if x_p > self.rect_view_camera.x + self.rect_view_camera.width / 4:
                    rect_moved = self.rect_view_camera.copy()
                    rect_moved.x = x_p - self.rect_view_camera.width / 4

                    if self.map_handler.is_rect_out_of_map_bounds(rect_moved):
                        return

                    self.rect_view_camera = rect_moved

                    if not self.rect_hidden_camera.contains(self.rect_view_camera):
                        # If new pos is out of the buffer, we create a new one!
                        self.__create_buffer()

    def is_rect_out_of_camera_bounds(self, rect):
        return not self.rect_view_camera.contains(rect)
Пример #8
0
 def testSpan2_2(self):
     # horizontally spans only one tile                          
     baseRect = Rect(6 * TILE_SIZE + 18, 1 * TILE_SIZE + 8, 28, 18)
     # [1] [1]
     baseRect.move_ip(0, 0)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1] [1,2] [1,2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1,2] [1,2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [1,2] [1,2] [2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [2] [2] [X] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [X] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [X] [X] [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #9
0
 def testSpan2_1(self):
     # horizontally spans only one tile                          
     baseRect = Rect(5 * TILE_SIZE + 18, 2 * TILE_SIZE + 8, 28, 18)
     # [1,2] [1,2] [S2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [2] [S1.5] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isVerticalValid(2, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isHorizontalValid(2, baseRect))
     # [S1.5] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, 0), rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [X] [S1.5] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, -1), rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, 0), rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [1] [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isHorizontalValid(1, baseRect))
     # [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1] [1] [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #10
0
 def testSpan1_1(self):
     # horizontally spans only one tile                          
     baseRect = Rect(5 * TILE_SIZE + 2, 2 * TILE_SIZE + 8, 28, 18)
     # [1,2] [S2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5] [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5] [S1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #11
0
class RpgSprite(pygame.sprite.Sprite):
    def __init__(self, spriteFrames, position=(0, 0)):
        pygame.sprite.Sprite.__init__(self)
        # properties common to all RpgSprites
        self.spriteFrames = spriteFrames
        self.position = [i * SCALAR for i in position]
        self.image, temp = self.spriteFrames.advanceFrame(0)
        # indicates if this sprite stands upright
        self.upright = True
        # indicates if this sprite is currently visible
        self.inView = False
        # indicates if this sprite is currently masked by any map tiles
        self.masked = False
        # indicates if this sprite should be removed on next update
        self.toRemove = False

    def setup(self, uid, rpgMap, eventBus):
        self.uid = uid
        self.rpgMap = rpgMap
        self.eventBus = eventBus

    def setTilePosition(self, tx, ty, level):
        self.tilePosition = (tx, ty)
        self.setPixelPosition(tx * TILE_SIZE + self.position[0],
                              ty * TILE_SIZE + self.position[1], level)

    def setPixelPosition(self, px=0, py=0, level=None):
        # main rect
        self.rect = self.image.get_rect()
        # other rectangles as required by the game engine
        self.mapRect = self.image.get_rect()
        self.initBaseRect()
        # if required, move to the requested position
        if level:
            self.level = level
        if px > 0 or py > 0:
            self.doMove(px, py)

    def initBaseRect(self):
        baseRectWidth = self.mapRect.width
        baseRectHeight = BASE_RECT_HEIGHT
        if hasattr(self, "baseRectSize"):
            baseRectWidth = self.baseRectSize[0]
            baseRectHeight = self.baseRectSize[1]
        baseRectTop = self.getBaseRectTop(baseRectHeight)
        baseRectLeft = (self.mapRect.width - baseRectWidth) / 2
        self.baseRect = Rect(baseRectLeft, baseRectTop, baseRectWidth,
                             baseRectHeight)
        # print self.uid, self.mapRect, self.baseRect

    def getBaseRectTop(self, baseRectHeight):
        return self.mapRect.bottom - baseRectHeight

    def doMove(self, px, py):
        self.mapRect.move_ip(px, py)
        self.baseRect.move_ip(px, py)
        # a pseudo z order is used to test if one sprite is behind another
        self.z = int(self.mapRect.bottom + self.level * TILE_SIZE)
        # print self.uid, self.mapRect, self.baseRect

    def clearMasks(self):
        if self.masked:
            self.masked = False
            self.spriteFrames.repairCurrentFrame()

    def applyMasks(self):
        # masks is a map of lists, keyed on the associated tile points
        masks = self.rpgMap.getMasks(self)
        if len(masks) > 0:
            self.masked = True
            for tilePoint in masks:
                px = tilePoint[0] * view.TILE_SIZE - self.mapRect.left
                py = tilePoint[1] * view.TILE_SIZE - self.mapRect.top
                [self.image.blit(mask, (px, py)) for mask in masks[tilePoint]]

    def advanceFrame(self, increment, metadata):
        self.image, frameIndex = self.spriteFrames.advanceFrame(
            increment, **metadata)
        self.playSound(frameIndex)

    def playSound(self, frameIndex):
        pass

    def isIntersecting(self, sprite):
        if self != sprite and self.level == sprite.level and self.baseRect.colliderect(
                sprite.baseRect):
            return True
        return False

    def processCollision(self, player):
        pass

    def processAction(self, player):
        pass
Пример #12
0
 def testSpan2_2(self):
     # horizontally spans only one tile
     baseRect = Rect(6 * TILE_SIZE + 18, 1 * TILE_SIZE + 8, 28, 18)
     # [1] [1]
     baseRect.move_ip(0, 0)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1] [1,2] [1,2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1,2] [1,2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [1,2] [1,2] [2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [2] [2] [X] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [X] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [X] [X] [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #13
0
 def testSpan2_1(self):
     # horizontally spans only one tile
     baseRect = Rect(5 * TILE_SIZE + 18, 2 * TILE_SIZE + 8, 28, 18)
     # [1,2] [1,2] [S2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [2] [S1.5] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isVerticalValid(2, baseRect))
     self.assertEqual((True, 2, -1), rpgMap.isHorizontalValid(2, baseRect))
     # [S1.5] [X]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1),
                      rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, 0),
                      rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [X] [S1.5] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1.5, -1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1),
                      rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, -1),
                      rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1.5, -1),
                      rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((False, 1.5, 0),
                      rpgMap.isHorizontalValid(1.5, baseRect))
     # [S1.5] [1] [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isVerticalValid(1, baseRect))
     self.assertEqual((True, 1, -1), rpgMap.isVerticalValid(1.5, baseRect))
     self.assertEqual((True, 1, 1), rpgMap.isHorizontalValid(1, baseRect))
     # [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1] [1] [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(4, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getSpanTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #14
0
 def testSpan1_1(self):
     # horizontally spans only one tile
     baseRect = Rect(5 * TILE_SIZE + 2, 2 * TILE_SIZE + 8, 28, 18)
     # [1,2] [S2]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S2] [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((False, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5] [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((True, 1.5), rpgMap.isMoveValid(2, baseRect))
     # [S1.5] [S1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [S1] [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(2, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 1.5), rpgMap.isMoveValid(1.5, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
     # [1]
     baseRect.move_ip(0, 16)
     self.assertEqual(1, len(rpgMap.getBaseRectTiles(baseRect)))
     self.assertEqual((True, 1), rpgMap.isMoveValid(1, baseRect))
     self.assertEqual((False, 2), rpgMap.isMoveValid(2, baseRect))
Пример #15
0
class SmartSprite(Sprite):
    __slots__ = ("_group", "_zindex", "_dirty", "_alpha", "_visible",
                 "damaged", "last_state", "rect", "image")

    def __init__(self, *args):
        if not hasattr(self, "image"):
            self.image = None
        if not hasattr(self, "rect"):
            self.rect = Rect(0, 0, 32, 32)

        self._group = args[0]
        self._visible = False
        self._zindex = 0
        self._dirty = True
        self._alpha = 255
        self.damaged = False
        self.damaged_areas = []
        self.last_state = None

        Sprite.__init__(self, *args)

    # __init__()

    def __cmp__(self, obj):
        return cmp(self._zindex, obj.zindex)

    # __cmp__()

    def get_dirty(self):
        return self._dirty

    # get_dirty()

    def set_dirty(self, v):
        self._dirty = v
        if v:
            self._group.dirty = True

    # set_dirty()
    dirty = property(get_dirty, set_dirty)

    def get_alpha(self):
        return self._alpha

    # get_alpha()

    def set_alpha(self, v):
        if v == self._alpha:
            return

        self._alpha = v
        if self.image:
            self.image.set_alpha(v, RLEACCEL)
            self.dirty = True

    # set_alpha()
    alpha = property(get_alpha, set_alpha)

    def get_zindex(self):
        return self._zindex

    # get_zindex()

    def set_zindex(self, v):
        if v == self._zindex:
            return

        self._zindex = v

    # set_zindex()
    zindex = property(get_zindex, set_zindex)

    def get_visible(self):
        return self._visible

    # get_visible()

    def set_visible(self, v):
        if v == self._visible:
            return

        self._visible = v
        self.dirty = True

    # set_visible()
    visible = property(get_visible, set_visible)

    def show(self):
        self.visible = True

    # show()

    def hide(self):
        self.visible = False

    # hide()

    def move(self, x, y):
        r = self.rect
        d = False

        if r.left != x:
            r.left = x
            d = True

        if r.top != y:
            r.top = y
            d = True

        if d:
            self.dirty = True

    # move()

    def move_rel(self, dx, dy):
        if dx == 0 and dy == 0:
            return

        self.rect.move_ip(dx, dy)
        self.dirty = True

    # move_rel()

    def last_state_changed(self):
        return self.last_state and self.last_state.changed()

    # last_state_changed()

    def mouse_click(self, x, y):
        pass

    # mouse_click()

    def __str__(self):
        return ("%s(rect=%s, zindex=%s, visible=%s, dirty=%s, "
                "alpha=%s, damaged=%s, group=%s, last_state=%s)" )% \
                (self.__class__.__name__, self.rect, self.zindex,
                 self.visible, self.dirty, self.alpha, self.damaged,
                 self._group, self.last_state)
Пример #16
0
class SmartSprite(Sprite):
    __slots__ = ("_group", "_zindex", "_dirty", "_alpha", "_visible",
                 "damaged", "last_state", "rect", "image")

    def __init__(self, *args):
        if not hasattr(self, "image"):
            self.image = None
        if not hasattr(self, "rect"):
            self.rect = Rect(0, 0, 32, 32)

        self._group = args[0]
        self._visible = False
        self._zindex = 0
        self._dirty = True
        self._alpha = 255
        self.damaged = False
        self.damaged_areas = []
        self.last_state = None

        Sprite.__init__(self, *args)
    # __init__()


    def __cmp__(self, obj):
        return cmp(self._zindex, obj.zindex)
    # __cmp__()


    def get_dirty(self):
        return self._dirty
    # get_dirty()


    def set_dirty(self, v):
        self._dirty = v
        if v:
            self._group.dirty = True
    # set_dirty()
    dirty = property(get_dirty, set_dirty)


    def get_alpha(self):
        return self._alpha
    # get_alpha()


    def set_alpha(self, v):
        if v == self._alpha:
            return

        self._alpha = v
        if self.image:
            self.image.set_alpha(v, RLEACCEL)
            self.dirty = True
    # set_alpha()
    alpha = property(get_alpha, set_alpha)


    def get_zindex(self):
        return self._zindex
    # get_zindex()


    def set_zindex(self, v):
        if v == self._zindex:
            return

        self._zindex = v
    # set_zindex()
    zindex = property(get_zindex, set_zindex)


    def get_visible(self):
        return self._visible
    # get_visible()


    def set_visible(self, v):
        if v == self._visible:
            return

        self._visible = v
        self.dirty = True
    # set_visible()
    visible = property(get_visible, set_visible)


    def show(self):
        self.visible = True
    # show()


    def hide(self):
        self.visible = False
    # hide()


    def move(self, x, y):
        r = self.rect
        d = False

        if r.left != x:
            r.left = x
            d = True

        if r.top != y:
            r.top = y
            d = True

        if d:
            self.dirty = True
    # move()


    def move_rel(self, dx, dy):
        if dx == 0 and dy == 0:
            return

        self.rect.move_ip(dx, dy)
        self.dirty = True
    # move_rel()


    def last_state_changed(self):
        return self.last_state and self.last_state.changed()
    # last_state_changed()


    def mouse_click(self, x, y):
        pass
    # mouse_click()


    def __str__(self):
        return ("%s(rect=%s, zindex=%s, visible=%s, dirty=%s, "
                "alpha=%s, damaged=%s, group=%s, last_state=%s)" )% \
                (self.__class__.__name__, self.rect, self.zindex,
                 self.visible, self.dirty, self.alpha, self.damaged,
                 self._group, self.last_state)