回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在用pygame制作一个游戏,我希望当鼠标悬停在我的文本上时,框会变暗。你知道吗</p>
<p>到目前为止,我掌握的情况如下:</p>
<p>在事件处理循环中:
(<code>CheckToUnshadeBoxes</code>中的元组是一对文本表面及其框(来自<code>get_rect</code>方法))</p>
<pre><code>elif event.type == MOUSEMOTION:
CheckToShadeBoxes(event, LERect, LoadRect, PlayRect)
CheckToUnshadeBoxes(event, (LERect, LESurf),
(LoadRect, LoadSurf), (PlayRect, PlaySurf))
</code></pre>
<p>这里是<code>CheckToShadeBoxes</code>:</p>
<pre><code>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))
</code></pre>
<p>和<code>CheckToUnshadeBoxes</code>:</p>
<pre><code>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))
</code></pre>
<p>这个很好用!只是当我把鼠标移到盒子里面的时候,盒子会越来越暗,直到你看不到文本为止!我知道这是一个小细节,但它已经困扰了我很长时间,我不知道如何修复它。你知道吗</p>
<p>顺便说一下,如果不够明显的话,<code>COLOURS</code>是一个带有字符串键和RGB元组值的字典,<code>SURFACE</code>是我的主绘图面。你知道吗</p>
<p>如果你对我的代码有任何疑问,或者我做过的任何事情,请发表评论!你知道吗</p>