2017-12-31 199 views
1

私はpygameでインクリメンタル/クリッカーゲームを設計しており、クリックごとに$ counterを1ずつ変更したいと思います。しかし、私はボタンを押したままにすることができ、私はこれを制限する方法を見つけることができません。Pygame mouse hold

マイボタンクラスは以下の通りです:押されてからボタンを停止する方法は

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 

ありますか?

すべてのご協力ありがとうございます。

全コード:

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

あなたがイベントループにbutton.check()呼び出しを移動した場合、それは一度だけのマウスクリックの代わりに、すべてのフレーム毎に呼び出されます。

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()が問題となる可能性があります。あなたのbutton.check()

+0

これは非常に役に立ちます!ありがとう! – Coder22

-1

それはお金に1を追加して、マウスがクリックされなくなるまで待つべき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