私は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()
これは非常に役に立ちます!ありがとう! – Coder22