赞
踩
题目描述
不加pygame.sprite.groupcollide()代码,运行一切正常没有问题
加了pygame.sprite.groupcollide()代码运行时按下空格就发生错误了。不按空格就正常运行
Traceback (most recent call last):
File "alien_invasion.py", line 41, in
run_game()
File "alien_invasion.py", line 33, in run_game
gf.update_bullet(bullets,aliens)
File "G:\python_work\alien_invasion\game_functions.py", line 115, in update_bullet
cllisions = pygame.sprite.groupcollide(bullets,aliens,True,True)
File "C:\Users\靖\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1550, in groupcollide
c = SC(s, groupb, dokillb, collided)
File "C:\Users\靖\AppData\Roaming\Python\Python36\site-packages\pygame\sprite.py", line 1513, in spritecollide
spritecollide = sprite.rect.colliderect
AttributeError: 'Bullet' object has no attribute 'rect'
题目来源及自己的思路
题目时python编程入门到实践的例题,自己打的和书上没什么差别
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
# alien_invasion.py
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
#初始化游戏并创建一个屏幕对象
pygame.init()
#实例化Settings类
ai_settings = Settings()
#创建指定大小的屏幕
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
#创建一个用于储存子弹,外星飞船的编组
bullets = Group()
aliens = Group()
#游戏标题
pygame.display.set_caption('Alien Invasion')
#创建飞船,实例化Ship类
ship = Ship(ai_settings,screen)
#创造外星人飞船
gf.create_fleet(aliens,screen,ai_settings,ship.ship_rect.height)
#游戏主循环
while True:
#监视鼠标与键盘的事件
gf.check_events(ship,screen,ai_settings,bullets)
#更新飞机位置
ship.update()
#更新子弹位置并删除无用子弹
gf.update_bullet(bullets,aliens)
#更新外星人位
gf.update_alien(aliens,ai_settings)
#更新屏幕
gf.update_screen(ai_settings,screen,ship,bullets,aliens)
run_game()
# settings.py
class Settings():
'''储存游戏的各种设置'''
def __init__(self):
#屏幕大小
self.screen_width = 1200
self.screen_height = 800
#背景颜
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。