Python, pygame mouse position and which button is pressed











up vote
1
down vote

favorite












I have been trying to get my code collecting which mouse button is pressed and its position yet whenever I run the below code the pygame window freezes and the shell/code keeps outputting the starting position of the mouse. Does anybody know why this happens and more importantly how to fix it?
(For the code below I used this website https://www.pygame.org/docs/ref/mouse.html and other stack overflow answers yet they were not specific enough for my problem.)



clock = pygame.time.Clock()
# Set the height and width of the screen
screen = pygame.display.set_mode([700,400])

pygame.display.set_caption("Operation Crustacean")


while True:
clock.tick(1)
screen.fill(background_colour)

click=pygame.mouse.get_pressed()
mousex,mousey=pygame.mouse.get_pos()

print(click)
print(mousex,mousey)
pygame.display.flip()









share|improve this question









New contributor




J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    I have been trying to get my code collecting which mouse button is pressed and its position yet whenever I run the below code the pygame window freezes and the shell/code keeps outputting the starting position of the mouse. Does anybody know why this happens and more importantly how to fix it?
    (For the code below I used this website https://www.pygame.org/docs/ref/mouse.html and other stack overflow answers yet they were not specific enough for my problem.)



    clock = pygame.time.Clock()
    # Set the height and width of the screen
    screen = pygame.display.set_mode([700,400])

    pygame.display.set_caption("Operation Crustacean")


    while True:
    clock.tick(1)
    screen.fill(background_colour)

    click=pygame.mouse.get_pressed()
    mousex,mousey=pygame.mouse.get_pos()

    print(click)
    print(mousex,mousey)
    pygame.display.flip()









    share|improve this question









    New contributor




    J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have been trying to get my code collecting which mouse button is pressed and its position yet whenever I run the below code the pygame window freezes and the shell/code keeps outputting the starting position of the mouse. Does anybody know why this happens and more importantly how to fix it?
      (For the code below I used this website https://www.pygame.org/docs/ref/mouse.html and other stack overflow answers yet they were not specific enough for my problem.)



      clock = pygame.time.Clock()
      # Set the height and width of the screen
      screen = pygame.display.set_mode([700,400])

      pygame.display.set_caption("Operation Crustacean")


      while True:
      clock.tick(1)
      screen.fill(background_colour)

      click=pygame.mouse.get_pressed()
      mousex,mousey=pygame.mouse.get_pos()

      print(click)
      print(mousex,mousey)
      pygame.display.flip()









      share|improve this question









      New contributor




      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      I have been trying to get my code collecting which mouse button is pressed and its position yet whenever I run the below code the pygame window freezes and the shell/code keeps outputting the starting position of the mouse. Does anybody know why this happens and more importantly how to fix it?
      (For the code below I used this website https://www.pygame.org/docs/ref/mouse.html and other stack overflow answers yet they were not specific enough for my problem.)



      clock = pygame.time.Clock()
      # Set the height and width of the screen
      screen = pygame.display.set_mode([700,400])

      pygame.display.set_caption("Operation Crustacean")


      while True:
      clock.tick(1)
      screen.fill(background_colour)

      click=pygame.mouse.get_pressed()
      mousex,mousey=pygame.mouse.get_pos()

      print(click)
      print(mousex,mousey)
      pygame.display.flip()






      python pygame






      share|improve this question









      New contributor




      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday





















      New contributor




      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      J.Garry

      83




      83




      New contributor




      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      J.Garry is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You have to call one of the pygame.event functions regularly (for example pygame.event.pump or for event in pygame.event.get():), otherwise pygame.mouse.get_pressed (and some joystick functions) won't work correctly and the pygame window will become unresponsive after a while.



          Here's a runnable example:



          import pygame


          pygame.init()
          screen = pygame.display.set_mode((640, 480))
          clock = pygame.time.Clock()
          BG_COLOR = pygame.Color('gray12')

          done = False
          while not done:
          # This event loop empties the event queue each frame.
          for event in pygame.event.get():
          # Quit by pressing the X button of the window.
          if event.type == pygame.QUIT:
          done = True
          elif event.type == pygame.MOUSEBUTTONDOWN:
          # MOUSEBUTTONDOWN events have a pos and a button attribute
          # which you can use as well. This will be printed once per
          # event / mouse click.
          print('In the event loop:', event.pos, event.button)

          # Instead of the event loop above you could also call pygame.event.pump
          # each frame to prevent the window from freezing. Comment it out to check it.
          # pygame.event.pump()

          click = pygame.mouse.get_pressed()
          mousex, mousey = pygame.mouse.get_pos()
          print(click, mousex, mousey)

          screen.fill(BG_COLOR)
          pygame.display.flip()
          clock.tick(60) # Limit the frame rate to 60 FPS.





          share|improve this answer























          • Would any pygame.event work?
            – J.Garry
            2 days ago






          • 1




            I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
            – skrx
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          J.Garry is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238491%2fpython-pygame-mouse-position-and-which-button-is-pressed%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          You have to call one of the pygame.event functions regularly (for example pygame.event.pump or for event in pygame.event.get():), otherwise pygame.mouse.get_pressed (and some joystick functions) won't work correctly and the pygame window will become unresponsive after a while.



          Here's a runnable example:



          import pygame


          pygame.init()
          screen = pygame.display.set_mode((640, 480))
          clock = pygame.time.Clock()
          BG_COLOR = pygame.Color('gray12')

          done = False
          while not done:
          # This event loop empties the event queue each frame.
          for event in pygame.event.get():
          # Quit by pressing the X button of the window.
          if event.type == pygame.QUIT:
          done = True
          elif event.type == pygame.MOUSEBUTTONDOWN:
          # MOUSEBUTTONDOWN events have a pos and a button attribute
          # which you can use as well. This will be printed once per
          # event / mouse click.
          print('In the event loop:', event.pos, event.button)

          # Instead of the event loop above you could also call pygame.event.pump
          # each frame to prevent the window from freezing. Comment it out to check it.
          # pygame.event.pump()

          click = pygame.mouse.get_pressed()
          mousex, mousey = pygame.mouse.get_pos()
          print(click, mousex, mousey)

          screen.fill(BG_COLOR)
          pygame.display.flip()
          clock.tick(60) # Limit the frame rate to 60 FPS.





          share|improve this answer























          • Would any pygame.event work?
            – J.Garry
            2 days ago






          • 1




            I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
            – skrx
            2 days ago















          up vote
          1
          down vote



          accepted










          You have to call one of the pygame.event functions regularly (for example pygame.event.pump or for event in pygame.event.get():), otherwise pygame.mouse.get_pressed (and some joystick functions) won't work correctly and the pygame window will become unresponsive after a while.



          Here's a runnable example:



          import pygame


          pygame.init()
          screen = pygame.display.set_mode((640, 480))
          clock = pygame.time.Clock()
          BG_COLOR = pygame.Color('gray12')

          done = False
          while not done:
          # This event loop empties the event queue each frame.
          for event in pygame.event.get():
          # Quit by pressing the X button of the window.
          if event.type == pygame.QUIT:
          done = True
          elif event.type == pygame.MOUSEBUTTONDOWN:
          # MOUSEBUTTONDOWN events have a pos and a button attribute
          # which you can use as well. This will be printed once per
          # event / mouse click.
          print('In the event loop:', event.pos, event.button)

          # Instead of the event loop above you could also call pygame.event.pump
          # each frame to prevent the window from freezing. Comment it out to check it.
          # pygame.event.pump()

          click = pygame.mouse.get_pressed()
          mousex, mousey = pygame.mouse.get_pos()
          print(click, mousex, mousey)

          screen.fill(BG_COLOR)
          pygame.display.flip()
          clock.tick(60) # Limit the frame rate to 60 FPS.





          share|improve this answer























          • Would any pygame.event work?
            – J.Garry
            2 days ago






          • 1




            I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
            – skrx
            2 days ago













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          You have to call one of the pygame.event functions regularly (for example pygame.event.pump or for event in pygame.event.get():), otherwise pygame.mouse.get_pressed (and some joystick functions) won't work correctly and the pygame window will become unresponsive after a while.



          Here's a runnable example:



          import pygame


          pygame.init()
          screen = pygame.display.set_mode((640, 480))
          clock = pygame.time.Clock()
          BG_COLOR = pygame.Color('gray12')

          done = False
          while not done:
          # This event loop empties the event queue each frame.
          for event in pygame.event.get():
          # Quit by pressing the X button of the window.
          if event.type == pygame.QUIT:
          done = True
          elif event.type == pygame.MOUSEBUTTONDOWN:
          # MOUSEBUTTONDOWN events have a pos and a button attribute
          # which you can use as well. This will be printed once per
          # event / mouse click.
          print('In the event loop:', event.pos, event.button)

          # Instead of the event loop above you could also call pygame.event.pump
          # each frame to prevent the window from freezing. Comment it out to check it.
          # pygame.event.pump()

          click = pygame.mouse.get_pressed()
          mousex, mousey = pygame.mouse.get_pos()
          print(click, mousex, mousey)

          screen.fill(BG_COLOR)
          pygame.display.flip()
          clock.tick(60) # Limit the frame rate to 60 FPS.





          share|improve this answer














          You have to call one of the pygame.event functions regularly (for example pygame.event.pump or for event in pygame.event.get():), otherwise pygame.mouse.get_pressed (and some joystick functions) won't work correctly and the pygame window will become unresponsive after a while.



          Here's a runnable example:



          import pygame


          pygame.init()
          screen = pygame.display.set_mode((640, 480))
          clock = pygame.time.Clock()
          BG_COLOR = pygame.Color('gray12')

          done = False
          while not done:
          # This event loop empties the event queue each frame.
          for event in pygame.event.get():
          # Quit by pressing the X button of the window.
          if event.type == pygame.QUIT:
          done = True
          elif event.type == pygame.MOUSEBUTTONDOWN:
          # MOUSEBUTTONDOWN events have a pos and a button attribute
          # which you can use as well. This will be printed once per
          # event / mouse click.
          print('In the event loop:', event.pos, event.button)

          # Instead of the event loop above you could also call pygame.event.pump
          # each frame to prevent the window from freezing. Comment it out to check it.
          # pygame.event.pump()

          click = pygame.mouse.get_pressed()
          mousex, mousey = pygame.mouse.get_pos()
          print(click, mousex, mousey)

          screen.fill(BG_COLOR)
          pygame.display.flip()
          clock.tick(60) # Limit the frame rate to 60 FPS.






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          skrx

          14.7k31833




          14.7k31833












          • Would any pygame.event work?
            – J.Garry
            2 days ago






          • 1




            I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
            – skrx
            2 days ago


















          • Would any pygame.event work?
            – J.Garry
            2 days ago






          • 1




            I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
            – skrx
            2 days ago
















          Would any pygame.event work?
          – J.Garry
          2 days ago




          Would any pygame.event work?
          – J.Garry
          2 days ago




          1




          1




          I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
          – skrx
          2 days ago




          I've added the example in which I show you how to use an event loop or pygame.event.pump. You could also use pygame.event.wait or pygame.event.poll.
          – skrx
          2 days ago










          J.Garry is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          J.Garry is a new contributor. Be nice, and check out our Code of Conduct.













          J.Garry is a new contributor. Be nice, and check out our Code of Conduct.












          J.Garry is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238491%2fpython-pygame-mouse-position-and-which-button-is-pressed%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values