[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Clearing the mouse and keyboard state



are you saying you want to change what pygame.mouse.get_pressed()
returns to you? if so, I'd say that changing what a function like that
returns would be bad practice - the function is supposed to tell you
which mouse buttons were held down last time you called a pygame.event
function to process event messages. If you did something to change
that, the function wouldn't be doing what it's supposed to and doing
that could lead to bugs. A better thing to do would be to use some
method that tells you what you actually want to know.

So what do you hope to accomplish by clearing the pressing of a mouse button?

---
If you are simply trying to figure out when the button was clicked
(i.e. the first frame where it was pressed down) the best thing would
probably be to write a message handler for the pygame.MOUSEBUTTONDOWN
message, and then store whatever mouse data you need in that handler.
Then you clear/initialize the data as appropriate before calls to
pygame.event message processing functions. One good thing about this
approach, is it enables you to store the actual click positions.
Imagine that somebody clicked and moved the mouse after - the mouse
position wouldn't be where they clicked anymore, so "get_pos" won't be
able to tell you where the user clicked.

hopefully the code below helps illustrate what I mean:

class MyGame:
  def __init__(self):
    self.left_button_clicked = False
    self.left_button_click_pos = None

  def processEvents(self):
    self.left_button_clicked = False
    self.left_button_click_pos = None
     for event in pygame.event.get():
       if event.type == pygame.MOUSEBUTTONDOWN and event.button == 0:
         self.left_button_clicked = True
         self.left_button_click_pos = event.pos

---

another option to figure out when the button clicked, would be to do
store a flag telling you whether the button was pressed last frame,
and report stuff as a click when it is pressed now, but wasn't pressed
last time. so you'd have some object with something like this:

class GameObject:
  def __init__(self):
    self.this_button_state = pygame.mouse.get_pressed()
    self.last_button_state = self.this_button_state

  def update_mouse_state(self):
    self.last_button_state = self.this_button_state
    self.this_button_state = pygame.mouse_get_pressed()

  def get_clicked(self):
    click_state = []
    for button_index in xrange(len(self.this_button_state)):
      click_state.append(self.this_button_state[button_index] and not
self.last_button_state[button_index])
    return click_state

and you'd call update_mouse_state once a frame, and call get_clicked
when you want to know which buttons were clicked. Code like that
should be fine if you don't care about exact mouse click positions. If
you do care about exact click positions, then use the mouse events.

On Jan 6, 2008 8:35 AM, 110110010 <Josef.Horn@xxxxxxxxx> wrote:
> Hello again.
> I've got a question: How can  I clear the pressing of a mouse button or a key. I mean I've pressed a mouse button in my game and i want a function to stop the pressed state of that button.
> (Finally sorry for my English, I'm czech :) )
>