Pygame中的鼠标悬停 [英] Mouseover in Pygame

查看:293
本文介绍了Pygame中的鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个标题听起来很像 在Pygame中检测鼠标悬停在图像上 但有一个错误.我尝试遵循该问题的两个答案,但从未成功.这是我的代码的一部分:

I know this title sounds a lot like Detect mouseover an image in Pygame but there's an error. I've tried following both answers from that question, but it never worked. Here is a part of my code:

title = font3.render(('SQUASH!'), True, white)
play = font1.render(('PLAY'), True, white)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit((title), (400, 400))
    screen.blit(play, (theposition))
    if play.collide_point(pygame.mouse.get_pos()):
        hi = font1.render(('hi'), True, white)
        screen.blit(hi, (300, 300))
    pygame.display.flip()
    time.sleep(0.000000000000000000000000000000000000000000000000000000001)
    pygame.display.flip()

基本上,"hi"是使用第一个答案的测试.如果我将鼠标悬停在按钮上,则尝试更改文本(粗体,下划线等).它似乎不起作用.

The 'hi' is basically a test using the first answer. I am trying to make the text change (bold, underline etc.) if I hover over a button with the mouse. It doesn't seem to work.

推荐答案

下面是一些示例代码:

import pygame
from pygame.locals import *
import time

if __name__ == "__main__":
    pygame.init()
    size = (700, 700)
    screen = pygame.display.set_mode(size)
    font = pygame.font.SysFont('Impact',20,16)

    title = font.render(('SQUASH!'), True, (255,255,255))
    play = font.render(('PLAY'), True, (255,255,255))
    play_r = play.get_rect()
    play_r.x, play_r.y = 300,300

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
        screen.fill((0,255,0,))
        screen.blit(title, (400, 400))
        screen.blit(play, (300,300))
        if play_r.collidepoint(pygame.mouse.get_pos()):
            print 'Detected'
        time.sleep(0.000000000000000000000000000000000000000000000000000000001)
        pygame.display.flip()

您必须创建一个get,以准确告诉您在哪里知道使用collidepoint()的文本.现在,当您将鼠标悬停在文本上时,它将打印出Detected到控制台.

you have to create a get to tell exactly where the text is know that there is a rect you use collidepoint() and now when you mouse over the text it will print Detected to the console

这篇关于Pygame中的鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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