如何让pygame按钮只点击一次注册?
how to get pygame button to register only one click?
我正在 pygame 中制作一个带有可按下按钮的游戏,但我希望它在单击时只做一件事。只要您按住它,下面的代码就会打印 "button pressed" 。更改此代码以使其每次点击仅打印一次的优雅方法是什么?
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((640, 480),0,32)
clock = pygame.time.Clock()
def makeButton(x,y,width,height):
if x + width > cur[0] > x and y + height > cur[1] > y:
if click == (1,0,0):
print "button pressed"
square = pygame.Rect((0,0), (32,32))
while True:
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
makeButton(square.left,square.top,square.width,square.height)
screen.fill((255,255,255))
screen.fill((55,155,0), square)
pygame.display.update()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
我不只是拥有一个方法,而是制作一个按钮 class 并为您需要的每个按钮实例化它。您还尝试创建自己的鼠标事件处理程序,每次游戏循环运行时都会运行,但在 pygame 中最好只在鼠标事件发生时将鼠标信息传递给此类按钮对象。只要有点击,MOUSEBUTTONDOWN 和 MOUSEBUTTONUP 事件类型就会被传递到事件队列。要创建您描述的行为,您可以让按钮操作仅在鼠标位于 MOUSEBUTTONUP 事件的按钮范围内时执行。 This page provides a good intro into pygame input handling and this 页面提供了关于在 pygame 中创建通用按钮 class 的详细(虽然有些高级)讨论。
一种简单且更有效的方法来执行您想要的操作,即显式检查 pygame.MOUSEBUTTONDOWN
事件并仅在必要时进行鼠标事件处理。您还可以通过使用知道如何进行碰撞检测的 pygame 的 Rect
class 来简化 makeButton()
中的逻辑。
我的意思是:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((640, 480),0,32)
clock = pygame.time.Clock()
def makeButton(cur, rect):
if rect.collidepoint(cur):
print "button pressed"
square = pygame.Rect((0,0), (32,32))
while True:
screen.fill((255,255,255))
screen.fill((55,155,0), square)
pygame.display.update()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left mouse button?
makeButton(event.pos, square)
我不知道这是否有帮助,但这对我有用。
我从这样的代码开始:
..........ygame.mouse.get_pressed()
if x+ button_width > mouse[0] > x and y + button_height > mouse[1] > y:
pygame.draw.rect(screen, ac, ([x, y],[button_width, button_height])
if click[0] == 1:
if bn == 0:
button_0()
if bn == 1:
button_1()
if bn == 2:
button_2()
if bn == 3:
button_3()
if bn == 4:
button_4()
if bn == 5:
button_5()
添加一个全局变量 is_clicked = false with the other
程序开头的变量,全局语句
在函数中,以及 2 个 if 语句(6 行代码)。
改为:
# (msg, (x, y coordinates),width,height,inactive color, active color,
# button number) all the info to place and label the buttons.
def 按钮(msg, x, y, w, h, ic, ac, bn):
全球 is_clicked
鼠标 = pygame.mouse.get_pos()
点击 = pygame.mouse.get_pressed()
如果 x+ button_width > 鼠标[0] > x 和 y+ button_height > 鼠标[1] > y:
pygame.draw.rect(屏幕, 交流, ([x, y],[button_width, button_height]))
if click[0] == 1 and is_clicked == False:
is_clicked = True
if bn == 0:
button_0()
if bn == 1:
button_1()
if bn == 2:
button_2()
if bn == 3:
button_3()
if bn == 4:
button_4()
if bn == 5:
button_5()
if click[0] == 0 and is_clicked == True:
is_clicked = False # set is_clicked back to False after mouse
# button up.
我正在 pygame 中制作一个带有可按下按钮的游戏,但我希望它在单击时只做一件事。只要您按住它,下面的代码就会打印 "button pressed" 。更改此代码以使其每次点击仅打印一次的优雅方法是什么?
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((640, 480),0,32)
clock = pygame.time.Clock()
def makeButton(x,y,width,height):
if x + width > cur[0] > x and y + height > cur[1] > y:
if click == (1,0,0):
print "button pressed"
square = pygame.Rect((0,0), (32,32))
while True:
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
makeButton(square.left,square.top,square.width,square.height)
screen.fill((255,255,255))
screen.fill((55,155,0), square)
pygame.display.update()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
我不只是拥有一个方法,而是制作一个按钮 class 并为您需要的每个按钮实例化它。您还尝试创建自己的鼠标事件处理程序,每次游戏循环运行时都会运行,但在 pygame 中最好只在鼠标事件发生时将鼠标信息传递给此类按钮对象。只要有点击,MOUSEBUTTONDOWN 和 MOUSEBUTTONUP 事件类型就会被传递到事件队列。要创建您描述的行为,您可以让按钮操作仅在鼠标位于 MOUSEBUTTONUP 事件的按钮范围内时执行。 This page provides a good intro into pygame input handling and this 页面提供了关于在 pygame 中创建通用按钮 class 的详细(虽然有些高级)讨论。
一种简单且更有效的方法来执行您想要的操作,即显式检查 pygame.MOUSEBUTTONDOWN
事件并仅在必要时进行鼠标事件处理。您还可以通过使用知道如何进行碰撞检测的 pygame 的 Rect
class 来简化 makeButton()
中的逻辑。
我的意思是:
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((640, 480),0,32)
clock = pygame.time.Clock()
def makeButton(cur, rect):
if rect.collidepoint(cur):
print "button pressed"
square = pygame.Rect((0,0), (32,32))
while True:
screen.fill((255,255,255))
screen.fill((55,155,0), square)
pygame.display.update()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: # left mouse button?
makeButton(event.pos, square)
我不知道这是否有帮助,但这对我有用。 我从这样的代码开始:
..........ygame.mouse.get_pressed()
if x+ button_width > mouse[0] > x and y + button_height > mouse[1] > y:
pygame.draw.rect(screen, ac, ([x, y],[button_width, button_height])
if click[0] == 1:
if bn == 0:
button_0()
if bn == 1:
button_1()
if bn == 2:
button_2()
if bn == 3:
button_3()
if bn == 4:
button_4()
if bn == 5:
button_5()
添加一个全局变量 is_clicked = false with the other 程序开头的变量,全局语句 在函数中,以及 2 个 if 语句(6 行代码)。 改为:
# (msg, (x, y coordinates),width,height,inactive color, active color,
# button number) all the info to place and label the buttons.
def 按钮(msg, x, y, w, h, ic, ac, bn): 全球 is_clicked 鼠标 = pygame.mouse.get_pos() 点击 = pygame.mouse.get_pressed() 如果 x+ button_width > 鼠标[0] > x 和 y+ button_height > 鼠标[1] > y: pygame.draw.rect(屏幕, 交流, ([x, y],[button_width, button_height]))
if click[0] == 1 and is_clicked == False:
is_clicked = True
if bn == 0:
button_0()
if bn == 1:
button_1()
if bn == 2:
button_2()
if bn == 3:
button_3()
if bn == 4:
button_4()
if bn == 5:
button_5()
if click[0] == 0 and is_clicked == True:
is_clicked = False # set is_clicked back to False after mouse
# button up.