2016-04-17 12 views
0

私のスプライトがblitできません。ただし、Rectangleコードのコメントを外してスプライトコードにコメントを付けると、完全にうまく描画されます。Python/Pygame - スプライトがブリットに失敗する

イメージをロードするときにエラーメッセージが表示されません。スプライトは表示されません。コントロールはまた、壁に衝突して右に機能していない場合を除き、空中で「浮動」しているように見えます。私はそれが直接blitが働くので、イメージを見つけることができることを知っています。

Rectコードのコメントを外してload_image()コードにコメントするたびに、コントロールは完全に機能します。

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

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

Iは直接画像を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() 
+0

エンティティグループにスプライトを追加したようには見えないため、空です。 – marienbad

+0

プログラムのwhileループと、私がエンティティリストにプレーヤーを追加していることを示すコードを省略しました。しかし、 'entities.add(player)'と言うピースがあるので、問題はありません。 –

+0

これは実行可能なコードではないため、テストするのは難しいです。 – marienbad

答えて

0

あなたは(Camera.apply()メソッドで)これを行う場合は、次の

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

のyコ-ordinateは-25にあるので、画像を画面外に表示しています。なぜこのようなことがわかりませんが、これが問題です。

+0

ありがとうございます。私はこれを理解するために最善を尽くします。 –

関連する問題