我正在学习在pygame中编码。我面临的问题是,我无法擦除以前的精灵。我的意思是图像只是沿着屏幕延伸。如何移动精灵而不是扩展精灵?
def check_edges(self):
screen_rect = self.screen.get_rect()
if self.rect.right >= screen_rect.right: #if alien has caught up the right edge of the screen
return True
elif self.rect.left <= 0: #if alien has caught up the left edge of the screen
return True
def update_aliens(custom_settings, aliens):
check_fleet_edges(custom_settings, aliens)
aliens.update()
def check_fleet_edges(custom_settings, aliens):
for alien in aliens.sprites():
if alien.check_edges():
change_fleet_direction(custom_settings, aliens)
break
def change_fleet_direction(custom_settings, aliens):
for alien in aliens.sprites():
alien.rect.y += custom_settings.fleet_drop_speed #fleet_drop_speed is the value of whole fleet's speed
custom_settings.fleet_direction *= -1 #change direction of fleet's moving
我的绘图代码就在这里:
def update_screen(custom_settings, screen, spaceship, aliens, bullets, rockets, stars):
screen.fill(custom_settings.bg_color)
for bullet in bullets.sprites():#возвращает список спрайтов в группе
bullet.draw_bullet()
for rocket in rockets.sprites():
rocket.draw_rocket()
stars.draw(screen)
aliens.draw(screen)
spaceship.blitme()
pygame.display.flip()
这是我的主循环:
while True:
gf.check_events(custom_settings, screen, spaceship, bullets, rockets)
spaceship.update()
bullets.update()
rockets.update()
gf.update_ammunition(bullets, rockets)
gf.update_aliens(custom_settings, aliens, screen)
gf.create_fleet(custom_settings, screen, spaceship, aliens)
gf.starsky(custom_settings, screen, spaceship, stars)
gf.update_screen(custom_settings, screen, spaceship, aliens, bullets, rockets,
stars)
clock.tick(FPS)
print(clock.get_fps())
答案 0 :(得分:0)
问题在于精灵会无限生成。我应该限制外星人的数量
def create_fleet(custom_settings, screen, spaceship, aliens):
alien = Alien(custom_settings, screen)
number_aliens_x = get_number_aliens_x(custom_settings, alien.rect.width)
number_rows = get_number_rows(custom_settings, spaceship.rect.height, alien.rect.height)
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
if len(aliens) <= 48:
create_alien(custom_settings, screen, aliens, alien_number, row_number)
else:
aliens.remove(alien)