简体   繁体   English

当鼠标在游戏中徘徊时如何遮挡一个盒子?

[英]How to shade a box when mouse hovers in pygame?

I am making a game, with pygame, and i want it to be that when the mouse hovers over my text, the box gets shaded. 我正在制作一个游戏,使用pygame,我希望它是当鼠标悬停在我的文本上时,框会被遮挡。

here is what I have so far: 这是我到目前为止:

in the event-handling loop: (tuples in CheckToUnshadeBoxes are pairs of text-surfaces and their boxes (from get_rect method)) 在事件处理循环中:( CheckToUnshadeBoxes中的元组是一对文本表面及其框(来自get_rect方法))

elif event.type == MOUSEMOTION:
    CheckToShadeBoxes(event, LERect, LoadRect, PlayRect)
    CheckToUnshadeBoxes(event, (LERect, LESurf),
                        (LoadRect, LoadSurf), (PlayRect, PlaySurf))

here is CheckToShadeBoxes : 这是CheckToShadeBoxes

def CheckToShadeBoxes(event, *args):
    '''
    shade the box the mouse is over
    '''

    for rect in args:
        s = pg.Surface((rect.width, rect.height))
        s.set_alpha(50)
        print(rect.x, rect.y)
        s.fill(COLOURS['gray'])
        x, y = event.pos
        if rect.collidepoint(x, y):
            SURFACE.blit(s, (rect.x, rect.y))

and CheckToUnshadeBoxes : CheckToUnshadeBoxes

def CheckToUnshadeBoxes(event, *args):
    ''' if the mouse moves out of the box, the box will become unshaded.'''
    for (rect, TextSurf) in args:

        x, y = event.pos
        if not rect.collidepoint(x, y):
            SURFACE.blit(TextSurf, (rect.x, rect.y))

This works fine! 这很好用! except that when I move the mouse inside of the box, the box will continue to get darker and darker until you cant even see the text! 除了当我将鼠标移到盒子里面时,盒子会继续变暗和变暗,直到你看不到文字! I know it is a small detail, but it has been bugging me for a long time and I don't know how to fix it. 我知道这是一个小细节,但它已经困扰了我很长一段时间,我不知道如何解决它。

by the way, if it is not evident enough, COLOURS is a dictionary with string keys and RGB tuple values, and SURFACE is my main drawing surface. 顺便说一句,如果不够明显, COLOURS是一个包含字符串键和RGB元组值的字典,而SURFACE是我的主要绘图表面。

If you have any questions about my code, or anything I have done just comment! 如果您对我的代码有任何疑问,或者我做过任何评论!

If the rects you are passing are your own derived class, then I recommend making a .is_shaded class variable and checking to make sure the variable is false before shading it. 如果您传递的rects是您自己的派生类,那么我建议您创建一个.is_shaded类变量并检查以确保该变量在着色之前为false。

If these rects aren't your own class extending another, then I recommend you make one, as it would make it much simpler 如果这些rects不是你自己的类扩展另一个,那么我建议你做一个,因为它会使它更简单

The problem with your code is that you keep adding in dark rectangles when the mouse is hovered over. 代码的问题在于,当鼠标悬停在上方时,您会继续添加深色矩形。 No wonder it gets darker and darker. 难怪它会变得更暗更暗。

All that is needed is for your code to be shuffled around a little. 所需要的只是让您的代码稍微改变一下。

Inside your event handling, you should call a function to check if it should shade the text. 在事件处理中,您应该调用一个函数来检查它是否应该对文本进行着色。 This should return a Boolean value. 这应该返回一个布尔值。 Store this in a variable. 将其存储在变量中。 Most of the code from the CheckToShadeBoxes function will do the trick. 来自CheckToShadeBoxes函数的大多数代码都可以解决这个问题。

In your render section, you should render just one surface on top of your text, based on whether the Boolean value is true or not. 在渲染部分中,您应该根据布尔值是否为真来渲染文本顶部的一个曲面。 The code that creates a gray surface in the CheckToShadeBoxes function will work but make sure it is only one surface. CheckToShadeBoxes函数中创建灰色曲面的代码将起作用,但要确保它只是一个曲面。 Don't create a new surface in every iteration. 不要在每次迭代中创建新曲面。 Define it outside once and blit it to the screen inside the loop. 在外面定义一次并将其blit到循环内的屏幕。

This should fix your problem! 这应该可以解决你的问题!

I hope this answer helps you! 我希望这个答案可以帮到你! If you have any further questions please feel free to post a comment below! 如果您有任何其他问题,请随时在下面发表评论!

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

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