我正在开发一款游戏,该游戏使用向左和向右键移动太空飞船,并使用空格键发射子弹,但是按下空格键会使游戏崩溃。 我不知道是什么原因造成的。
这是我的代码:
https://github.com/Eid-alhamali/Eid/tree/master/alien_invasion
这是错误
Traceback (most recent call last):
File "c:/Users/eidha/OneDrive/سطح المكتب/alien_invasion/alien_invasion.py", line 32, in <module>
run_game()
File "c:/Users/eidha/OneDrive/سطح المكتب/alien_invasion/alien_invasion.py", line 27, in run_game
gf.check_events(ai_settings, screen, ship, bullets)
File "c:\Users\eidha\OneDrive\سطح المكتب\alien_invasion\game_functions.py", line 34, in
check_events
check_keydown_events(event, ai_settings, screen, ship, bullets)
File "c:\Users\eidha\OneDrive\سطح المكتب\alien_invasion\game_functions.py", line 17, in
check_keydown_events
new_bullet = Bullet(ai_settings, screen, ship)
File "C:\Users\eidha\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\pygame\sprite.py", line 124, in __init__
self.add(*groups)
File "C:\Users\eidha\AppData\Local\Programs\Python\Python37-32\lib\site-
packages\pygame\sprite.py", line 142, in add
self.add(*group)
TypeError: add() argument after * must be an iterable, not Settings
答案 0 :(得分:0)
Bullet中的__init__
函数大写,这意味着它不会覆盖默认的Sprite组。因此,您实际上是在调用Sprite __init__
函数,并且您要传递ai_settings(一个Settings
对象)作为*groups
参数。
class Bullet(Sprite):
"""A Class To Manage Bullets Fired From The Ship"""
def __init__(self, ai_settings, screen, ship): # Notice lowercase
super().__init__()
...