照相机跟随英雄

时间:2017-10-08 17:59:51

标签: python-3.x scroll pygame

追随英雄的相机

我在pygame中有一个错误,我试图让相机跟随英雄,但它似乎是我的游戏的两个实例。顶视图没有滚动。底部视图具有按我想要的方式滚动。两种观点都让我的英雄做同样的事情(射击,跳跃,移动)。任何人都可以帮我找到并解释为什么会这样吗?

pygame Screen --- has two identical games with different camera views

myCamera.py

class Camera:
    def __init__(self, camera_function, w, h):
        x, y = 0, 0
        self.cameraFrame = pygame.Rect(x, y, w, h)
        self.camera_function = camera_function

    def moveTiles(self, entity):
        return entity.rect.move(self.cameraFrame.topleft)

    def updateCamera(self, entity, screenWidth, screenHeight):
        self.cameraFrame = self.camera_function(screenWidth, screenHeight, self.cameraFrame, entity.rect)


def camera_function(screenWidth, screenHeight, camera, target_rect):
    half_width = screenWidth/2
    half_height = screenHeight/2
    left, top, _, _ = target_rect
    _, _, width, height = camera
    left, top, _, _ = half_width - left, \
                      half_height - top, \
                      width, \
                      height
    left = min(0, left)
    left = max(-(camera.width - screenWidth), left)
    top = min(0, top)
    top = max(-(camera.height - screenHeight), top)
    return pygame.Rect(left, top, width, height)

myGame.py

    level = [
    "----------------------------------------",
    "-                                      -",
    "-                                      -",
    "-                                      -",
    "-                                      -",
    "-                                      -",
    "-                                      -",
    "-                                      -",
    "----------------------------------------",
]

x, y = 0, 0
for row in level:
    for symbol in row:
        if symbol == "-":
            w, h = TILE_SIZE, TILE_SIZE
            sprite = mySprites.Entity(x, y, w, h)
            wallSprites.add(sprite)
            allSprites.add(sprite)
        x += TILE_SIZE
    y += TILE_SIZE
    x = 0


def getLevelSize(level):
    width = len(level[0]) * TILE_SIZE
    height = len(level) * TILE_SIZE
    return [width, height]



[levelWidth, levelHeight] = getLevelSize(level)

done = False
clock = pygame.time.Clock()
camera = myCamera.Camera(myCamera.camera_function,
                         levelWidth, levelHeight)

fps = 60

while not done:
    # Event Handling
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                hero.moveLeft()
            elif event.key == pygame.K_RIGHT:
                hero.moveRight()
            elif event.key == pygame.K_UP:
                hero.aimUp()
                hero.stopMoving()
            elif event.key == pygame.K_DOWN:
                hero.aimDown()
                hero.stopMoving()
            elif event.key == pygame.K_SPACE:
                w, h = 5, 5
                bullet = mySprites.Bullet(hero.rect.centerx, hero.rect.centery, w, h)
                bullet.setHeading(hero.shootingAngle)
                allSprites.add(bullet)
                bulletSprites.add(bullet)
            elif event.key == pygame.K_j:
                hero.jump()
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                hero.moveLeft()
            elif event.key == pygame.K_RIGHT:
                hero.moveRight()


    # Clear the Screen
    screen.fill(myColors.getColor("white"))

    # Collision Detection
    L = pygame.sprite.spritecollide(hero, wallSprites, False)
    for wall in L:
        if hero.dx > 0:
            hero.rect.right = wall.rect.left
            hero.dx = 0
        elif hero.dx < 0:
            hero.rect.left = wall.rect.right
            hero.dx = 0
        elif hero.dy > 0:
            hero.rect.bottom = wall.rect.top
            hero.dy = 0
            hero.onGround = True
        elif hero.dy < 0:
            hero.rect.top = wall.rect.bottom
            hero.dy = 0

    pygame.sprite.groupcollide(bulletSprites, wallSprites, True, False)

    # Draw Sprites
    allSprites.draw(screen)

    # Update Hero
    hero.update()

    # Update Camera
    camera.updateCamera(hero, SCREEN_WIDTH, SCREEN_HEIGHT)


    for sprite in allSprites:

        if isinstance(sprite, mySprites.Bullet):
            sprite.update()
        screen.blit(sprite.image, camera.moveTiles(sprite))



    # Update Display
    pygame.display.flip()

    # Next Frame
    clock.tick(fps)

pygame.quit()

1 个答案:

答案 0 :(得分:0)

解决。我意识到我正在画两次精灵。一旦使用allSprites.draw(),另一个使用screen.blit()。通过删除allSprites.draw(),问题得以解决。