我会先给你看我的代码(在主循环之外):
START_BAT_COUNT = 10
BAT_IMAGE_PATH = os.path.join( 'Sprites', 'Bat_enemy', 'Bat-1.png' )
bat_image = pygame.image.load(BAT_IMAGE_PATH).convert_alpha()
bat_image = pygame.transform.scale(bat_image, (80, 70))
class Bat(pygame.sprite.Sprite):
def __init__(self, bat_x, bat_y, bat_image, bat_health, bat_immune):
pygame.sprite.Sprite.__init__(self)
self.bat_health = bat_health
self.bat_immune = bat_immune
self.image = bat_image
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.topleft = (bat_x, bat_y)
self.bat_x = bat_x
self.bat_y = bat_y
def update(self):
self.bat_x += 500
all_bats = pygame.sprite.Group()
for i in range(START_BAT_COUNT):
bat_x = (random.randint(0, 500))
bat_y = (random.randint(0, 500))
bat_health = 5
bat_immune = False
new_bat = Bat(bat_x, bat_y, bat_image, bat_health, bat_immune)
all_bats.add(new_bat)主循环内部:
all_bats.update()
all_bats.draw(display)在update()中,我在每次读取代码时将bat_x的值增加500,并且我知道bat_x的值是增加的,因为我已经通过打印bat_x的值并观察它们的增加进行了测试。我的问题是,有没有一种方法可以增加bat_x,并让它真正移动我的球棒?到目前为止,变量会增加,但球棒不会移动。谢谢
发布于 2020-03-30 20:55:29
你的显示器的尺寸是多少?如果你在x方向上移动500像素的bat,那么一旦它开始移动,它就会立即飞离屏幕。此外,您的球拍可能不会移动,因为您没有更新其矩形的位置。
在队伍中
def update(self):
self.bat_x += 500试一试
def update(self):
self.rect.move_ip(500, 0)其中move_ip就地移动矩形,并在每次更新时将bat的x坐标增加500。
https://stackoverflow.com/questions/60930138
复制相似问题