问题描述
您好,我正在努力加快我的横向射击游戏的速度。但是当我试图将外星人舰队向下和向左移动时遇到了问题。我已经设置了 self.rect.bottom >= screen_rect.bottom
。但这段代码只对我最上面一排的外星人有反应。我尝试设置一个数值,但这并没有给我想要的结果。非常感谢任何帮助。
这是我的外星人代码:
外星人.py
def check_edges(self):
"""Return True if alien is at edge of screen."""
screen_rect = self.screen.get_rect()
if self.rect.bottom >= screen_rect.bottom or self.rect.top <= 0:
return True
def update(self):
"""Move the alien up or down."""
self.y += (self.settings.alien_speed *
self.settings.fleet_direction)
self.rect.y = self.y
在alien_invasion.py
def _update_aliens(self):
"""
Check if the fleet is at an edge,then update the positions of all aliens in the fleet.
"""
self._check_fleet_edges()
self.aliens.update()
def _create_fleet(self):
"""Create the fleet of aliens."""
# Create an alien and find the number of aliens in a row.
# Spacing between each alien is equal to one alien height.
alien = Alien(self)
alien_width = alien.rect.width
alien_height = alien.rect.height
# Determine the number of rows of aliens that fit on the screen.
ship_width = self.ship.rect.width
available_space_x = (self.settings.screen_width -
(2 * alien_width) + ship_width)
number_rows = available_space_x // (3 * alien_width)
available_space_y = self.settings.screen_height + (2 * alien_height)
number_aliens_y = available_space_y // (2 * alien_height)
# Create full fleet of aliens.
for alien_number in range(number_aliens_y):
for row_number in range(number_rows):
self._create_alien(alien_number,row_number)
def _create_alien(self,alien_number,row_number):
"""Create an alien and place it in the row."""
alien = Alien(self)
alien_width = alien.rect.width
alien_height = alien.rect.height
alien.rect.x = 1200 - (2 * alien_width) - (2 * alien.rect.width) * row_number
alien.y = alien_height + 2 * alien_height * alien_number
alien.rect.y = alien.y
self.aliens.add(alien)
def _check_fleet_edges(self):
"""Respond appropriately if any aliens have reached an edge."""
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
def _change_fleet_direction(self):
"""Drop the entire fleet and change the fleet's direction"""
for alien in self.aliens.sprites():
alien.rect.x -= self.settings.fleet_left_speed
self.settings.fleet_direction *= -1
解决方法
您的 for
循环在第一个外星人之后立即中断。
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
break
语句必须位于 if
语句的代码块中。这是Indentation
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
我建议让您的代码更加健壮。如果外星人到达顶部,则新的舰队方向应该是向下的。如果外星人到达底部,新的舰队方向应该是向上的。
编写方法 _set_fleet_direction
而不是 _change_fleet_direction
。该方法具有指示新方向的 direction
参数。参数必须为 1 表示向下,-1 表示向上。新的 fleet_direction
可以用当前的绝对值 (abs(x)
) 乘以新的方向来计算:
def _set_fleet_direction(self,direction):
self.settings.fleet_direction = abs(self.settings.fleet_direction) * direction
根据范围检查设置方向:
def _check_fleet_edges(self):
"""Respond appropriately if any aliens have reached an edge."""
screen_rect = self.screen.get_rect()
for alien in self.aliens.sprites():
if alien.top <= screen_rect.top:
self._set_fleet_direction(1) # new direction is down
break
elif alien.bottom >= screen_rect.bottom
self._set_fleet_direction(-1) # new direction is up
break
,
所以我最终跌跌撞撞地找到了解决方案。
它有效,但我不确定这是最好的方法。
我最终保留了
def _change_fleet_direction(self):
我应该保留它作为它自己的辅助函数还是可以以某种方式将它包含在 def _set_fleet_direction(self,direction):
中?
我觉得这个游戏比我原来的 Alien_invasion 游戏要滞后一些。
有什么建议吗?
这是我最终得到的解决方案:
def _check_fleet_edges(self):
screen_rect = self.screen.get_rect()
for alien in self.aliens.sprites():
if alien.rect.top <= screen_rect.top:
self._set_fleet_direction(1) # new direction is down
break
elif alien.rect.bottom >= screen_rect.bottom:
self._set_fleet_direction(-1) # new direction is up
break
for alien in self.aliens.sprites():
if alien.check_edges():
self._change_fleet_direction()
break
def _change_fleet_direction(self):
"""Moving the aliens left"""
for alien in self.aliens.sprites():
alien.rect.x -= self.settings.fleet_left_speed
def _set_fleet_direction(self,direction):
self.settings.fleet_direction = abs(self.settings.fleet_direction) *
direction