如何用python编写按钮(没有Tkinter)?

如何用python编写按钮(没有Tkinter)?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我最近开始学习Python3,我试图通过导入Python3制作一个游戏。我试着做一份菜单,但我正在努力。我只是想让它看起来像一个按钮,当你将鼠标悬停在矩形上时,它会改变颜色,但它不起作用。我已经试过一些东西,但不起作用。无论如何,以下是完整的代码: 这是我尝试制作按钮的部分: def game_intro(): intro = True gameDisplay.fill(white) largeText = pygame.font.Font('freesansbold.

我最近开始学习Python3,我试图通过导入Python3制作一个游戏。我试着做一份菜单,但我正在努力。我只是想让它看起来像一个按钮,当你将鼠标悬停在矩形上时,它会改变颜色,但它不起作用。我已经试过一些东西,但不起作用。无论如何,以下是完整的代码:

这是我尝试制作按钮的部分:

def game_intro():
    intro = True

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
        pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

    smallText = pygame.font.Font('freesansbold.ttf' ,20)
    textSurf, textRect = text_objects("START!", smallText)
    textRect.center = ( (150+(100/2)), (450+(430/2)) )
    gameDisplay.blit(textSurf, textRect)

    pygame.draw.rect(gameDisplay, red, (550, 430, 100, 50))

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

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

这里的问题是,在函数开始时,您只进行了一次鼠标悬停测试。如果鼠标稍后移动到您的矩形中,这无关紧要,因为您再也不会进行测试

您要做的是将其移动到事件循环中。PyGame事件循环中的一个棘手问题是,对于…循环中的事件,您希望每个事件内部运行一次代码,而对于外部while intro循环,您只希望每个批运行一次代码。在这里,我假设您希望每个事件执行一次。因此:

def game_intro():
    intro = True

    # ... other setup stuff 

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

            mouse = pygame.mouse.get_pos()

            if 150+100 > mouse[0] > 150 and 430+50 > mouse[1] > 430:
                pygame.draw.rect(gameDisplay, bright_green, (150,430,100,50))
            else:
                pygame.draw.rect(gameDisplay, green, (150, 430, 100, 50))

看起来你只做过一次的其他事情也属于循环,所以你的游戏可能仍然有一些问题。但这应该会让你克服困难,并向你展示如何着手解决其他问题。

这里有一个按钮应该适合你的需要:

class Button(object):
    global screen_width,screen_height,screen
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color
        self.background_color=background_color

    def check(self):
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        pygame.draw.rect(screen, self.background_color,(self.rect),0)
        drawTextcenter(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)  
        pygame.draw.rect(screen,self.text_color,self.rect,3)
使用“绘制”功能绘制按钮,使用“检查”功能查看按钮是否被按下

在主循环中实现:

button=Button(x,y,width,height,text_color,background_color,text)
while not done:
    for event in pygame.event.get():
            if event.type==QUIT:
                terminate()
            elif event.type==pygame.MOUSEBUTTONDOWN:
                if button.check():
                       #what to do when button is pressed

    #fill screen with background
    screen.fill(background)

    button.draw()

    pygame.display.flip()
    clock.tick(fps)

这就是我所做的,它现在起作用了:

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            # print(event)
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = text_objects("Run Abush Run!", largeText)
    TextRect.center = ((display_width / 2), (display_height / 2))
    gameDisplay.blit(TextSurf, TextRect)

    mouse = pygame.mouse.get_pos()

    # print(mouse)

    if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
        pygame.draw.rect(gameDisplay, bright_green, (150, 450, 100, 50))
    else:
        pygame.draw.rect(gameDisplay, green, (150, 450, 100, 50))
    pygame.draw.rect(gameDisplay, red, (550, 450, 100, 50))
    pygame.display.update()
    clock.tick(15)

感谢@abarnert的帮助

请在问题中输入代码,而不是外部链接。还有,一个垃圾桶比把所有的东西都扔掉要好得多。@abarnert我照你说的做了。好多了。虽然我认为你在这里粘贴的缩进是错误的。这其中有多少是游戏介绍功能的一部分?@abarnert我编辑了间距。我想他在代码中是对的,但在标记中把它搞糟了,只在我描述的pygame中实现简单按钮的几种方法的第一行中隔开。您还可以查看一些pygame GUI库,如SGC。@abarmert似乎是合法的。