如何修复无法正确加载的对象图像

时间:2019-03-29 09:31:29

标签: python python-3.x image class pygame

我正在编写一个代码,可以在屏幕上产生几个敌人。 敌人属性过去采用了用于所有敌人的颜色属性,现在我尝试将其更改为可以加载的图像。 我也对此感兴趣,以便可以从图像中获取get_rect(),以检查敌人何时在屏幕上的某个位置。

问题在于,当我尝试用image属性替换color属性时,先前的代码是如此依赖于使用color属性加载敌人,以至于我很难使代码正常工作。 使用我编写的当前代码,它无法正确加载图像。

这是我开始使用的旧版本(功能,使用color属性):


import sys
import pygame as pg


class Enemy:

    def __init__(self, pos, color):
        self.rect = pg.Rect(pos, (26, 45))
        self.color = color
        self.pos = pg.math.Vector2(pos)

    def update(self, player):
        vel = (player.center - self.pos).normalize() * 4
        self.pos += vel
        self.rect.center = self.pos

    def draw(self, screen):
        pg.draw.rect(screen, self.color, self.rect)


def main():
    screen = pg.display.set_mode((640, 480))
    bg_color = pg.Color('gray12')
    player_color = pg.Color('dodgerblue1')
    enemy_color = pg.Color('sienna1')
    clock = pg.time.Clock()
    player = pg.Rect((100, 300), (26, 50))
    enemy_list = [Enemy((100, 300), enemy_color)]

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_f:
                    enemy_list.append(Enemy((400, 0), enemy_color))

        keys = pg.key.get_pressed()
        if keys[pg.K_a]:
            player.x -= 5
        elif keys[pg.K_d]:
            player.x += 5
        if keys[pg.K_w]:
            player.y -= 5
        elif keys[pg.K_s]:
            player.y += 5

        for enemy in enemy_list:
            enemy.update(player)

        screen.fill(bg_color)
        pg.draw.rect(screen, player_color, player)
        for enemy in enemy_list:
            enemy.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

以下是我尝试更改的代码,但到目前为止无法正常工作:


import sys
import pygame as pg
import os
from os import path

pg.init()

screen = pg.display.set_mode((640, 480))

game_folder = os.path.dirname(__file__)
img_folder = os.path.join(game_folder, "images")

enemy_img = pg.image.load(path.join(img_folder, "goblin.png"))

class Enemy(pg.sprite.Sprite):
    def __init__(self, pos, image):
      pg.sprite.Sprite.__init__(self)
      self.image = pg.transform.scale(enemy_img, (48,37))
      self.image.set_colorkey((0,0,0))
      self.rect = self.image.get_rect()
      self.pos = pg.math.Vector2(pos)

    def update(self, player):
        vel = [-5,0]
        self.pos += vel
        self.rect.center = self.pos

    def draw(self, screen):
        screen.blit(Enemy.image, Enemy.rect)


def main():
    bg_color = pg.Color('gray12')
    player_color = pg.Color('dodgerblue1')
    clock = pg.time.Clock()
    player = pg.Rect((100, 300), (26, 50))
    enemy_list = [Enemy((100, 300), enemy_img)]

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_f:
                    enemy_list.append(Enemy((400, 0), enemy_img))

        keys = pg.key.get_pressed()
        if keys[pg.K_a]:
            player.x -= 5
        elif keys[pg.K_d]:
            player.x += 5
        if keys[pg.K_w]:
            player.y -= 5
        elif keys[pg.K_s]:
            player.y += 5

        for enemy in enemy_list:
            enemy.update(player)

        screen.fill(bg_color)
        #pg.draw.rect(screen, player_color, player)
        for enemy in enemy_list:
            enemy.draw(screen)

        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()
    sys.exit()

有人可以帮助我找出问题所在以及应该更改或添加的代码吗?非常感谢您的宝贵时间。

1 个答案:

答案 0 :(得分:1)

您的可能是这一行:

screen.blit(Enemy.image, Enemy.rect)

此处您尝试访问image的{​​{1}} class属性,但是Enemy类没有这样的属性,并且也没有Enemy属性。

您可以使用

来解决此问题
rect

将使用实例属性

但是,由于您已经使用了screen.blit(self.image, self.rect) 类,所以不必费心自己在屏幕上绘制精灵并使用Group

Sprite