clickCounter
마우스 버튼을 클릭하고 (버튼을 클릭 할 때) 누르고 있으면 증가 하는 변수 가 있습니다. 버튼을 누르고있을 때만 값이 증가하지 않도록하려면 어떻게해야합니까?
버튼 기능 (클래스로 변환하는 것이 더 쉬울까요?) :
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf", 20) # constant has been declared, try deleting line when done w/ proj
textSurf, textRect = textBox(message, smallText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
메인 루프
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
gameDisplay.fill(white)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
textObject(str(clickCounter), black, mediumText, 200, 200)
pygame.display.update()
clock.tick(20)
모든 코드 :
import pygame
pygame.init()
displayWidth = 700
displayHeight = displayWidth
gameDisplay = pygame.display.set_mode((700,700))
clock = pygame.time.Clock()
black = (0, 0, 0)
brightBlue = (0, 0, 225)
blue = (0, 0, 255)
white = (255, 255, 255) #predef colors to make temp rgb value coding easier
closeGame = False
mediumText = pygame.font.Font("freesansbold.ttf", 70) #initalize font
clickCounter = 0
#def fontSize (pxsize):
#pygame.font.Font("freesandsbold.ttf", pxsize)
def textObject (text, color, font, x, y):
storedGenerate = font.render(text, 1, ((color)))
gameDisplay.blit(storedGenerate, (x,y))
def textBox(text, font): #purely for redturning rectangle around font// used for btn function only, tObject for displaying text
textSurface = font.render(text, True, black) #swap black for rgb later
return textSurface, textSurface.get_rect()
def button(message, x, y, w, h, activeRGB, inactiveRGB, action=None): #example of color param for line 61/63
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
click = pygame.mouse.get_pressed()
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if click[0] ==1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf", 20) # constant has been declared, try deleting line when done w/ proj
textSurf, textRect = textBox(message, smallText)
textRect.center = ( (x + (w/2)), y+(h/2))
gameDisplay.blit(textSurf, textRect)
closeGame = False
while not closeGame: # this keeps the window from shutting down
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
print(thing)
gameDisplay.fill(white)
button("Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
textObject(str(clickCounter), black, mediumText, 200, 200)
pygame.display.update()
clock.tick(20)
gameDisplay.fill(white)
pygame.display.update()
#logicloop()
pygame.quit()
quit()
내 초기 생각은 사용하지 pygame.mouse.get_pressed()
않고 대신 마우스 버튼을 사용하는 것이었지만 제대로 작동하지 못했습니다. 도움을 주시면 감사하겠습니다.
MOUSEBUTTONDOWN
대신 이벤트를 평가하십시오 pygame.mouse.get_pressed
. 이벤트는 마우스 버튼을 누를 때 한 번 발생합니다.
누른 마우스 버튼을 변수에 저장합니다 mousebutton
.
while not closeGame: # this keeps the window from shutting down
mousebutton = 0
for thing in pygame.event.get():
if thing.type == pygame.QUIT:
closeGame = True
elif thing.type == pygame.MOUSEBUTTONDOWN:
mousebutton = thing.button
변수를 함수에 전달하십시오 button
.
button(mousebutton, "Click me!", 300, 300, 100, 100, blue, brightBlue, "countUP")
기능에서 왼쪽 버튼을 눌렀는지 평가하십시오.
def button(mousebutton, message, x, y, w, h, activeRGB, inactiveRGB, action=None):
mouse = pygame.mouse.get_pos() #get location of mouse recorded by pygame
global clickCounter
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, activeRGB, (x, y, w, h))
if mousebutton == 1 and action != None:
if action == "countUP":
clickCounter+= 1
print(str(clickCounter))
pygame.display.update()
else:
pygame.draw.rect(gameDisplay, inactiveRGB, (x, y, w, h))
# [...]
이벤트 의 .button
속성은 MOUSEBUTTONDOWN
왼쪽 버튼에 대해 1, 중간 버튼에 대해 2, 마우스 휠이 롤업되면 각각 4, 마우스 휠이 롤 다운되면 5를 반환합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다