如果我的光标在框内时被按下而不是被按下然后被拖入,我如何 return 为真?
How can I return true if my cursor is pressed while in a box and not pressed and then dragged in?
我在 pygame 中有一个框,如果在光标位于框内时按下鼠标按钮,我想将一些文本打印到控制台。问题是如果按下鼠标按钮然后将其拖入框中,我不想打印文本。
我试过:
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100 and pygame.mouse.get_pressed()[0]:
scrollButtonColour = (25,25,25)
print(mousePos)
else:
scrollButtonColour = (50,50,50)
干杯。
编辑:
这是完整的测试代码(自发布问题后我做了一些小改动):
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Scroll Bar")
clock = pygame.time.Clock()
FPS = 60
gameExit = False
scrollButtonColour = (50,50,50)
while not gameExit:
mousePos = pygame.mouse.get_pos()
scrollButtonColour = (50,50,50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100:
if pygame.mouse.get_pressed()[0]:
scrollButtonColour = (25,25,25)
print(mousePos)
gameDisplay.fill(green)
pygame.draw.rect(gameDisplay, red, (200,200,100,100))
pygame.draw.rect(gameDisplay, scrollButtonColour, (500, 0, 30, 100))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
确保仅当玩家在
框并且在玩家将鼠标拖入对象后不释放鼠标
你可以使用 pygame.MOUSEBUTTONDOWN
。
这样你就可以检查鼠标是否真的被按下了。如果你在之后这样做
您已检查鼠标位置是否在对象内部,将鼠标拖入
不会打印对象,只会在对象内部单击。
代码如下所示:
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100:
if event.type == pygame.MOUSEBUTTONDOWN :
scrollButtonColour = (25,25,25)
print(mousePos)
要使此代码正常工作,您必须将此代码放入检查事件的循环中
我会先检查鼠标按钮是否被按下,然后如果按钮 rect 与鼠标 pos 发生碰撞,则更改按钮的颜色并打印文本。释放按钮后,您可以重置颜色。我有一个示例,它还向您展示了如何使用 pygame.Rect 及其碰撞点方法。由于鼠标事件有一个 pos
属性,您可以使用它而不是调用 pygame.mouse.get_pos()
。
import sys
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
# Rect(x_pos, y_pos, width, height)
scrollButton = pygame.Rect(500, 0, 30, 100)
scrollButtonColour = (50, 50, 50)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.MOUSEBUTTONDOWN:
if scrollButton.collidepoint(event.pos):
scrollButtonColour = (25, 25, 25)
print(event.pos)
if event.type == pygame.MOUSEBUTTONUP:
scrollButtonColour = (50, 50, 50)
gameDisplay.fill((0, 255, 0))
pygame.draw.rect(gameDisplay, scrollButtonColour, scrollButton)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()
如果你想制作更多的按钮或其他GUI元素,最好为它们定义类。将实例放入列表或精灵组中,并在事件循环中将事件传递给实例以处理它们。
我在 pygame 中有一个框,如果在光标位于框内时按下鼠标按钮,我想将一些文本打印到控制台。问题是如果按下鼠标按钮然后将其拖入框中,我不想打印文本。
我试过:
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100 and pygame.mouse.get_pressed()[0]:
scrollButtonColour = (25,25,25)
print(mousePos)
else:
scrollButtonColour = (50,50,50)
干杯。
编辑:
这是完整的测试代码(自发布问题后我做了一些小改动):
import pygame
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Scroll Bar")
clock = pygame.time.Clock()
FPS = 60
gameExit = False
scrollButtonColour = (50,50,50)
while not gameExit:
mousePos = pygame.mouse.get_pos()
scrollButtonColour = (50,50,50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100:
if pygame.mouse.get_pressed()[0]:
scrollButtonColour = (25,25,25)
print(mousePos)
gameDisplay.fill(green)
pygame.draw.rect(gameDisplay, red, (200,200,100,100))
pygame.draw.rect(gameDisplay, scrollButtonColour, (500, 0, 30, 100))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
确保仅当玩家在
框并且在玩家将鼠标拖入对象后不释放鼠标
你可以使用 pygame.MOUSEBUTTONDOWN
。
这样你就可以检查鼠标是否真的被按下了。如果你在之后这样做 您已检查鼠标位置是否在对象内部,将鼠标拖入 不会打印对象,只会在对象内部单击。
代码如下所示:
if mousePos[0] >= 500 and mousePos[0] <= 530 and mousePos[1] >= 0 and mousePos[1] <= 100:
if event.type == pygame.MOUSEBUTTONDOWN :
scrollButtonColour = (25,25,25)
print(mousePos)
要使此代码正常工作,您必须将此代码放入检查事件的循环中
我会先检查鼠标按钮是否被按下,然后如果按钮 rect 与鼠标 pos 发生碰撞,则更改按钮的颜色并打印文本。释放按钮后,您可以重置颜色。我有一个示例,它还向您展示了如何使用 pygame.Rect 及其碰撞点方法。由于鼠标事件有一个 pos
属性,您可以使用它而不是调用 pygame.mouse.get_pos()
。
import sys
import pygame
pygame.init()
gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()
# Rect(x_pos, y_pos, width, height)
scrollButton = pygame.Rect(500, 0, 30, 100)
scrollButtonColour = (50, 50, 50)
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.MOUSEBUTTONDOWN:
if scrollButton.collidepoint(event.pos):
scrollButtonColour = (25, 25, 25)
print(event.pos)
if event.type == pygame.MOUSEBUTTONUP:
scrollButtonColour = (50, 50, 50)
gameDisplay.fill((0, 255, 0))
pygame.draw.rect(gameDisplay, scrollButtonColour, scrollButton)
pygame.display.update()
clock.tick(60)
pygame.quit()
sys.exit()
如果你想制作更多的按钮或其他GUI元素,最好为它们定义类。将实例放入列表或精灵组中,并在事件循环中将事件传递给实例以处理它们。