Fred B
Fred B

Reputation: 99

Pygame seems to skip an if statement on whether or not I click again

In making my chess game function for moving pieces, I've created a system that should:

  1. Look for a click
  2. See if a piece was clicked on
  3. Determine what piece was clicked on
  4. Click on a square to move to
  5. Move said piece to said square

I've written the follow code for this:

        if event.type == pygame.MOUSEBUTTONDOWN and moving == False:
            mousepos = pygame.mouse.get_pos()
            roundedmouse1 = rounddown80(mousepos[0]) #function to found out what square was clicked
            roundedmouse2 = rounddown80(mousepos[1]) #function to found out what square was clicked
            mousecoords = [roundedmouse1,roundedmouse2]
            for key,value in piecepositionsdict.items():
                if int(value[0]) == int(mousecoords[0]) and int(value[1]) == int(mousecoords[1]):
                    x = coordstosquaredict[str(mousecoords)]
                    print("You have clicked",whatpiece(x),"on square",x)
                    print("Click a square to move the piece to:")
                    moving = True
                    time.sleep(0.5)
                    #this should be where the program stops until it gets another click
                    if event.type == pygame.MOUSEBUTTONDOWN and moving == True:
                        mousepos2 = pygame.mouse.get_pos()
                        roundedmouse21 = rounddown80(mousepos2[0])
                        roundedmouse22 = rounddown80(mousepos2[1])
                        mousecoords2 = [roundedmouse21, roundedmouse22]
                        print(mousecoords2)
                        print(piecepositionsdict[whatpiece(x)+"pos"])
                        piecepositionsdict[whatpiece(x)+"pos"] = mousecoords2
                        print(piecepositionsdict[whatpiece(x) + "pos"])

However, the program goes right past the second click check, as shown by how it prints out mousecoords 2 etc. when I have only clicked once.

What have I done wrong to cause this error?

Upvotes: 1

Views: 156

Answers (2)

Jesper Svensson
Jesper Svensson

Reputation: 38

Your code will run in a single frame and the code will only register one click during that frame. You need to have a function thats gets called everytime you click on the screen.

See my sort of example bellow:

#Holder for the piece you have selected
currentPiece = piece

##Mouse click event/function##
if currentPiece == None:
    getPiece()
else:
    movePiece()

##Get piece function##
def getPiece():
    #Your function here which sets
    #currentPiece to what you selected.

##Move function##
    def movePiece():
    #Moves the currentPieece to the selected spot

Upvotes: -1

Valentino
Valentino

Reputation: 7361

Games are event driven applications. In event driven application you have a main loop checking for all events, and code executed when an event is detected. The starting point of your logic is always the event.

In your case the event is the clicking of the button. You have to check for the click only once in your code (in the main loop) and then determine the action the code has to do. If different actions may be triggered by the same event, you need some additional logic or flags to determine which action should be executed.

In your case you have an event (mouse click) and two possible actions, check what pieces has been clicked, or move the piece.

So the code should be designed like this:

def check_and_grab(position):
    # code here

def move_piece(position):
    # code here

piece_grabbed = None

while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTODOWN:
            if piece_grabbed is None:
                check_and_grab(event.pos)
            else:
                move_piece(event.pos)

piece_grabbed is a variable storing the piece grabbed: if is None, no piece has been grabbed.

check_and_grab(position) should check if there is a piece at the clicked position and, if so, set piece_grabbed to that piece. It implements your points 2 and 3.

move_piece(position) should move the grabbed piece at the new position and after that, set again grabbed_piece = None. It implements your point 5.

Upvotes: 2

Related Questions