Ошибка в написании кода: AttributeError: 'Gun' object has no attribute 'empty'

WyRiX

Новичок
Пользователь
Фев 10, 2022
13
1
3
Python:
import pygame, sys
from bullet import Bullet
from ino import Ino
import time

def events(screen, gun, bullets):
    """обработка событий"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            #вправо
            if event.key == pygame.K_d:
                gun.mright = True
            elif event.key == pygame.K_a:
                gun.mleft = True
            elif event.key == pygame.K_SPACE:
                new_bullet = Bullet(screen, gun)
                bullets.add(new_bullet)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_d:
                gun.mright = False
            elif event.key == pygame.K_a:
                gun.mleft = False

def update(bg_color, screen, gun, inos, bullets):
    """Обновление экрана"""
    screen.fill(bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    gun.output()
    inos.draw(screen)
    pygame.display.flip()

def update_bullets(inos, bullets):
    """обновление позиции пуль"""
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
    collections = pygame.sprite.groupcollide(bullets, inos, True, True)

def gun_kill(stats, screen, inos, bullets, gun):
    """столкновение пушки и армии"""
    stats.guns_left -= 1
    inos.empty()
    bullets.empty()
    create_army(screen, inos)
    gun.create_gun()
    time.sleep(1)

def update_inos(inos, bullets, screen, stats, gun):
    """обновляет позицию инопланетян"""
    inos.update()
    if pygame.sprite.spritecollideany(gun, inos):
        gun_kill(stats, screen, inos, bullets, gun)
    inos_check(stats, screen, gun, inos, bullets)

def inos_check(stats, screen, gun, inos, bullets):
    """Проверка пришельцев до края экрана"""
    screen_rect = screen.get_rect()
    for ino in inos.sprites():
        if ino.rect.bottom >= screen_rect.bottom:
            gun_kill(stats, screen, gun, inos, bullets)
            break

def create_army(screen, inos):
    """создание армии пришкльцев"""
    ino = Ino(screen)
    ino_width = ino.rect.width
    number_ino_x = int((700 - 2 * ino_width) / ino_width)
    ino_height = ino.rect.height
    number_ino_y = int((800 - 100 - 2 * ino_height) / ino_height)

    for row_number in range(number_ino_x - 3):
        for ino_number in range(number_ino_x):
            ino = Ino(screen)
            ino.x = ino_width + (ino_width * ino_number)
            ino.y = ino_height + (ino_height * row_number)
            ino.rect.x = ino.x
            ino.rect.y = ino.rect.height + ino.rect.height * row_number
            inos.add(ino)


после выдает ошибку:
File "C:\Users\Матвей Захарченко\PycharmProjects\PythonProjects\controles.py", line 47, in gun_kill
inos.empty()
AttributeError: 'Gun' object has no attribute 'empty'

1644510164851.png
Работаю на Windows 10
Питон 3.9.10
 

stud_55

Модератор
Команда форума
Модератор
Апр 3, 2020
1 522
672
113
AttributeError: 'Gun' object has no attribute 'empty'
Ошибка снова возникает из-за неправильного порядка аргументов переданных в функцию gun_kill(), которая вызывается внутри функции inos_check().
Чтобы исправить замените строку:
Python:
gun_kill(stats, screen, gun, inos, bullets)
на такую
Python:
gun_kill(stats, screen, inos, bullets, gun)
 

Форум IT Специалистов