解决python入门与实践 出现'Group' object has no attribute 'get_rect
题目描述
get_rect()应该是pygame中的一种方法,为什么不能使用?而是被当成Group()中的变量
题目来源及自己的思路
已经将所有函数和类合并,直接复制运行即可,请大神解答一下
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
alien_invasion.py(主程序)
import sysimport pygame
from pygame.sprite import Sprite
from pygame.sprite import Group
import game_functions as gf
class Settings():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (230,230,230)
self.ship_speed_factor = 2.5
self.bullet_speed_factor = 1
self.bullet_width = 3
self.bullet_height = 10
self.bullet_color = 60,60,60
self.bullets_allowed = 3
self.alien_speed_factor = 1
self.fleet_drop_speed = 10
self.fleet_direction = 1
class Ship:
def __init__(self,ai_settings,screen):
self.screen = screen
self.ai_settings = ai_settings
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
class Bullet(Sprite):
# 对飞行子弹的管理
def __init__(self,ai_settings,screen,ship):
# 在飞船所处位置创建子弹
super(Bullet,self).__init__()
self.screen =screen
class Alien(Sprite):
def __init__(self,ai_settings,screen):
super(Alien,self).__init__()
self.screen = screen
self.ai_settings = ai_settings
# 加载图片确认rect
self.image = pygame.image.load('images/alien.bmp')
self.rect = self.image.get_rect()
# 每个外星人在屏幕左上角
self.rect.x = self.rect.width
self.rect.y = self.rect.height
# 存储外星人的位置
self.x = float(self.rect.x)
def blitme(self):
self.screen.blit(self.image,self.rect)
def update(self):
self.x += self.ai_settings.alien_speed_factor
self.rect.x = self.x
def check_edges(self):
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right:
return True
elif self.rect.left <= 0:
return True
def update(self):
self.x += (self.ai_settings.alien_speed_factor *
self.ai_settings.fleet_direction)
self.rect.x = self.x
def get_number_aliens_x(ai_settings,alien_width):
avaliable_space_x = ai_settings.screen_width - 2 * alien_width
number_aliens_x = int(avaliable_space_x / (2 * alien_width))
return number_aliens_x
def get_number_rows(ai_settings,ship_height,alien_height):
avaliable_space_y = (ai_settings.screen_height -
(3 * alien_height) - ship_height)
number_rows = int(avaliable_space_y / (2 * alien_height))
return number_rows
def create_alien(ai_settings,screen,aliens,alien_number,row_number):
alien = Alien(ai_settings,aliens)
alien_width = alien.rect.width
alien.x = alien_width + 2 * alien_width *alien_number
alien.rect.x = alien.x
alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number
aliens.add(alien)
def create_fleet(ai_settings, screen, ship, aliens):
# 创建一个外星人,并计算一行有几个外星人
alien = Alien(ai_settings,screen)
number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
number_rows = get_number_rows(ai_settings, ship.rect.height,
alien.rect.height)
# 创建第一行外星人
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
create_alien(ai_settings, screen, aliens, alien_number,
row_number)
def check_fleet_edges(ai_settings, aliens):
for alien in aliens.sprites():
if alien.check_edges():
change_fleet_direction(ai_settings, aliens)
break
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# 创建一个飞船
ship = Ship(ai_settings,screen)
# 创建一个用于储存子弹的编组和一个外星人编组
bullets = Group()
aliens = Group()
# 创建外星人群
gf.create_fleet(ai_settings,screen,ship,aliens)
# 开始游戏主循环
while True:
gf.update_aliens(ai_settings,aliens)
run_game()
你期待的结果是什么?实际看到的错误信息又是什么?
可以直接运行方法get_rect()
实际看到:
File "D:/python/alien_invasion/untitled0.py", line 145, in <module> run_game()
File "D:/python/alien_invasion/untitled0.py", line 142, in run_game
gf.update_aliens(ai_settings,aliens)
File "D:\python\alien_invasion\game_functions.py", line 112, in update_aliens
check_fleet_edges(ai_settings, aliens)
File "D:\python\alien_invasion\game_functions.py", line 101, in check_fleet_edges
if alien.check_edges():
File "D:\python\alien_invasion\alien.py", line 37, in check_edges
screen_rect = self.screen.get_rect()
以上是 解决python入门与实践 出现'Group' object has no attribute 'get_rect 的全部内容, 来源链接: utcz.com/a/159725.html