简体   繁体   English

Pygame:怪异的bug

[英]Pygame: Weird blitting bug

I've been working on a simple main menu in Python 3.1 and PyGame 2.7. 我一直在研究Python 3.1和PyGame 2.7中的简单主菜单。 When the mouse cursor is within a certain area, text on the screen should become white in color. 当鼠标光标在某个区域内时,屏幕上的文本应变为白色。 I'm using functions to blit the screen. 我正在使用功能来消光屏幕。 The first function blits the main menu normally, and the other two functions blit the menu with some text highlighted. 第一个功能正常关闭主菜单,其他两个功能突出显示某些文本,使菜单关闭。 Here are the functions: 功能如下:

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

Below, I'm using the variables x and y to check if the mouse is hovering over the buttons. 在下面,我使用变量x和y检查鼠标是否悬停在按钮上。 The first set of two numbers is the range of the x coordinate, and the second set of two is the range for the y coordinate. 第一组的两个数字是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()

For some reason, when the mouse is over "NEW GAME" it won't change color. 由于某些原因,当鼠标悬停在“ NEW GAME”上时,它不会改变颜色。 When the mouse is over "LOAD GAME" it will change color. 当鼠标移到“ LOAD GAME”上时,它将改变颜色。 Before I added the load game stuff, it would change color, but now only load game will. 在添加加载游戏的东西之前,它会改变颜色,但是现在只有加载游戏会改变颜色。 Any ideas on why it isn't working? 关于为什么它不起作用的任何想法?

By the way, I do have code in the game loop that updates the screen. 顺便说一句,我确实在游戏循环中有代码可以更新屏幕。 I also have code to set up the font. 我也有设置字体的代码。

The menu is being drawn twice. 菜单被绘制两次。 When the mouse check code evaluates the cursor is over NEW GAME , draw_main_menu_ng is being called. 当鼠标检查代码确定光标在NEW GAME draw_main_menu_ng 将调用draw_main_menu_ng However, the check continues, and when it evaluates the cursor is not over LOAD GAME , it also calls draw_main_menu , negating the effects of the call to draw_main_menu_ng . 但是,检查继续进行,并且当它评估游标超过LOAD GAME时 ,它还会调用draw_main_menu ,从而消除对draw_main_menu_ng的调用的影响。

A minimal solution is to return once the cursor is inside an option: 最小的解决方案是将光标置于选项内后return

if new_game_x_pos == True and new_game_y_pos == True:
    draw_main_menu_ng()
    return

Alternative Solution 替代解决方案

I've added a full example of how I would make a highlight-able menu . 我添加了一个完整的示例,说明如何制作可突出显示的菜单 I realize it is much more than you are asking about, but if you just added a return statement to your code, you would likely run into other problems afterward. 我意识到这远远超出了您的要求,但是如果您只是在代码中添加了return语句,那么以后可能会遇到其他问题。 I made the full example so you can see a good way to avoid using literals and nearly identical statements. 我做了完整的示例,因此您可以看到避免使用文字和几乎相同的语句的好方法。 I feel comfortable posting it because you would have to understand it to integrate it into a game. 我发布它很自在,因为您必须了解它才能将其集成到游戏中。

Instead of all the if statements, it would be much easier to make a rect around the text and use the function rect.collidepoint((x, y)) (return bool). 代替所有的if语句,在文本周围创建一个rect并使用rect.collidepoint((x,y))函数(返回布尔值)会容易得多。 It will make your code much shorter and probably faster. 这将使您的代码更短,甚至可能更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM