I'm trying to create a simple simulated battle game themed in football(a.k.a soccer), and right now I'm creating a "market" phase in which the game player can buy athletes with the gold that he has.
I created buttons of the athletes and displayed them on the screen.
When the player clicks the buttons, the player's money should be updated so that the athlete's price is deducted from the player's money.
The problem is that if i use pygame.mouse.get_pressed()[0]
as the click variable, it loops until the mouse click is released, so it keeps deducting the player's money and deducts way much more than it should.
I also tried using a variable clicked
linked with if event.type==pygame.MOUSEBUTTONDOWN:
so that clicked
becomes true when MOUSEBUTTONDOWN
but then the buttons won't detect the click.
I pasted the relevant parts from my code below. I'm a beginner in Python, so any other advice would be appreciated as well!
clicked=False
def button(msg,x,y,w,h,iCol,aCol,athlete):
mouse = pygame.mouse.get_pos()
#click = pygame.mouse.get_pressed()
if x < mouse[0] < x+w and y < mouse[1] < y+h:
pygame.draw.rect(window, aCol, (x,y,w,h))
#if click[0]:
if clicked:
print('button clicked') ##--------THIS PART NOT WORKING!!
for p in players:
if p.turn==True:
p.buy_athlete(athlete)
#else:
#print('not clicked')
else:
pygame.draw.rect(window, iCol, (x,y,w,h))
#MAIN LOOP
def main_loop():
isRunning=True
while isRunning:
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
if event.type==pygame.MOUSEBUTTONDOWN:
clicked=True
print(clicked)
for idx,item in enumerate(athletes):
row = idx//7
col = idx-row*7
item.bpos_x=10+110*col
item.bpos_y=10+60*row
display_stats(item)
button('['+item.pos+']'+item.name, item.bpos_x, item.bpos_y , 100, 50, Color.Blue, Color.LightBlue,item)
display_player_roster()
pygame.display.update()
pygame.quit()
sys.exit()