Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame中的鼠标器_Python_Python 2.7_Mouse_Pygame_Mouseover - Fatal编程技术网

Python Pygame中的鼠标器

Python Pygame中的鼠标器,python,python-2.7,mouse,pygame,mouseover,Python,Python 2.7,Mouse,Pygame,Mouseover,我知道这个标题听起来很像 但是有一个错误。我试着从那个问题中找出两个答案,但都没用。以下是我代码的一部分: 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()

我知道这个标题听起来很像 但是有一个错误。我试着从那个问题中找出两个答案,但都没用。以下是我代码的一部分:

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”基本上是使用第一个答案的测试。如果我用鼠标悬停在某个按钮上,我会尝试更改文本(粗体、下划线等)。它似乎不起作用。

以下是一些示例代码:

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来准确地告诉文本的位置,知道您使用了一个rect
collidepoint()
,现在当您将鼠标悬停在文本上时,它会将检测到的文本打印到控制台上

import pygame

pygame.init()
screen = pygame.display.set_mode((200, 200))

# the 'normal' font
base_font = pygame.font.SysFont('Arial', 20, 16)

# the 'mouse-over' font
mod_font  = pygame.font.SysFont('Arial', 20, 16)
mod_font.set_underline(True)

# always use some kind of cache when rendering font
# font rendering is very expensive, and it *will*
# slow down your application if you render some text
cache = {}
def render(text, mod=False):
    if not mod in cache: 
        cache[mod] = {}
    if not text in cache[mod]: 
        cache[mod][text] = (mod_font if mod else base_font).render(text, True, (255, 255, 255))
    return cache[mod][text]

# create a list of (text, Rect)-tuples
x, y = 10, 20
objetcs = [(t, render(t).get_rect()) for t in ('Hi', 'please', 'click', 'me')]    

# just move some objects around
for (_, r) in objetcs:
    r.top, r.left = y, x
    x *= 2
    y += 35

# always use a Clock to keep your framerate constant
clock = pygame.time.Clock()

run = True
while run:
    # clear the screen
    screen.fill((0, 0, 0))

    # draw all objects
    for (t, r) in objetcs:
        # decide if we want to render the text with or without the underline
        # based on the result of collidepoint(pygame.mouse.get_pos())
        # note that collidepoint is a method of Rect, not Surface
        screen.blit(render(t, r.collidepoint(pygame.mouse.get_pos())), r)

    # check which item is clicked
    if pygame.event.get(pygame.MOUSEBUTTONDOWN):
        # calling 'render' over and over again is cheap now, since the resut is cached
        for text in [t for (t, r) in objetcs if r.collidepoint(pygame.mouse.get_pos())]:
            print "'{}' was clicked".format(text)

    # draw the screen ONCE
    pygame.display.flip()
    if pygame.event.get(pygame.QUIT): run = False
    # clear all unwanted events
    pygame.event.clear()
    clock.tick(60)

+1用于使用time.sleep()等待的时间比最小的时间间隔短1。有什么问题?2.您只想调用一个display.flip()。3.您应该使用MOUSEMOTION事件来检测鼠标悬停。4.你从不清除屏幕。你如何清除屏幕?我在很多地方都找过,但我一直没找到答案。你如何在一个(或一组)物体上检测情绪?