微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Pygame 一次只绘制一个“按钮”

如何解决Pygame 一次只绘制一个“按钮”

我是 pygame 的新手,所以可能有些东西需要整理;因为这只是一个大学项目,所以我暂时不太关心。

class button():
    def __init__(self,color,x,y,width,height,text=''):
        self.color = color
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text

    def draw(self,screen,outline=None):
        #Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(screen,outline,(self.x-2,self.y-2,self.width+4,self.height+4),0)
            
        pygame.draw.rect(screen,self.color,(self.x,self.y,self.width,self.height),0)
        
        if self.text != '':
            font = pygame.font.Font('/Users/luke/Documents/Education/UEA/Masters /Application Programming/Assignment 2/Practise/digital-7 (italic).ttf',30)
            text = font.render(self.text,True,black)
            screen.blit(text,(self.x + (self.width/2 - text.get_width()/2),self.y + (self.height/2 - text.get_height()/2)))

    def isOver(self,pos):
        # self.x + self.width > pos[0] > self.x and self.y + self.height > pos[1] > self.y:
        #Pos is the mouse position or a tuple of (x,y) coordinates
        if self.x + self.width > pos[0] > self.x and self.y + self.height > pos[1] > self.y:
            return True
        else:
            return False

我已经将 Tech with Tim 代码用于“按钮”的类,它可以正常工作,然后我创建了一个函数来在我想使用它时调用按钮(它应该正常工作,在大多数情况下)。

def draw_button(name,msg,active,inactive,w,h,action=None):
    name = button(inactive,msg)
    name.draw(screen,outline)
    butn = True
    while butn:
        clock.tick(60)
        name.draw(screen,outline)
        pygame.display.flip()

        for event in pygame.event.get():
            pos = pygame.mouse.get_pos()
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            
            if event.type == pygame.MOUSEBUTTONDOWN:
                if name.isOver(pos):
                    action()
            
            if event.type == pygame.MOUSEMOTION:

                if name.isOver(pos):
                    name.color = active
                else:
                    name.color = inactive

我也不太确定为什么缩进没有上传到这个(但它们在我的代码中是正确的)所以我知道这不是问题..但是,当调用函数时,只有第一个出现。让我告诉你我的意思。

标题画面代码为例:

def titlescreen():
    active = True

    while active:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit_game()

        screen.fill(background)
        title_text('Welcome To')
        titleimage()

        # btn("Start",240,380,150,50,button,btn_hover,mainmenu)
        # btn("Quit",420,exit_game)

        draw_button("Start_button","START",black,buttonnormal,mainmenu)

        draw_button("exit_button","quit",exit_game)


        pygame.display.update()
        clock.tick(60)

运行时,它会创建:

Start_button appearing

如您所见,仅创建了“start_button”按钮。反之,当 Start_button 注释掉时,exit_button 出现。

exit_button appearing

所以我的问题是,我如何让它们同时出现...

提前致谢。

编辑: 这是显示所有按钮的新代码,但它们会闪烁。

enter image description here

解决方法

当然可以,因为方法 while 中有一个额外的 draw_button 循环。您有一个应用程序循环。用它!您根本不需要 draw_button 中的 while 循环。

您还需要从 draw_button 中删除显示更新。在应用程序循环结束时更新一次显示就足够了。多次调用 pygame.display.update()pygame.display.flip() 会导致闪烁。
Why is the PyGame animation is flickering

接下来您将遇到事件问题。 pygame.event.get() 获取所有消息并将其从队列中删除pygame.event.get() 获取所有消息并将其从队列中删除。请参阅文档:

这将获取所有消息并将它们从队列中删除。 [...]

如果在多个事件循环中调用 pygame.event.get(),则只有一个循环接收事件,但不会所有循环都接收所有事件。因此,似乎错过了一些事件。
获取事件一次并在多个循环中使用它们或将列表或事件传递给处理它们的函数和方法。
Faster version of 'pygame.event.get()'. Why are events being missed and why are the events delayed?

def draw_button(button,outline,active,inactive,event_list,action=None):

    mouse_pos = pygame.mouse.get_pos()
    if button.isOver(mouse_pos):
        button.color = active
    else:
        button.color = inactive

    button.draw(screen,outline)
    
    for event in event_list:
        if event.type == pygame.MOUSEBUTTONDOWN:
            if action and button.isOver(event.pos):
                action()
def titlescreen():
    active = True

    start_button = button(buttonnormal,240,380,150,50,"START")
    quit_button = button(buttonnormal,420,"quit")

    while active:
        
        event_list = pygame.event.get()
        for event in event_list:
            if event.type == pygame.QUIT:
                exit_game()

        screen.fill(background)
        title_text('Welcome To')
        titleimage()

        draw_button(start_button,black,btn_hover,buttonnormal,mainmenu)
        draw_button(quit_button,exit_game)

        pygame.display.update()
        clock.tick(60)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。