首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pygame:奇怪的bug bug

Pygame:奇怪的bug bug
EN

Stack Overflow用户
提问于 2012-05-24 10:46:04
回答 2查看 2.5K关注 0票数 2

我一直在用Python3.1和PyGame 2.7开发一个简单的主菜单。当鼠标光标位于某个区域内时,屏幕上的文本应变为白色。我正在使用函数对屏幕进行blit。第一个函数blits主菜单正常,另外两个函数blit菜单,一些文本高亮显示。以下是函数:

代码语言:javascript
复制
def draw_main_menu(): #draw main menu normally
    screen.fill(BLACK) #clears screen

    #set up text
    new_text = menu_font.render('NEW GAME', 1, (100, 100, 100))
    load_text = menu_font.render('LOAD GAME', 1, (100, 100, 100))
    options_text = menu_font.render('OPTIONS', 1, (100, 100, 100))
    quit_text = menu_font.render('QUIT', 1,  (100, 100, 100))

    #blit text
    screen.blit(new_text, (340, 425))
    screen.blit(load_text, (335, 455))
    screen.blit(options_text, (350, 485))
    screen.blit(quit_text, (373, 515))

def draw_main_menu_ng(): #draw main menu with new game text in white
    screen.fill(BLACK)

    new_text = menu_font.render('NEW GAME', 1, (255, 255, 255))
    load_text = menu_font.render('LOAD GAME', 1, (100, 100, 100))
    options_text = menu_font.render('OPTIONS', 1, (100, 100, 100))
    quit_text = menu_font.render('QUIT', 1,  (100, 100, 100))

    screen.blit(new_text, (340, 425))
    screen.blit(load_text, (335, 455))
    screen.blit(options_text, (350, 485))
    screen.blit(quit_text, (373, 515))

def draw_main_menu_lg(): #draw main menu with load game text in white
    screen.fill(BLACK)

    new_text = menu_font.render('NEW GAME', 1, (100, 100, 100))
    load_text = menu_font.render('LOAD GAME', 1, (255, 255, 255))
    options_text = menu_font.render('OPTIONS', 1, (100, 100, 100))
    quit_text = menu_font.render('QUIT', 1,  (100, 100, 100))

    screen.blit(new_text, (340, 425))
    screen.blit(load_text, (335, 455))
    screen.blit(options_text, (350, 485))
    screen.blit(quit_text, (373, 515))

下面,我使用变量x和y来检查鼠标是否悬停在按钮上。第一组两个数字是x坐标的范围,第二组两个数字是y坐标的范围。

代码语言:javascript
复制
x,y = pygame.mouse.get_pos()
#set up new game mouse positions
if x >= 340 and x <= 465:
    new_game_x_pos = True
if x < 340  or x > 465:
    new_game_x_pos = False

if y >= 425 and y <= 445:
    new_game_y_pos = True
if y < 425 or y > 445:
    new_game_y_pos = False

if new_game_x_pos == True and new_game_y_pos == True:
    draw_main_menu_ng()
if new_game_x_pos == False or new_game_y_pos == False:
    draw_main_menu()

#set up load game mouse positions
if x >= 335 and x <= 470:
    load_game_x_pos = True
if x < 335 or x > 470:
    load_game_x_pos = False

if y >= 455 and y <= 475:
    load_game_y_pos = True
if y < 455 or y > 475:
    load_game_y_pos = False

if load_game_x_pos == True and load_game_y_pos == True:
    draw_main_menu_lg()
if load_game_x_pos == False or load_game_y_pos == False:
    draw_main_menu()

由于某些原因,当鼠标停留在“新游戏”上时,它不会改变颜色。当鼠标在“加载游戏”上方时,它会改变颜色。在我添加加载游戏的东西之前,它会改变颜色,但现在只有加载游戏才会。有没有关于为什么它不工作的想法?

顺便说一下,我在游戏循环中有更新屏幕的代码。我还有设置字体的代码。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-05-25 11:07:12

菜单被绘制了两次。当鼠标检查代码评估光标在新游戏上时,将调用draw_main_menu_ng。但是,检查仍在继续,当它评估游标没有过载时,它还会调用draw_main_menu,从而取消对draw_main_menu_ng的调用效果。

最小的解决方案是在光标位于选项内部时使用return

代码语言:javascript
复制
if new_game_x_pos == True and new_game_y_pos == True:
    draw_main_menu_ng()
    return

替代解决方案

我添加了a full example of how I would make a highlight-able menu。我知道这比您所要求的要多得多,但是如果您只是在代码中添加了一条return语句,那么之后您很可能会遇到其他问题。我制作了完整的示例,这样您就可以看到一种避免使用文字和几乎相同的语句的好方法。我觉得把它贴出来很舒服,因为你必须理解它才能把它整合到游戏中。

票数 1
EN

Stack Overflow用户

发布于 2012-09-01 22:01:18

与所有if语句相比,在文本周围创建一个rect并使用函数rect.collidepoint((x,y)) (return bool)会容易得多。它会让你的代码变得更短,甚至更快。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10730464

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档