简体   繁体   English

类对象不可调用

[英]class object is not callable

I am trying to recreate one of those old 2d space shooters, and have got most of it done, I have got stuck on the actual part of shooting though. 我正在尝试重新创建那些旧的2D空间射击游戏之一,并且已经完成了大部分工作,但是我仍然停留在射击的实际部分上。

Also would I use pythagaros' theorem to calculate collisions between the bullets and the enemy as well as collisions between the enemies and the player? 我还会使用pythagaros定理来计算子弹与敌人之间的碰撞以及敌人与玩家之间的碰撞吗?

Here is the code: 这是代码:

import pygame, random, os

pygame.init()
w, h = 1366, 768
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption('SPACESHIPSHOOTER')
clock = pygame.time.Clock()

class player:
    def __init__(self, pos=[w//2, h//7]):
        self.pos = pos
        self.player = pygame.image.load(os.path.join('GameData', 'player.png'))
        self.player = pygame.transform.scale(self.player, (100, 83))

class enemy:
    def __init__(self, pos=[random.randint(0, 1366), 700]):
        self.pos = pos
        self.enemy = pygame.image.load(os.path.join('GameData', 'enemy.png'))
        self.enemy = pygame.transform.scale(self.enemy, (100, 83))

class bullet:
    def __init__(self, pos):
        self.pos = pos
        self.bullet = pygame.image.load(os.path.join('GameData', 'missile.png'))
        self.bullet = pygame.transform.scale(self.bullet, (50, 41))
        self.exist = 0
    def update(self):
        if self.pos[1] < 700 and self.exist == 1:
            self.pos[1] -= 5

you = player()
enemies = [enemy()]
bullets = []

running = True
speed = 1.2
clock = 0
xv = 0

while running:
    screen.fill((0, 0, 0))
    clock += 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT: running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                running = False
            if event.key == pygame.K_d:
                xv = 8
            if event.key == pygame.K_a:
                xv = -8
            if event.key == pygame.K_SPACE:
                bullets.append(bullet([you.pos[0], you.pos[1]]))
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_d or event.key == pygame.K_a:
                xv = 0
    you.pos[0] += xv
    if len(bullets) > 0:
        for bullet in bullets:
            bullet.exist=1
            bullet.update()
    for badguy in enemies:
        if badguy.pos[1] <= 5:
            del enemies[enemies.index(badguy)]
            enemies.append(enemy([random.randint(0, 1366), 700]))
        badguy.pos[1] = badguy.pos[1] - speed
        screen.blit(badguy.enemy, (badguy.pos[0], badguy.pos[1]))
    if clock % 750 == 0:
        speed += 0.05
    if clock % 1200 == 0:
        enemies.append(enemy([random.randint(0, 1366), 700]))
    screen.blit(you.player, (you.pos[0], you.pos[1]))
    pygame.display.flip()

pygame.quit()
quit()

That's the code of the game and the error I keep getting is: 那是游戏的代码,我不断得到的错误是:

Traceback (most recent call last):
 File "D:/spaceshipgame/game.py", line 53, in <module>
    bullets.append(bullet([you.pos[0], you.pos[1]]))
TypeError: 'bullet' object is not callable

Thanks in advance 提前致谢

for bullet in bullets: defines a variable of same name bullet which overwrites the class object. for bullet in bullets:定义一个同名的bullet变量,该变量将覆盖类对象。 Rename either class or variable. 重命名类或变量。

Conventionally upercase CamelCase is used for class names, lowercase_underscored for variables. 常规上,大写CamelCase用于类名, lowercase_underscored用于变量。

Complete Python style guide 完整的Python样式指南

The problem is the line 问题是线

for bullet in bullets:

where you overwrite the class bullet with the variable bullet . 在这里用变量bullet覆盖类bullet bullet That's why the convention exists to write classes with capital letter Bullet . 这就是存在使用大写字母Bullet编写类的约定的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM