'Group' object has no attribute ‘xxx’

引用文字### 题目描述
我想创建一个可以发射子弹的飞船,按照书上编写之后老是显示
AttributeError: 'Group' object has no attribute '控制子弹'
请好心人看看,跪求

题目来源及自己的思路

相关代码

import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
    def __init__(self,a,屏幕,飞船):
        super().__init__()
        self.屏幕 = 屏幕
        self.矩形 = pygame.Rect(0,0,a.子弹长度,a.子弹宽度)
        self.矩形.centeyx = 飞船.矩形.centerx
        self.矩形.top = 飞船.矩形.top
        self.y =(self.矩形.y)
        self.子弹颜色= a.子弹颜色
        self.子弹速度 = a.子弹速度

    def 控制子弹(self):
        self.y -= self.子弹速度
        self.矩形.y = self.y
        
    def 绘制子弹(self):
        pygeme.draw.rect(self.a,self.子弹颜色,self.矩形)
        
```

    


----------


```

import sys
import pygame
from 子弹 import Bullet
def 绘制屏幕(飞船,屏幕,):
    #给创建的屏幕填充颜色
    屏幕.fill(a.屏幕颜色)
    飞船.绘制飞船()
 
    #绘制屏幕
    pygame.display.flip()
def 相应鼠标键盘(飞船,屏幕,子弹组,a):                                                                                                                                                                                
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN: 
           开启绘制飞船(event,飞船,屏幕,子弹组,a)
        elif event.type == pygame.KEYUP:
            关闭绘制飞船(event,飞船)
    
def 开启绘制飞船(event,飞船,屏幕,子弹组,a):
    if  event.key == pygame.K_RIGHT:
        飞船.a = True
    elif event.key == pygame.K_LEFT:
        飞船.b = True
    elif event.key == pygame.K_SPACE:
        新子弹 = Bullet(a,屏幕,飞船)
        子弹组.add(新子弹)
   

def 关闭绘制飞船(event,飞船):
    if event.key == pygame.K_RIGHT:
        飞船.a = False
    elif event.key == pygame.K_LEFT:
        飞船.b = False


----------


----------
import sys
import pygame
from 游戏数据 import 游戏数据
from 飞船 import Ship
import 所有函数 as l
from pygame.sprite import Group
def 开始游戏():
    #此实例为了调取游戏数据方便
    a=游戏数据()
    #创建屏幕
    屏幕 = pygame.display.set_mode((a.屏幕长度,a.屏幕宽度))
    #给游戏取名
    pygame.display.set_caption('大战外星人')
    子弹组 = Group()
    飞船 = Ship(屏幕,a)
    while True:
        l.相应鼠标键盘(飞船,a,屏幕,子弹组)
        飞船.操控飞船()
        子弹组.控制子弹()
        l.绘制屏幕(飞船,屏幕,a,子弹组)
        
            
开始游戏()

你期待的结果是什么?实际看到的错误信息又是什么?

不太会英文,于是把可以用中文代替的全部改用中文。没有贴出全部代码,因为剩下的没有改动,错误应给出现在这三篇代码。
Traceback (most recent call last):
File "C:大战外星人游戏.py", line 23, in <module>

开始游戏()

File "C:大战外星人游戏.py", line 19, in 开始游戏

子弹组.控制子弹()

AttributeError: 'Group' object has no attribute '控制子弹'
这个是错误提示,跪求好心人。。。
找了一下午找不到错误在哪

阅读 8k
4 个回答
新手上路,请多包涵

主代码漏掉pygame.inint 复杂的Group 蛋疼的报错....

问题在这里

子弹组 = Group()
飞船 = Ship(屏幕,a)
while True:
    l.相应鼠标键盘(飞船,a,屏幕,子弹组)
    飞船.操控飞船()
    子弹组.控制子弹()
    l.绘制屏幕(飞船,屏幕,a,子弹组)

子弹组是 pygame.sprite.Group实例化的一个对象,怎么会有控制子弹()这个方法呢

pygame.sprite.Group

所以应该是子弹组 = Group()这里实例化错了, 控制子弹()方法只有Bullet对象有

新手上路,请多包涵

def update_bullets(bullets):

for bullet in bullets.sprites():    # 这里也得把每一个元素都分开更新位置才行,group组找到这里调用不了位置更新函数
    bullet.update_position()

# 子弹消失移除屏幕外子弹
for bullet in bullets.copy():
    if bullet.rect.bottom <= 0:
        bullets.remove(bullet)
新手上路,请多包涵

这个地方应该是传递参数不正确
bullet没有相应的rect属性,
没有正确创建一个新的子弹
应该是调用创建子弹的方法时参数错误
检查这个

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题