python - 由于 PyGame 错误需要帮助

标签 python python-3.x pygame

我正在使用 Python 3.5.1 和 PyGame 开发 Space Invaders 克隆。我的问题是我无法将 Sprite 加载到屏幕上。我不断收到错误:

回溯(最近一次调用最后一次): 文件“C:\Users\supmanigga\Documents\Space Invaders\spaceinvaders.py”,第 44 行,位于 allSprites.draw(屏幕) 文件“C:\Users\supmanigga\AppData\Local\Programs\Python\Python35\lib\site-packages\pygame\sprite.py”,第 475 行,在绘制中 self.spritedict[spr] = surface_blit(spr.image, spr.rect) 属性错误:“船舶”对象没有属性“图像”

我的代码如下:

import pygame
import sys

width = 500
height = 700

white = (255, 255, 255)
black = (0, 0, 0)

score = 0

screen = pygame.display.set_mode([width, height])

class Ship(pygame.sprite.Sprite):
    def _init_(self):
        sprite.Sprite._init_(self)
        self.image = pygame.image.load("player").convert()
        self.rect = self.image.get_rect()

class Enemy(pygame.sprite.Sprite):
    def _init_(self):
        sprite.Sprite._init_(self)
        self.image = pygame.image.load("enemy").convert()
        self.rect = self.image.get_rect()

class Bullet(pygame.sprite.Sprite):
    def _init_(self):
        sprite.Sprite._init_(self)
        self.image = pygame.image.load("laser").convert()
        self.rect = self.image.get_rect()

player = Ship()
allSprites = pygame.sprite.Group()
allSprites.add(player)

running = True
while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    screen.fill(black)

    allSprites.draw(screen)

    pygame.display.flip()

pygame.quit()

最佳答案

def _init_(self): 应该是 def __init__(self):

否则,self.image = pygame.image.load("player").convert() 行永远不会执行,因此您的 Ship 实例将具有没有 image 属性。

关于python - 由于 PyGame 错误需要帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159513/

相关文章:

python-3.x - 使用 PySpark 和 Jupyter 使用 Spacy 解析文本时出错

Python3.2 : Extended sum() function -- compute the "value" of a list, 除非不加法

python - 当我使用 pygame.transform.rotate() 时,它会移动我的图像,这样,如果连续使用,它将离开屏幕。为什么?

python - 错误CreatImage()-opencv

python - 在定义的条件下使用 pandas value_counts()

python - select_lated 不加入标记为 null=True 的列

python - C-扩展 : Ref count of generators

python - QUIT pygame 事件未定义

python - 怎样才能让球朝正确的方向弹跳

python - 在没有 Visual Studio 的情况下为 python 安装 MS C++ 14.0