python Pygame中显示相同图像

31moq8wy  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(150)

基本上,我试图在Pygame中为两个图像创建一个矩形。但是当程序运行时,只有一个图像显示(橙子的)-我看不到蓝色的。我该如何解决这个问题?

class Button():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        one_player_image = pygame.image.load('one_player.jpg').convert()
        self.one_player_image = pygame.transform.scale(one_player_image,(50,50))
        self.image_rect1 = self.one_player_image.get_rect()
        self.image_rect1.center = (x,y)
        two_player_image = pygame.image.load('two_player.jpg').convert()
        self.two_player_image = pygame.transform.scale(two_player_image,(50,50))
        self.image_rect2 = self.two_player_image.get_rect()
        self.image_rect2.center = (x,y)  
        
    def draw_buttons(self):
        WINDOW.blit(self.one_player_image,(self.image_rect1.x,self.image_rect1.y))
        WINDOW.blit(self.two_player_image,(self.image_rect2.x,self.image_rect2.y))

one_player_button = Button(100,300)
two_player_button = Button(700,400)
mu0hgdu0

mu0hgdu01#

按钮class仅需具有1个矩形和1个图像:

class Button():
    def __init__(self, x, y, filename):
        image = pygame.image.load(filename).convert()
        self.image = pygame.transform.scale(image, (50, 50))
        self.rect = self.image.get_rect(center = (x, y))
        
    def draw_buttons(self):
        WINDOW.blit(self.image, self.rect)

但是,您需要创建该类的2个示例对象,每个按钮一个:

one_player_button = Button(100, 300, 'one_player.jpg')
two_player_button = Button(700, 400, 'two_player.jpg')

必须在应用程序循环中绘制这两个按钮:

run = True
while run:
    # [...]

    one_player_button.draw()
    two_player_button.draw()

    # [...]

相关问题