2017-12-31 42 views
1

파이 게임에서 증분/크리커 게임을 디자인 중이고 클릭당 1 회씩 카운터를 변경하고 싶습니다. 그러나, 나는 단추를 누르고, 이것을 제한하는 방법을 찾을 수 없다.파이 게임 마우스 홀드

내 버튼 클래스는 다음과 같습니다 : 아래로 개최되는 버튼을 막을 방법은

class Button(object): 
    def __init__(self,x,y,width,height,color): 
     self.rect=(x,y,width,height) 
     self.image=pygame.draw.rect(screen, color,(self.rect),) 
     self.x=x 
     self.y=y 
     self.width=width 
     self.height=height 
    def check(self): 
     mouse=pygame.mouse.get_pos() 
     if self.x+self.width >mouse[0] > self.x and self.y+self.height >mouse[1] > self.y: 
      if pygame.mouse.get_pressed()[0]==True: 
       return True 

있습니까?

모든 도움을 주실 수 있습니다!

전체 코드는 : 당신이 이벤트 루프에 button.check() 전화를 이동하는 경우

import pygame, sys 
from pygame.locals import * 

pygame.init() 

light_gray=(211,211,211) 
black=(0,0,0) 
white=(255,255,255) 

def terminate(): 
    pygame.quit() 
    sys.exit() 

def drawText(text, font, screen, x, y,color): 
    textobj = font.render(text,True, color) 
    textrect = textobj.get_rect(center=(x,y)) 
    screen.blit(textobj, textrect) 

class Button(object): 
    def __init__(self,x,y,width,height,color): 
     self.rect=(x,y,width,height) 
     self.image=pygame.draw.rect(screen, color,(self.rect),) 
     self.x=x 
     self.y=y 
     self.width=width 
     self.height=height 
    def check(self): 
     mouse=pygame.mouse.get_pos() 
     if self.x+self.width >mouse[0] > self.x and self.y+self.height >mouse[1] > self.y: 
      if pygame.mouse.get_pressed()[0]==True: 
       return True 

clock=pygame.time.Clock() 

font=pygame.font.SysFont(None,50) 

screen_width=1300 
screen_height=700 
screen=pygame.display.set_mode([screen_width,screen_height]) 

pygame.display.set_caption('Clicker') 

done=False 

money=0 

sprites=pygame.sprite.Group() 

button=Button(25,screen_height-125,500,100,light_gray) 

while not done: 
    for event in pygame.event.get(): 
      if event.type==QUIT: 
       terminate() 

    if button.check()==True: 
     money+=1 

    screen.fill(light_gray) 

    sprites.draw(screen) 

    pygame.draw.rect(screen,black,button.rect, 3) 
    text_width, text_height=font.size('Click!') 
    drawText('Click!', font,screen,button.x+button.width/2,button.y+button.height/2,black) 

    drawText('$'+str(money),font,screen,screen_width/2,25,black) 

    pygame.display.flip() 

    clock.tick(15) 

pygame.quit() 

답변

2

, 그것은 단지 한 번 마우스 클릭 대신 매 프레임마다 호출됩니다.

while not done: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      terminate() 
     elif event.type == pygame.MOUSEBUTTONDOWN: 
      if button.check(): 
       money += 1 

또한 마우스 버튼과 충돌 있는지 확인하기 위해이 pygame.RectButton 클래스에서, 당신은 단순히 pygame.Rect.collidepoint 메서드를 호출 할 수 있습니다 사용하는 것이 좋습니다. 당신이 버튼을 사용하려면 마우스 버튼을 누르고있는 경우는 단 한 번 클릭 한하지 않을 경우

class Button(object): 
    def __init__(self,x,y,width,height,color): 
     self.rect = pygame.Rect(x,y,width,height) 
     self.image=pygame.draw.rect(screen, color,(self.rect),) 
     self.x=x 
     self.y=y 
     self.width=width 
     self.height=height 

    def check(self): 
     return self.rect.collidepoint(pygame.mouse.get_pos()) 

pygame.mouse.get_pressed()

가 있기 때문에 단지 확인, 문제가 될 수 있습니다. 에서

+0

이것은 매우 유용합니다! 감사! – Coder22

-1

당신이 돈을 추가하고 마우스를 클릭하지 될 때까지 기다려야한다 button.check() if 문 :

if button.check()==True: 
    money+=1 
    while pygame.mouse.get_pressed()[0]==True: 
     clock.tick(10) 

그 권리인가?

+0

아쉽게도 작동하지 않습니다. – Coder22

+1

PyGame (또는 GUI)에서 while 루프는 좋은 생각이 아닙니다. 그것은 프로그램의 다른 중요한 요소들을 멈추게합니다. 그래서 프로그램이 멈출 수 있습니다. 게다가 시스템에서 이벤트를 얻지 못하면'get_pressed() '로 얻은 데이터를 업데이트하지 않을 수 있습니다. – furas

+0

어떻게 프로그램으로 구현할 수 있습니까? – Coder22