Pygame:奇怪的blitting bug

时间:2012-05-24 02:46:04

标签: python pygame

我一直在使用Python 3.1和PyGame 2.7中的简单主菜单。当鼠标光标位于某个区域内时,屏幕上的文本应变为白色。我正在使用功能来点亮屏幕。第一个功能正常打开主菜单,另外两个功能会在菜单中突出显示一些文本。 以下是功能:

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坐标的范围。

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()

出于某种原因,当鼠标结束时#34; NEW GAME&#34;它不会改变颜色。当鼠标结束时#34; LOAD GAME&#34;它会改变颜色。在我添加加载游戏之前,它会改变颜色,但现在只加载游戏。关于它为什么不起作用的任何想法?

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

2 个答案:

答案 0 :(得分:1)

菜单正在绘制两次。当鼠标检查代码评估光标在 NEW GAME 上时,draw_main_menu_ng 调用。但是,检查会继续,当它评估光标而不是 LOAD GAME 时,它也会调用draw_main_menu,否定调用draw_main_menu_ng的效果1}}。

一旦光标位于选项中,最小的解决方案就是return

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 :(得分:0)

而不是所有的if语句,在文本周围做一个矩形并使用函数rect.collidepoint((x,y))(返回bool)要容易得多。它会使你的代码更短,也可能更快。

相关问题