如何将图像或图标添加到 Pygame 中的按钮矩形? [英] How can I add an image or icon to a button rectangle in Pygame?

查看:77
本文介绍了如何将图像或图标添加到 Pygame 中的按钮矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

def button(x, y, w, h, ic, ac, action=None):鼠标 = pygame.mouse.get_pos()点击 = pygame.mouse.get_pressed()如果 x + w >鼠标[0] >x 和 y + h >鼠标[1] >y:pygame.draw.rect(屏幕,ac,(x,y,w,h))如果 click[0] == 1 和 action!= None:如果动作==继续":测验()别的:pygame.draw.rect(屏幕,ic,(x,y,w,h))pygame.display.update()为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()screen.blit(randomList, [0, 0])按钮(399、390、300、50、红色、亮红色、继续")pygame.display.update()

解决方案

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

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

然后 blit 它是矩形的顶部:

def button(x, y, w, h, ic, ac, img, imgon, action=None):鼠标 = pygame.mouse.get_pos()点击 = pygame.mouse.get_pressed()rect = pygame.Rect(x, y, w, h)on_button = rect.collidepoint(鼠标)如果 on_button:pygame.draw.rect(屏幕,交流,矩形)screen.blit(imgon, imgon.get_rect(center = rect.center))别的:pygame.draw.rect(屏幕,IC,矩形)screen.blit(img, img.get_rect(center = rect.center))如果 on_button:如果 click[0] == 1 和 action!= None:如果动作==继续":测验()image = pygame.image.load('my_image.png').convert_alpha()imageOn = pygame.image.load('my_image_on.png').convert_alpha()为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:pygame.quit()放弃()screen.blit(randomList, [0, 0])按钮(399、390、300、50、红色、亮红色、图像、图像开启、继续")pygame.display.update()


在 pygame 中,按钮只不过是一个

导入pygame类精灵对象(pygame.sprite.Sprite):def __init__(self, x, y, 文件名):super().__init__()img = pygame.image.load(文件名).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_imageself.rect = self.image.get_rect(center = (x, y))self.hover = 假定义更新(自我):self.hover = self.rect.collidepoint(pygame.mouse.get_pos())self.image = self.hover_image if self.hover else self.original_imagepygame.init()窗口 = pygame.display.set_mode((300, 300))时钟 = 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'),])运行 = 真运行时:时钟滴答(60)event_list = pygame.event.get()对于 event_list 中的事件:如果 event.type == pygame.QUIT:运行 = 错误组.更新()window.fill(0)group.draw(窗口)pygame.display.flip()pygame.quit()出口()

I am a newbie to Pygame and I have created already the codes for my button but I still have a problem because I don't know how will I put an image instead of the solid color red in a rectangle. Here is my codes, hope you can help!

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()

解决方案

All you have to do is to load an image:

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

And blit it an top of the rectangle:

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()


In pygame a button is nothing more than a pygame.Surface object. It is completely irrelevant whether a text or an image is on the button. I recommend to represent the buttons by pygame.sprite.Sprite objects.

See also Pygame mouse clicking detection respectively Mouse and Sprite.

Minimal example:

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()

这篇关于如何将图像或图标添加到 Pygame 中的按钮矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆