【问题标题】:Python/Pygame - Sprite fails to blitPython/Pygame - Sprite 无法 blit
【发布时间】:2016-04-17 19:53:58
【问题描述】:

我的玩家精灵未能 blit。但是,当我取消注释 Rectangle 代码并注释 sprite 代码时,它会非常好。

加载图像时我没有收到错误消息,精灵根本不显示。控件也有点不稳定,它似乎“漂浮”在空中,除非它与墙壁碰撞并且跳跃不能正常工作。我知道它可以找到图像,因为直接 blit 有效。

每当我取消注释 Rect 代码并注释 load_image() 代码时,控件功能完美。

Here is an image of the screen with load_image().

Here is an image of the screen without load_image().

我尝试直接对图像进行 bilting,而不是使用具有指定坐标的 for e in entities 循环。它起作用了,但是由于使用了特定的 xy 值,图像显然停留在同一个地方,因此停留在屏幕上的坐标而不是显示玩家的动作。每当我运行screen.blit(player.image, camera.apply(player)) 时,它根本不会出现。

我在代码中省略了某些东西(关卡数组、其他类等),但我相信必需品就在这里。如果这有一个非常简单的解决方法,好吧,我会觉得很愚蠢:/

class Entity(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)

class Player(Entity):

  def __init__(self, x, y):
    Entity.__init__(self)
    self.xlevel = 0
    self.ylevel = 0
    self.onGround = False
   # self.image = Surface((32, 32))
   # self.image.fill(Color("#000000"))
   # self.image.convert()
   # self.rect = Rect(x, y, 32, 32)
    self.image, self.rect = load_image("zur.png", colorkey=((255, 255, 255)))


class Camera(object):
  def __init__(self, camera_func, width, height):
    self.camera_func = camera_func
    self.state = Rect(0, 0, width, height)

  def apply(self, target):
    return target.rect.move(self.state.topleft)

  def update(self, target):
    self.state = self.camera_func(self.state, target.rect)

def simple_camera(camera, target_rect):
  l, t, _, _ = target_rect
  _, _, w, h = camera
  return Rect(-l + HALF_WIDTH, -t + HALF_HEIGHT, w, h)

def complex_camera(camera, target_rect):
  l, t, _, _ = target_rect
  _, _, w, h = camera
  l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h # center player

  l = min(0, l)                           # stop scrolling at the left edge
  l = max(-(camera.width-WIN_WIDTH), l)   # stop scrolling at the right edge
  t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom
  t = min(0, t)                           # stop scrolling at the top

  return Rect(l, t, w, h)

def load_image(name, colorkey=None):
  fullname = os.path.join('assets', name)
  try:
    image = pygame.image.load(fullname)
  except pygame.error, message:
    print 'Cannot load image ' + name + ". Exiting."
    raise SystemExit, message
  image = image.convert()
  if colorkey is not None:
    if colorkey is -1:
      colorkey = image.get_at((0, 0))
    image.set_colorkey(colorkey, RLEACCEL)
  return image, image.get_rect()

def main():
  global cameraX, cameraY
  global mouseX, mouseY
  global bulletExists
  mouseX = 0
  mouseY = 0
  bulletExists = False
  pygame.init()

  screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)  
  pygame.display.set_caption("Tale of Zur")
  clock = pygame.time.Clock()

  up = down = left = right = running = False
  x = y = 0

  background_images = []
  platforms = []

  background_images.append(pygame.image.load("assets/starting_background_1.jpg").convert())

  entities = pygame.sprite.Group()
  player = Player(32, 32)
  bulletx = int(player.xlevel)
  bullety = int(player.ylevel)



    bullet = Bullet(player.rect.left, player.rect.top)
    bullet.update(player.rect.left, player.rect.top)
    screen.blit(background_images[0], [0, 0])


    if bulletExists is True:
      for i in range(10):
        if up and i is not 10:
          screen.blit(bullet.image, [bullet.bulletxpos + i, player.rect.top])  
        else:
          screen.blit(bullet.image, [bullet.bulletxpos - i, player.rect.top])


    camera.update(player)
    player.update(up, down, left, right, running, platforms)

    for e in entities:
      screen.blit(e.image, camera.apply(e))
    pygame.display.update()

【问题讨论】:

  • 您似乎没有将精灵添加到实体组,因此它是空的。
  • 我省略了程序的 while 循环和显示我将播放器添加到实体列表的代码。但是,有一篇文章写着entities.add(player),所以这不是问题。
  • 这不是一段可运行的代码,所以很难测试。
  • 道歉。 Here 是。

标签: python python-2.7 pygame


【解决方案1】:

如果你这样做(在 Camera.apply() 方法中):

r = target.rect.move(self.state.topleft)
print r

y 坐标为 -25,因此它会将图像从屏幕上移出。我不确定这是为什么,但这就是问题所在。

【讨论】:

  • 谢谢。我会尽力解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-30
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
相关资源
最近更新 更多