Pygame:奇怪的 blitting 错误 [英] Pygame: Weird blitting bug

查看:48
本文介绍了Pygame:奇怪的 blitting 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在用 Python 3.1 和 PyGame 2.7 开发一个简单的主菜单.当鼠标光标在某个区域内时,屏幕上的文本应变为白色.我正在使用函数来闪烁屏幕.第一个函数正常闪烁主菜单,其他两个函数闪烁菜单并突出显示一些文本.以下是功能:

I've been working on a simple main menu in Python 3.1 and 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))

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

Below, I'm using the variables x and y to check if the mouse is hovering over the buttons. 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 = 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()

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

For some reason, when the mouse is over "NEW GAME" it won't change color. When the mouse is over "LOAD GAME" it will change color. 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.

推荐答案

菜单被绘制两次.当鼠标检查代码评估光标在NEW GAME 上时,draw_main_menu_ng 被调用.然而,检查继续,当它评估光标not超过LOAD GAME时,它也调用draw_main_menu,否定调用的效果draw_main_menu_ng.

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

一个最小的解决方案是在光标位于选项内时return:

A minimal solution is to return once the cursor is inside an option:

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

<小时>

替代方案

我添加了我将如何制作高亮菜单的完整示例.我意识到这比您要求的要多得多,但是如果您只是在代码中添加了 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. 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.

这篇关于Pygame:奇怪的 blitting 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆