pygame精灵.组-从组中传递参数



我正在试用pygame,使用Sprites&分组,让自己在传递争论时有点困惑。

基本上,我有一个包装类MyGame((作为my_game((。我有pygame.sprite.Group((-all_sprites。

然后,我为我的外星人创建了一个类Alien(pygame.sprite.sprite(,并将每个外星人添加到我的all_sprites组中。

我希望我的外星人小组在屏幕上追踪(他们这样做(,然后所有人都作为一个区块,一个小组(他们不这样做(。

我可以在self.all_sprites.update((之后的主循环中循环我的all_sprites.Group,并查看alie.rect.right>WIDTH或alie.rect.left<0,然后在组中循环,再次调用外星人.end_row((,然后打破循环,这样它就不会为屏幕边缘的每个外星人运行,但这看起来很笨拙。

我曾尝试在MyGame self.client_drop=False中设置标志,但当我尝试在alien类中设置my_game.alient_drop=True时,它无法识别my_game-未定义。我有点困惑,因为MyGame正在创建Alien的实例,所以它们应该被MyGame的范围所包围?

我可以将MyGame self.alion_drop传递到我的alien init((中,但当我更新alien self.alia_drop时,它不会更新MyGame.alient_drop。我得到它是因为它在alien本地创建了一个新变量。

似乎没有办法通过Group.update((传递参数,我想这是因为它只是在组内的所有Sprite上调用.update(。我找不到一个简单的方法来修改Group.update((函数,这样我就可以传递值,公平地说,我可能无论如何都不想在那里混日子。

我也无法通过update((返回True。

我有点陷入了困境。。。

我知道self.外星人.row_end((可能不起作用,我必须循环self.aliens并调用每个外星人.row-end((,但目前还没有到那个地步。

import pygame
WIDTH = 360 # width of our game window
HEIGHT = 480 # height of our game window
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
class MyGame():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.all_sprites = pygame.sprite.Group()
self.alien_drop = False
for row_index in range(4):
for column_index in range(6):
alien = Alien(20 + (column_index * 40), 20 + (row_index *40))
alien.add(self.all_sprites)
while True:
self.screen.fill(BLACK)
self.all_sprites.update()
if self.alien_drop:
self.aliens.row_end()
self.all_sprites.draw(self.screen)
pygame.display.update()
self.clock.tick(60)
class Alien(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((25, 25))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.image.fill(GREEN)
self.dx = 1
def update(self):
self.rect.x += self.dx
if self.rect.right >= WIDTH or self.rect.left <= 0:
my_game.alien_drop = True
def row_end(self):
self.dx *= -1
self.rect.y += 40
if __name__ == "__main__":
my_game = MyGame()

因此,要进行传统的太空入侵者的移动和掉落,当任何入侵者击中任何一侧时,整排都会掉落。

在下面的示例中,我修改了Alien.update()以检测屏幕侧何时被击中,但如果是,则仅将布尔标志Alien.drop设置为True。

算法变成:首先移动所有外星人。接下来,通过检查此标志来检查是否有外星人撞到了侧墙。如果是这样,将每个外星人向下移动1步&停止检查。

Alien.row_end()功能可向下移动外星人。它还需要清除Alien.drop标志,这样它们就不会再次全部下移。

import pygame
WIDTH = 360 # width of our game window
HEIGHT = 480 # height of our game window
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
class MyGame():
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.clock = pygame.time.Clock()
self.all_sprites = pygame.sprite.Group()
self.alien_drop = False
for row_index in range(4):
for column_index in range(6):
alien = Alien(20 + (column_index * 40), 20 + (row_index *40))
alien.add(self.all_sprites)
exiting = False
while not exiting:
# Handle events
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
exiting = True  # exit this loop
self.screen.fill(BLACK)
self.all_sprites.update()
for alien1 in self.all_sprites:
if ( alien1.shouldDrop() ):
### If any alien drops, we all drop
for alien2 in self.all_sprites:
alien2.row_end()
break  # only drop once
if self.alien_drop:
self.aliens.row_end()
self.all_sprites.draw(self.screen)
pygame.display.update()
self.clock.tick(60)
class Alien(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((25, 25))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.image.fill(GREEN)
self.dx = 1
self.drop = False  # should I drop-down a level
def update(self):
self.rect.x += self.dx
if self.rect.right >= WIDTH or self.rect.left <= 0:
self.drop = True
else:
self.drop = False
def shouldDrop( self ):
""" In this alien in a position where it should drop down
one row in the screen-space (i.e.: it's hit a side)  """
return self.drop
def row_end(self):
self.dx *= -1
self.rect.y += 40
self.drop = False    # drop drop again just yet
if __name__ == "__main__":
my_game = MyGame()

最新更新