【发布时间】:2023-04-03 09:48:01
【问题描述】:
您好,我正在努力让我的横向射击游戏加快速度。但是当我试图将外星人舰队向下和向左移动时遇到了问题。我已经设置了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
【问题讨论】:
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python 速成课程,第 2 版。侧身射手。外星人继续向下移动,直到顶行撞到底部 - Python技术站