簡體   English   中英

Python / Pygame-Sprite無法啟動

[英]Python/Pygame - Sprite fails to blit

我的播放器精靈無法啟動。 但是,當我取消對Rectangle代碼的注釋並注釋Sprite代碼時,它會完全消失。

加載圖像時我沒有收到錯誤消息,精靈根本不顯示。 控件也表現得有些呆板,除非它與牆壁碰撞並且跳躍無法正常工作,否則它似乎在空中“漂浮”。 我知道它可以找到圖像,因為直接blit可以工作。

每當我取消對Rect代碼的注釋並對load_image()代碼進行注釋時,控件就可以正常運行。

這是帶有load_image()的屏幕圖像。

這是沒有load_image()的屏幕圖像。

我已經嘗試過直接對圖片進行帳單處理,而不是for e in entities具有指定坐標的for e in entities循環中使用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()

如果執行此操作(在Camera.apply()方法中):

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

y坐標為-25,因此會在屏幕外閃爍圖像。 我不確定為什么會這樣,但這就是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM