python - 如何阻止 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)
解决方案
您可以使用如下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
.
推荐阅读
- powershell - Powershell:嵌入式哈希表写入 CSV
- javascript - dotnet core - 多个 SPA - 从一个 SPA 重定向到另一个 SPA、NextJS 和 React
- .net - ASP .NET MVC 应用程序在具有多个环境时如何确定使用哪个配置文件?
- php - PHP - 获取带或不带密钥的 URL 参数
- python - 基于 row -index 0 删除列
- javascript - Vue 应用程序在为生产环境构建后无法正常工作
- python - 使用另一个数组的值作为索引填充 (10000,2) 数组的行
- html - Veutify - 无法在 v-card-title 内将 v-img 和 v-text 完全居中对齐
- python - 在不绕过 SSL 验证的情况下使用 urllib 时如何解决 [SSL: CERTIFICATE_VERIFY_FAILED] 错误
- java - 想要在不设置 classpath 或将 jar 文件放入 java 预定义库的情况下访问 jar 中的 .class 文件?