Skip to main content
 首页 » 编程设计

python之在pygame中创建两个运动图像

2025年05月04日15haluo1

我有一个垂直线的图像,想创建两个。我希望它们在沿 x 轴移动时随机分开并缩小间隙(同时保持最小和最大距离 - 我正在等待先解决这个问题)。 Y 坐标不会移动。

有很多使用 pygame 创建更复杂的 Sprite 和游戏的例子,但我找不到一个足够简单的例子来确定我应该做什么,所以任何帮助都将不胜感激。此代码基于找到的一个简单示例 here.

这是我目前拥有的最基本的代码。

class Squeeze(pygame.sprite.Sprite): 
 
    def __init__(self): 
        pygame.sprite.Sprite.__init__(self) 
        self.img_load() 
 
    def update(self): 
        self.rect.x += random.randint(-1, 1) 
        if self.rect.x >= 1920: 
            self.rect.x += -1 
        if self.rect.x <= 0: 
            self.rect.x += 1 
 
    def img_load(self): 
        self.image = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT]) 
        self.image.fill(BLACK) 
        self.rect = self.image.get_rect() 
 
 
pygame.init() 
 
SCREEN_WIDTH = 1920 
SCREEN_HEIGHT = 1080 
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) 
FPS = 60 
wall = pygame.image.load('wall.png') 
wall_list = pygame.sprite.Group() 
 
BLACK = (0, 0, 0) 
for i in range(2): 
    wall = Squeeze() 
    wall.rect.x = random.randrange(SCREEN_WIDTH) 
    wall.rect.y = random.randrange(SCREEN_HEIGHT) 
    wall_list.add(wall) 
 
done = False 
clock = pygame.time.Clock() 
 
while not done: 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            done = True 
 
    screen.fill(BLACK) 
    wall_list.update() 
    wall_list.draw(screen) 
 
    clock.tick(60) 
    pygame.display.flip() 
 
pygame.quit() 

我对 pygame.sprite.Group() 交易感到困惑。如何让两个图像分别加载和操作?也欢迎任何其他建议。

请您参考如下方法:

基本上“ Sprite ”是游戏中可见的游戏对象。游戏 pygame.sprite.Sprite类被称为所有这些对象的“基类”。

每次创建 Squeeze 类时,您都派生形成 Pygames 内置 Sprite 基类:

class Squeeze(pygame.sprite.Sprite): 
    def __init__(self): 
        ... 

pygame.sprite.Sprite 类的每个对象(或实例)都包含

  • 一个.update()方法与
  • .image 和
  • .rect 属性。

作为documentation状态,“* ...派生类将要覆盖 Sprite.update() 并分配一个 Sprite.image 和 Sprite.rect属性.*"
您通过在 Squeeze 类的 __init__() 方法中调用您的 img_load() 方法来做到这一点。

现在的问题是您的 .rect 被指定为与您的屏幕相同的尺寸 (self.image = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT])) .image 属性与您的显示屏颜色相同,因此您看不到它。

尝试将您的img_load() 方法修改为

def img_load(self, img): 
    self.image = img 
    self.rect = self.image.get_rect() 

如您所见,修改后的 img_load() 方法需要一个图像对象——它被传递给 img 参数。所以你还需要改变你的 __init__() 方法:

def __init__(self, img): 
    pygame.sprite.Sprite.__init__(self) 
    self.img_load(img) 

现在实例化(例如创建类的对象)Squeeze 对象时,您需要将图像(在我们的例子中为 wall_img)传递给构造函数:

#... main programm 
 
wall_img = pygame.image.load('wall.png') #load an image 
wall_list = pygame.sprite.Group() #create a sprite group 
 
BLACK = (0, 0, 0) 
for i in range(2): 
    wall = Squeeze(wall_img) #create a sprite 
    wall.rect.x = random.randrange(SCREEN_WIDTH) 
    wall.rect.y = random.randrange(SCREEN_HEIGHT) 
 
    wall_list.add(wall) #add the sprite to the sprite group 

您使用的第二个内置类是 pygame.sprite.Group类 -- 一个容器,可以容纳和管理(修改)它包含的 Sprite 。

调用 Sprite 组实例的.update()方法,Pygame 自动调用该 Sprite 组中所有 Sprite 的update()方法团体。这意味着您可以通过实现其 update() 方法来控制 Sprite 的行为,就像您所做的那样:

#update method of the Squeeze class 
def update(self): 
    #any logic which changes sprite 
    self.rect.x += random.randint(-1, 1) 
    if self.rect.x >= 1920: 
        self.rect.x += -1 
    if self.rect.x <= 0: 
        self.rect.x += 1 

通过调用

wall_list.draw(screen) 

你的恶意将被 blitted 到 screen 表面上,使用源表面的 .image 属性和位置的 .rect 。

如果您现在不想拥有不同的图像,您只需将其他图像(或表面)传递给 Squeeze 构造函数:

#... main programm 
 
wall_imgs = [pygame.image.load('wall.png'), 
             pygame.image.load('other_wall.png')] #load images in list 
wall_list = pygame.sprite.Group() #create a sprite group 
 
BLACK = (0, 0, 0) 
for i in range(2): 
    wall = Squeeze(wall_imgs[i]) #create a sprite 
    wall.rect.x = random.randrange(SCREEN_WIDTH) 
    wall.rect.y = random.randrange(SCREEN_HEIGHT) 
 
    wall_list.add(wall) #add the sprite to the sprite group 

希望对您有所帮助:)