Pygame 按钮功能问题
Pygame button issue in functionality
所以我正在为游戏创建一个菜单,并且制作了一个按钮功能。这些按钮确实有效,但只是有时:
- 第一个按钮(2-Player)几乎每次都首先点击
- 第二个按钮(1-Player)不太像每点击 10 次
- 第三个按钮(分数)比
其他
这对我来说没有意义,因为所有按钮都使用相同的功能:
def button(msg,x,y,h):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(screen, RED, (x,y, BUTTON_WIDTH, h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText, WHITE)
textRect.center = ((x+(BUTTON_WIDTH/2)),(y+(h/2)))
screen.blit(textSurf, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if x+BUTTON_WIDTH > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, BRIGHT_RED, (x,y, BUTTON_WIDTH, h))
screen.blit(textSurf, textRect)
if click[0] == 1:
return True
def intro_screen():
intro = True
while intro:
screen.fill(GREEN)
if button("2-Player",245,145,BUTTON_HEIGHT):
multiplayer_loop()
if button("1-Player",245,270,BUTTON_HEIGHT):
login_screen(True)
if button("Scores",245,395,40):
login_screen(False)
screen.blit(TITLE, (120, 5))
pygame.display.update()
pygame.init()
intro_screen()
Here is how the menu looks
非常感谢任何帮助,谢谢。
问题是按钮函数中的事件循环。请注意,pygame.event.get()
获取所有消息并从队列中删除 它们。
所以 1 个按钮(主要是第一个按钮)将获得事件,而其他按钮则没有事件。
从按钮中删除 pygame.event.get()
。在主应用程序循环中获取事件并将事件列表传递给按钮函数。
无论如何你根本不需要按钮函数中的事件循环,因为你通过 pygame.mouse.get_pressed()
.
评估按钮的状态
但是,我建议使用 MOUSEBUTTONDOWN
事件。参见 pygame.event
。
def button(events,msg,x,y,h):
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText, WHITE)
textRect.center = button_rect.center
mouse = pygame.mouse.get_pos()
button_rect = pygame.Rect(x, y, BUTTON_WIDTH, h)
hit = button_rect.collidepoint(mouse)
pygame.draw.rect(screen, BRIGHT_RED if hit else RED, button_rect)
screen.blit(textSurf, textRect)
if hit:
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
return True
def intro_screen():
intro = True
while intro:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
screen.fill(GREEN)
if button(events , "2-Player",245,145,BUTTON_HEIGHT):
multiplayer_loop()
if button(events , "1-Player",245,270,BUTTON_HEIGHT):
login_screen(True)
if button(events , "Scores",245,395,40):
login_screen(False)
screen.blit(TITLE, (120, 5))
pygame.display.update()
注意,每帧应该只调用 1 次 pygame.event.get()
。
所以我正在为游戏创建一个菜单,并且制作了一个按钮功能。这些按钮确实有效,但只是有时:
- 第一个按钮(2-Player)几乎每次都首先点击
- 第二个按钮(1-Player)不太像每点击 10 次
- 第三个按钮(分数)比 其他
这对我来说没有意义,因为所有按钮都使用相同的功能:
def button(msg,x,y,h):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(screen, RED, (x,y, BUTTON_WIDTH, h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText, WHITE)
textRect.center = ((x+(BUTTON_WIDTH/2)),(y+(h/2)))
screen.blit(textSurf, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if x+BUTTON_WIDTH > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(screen, BRIGHT_RED, (x,y, BUTTON_WIDTH, h))
screen.blit(textSurf, textRect)
if click[0] == 1:
return True
def intro_screen():
intro = True
while intro:
screen.fill(GREEN)
if button("2-Player",245,145,BUTTON_HEIGHT):
multiplayer_loop()
if button("1-Player",245,270,BUTTON_HEIGHT):
login_screen(True)
if button("Scores",245,395,40):
login_screen(False)
screen.blit(TITLE, (120, 5))
pygame.display.update()
pygame.init()
intro_screen()
Here is how the menu looks
非常感谢任何帮助,谢谢。
问题是按钮函数中的事件循环。请注意,pygame.event.get()
获取所有消息并从队列中删除 它们。
所以 1 个按钮(主要是第一个按钮)将获得事件,而其他按钮则没有事件。
从按钮中删除 pygame.event.get()
。在主应用程序循环中获取事件并将事件列表传递给按钮函数。
无论如何你根本不需要按钮函数中的事件循环,因为你通过 pygame.mouse.get_pressed()
.
评估按钮的状态
但是,我建议使用 MOUSEBUTTONDOWN
事件。参见 pygame.event
。
def button(events,msg,x,y,h):
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText, WHITE)
textRect.center = button_rect.center
mouse = pygame.mouse.get_pos()
button_rect = pygame.Rect(x, y, BUTTON_WIDTH, h)
hit = button_rect.collidepoint(mouse)
pygame.draw.rect(screen, BRIGHT_RED if hit else RED, button_rect)
screen.blit(textSurf, textRect)
if hit:
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
return True
def intro_screen():
intro = True
while intro:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
sys.exit()
screen.fill(GREEN)
if button(events , "2-Player",245,145,BUTTON_HEIGHT):
multiplayer_loop()
if button(events , "1-Player",245,270,BUTTON_HEIGHT):
login_screen(True)
if button(events , "Scores",245,395,40):
login_screen(False)
screen.blit(TITLE, (120, 5))
pygame.display.update()
注意,每帧应该只调用 1 次 pygame.event.get()
。