首页 > 解决方案 > 当我想让我的精灵角色发射子弹时,我收到一条属性错误消息。我究竟做错了什么?

问题描述

我可以加载我的 pygame 窗口,但是当我按下空格按钮让我的精灵角色射击时,它会退出 pygame 窗口并给我一个属性错误。我究竟做错了什么?我该如何解决这个问题?我已经尝试过从子弹导入子弹,但这不起作用。请帮我解决这个错误,这样我就可以继续编码......非常感谢您的帮助。

自动驾驶仪.py 代码:

import pygame
import car
import debris

pygame.init()

#screen settings
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))

#fps
FPS = 120
clock = pygame.time.Clock()

#load images
bg = pygame.image.load('background/street.png').convert_alpha() # background

#define game variables


######################CAR/DEBRIS##########################

player = car.Player(1, 5)
debris = debris.Debris(1, 5)

##########################################################

#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()

debris_group.add(debris)

#game runs here
run = True
while run:

    #draw street
    screen.blit(bg, [0, 0])

    #update groups
    bullet_group.update()
    bullet_group.draw(screen)

    #draw debris
    debris.draw()

    #draw car
    player.draw()
    player.move()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        #check if key is down
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                run = False
            if event.key == pygame.K_a:
                player.movingLeft = True
            if event.key == pygame.K_d:
                player.movingRight = True
            if event.key == pygame.K_SPACE:
                player.shoot()

        #check if key is up
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a:
                player.movingLeft = False
            if event.key == pygame.K_d:
                player.movingRight = False

    #update the display
    pygame.display.update()
    pygame.display.flip()
    clock.tick(FPS)

pygame.quit()

car.py 代码:

import pygame

#screen settings
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))

#load bullets
bullets = pygame.image.load('car/bullet.png').convert_alpha()

#groups
bullet_group = pygame.sprite.Group()

#player class
class Player(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.speed = speed
        #self.x = x
        #self.y = y
        self.moving = True
        self.frame = 0
        self.flip = False
        self.direction = 0

        #load car
        self.images = []
        img = pygame.image.load('car/car.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()
        self.update_time = pygame.time.get_ticks()
        self.movingLeft = False
        self.movingRight = False
        self.rect.x = 465
        self.rect.y = 325

    #draw car to screen
    def draw(self):
        screen.blit(self.image, (self.rect.centerx, self.rect.centery))

    #move car
    def move(self):
        #reset the movement variables
        dx = 0
        dy = 0

        #moving variables
        if self.movingLeft and self.rect.x > 33:
            dx -= self.speed
            self.flip = True
            self.direction = -1
        if self.movingRight and self.rect.x < 900:
            dx += self.speed
            self.flip = False
            self.direction = 1

        #update rectangle position
        self.rect.x += dx
        self.rect.y += dy

    #shoot
    def shoot(self):
        bullet = bullets.Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
        bullet_group.add(bullet)

子弹.py代码:

import pygame
from autopilot import debris, bullet_group

#screen settings
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))

#load bullets
bullets = pygame.image.load('car/bullet.png').convert_alpha()

#bullet class
class Bullet(pygame.sprite.Sprite):
    def __init__(self, x, y, direction):
        pygame.sprite.Sprite.__init__(self)

        self.speed = 5
        self.image = bullets
        self.rect = self.image.get_rect()
        self.rect.center = (x,y)
        self.direction = direction

    def update(self):
        self.rect.centery -= self.speed
        #check if bullet has gone off screen
        if self.rect.top < 1:
            self.kill()
        #check collision with cement block
        if pygame.sprite.spritecollide(debris, bullet_group, False):
            if debris.alive:
                debris.health -= 25

碎片.py代码:

import pygame

#screen settings
WIDTH = 1000
HEIGHT = 400

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))

#debris class
class Debris(pygame.sprite.Sprite):
    def __init__(self, scale, speed):
        pygame.sprite.Sprite.__init__(self)

        self.x = 400
        self.y = HEIGHT / 2 - 200
        self.speed = speed
        self.vy = 0
        self.on_ground = True
        self.move = True
        self.health = 100
        self.max_health = self.health
        self.alive = True

        #load debris
        self.images = []
        img = pygame.image.load('debris/cement.png').convert_alpha()
        img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
        self.images.append(img)
        self.image = self.images[0]
        self.rect = self.image.get_rect()

    #draw debris to screen
    def draw(self):
        screen.blit(self.image,(self.x,self.y))

我的错误:

错误信息

标签: pythonpython-3.xpygame

解决方案


你想创建一个类的实例Bullet。因此,您必须导入Bullet

from bullet import Bullet

最后你可以使用这个类Bullet

class Player(pygame.sprite.Sprite):
    # [...]

    def shoot(self):
        bullet = Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
        bullet_group.add(bullet)

如果只想导入bullet.py文件,则必须重命名bullet变量。bullet否则变量和模块之间会出现名称冲突bullet

import bullet
class Player(pygame.sprite.Sprite):
    # [...]

    def shoot(self):
        bullet_object = bullet.Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
        bullet_group.add(bullet_object)

推荐阅读