外星人移动只有一个框架 图片不动?
这个是外星人 我对比着书重写了一遍还是这样 我也不知道哪里有错 就把alien全部发出来了
import pygame
from pygame.sprite import Sprite
class Alien(Sprite):
def __init__(self,screen,alien_settings): super(Alien, self).__init__()
self.screen=screen
self.alien_settings=alien_settings
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 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.rect.x+=
self.alien_settings.alien_speed_factor
*self.alien_settings.fleet_direction
这个是game——faction
def updte_screen(screen,alien_settings,ship,bullets,alien,aliens):
screen.fill(alien_settings.screen_color)for bullet in bullets.sprites():
bullet.draw_bullet()
ship.blitme()
alien.blitme()
aliens.draw(screen)
pygame.display.flip()
def update_bullet(bullets,aliens):
bullets.update()for bullet in bullets.copy():
if bullet.rect.bottom <= 0:
bullets.remove(bullet)
print(len(bullets))
collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
def get_number_alien_x(alien_settings,alien_width):
available_space_x=alien_settings.screen_width-2*alien_widthnumber_aliens_x=int(available_space_x/(2*alien_width))
return number_aliens_x
def get_number_rows(alien_settings, ship_height, alien_height):
available_space_y = (alien_settings.screen_high - (3 * alien_height) - ship_height)
number_rows = int(available_space_y / (2 * alien_height))
return number_rows
def create_alien(alien_settings,screen,aliens,alien_number,row_number):
alien=Alien(screen,alien_settings)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(alien_settings,screen,ship,aliens):
alien=Alien(screen,alien_settings)number_aliens_x = get_number_alien_x(alien_settings,alien.rect.width)
number_rows = get_number_rows(alien_settings, ship.rect.height,
alien.rect.height)
for row_number in range(number_rows):
for alien_number in range(number_aliens_x):
create_alien(alien_settings,screen,aliens,alien_number,row_number)
def check_fleet_edges(alien_settings,aliens):
for alien in aliens.sprites(): if alien.check_edges():
change_fleet_direction(alien_settings,aliens)
break
def change_fleet_direction(alien_settings,aliens):
for alien in aliens.sprites(): alien.rect.y += alien_settings.fleet_drop_speed
alien_settings.fleet_direction *= -1
回答:
外星人 update
方法错了
class Alien(Sprite): def __init__(self,screen,alien_settings):
super().__init__()
...
def update(self):
self.x += (self.ai_settings.alien_speed_factor * self.ai_settings.fleet_direction)
self.rect.x = self.x
以上是 外星人移动只有一个框架 图片不动? 的全部内容, 来源链接: utcz.com/p/938777.html