0

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()    
  • Looking at your code I can see that it checks to see whether p.turn is true (through `if p.turn==True:`) where is this function that is returning this? Furthermore can you confirm that when you click anywhere on screen it prints `True` – Ben10 Apr 19 '18 at 03:38
  • @Ben10 Yes it does print `True` when I click anywhere on the screen. It's just the part mentioned above in comments that's not working. In the game, the two players will take turns buying one player per turn. `if p.turn==True:` is used to check if it's either Player 1 or Player 2's turn to buy a player. – Jimmy Yook Apr 19 '18 at 04:12
  • Where it says `pygame.draw.rect(window, aCol, (x,y,w,h))` what is the point of this? Is it some sort of an animation for the button – Ben10 Apr 19 '18 at 04:15
  • Actually I want you to add this line to the top of the code. `global clicked` – Ben10 Apr 19 '18 at 04:16
  • @Ben10 will try. iCol and aCol are the colors for when the button is active(hovered) and non-active(not hovered). I should have added comments on that – Jimmy Yook Apr 19 '18 at 04:56
  • Please post a [minimal, complete and verfiable example](https://stackoverflow.com/help/mcve) that we can copy, run and test. – skrx Apr 19 '18 at 12:49

0 Answers0