【问题标题】:How can I add an image or icon to a button rectangle in Pygame?如何将图像或图标添加到 Pygame 中的按钮矩形?
【发布时间】:2021-03-07 11:37:09
【问题描述】:

我是 Pygame 的新手,我已经为我的按钮创建了代码,但我仍然有一个问题,因为我不知道如何在矩形中放置图像而不是纯色红色。这是我的代码,希望你能帮到你!

def button(x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    if x + w > mouse[0] > x and y + h > mouse[1] > y: 
        pygame.draw.rect(screen, ac, (x, y, w, h))
        if click[0] == 1 and action!= None:
            if action == "continue":
                quiz()

    else:
        pygame.draw.rect(screen, ic, (x, y, w, h))  
    pygame.display.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    screen.blit(randomList, [0, 0])
    button(399, 390, 300, 50, red, brightRed, "continue")
    pygame.display.update()

【问题讨论】:

    标签: python pygame pygame-surface


    【解决方案1】:

    你所要做的就是加载一张图片:

    my_image = pygame.image.load('my_image.png').convert_alpha()
    

    blit它位于矩形的顶部:

    def button(x, y, w, h, ic, ac, img, imgon, action=None):
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
    
        rect = pygame.Rect(x, y, w, h)
        on_button = rect.collidepoint(mouse)
        if on_button:
            pygame.draw.rect(screen, ac, rect)
            screen.blit(imgon, imgon.get_rect(center = rect.center))
        else:
            pygame.draw.rect(screen, ic, rect)
            screen.blit(img, img.get_rect(center = rect.center))
    
        if on_button:  
            if click[0] == 1 and action!= None:
                if action == "continue":
                    quiz()
    
    image = pygame.image.load('my_image.png').convert_alpha()
    imageOn = pygame.image.load('my_image_on.png').convert_alpha()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
    
        screen.blit(randomList, [0, 0])
        button(399, 390, 300, 50, red, brightRed, image, imageOn, "continue")
        pygame.display.update()
    


    在 pygame 中,按钮只不过是一个 pygame.Surface 对象。按钮上是文本还是图像完全无关紧要。我建议用pygame.sprite.Sprite 对象来表示按钮。

    另见Pygame mouse clicking detection,分别为MouseSprite

    小例子:

    import pygame
    
    class SpriteObject(pygame.sprite.Sprite):
        def __init__(self, x, y, filename):
            super().__init__() 
            img = pygame.image.load(filename).convert_alpha()
            self.original_image = pygame.Surface((70, 70))
            self.original_image.blit(img, img.get_rect(center = self.original_image.fill((127, 127, 127)).center))
            self.hover_image = pygame.Surface((70, 70))
            self.hover_image.blit(img, img.get_rect(center = self.hover_image.fill((228, 228, 228)).center))
            self.image = self.original_image 
            self.rect = self.image.get_rect(center = (x, y))
            self.hover = False
    
        def update(self):
            self.hover = self.rect.collidepoint(pygame.mouse.get_pos())
            self.image = self.hover_image if self.hover else self.original_image
    
    pygame.init()
    window = pygame.display.set_mode((300, 300))
    clock = pygame.time.Clock()
    
    group = pygame.sprite.Group([
        SpriteObject(window.get_width() // 3, window.get_height() // 3, 'Apple64.png'),
        SpriteObject(window.get_width() * 2 // 3, window.get_height() // 3, 'Banana64.png'),
        SpriteObject(window.get_width() // 3, window.get_height() * 2 // 3, 'Pear64.png'),
        SpriteObject(window.get_width() * 2// 3, window.get_height() * 2 // 3, 'Plums64.png'),
    ])
    
    run = True
    while run:
        clock.tick(60)
        event_list = pygame.event.get()
        for event in event_list:
            if event.type == pygame.QUIT:
                run = False 
    
        group.update()
    
        window.fill(0)
        group.draw(window)
        pygame.display.flip()
    
    pygame.quit()
    exit()
    

    【讨论】:

    • @annyeongkre 我已经改变了答案。如果不想画矩形,就跳过pygame.draw.rect(...)
    • @annyeongkre 您可以使用不同的参数调用button 4 次。
    • @annyeongkre 对不起,但这远远超出了最初的问题,无法在简短的评论中回答。评论部分无意提出其他问题。你必须Ask a new public question
    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2018-02-05
    相关资源
    最近更新 更多