首页 > 解决方案 > 如何阻止 pygame 按钮在单独的图像中重叠?

问题描述

我正在 pygame 中制作一个简单的基于文本的 RPG 游戏。我正在使用一个按钮类(不是我的,但使用和集成相当简单)来添加带有选项的按钮。但是,似乎第二张幻灯片上的按钮“坐下等待”选项与第一张幻灯片上的“否”按钮重叠。由于 No 按钮关闭游戏,按下 Sit and Wait 似乎也关闭了游戏。我尝试以不同的方式对 if-else 语句进行排序,但其他每个按钮似乎都很好。有小费吗?谢谢你。这是我的代码,对不起,我知道它很多,但我是初学者,还不擅长浓缩:

import pygame
pygame.init()
win=pygame.display.set_mode((800,700))
win.fill((255,255,255))
our_game_display=pygame.Surface((800,700))
font_name = pygame.font.get_default_font()
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, win, outline=None):
        # Call this method to draw the button on the screen
        if outline:
            pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0)
        pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0)
        if self.text != '':
            font = pygame.font.SysFont('comicsans', 30)
            text = font.render(self.text, 1, (255, 255, 255))
            win.blit(text, (self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2)))
    def isOver(self, pos):
        # Pos is the mouse position or a tuple of (x,y) coordinates
        if pos[0] > self.x and pos[0] < self.x + self.width:
            if pos[1] > self.y and pos[1] < self.y + self.height:
                return True
        return False
def draw_text(text, size, x, y):
        pygame.font.init()
        font = pygame.font.Font(font_name, size)
        text_surface = font.render(text, True, (255,255,255))
        text_rect = text_surface.get_rect()
        text_rect.center = (x, y)
        our_game_display.blit(text_surface, text_rect)
def yell_choice():
    our_game_display.fill((0, 0, 0))
    draw_text('You yell to see if anyone can hear you.', 20, 800 / 2, 700 / 2 - 100)
    win.blit(our_game_display, (0, 0))
    pygame.display.update()
def sit_choice():
    our_game_display.fill((0,0,0))
    draw_text('So you decided to sit and wait', 20, 800 / 2, 700 / 2 - 100)
    win.blit(our_game_display,(0,0))
    pygame.display.update()
def search_choice():
    our_game_display.fill((0,0,0))
    draw_text('So you decided to search', 20, 800 / 2, 700 / 2 - 100)
    win.blit(our_game_display,(0,0))
    pygame.display.update()
def beginning_question():
    our_game_display.fill((0, 0, 0))
    draw_text('The story of this game depends on your choices. Do you wish to play?', 20, 800 / 2, 700 / 2 - 100)
    win.blit(our_game_display, (0, 0))
    YesButton.draw(win, (255, 255, 255))
    NoButton.draw(win, (255, 255, 255))
    pygame.display.update()
def begin_game():
    our_game_display.fill((0,0,0))
    draw_text("You wake up, or...at least you think you do. Even though your eyes are open,", 20, 800 / 2, 700 / 2 - 300)
    draw_text("they still can't detect anything in the complete darkness that surrounds you.", 20, 800 / 2, 700 / 2 - 270)
    draw_text("What do you do?", 20, 800 / 2, 700 / 2 - 210)
    win.blit(our_game_display,(0,0))
    SitWaitButton.draw(win,(255,255,255))
    SearchButton.draw(win,(255,255,255))
    YellHelpButton.draw(win,(255,255,255))
    pygame.display.update()
#game loop
running = True
#color,x,y,width,height
YesButton=button((0,0,0),100,500,250,100,'Yes')
NoButton=button((0,0,0),450,500,250,100,'No')
SitWaitButton=button((0,0,0),500,500,200,50,'Sit and wait..')
SearchButton=button((0,0,0),250,600,600,50,'Get up and try to search your surroundings.')
YellHelpButton=button((0,0,0),250,400,600,50,'Yell to see if anyone is there.')
game_begun='Input' #beginning choice variable
search_option='Input'#search variable
sit_option='Input' #sitting variable
yell_option = 'Input'
while running:
    if search_option=='Go':
        search_choice()
    elif sit_option=='Go':
        sit_choice()
    elif yell_option=='Go':
        yell_choice()
    elif game_begun=='Go':
        begin_game()
    else:
        beginning_question()
    for event in pygame.event.get():
        pos=pygame.mouse.get_pos()
        if event.type==pygame.QUIT:
            running = False
            pygame.quit()
            quit()
    #yes and no buttons for beginning question
        if event.type==pygame.MOUSEBUTTONDOWN:
            if SitWaitButton.isOver(pos):
                sit_option = 'Go'
                print("this button is working")
            if SearchButton.isOver(pos):
                search_option = 'Go'
                print("this button is working")
            if YellHelpButton.isOver(pos):
                yell_option='Go'
                print("this button is working")
            if YesButton.isOver(pos):
                game_begun='Go'
            if NoButton.isOver(pos):
                running=False
                pygame.quit()
                quit()
        if event.type==pygame.MOUSEMOTION:
            if YellHelpButton.isOver(pos):
                YellHelpButton.color = (0, 0, 139)
            elif SitWaitButton.isOver(pos):
                SitWaitButton.color = (0, 0, 139)
            elif SearchButton.isOver(pos):
                SearchButton.color = (0, 0, 139)
            elif YesButton.isOver(pos):
                YesButton.color=(0,0,139)
            elif NoButton.isOver(pos):
                NoButton.color=(0,0,139)
            else:
                YesButton.color=(0,0,0)
                NoButton.color=(0,0,0)
                YellHelpButton.color = (0, 0, 0)
                SitWaitButton.color = (0, 0, 0)
                SearchButton.color = (0, 0, 0)

标签: pythonpygame

解决方案


您可以使用如下if-elif指令,这样如果用户在光标位于sit and wait按钮上方时单击鼠标,则您不会检查它是否也在quit按钮上方(因此您不会退出游戏):

            if SitWaitButton.isOver(pos):
                sit_option = 'Go'
                print("this button is working")
            elif NoButton.isOver(pos):
                pygame.quit()
                quit()

然而,更简洁的解决方案是记住当前实际显示了哪些按钮。例如,您可以有一个可见按钮列表,并且仅当该按钮在列表中时才检查光标是否位于按钮上方。

附带说明一下,您的running变量是无用的:您的主循环是while running,但一旦running设置为False您立即退出游戏。因此,您的循环条件永远不会评估为False.


推荐阅读