Using your mouse to its FULL potential!

Hey guys,

Gatordude here with team FALLENGATORJUICE. Today I wanted to tell you all how to accept input from the mouse so that you can maximize the potential of your pygames. First off, there are 3 types of events that the computer receives from the computer: MOUSEMOTION, MOUSEBUTTONDOWN, and MOUSEBUTTONUP. First lets discuss the latter two.

MOUSEBUTTONDOWN and MOUSEBUTTONUP both have two parameters: 'pos' and 'button.'

  • Pos refers to the position of the cursor when that event is called (either button up or down) and is shown in the form of a tuple. For example, if pos = (30,250) the cursor is 30 pixels to the right of the upper right corner (x value) and 250 pixels down from the top.

  • Button refers to the actual button that was pushed down or let go (buttonup). The left click on your mouse has a value of 1, the right click is 3, and the middle button (when you push down on the scrolling button) has a value of 3. So for example, found below is some simple code on checking for what button has been pushed

       for event in pygame.get():
            if event.type == MOUSEBUTTONDOWN and event.button == 1:
                  print "You have pressed down on the left mouse button!"
            if event.type == MOUSEBUTTONDOWN and event.button == 2:
                  print "You have pressed down on the middle button!"
            if event.type == MOUSEBUTTONDOWN and event.button == 3:
                  print "You have pressed down on the right mouse button!"

  • As you can see, if I press down on the left key, "You pressed down on the the left mouse button." In addition, you can substitute the MOUSEBUTTONUP event for MOUSEBUTTONDOWN.

  • Furthermore, you can use the 'pos' parameter to allow the user to click a certain section of the screen, say to talk to a character or to answer a math question. Below a sample of code accomplishing this.

     for event in pygame.get():
         if event.type == MOUSEBUTTONDOWN and event.button == 1:
             if event.pos[0] >10 and event.pos[0] <=70:
                if event.pos[1] >220 and event.pos[1] <280:
                    print "You have clicked in this region!"

  • Thus, with this code, if you left click within the region specified, (10,220) and (70,280), the phrase "You have clicked in this region!" will be printed to the screen. The brackets after 'pos' allow me to specify which paremeter of the position tuple (x value or y value) I am referring to. Zero refers to the x value and one refers to the y value.

  • The MOUSEMOTION event is a little different but is dealt with in similar ways. That is, the three parameters for MOUSEMOTION are pos, rel, and buttons. The 'buttons' and 'pos' parameters work exactly like their corresponding parameters in the MOUSEBUTTONDOWN and MOUSEBUTTONUP event types. The one new parameter is the 'rel' parameter which is a tuple containing the distance the mouse has moved since the last mouse motion event (sometimes called the mouse mickies)

  • The mouse is a very powerful part of the computer, and can certainly stretch the potentil of your pygame greatly from just using these three event types. See you all later and maybe I will be playing a great point-n-click game created by you!