0

i'm trying to build a battleship table game using pygame.
i would like to detect the events pygame.MOUSEBUTTONDOWN and and match it with the following pygame.MOUSEBUTTONUP. i'm trying to do this because i wanted to pass the coordinates at which blitting the Ship surface on the screen.

def main():    
    #click=True
    run=True
    clock=pygame.time.Clock()
    while run:
        clock.tick(fps)
        #print(pygame.mouse.get_pos())
        for event in pygame.event.get():
            l=[]
            if event.type == pygame.QUIT:
               pygame.quit()
               return
            if (event.type==pygame.MOUSEBUTTONDOWN):
                pos_start=pygame.mouse.get_pos()
                x_start=pos_start[0]
                y_start=pos_start[1]
                cell_coord_start=b.get_coord(x_start, y_start)#b is a pygame.display  
                l.append(cell_coord_start)
                x_end=0
                y_end=0
                #click=True                
                print("start", l)
                for eve in pygame.event.get():                
                        while eve.type!=pygame.MOUSEBUTTONUP:
                            print("loop")
                            if (event.type==pygame.MOUSEBUTTONUP):                    
                                pos_end=pygame.mouse.get_pos()
                                x_end+=pos_end[0]
                                y_end+=pos_end[1]
                                print(x_end)
                                print(y_end)
                                #click=False
                cell_coord_end=b.get_coord(x_end, y_end)
                l.append(cell_coord_end)
                #print("start, end", l)
                #print(cell_coord_end)

the output is okay until the second event loop, where it does not seem to enter the while loop at all. what i would like to do, at least in my mind is: "after having found a buttondown event, go ahead until you find a mousebuttonup event, save its position etc"

probably the problem is that i am trying to nest event loops, but i would not know how to do this without the double loop. i suppose it would be more convenient to do all that inside a function, but the logic behind should be the same i guess so the problem would persist anyway.

thank you

  • 1
    `eve.type` will evet change. You just get a new event when you call `pygame.event.get()`. Anyway, never create nested event loops. You have an application loop that runs continuously. Use it! – Rabbid76 Oct 27 '21 at 17:13
  • yeah i imagined that it wasnt the best solution, indeed i'm trying to solve it by checking the condition `event.type==pygame.MOUSEBUTTONUP` right after, i'll find a way, thanks for the hint. the main problem is that i'll have to compare those values outside the if statements – Lorenzo Consoli Oct 27 '21 at 17:19

0 Answers0