【问题标题】:Pygame: Weird blitting bugPygame:奇怪的 blitting bug
【发布时间】:2012-05-24 02:46:03
【问题描述】:

我一直在用 Python 3.1 和 PyGame 2.7 制作一个简单的主菜单。当鼠标光标在某个区域内时,屏幕上的文本应变为白色。我正在使用功能来blit屏幕。第一个函数正常地对主菜单进行 blit,另外两个函数通过突出显示一些文本来对菜单进行 blit。 以下是函数:

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

由于某种原因,当鼠标悬停在“新游戏”上时,它不会改变颜色。当鼠标悬停在“加载游戏”上时,它会改变颜色。在我添加加载游戏的东西之前,它会改变颜色,但现在只有加载游戏会。关于为什么它不起作用的任何想法?

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

【问题讨论】:

  • “不起作用”是什么意思。另外,我无法理解那里的许多if 语句;你在指定按钮的位置吗?
  • 在绘制函数结束时调用pygame.display.flip()。 (所以它会填充,blit 表面,然后翻转/更新。
  • @monkey 我的主循环中有这个。请仔细阅读问题,我在添加下一部分代码之前说过它有效。
  • @TankorSmash 我的意思是它不会破坏它。 if 语句正在检查鼠标是否在正确的位置。
  • @GoTeamZelda_ 的正确位置是什么?无论如何,我认为你应该用尽可能多的信息重写它,因为我们不知道其余的代码,而且总是有可能出现错误。最后,monkey 只是想提供帮助,这只是暗示您正确编写了 flip 函数,所以请注意我们是来提供帮助的。

标签: python pygame


【解决方案1】:

菜单被绘制了两次。当鼠标检查代码评估光标在 NEW GAME 上时,draw_main_menu_ng 正在调用。但是,检查继续进行,当它评估游标 not 超过 LOAD GAME 时,它还调用 draw_main_menu,否定调用 draw_main_menu_ng 的效果。

当光标位于选项内时,一个最小的解决方案是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 语句,那么之后您可能会遇到其他问题。我制作了完整的示例,因此您可以看到避免使用文字和几乎相同的语句的好方法。我很乐意发布它,因为您必须了解它才能将其集成到游戏中。

【讨论】:

    【解决方案2】:

    代替所有的 if 语句,在文本周围创建一个矩形并使用函数 rect.collidepoint((x, y)) (返回布尔值)会容易得多。它将使您的代码更短,甚至可能更快。

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多