Ich entwerfe ein inkrementelles/Clicker-Spiel in Pygame, und ich möchte den $ -Zähler bei jedem Klick um 1 ändern. Allerdings kann ich den Knopf gedrückt halten, und ich kann keinen Weg finden, dies zu begrenzen.Pygame Maus halten
ist My Button-Klasse unter:
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
Gibt es eine Möglichkeit, die Schaltfläche zu stoppen gedrückt gehalten?
Alle Hilfe ist willkommen!
Voll Code:
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()
Dies ist sehr hilfreich! Vielen Dank! – Coder22