尝试在 Python 中创建对象时出错 [英] Getting an Error Trying to Create an Object in Python

查看:70
本文介绍了尝试在 Python 中创建对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从 python 中的类创建一个对象,但出现错误,e_tank = EnemyTank()类型错误:组"对象不可调用"

I am trying to create an object from a class in python but I am getting an Error, "e_tank = EnemyTank() TypeError: 'Group' object is not callable"

我不确定这是什么意思,我试过谷歌,但我无法得到关于导致此错误的明确答案.有谁明白为什么我无法从 EnemyTank 类创建对象?

I am not sure what this means, I have tried Google but I couldn't get a clear answer on what is causing this error. Does anyone understand why I am unable to create an object from my EnemyTank Class?

这是我的代码:

#Image Variables
bg = 'bg.jpg'
bunk = 'bunker.png'
enemytank = 'enemy-tank.png'

#Import Pygame Modules
import pygame, sys
from pygame.locals import *

#Initializing the Screen
pygame.init()
screen = pygame.display.set_mode((640,360), 0, 32)

background = pygame.image.load(bg).convert()



bunker_x, bunker_y = (160,0)



class EnemyTank(pygame.sprite.Sprite):
    e_tank = pygame.image.load(enemytank).convert_alpha()
    def __init__(self, startpos):
        pygame.sprite.Sprite.__init__(self, self.groups)
        self.pos = startpos
        self.image = EnemyTank.image
        self.rect = self.image.get_rect()
    def update(self):
        self.rect.center = self.pos

class Bunker(pygame.sprite.Sprite):
    bunker = pygame.image.load(bunk).convert_alpha()
    def __init__(self, startpos):
        pygame.spriter.Sprite.__init__(self, self.groups)
        self.pos = startpos
        self.image = Bunker.image
        self.rect = self.image.get_rect()
    def getCollisionObjects(self, EnemyTank):
        if (EnemyTank not in self._allgroup, False):
            return False
        self._allgroup.remove(EnemyTank)
        result = pygame.sprite.spritecollide(EnemyTank, self._allgroup, False)
        self._allgroup.add(EnemyTank)
    def update(self):
        self.rect.center = self.pos


#Setting Up The Animation
x = 0
clock = pygame.time.Clock()
speed = 250

allgroup = pygame.sprite.Group()
EnemyTank = allgroup
Bunker = allgroup

e_tank = EnemyTank()
bunker = Bunker()5

#Main Loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(background, (0,0))
    screen.blit(bunker, (bunker_x, bunker_y))
    screen.blit(e_tank, (x, 0))
    pygame.display.flip()

    #Animation
    milli = clock.tick()
    seconds = milli/1000.
    dm = seconds*speed
    x += dm

    if x>640:
        x=0


    #Update the Screen
    pygame.display.update()

推荐答案

您已经声明了一个名为 EnemyTank 的类,然后您用以下行覆盖了它:

You've declared a class called EnemyTank and then you've overwritten it with this line:

EnemyTank = allgroup

此后的EnemyTank 不是一个类,而是一个组,不再可调用.你想要做的是:

EnemyTank after this point is not a class, but a group, and no longer callable. What you want to do is:

allgroup pygame.sprite.Group()
e_tank = EnemyTank()
allgroup.add(e_tank)
# Or..
e_tank.add(allgroup)

这篇关于尝试在 Python 中创建对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆