python - 太空侵略者 : Loading Images

标签 python python-3.x pygame

我正在尝试将《太空入侵者》作为一个项目,我观看了有关创建它的视频,并且我已经做了很多工作。

我已经尝试过很多次了,但总有一个地方出了问题,从而搞乱了整个事情。

这是我在这里发表的第一篇文章,我不太确定如何做到这一点...让我们看看...

我一直面临这个错误,而且它没有改变:

C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py:198: DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  pygame.draw.rect(window, (0, 255, 0), (
Traceback (most recent call last):
  File "C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py", line 415, in <module>
    main_menu()
  File "C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py", line 410, in main_menu
    main()
  File "C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py", line 377, in main
    boss.shoot()
  File "C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py", line 90, in shoot
    laser = Laser(self.x - 20, self.y, self.laser_img)
  File "C:/Users/Dell/PycharmProjects/Tutorial(2)/main.py", line 109, in __init__
    self.mask = pygame.mask.from_surface(self.img)
TypeError: argument 1 must be pygame.Surface, not None

谁能帮我解决这个问题吗?我不知道在这里做什么。

最佳答案

看这里这一行

            boss = Boss(random.randrange(340, 360), random.randrange(-700, -100))

这就是实例化 Boss 的方式

现在看看您如何定义 Boss 类的init

 def __init__(self, x, y, health=200):
        self.x = x
        self.y = y
        self.health = health
        self.ship_img = None
        self.laser_img = None
        self.lasers = []
        self.cool_down_counter = 0

请注意self.laser_img = None

现在请注意您遇到的错误:

    laser = Laser(self.x - 20, self.y, self.laser_img)

你的第三个参数是 None。现在看看错误的下一部分:

        self.mask = pygame.mask.from_surface(self.img)

self.img 实际上是 None,这就是您收到错误的原因。

关于python - 太空侵略者 : Loading Images,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63257873/

相关文章:

python - 获取实例的类名

python - 如何简化python中的for循环

python-3.x - 为什么我在Python中得到重复的输出?

python-3.x - 如何从 target.com 产品页面抓取产品价格?

python-3.x - 如何从 CSV 文件将数据插入 Influxdb

python - tensorflow上的“Illegal instruction (core dumped)”> 1.6

c++ - 带有模板化类和派生类的 Swig shared_ptr 宏

python - cx_Freeze Exe 应用程序一打开就关闭

python - 我需要帮助让我的物体四处移动

python - 与实时解释器一起运行 python 应用程序