def run(self):
self.done = []
self.stop = []
for x in range(NUM_EVENTS_TO_POST):
ee = event.Event(USEREVENT)
try_post = 1
# the pygame.event.post raises an exception if the event
# queue is full. so wait a little bit, and try again.
while try_post:
try:
event_module.post(ee)
try_post = 0
except:
pytime.sleep(0.001)
try_post = 1
if self.stop:
return
self.done.append(1)
python类event()的实例源码
def _mousemove_cb(self, widget, event):
# From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/
# if this is a hint, then let's get all the necessary
# information, if not it's all we need.
if event.is_hint:
win, x, y, state = event.window.get_device_position(event.device)
else:
x = event.x
y = event.y
state = event.get_state()
rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1])
self.__mouse_pos = (x, y)
self.__button_state = [
state & Gdk.ModifierType.BUTTON1_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0,
]
evt = pygame.event.Event(pygame.MOUSEMOTION,
pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
self._post(evt)
return True
def _mousemove_cb(self, widget, event):
# From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/
# if this is a hint, then let's get all the necessary
# information, if not it's all we need.
if event.is_hint:
win, x, y, state = event.window.get_device_position(event.device)
else:
x = event.x
y = event.y
state = event.get_state()
rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1])
self.__mouse_pos = (x, y)
self.__button_state = [
state & Gdk.ModifierType.BUTTON1_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0,
]
evt = pygame.event.Event(pygame.MOUSEMOTION,
pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
self._post(evt)
return True
def run(self):
self.done = []
self.stop = []
for x in range(NUM_EVENTS_TO_POST):
ee = event.Event(USEREVENT)
try_post = 1
# the pygame.event.post raises an exception if the event
# queue is full. so wait a little bit, and try again.
while try_post:
try:
event_module.post(ee)
try_post = 0
except:
pytime.sleep(0.001)
try_post = 1
if self.stop:
return
self.done.append(1)
def _mousemove_cb(self, widget, event):
# From http://www.learningpython.com/2006/07/25/writing-a-custom-widget-using-pygtk/
# if this is a hint, then let's get all the necessary
# information, if not it's all we need.
if event.is_hint:
win, x, y, state = event.window.get_device_position(event.device)
else:
x = event.x
y = event.y
state = event.get_state()
rel = (x - self.__mouse_pos[0], y - self.__mouse_pos[1])
self.__mouse_pos = (x, y)
self.__button_state = [
state & Gdk.ModifierType.BUTTON1_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON2_MASK and 1 or 0,
state & Gdk.ModifierType.BUTTON3_MASK and 1 or 0,
]
evt = pygame.event.Event(pygame.MOUSEMOTION,
pos=self.__mouse_pos, rel=rel, buttons=self.__button_state)
self._post(evt)
return True
def process_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
return False
# Main game logic
def get_key():
while 1:
event = pygame.event.poll()
if event.type == KEYDOWN:
return event.key
else:
pass
def _draw_cb(self, widget, event):
if pygame.display.get_init():
pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
return True
def _resize_cb(self, widget, event):
evt = pygame.event.Event(pygame.VIDEORESIZE,
size=(event.width,event.height), width=event.width, height=event.height)
pygame.event.post(evt)
return False # continue processing
def _quit_cb(self, data=None):
self.__stopped = True
pygame.event.post(pygame.event.Event(pygame.QUIT))
def _keydown_cb(self, widget, event):
key = event.keyval
if key in self.__held:
return True
else:
if self.__repeat[0] is not None:
self.__held_last_time[key] = pygame.time.get_ticks()
self.__held_time_left[key] = self.__repeat[0]
self.__held.add(key)
return self._keyevent(widget, event, pygame.KEYDOWN)
def _keyevent(self, widget, event, type):
key = Gdk.keyval_name(event.keyval)
if key is None:
# No idea what this key is.
return False
keycode = None
if key in self.key_trans:
keycode = self.key_trans[key]
elif hasattr(pygame, 'K_'+key.upper()):
keycode = getattr(pygame, 'K_'+key.upper())
elif hasattr(pygame, 'K_'+key.lower()):
keycode = getattr(pygame, 'K_'+key.lower())
elif key == 'XF86Start':
# view source request, specially handled...
self._mainwindow.view_source()
else:
print 'Key %s unrecognized' % key
if keycode is not None:
if type == pygame.KEYDOWN:
mod = self._keymods()
self.__keystate[keycode] = type == pygame.KEYDOWN
if type == pygame.KEYUP:
mod = self._keymods()
ukey = unichr(Gdk.keyval_to_unicode(event.keyval))
if ukey == '\000':
ukey = ''
evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod)
self._post(evt)
return True
def _mousedown_cb(self, widget, event):
self.__button_state[event.button-1] = 1
return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
def _mouseup_cb(self, widget, event):
self.__button_state[event.button-1] = 0
return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP)
def _mouseevent(self, widget, event, type):
evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y))
self._post(evt)
return True
def _draw_cb(self, widget, event):
if pygame.display.get_init():
pygame.event.post(pygame.event.Event(pygame.VIDEOEXPOSE))
return True
def _resize_cb(self, widget, event):
evt = pygame.event.Event(pygame.VIDEORESIZE,
size=(event.width,event.height), width=event.width, height=event.height)
pygame.event.post(evt)
return False # continue processing
def _quit_cb(self, data=None):
self.__stopped = True
pygame.event.post(pygame.event.Event(pygame.QUIT))
def _keydown_cb(self, widget, event):
key = event.keyval
if key in self.__held:
return True
else:
if self.__repeat[0] is not None:
self.__held_last_time[key] = pygame.time.get_ticks()
self.__held_time_left[key] = self.__repeat[0]
self.__held.add(key)
return self._keyevent(widget, event, pygame.KEYDOWN)
def _keyevent(self, widget, event, type):
key = Gdk.keyval_name(event.keyval)
if key is None:
# No idea what this key is.
return False
keycode = None
if key in self.key_trans:
keycode = self.key_trans[key]
elif hasattr(pygame, 'K_'+key.upper()):
keycode = getattr(pygame, 'K_'+key.upper())
elif hasattr(pygame, 'K_'+key.lower()):
keycode = getattr(pygame, 'K_'+key.lower())
elif key == 'XF86Start':
# view source request, specially handled...
self._mainwindow.view_source()
else:
print 'Key %s unrecognized' % key
if keycode is not None:
if type == pygame.KEYDOWN:
mod = self._keymods()
self.__keystate[keycode] = type == pygame.KEYDOWN
if type == pygame.KEYUP:
mod = self._keymods()
ukey = unichr(Gdk.keyval_to_unicode(event.keyval))
if ukey == '\000':
ukey = ''
evt = pygame.event.Event(type, key=keycode, unicode=ukey, mod=mod)
self._post(evt)
return True
def _mousedown_cb(self, widget, event):
self.__button_state[event.button-1] = 1
return self._mouseevent(widget, event, pygame.MOUSEBUTTONDOWN)
def _mouseup_cb(self, widget, event):
self.__button_state[event.button-1] = 0
return self._mouseevent(widget, event, pygame.MOUSEBUTTONUP)
def _mouseevent(self, widget, event, type):
evt = pygame.event.Event(type, button=event.button, pos=(event.x, event.y))
self._post(evt)
return True
def process_event(self, event: pygame.event):
pass
def process_event(self, event: pygame.event):
if event.type == KEYDOWN:
if event.key == CONTROLS[UP]:
self.find_select(UP)
elif event.key == CONTROLS[DOWN]:
self.find_select(DOWN)
elif event.key == K_RETURN:
if MENU_ITEMS[self.get_selected_item()] is "quit":
self.LOOP_RUN = False
else:
self.game.start(MENU_ITEMS[self.get_selected_item()])
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
self.LOOP_RUN = False
def start(self):
super(MainMenu, self).start()
while self.LOOP_RUN:
for event in pygame.event.get():
self.process_event(event)
self.update_screen()
pygame.display.flip()
def process_event(self, event: pygame.event):
if event.type == KEYDOWN:
if event.key == CONTROLS[UP]:
self.racket_p1.move(UP)
elif event.key == CONTROLS[DOWN]:
self.racket_p1.move(DOWN)
if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
self.LOOP_RUN = False
print('Ending the game...')
def start(self, game_type="one_player"):
print('Starting the game...')
super(Game, self).start()
self.game_type = game_type
while self.LOOP_RUN:
for event in pygame.event.get():
self.process_event(event)
self.update_screen()
self.ball.move(self.racket_p1, self.racket_p2)
self.racket_p2.auto_move(self.ball)
pygame.display.flip()
self.FPSCLOCK.tick(FPS)
def keyDown(self, event):
if self.selected:
if event.key == pygame.K_ESCAPE:
self.selected = False
elif event.key == pygame.K_BACKSPACE and len(self.current_string) > 0:
#self.current_string = self.current_string[:-1]
self.current_string.pop(self.curpos - 1)
self.curpos -= 1
elif event.key == pygame.K_DELETE and len(self.current_string) > 0:
#self.current_string = self.current_string[:-1]
self.current_string.pop(self.curpos)
elif event.key == pygame.K_RETURN:
if self.completeHandler != None:
self.completeHandler(self, string.join(self.current_string, ""))
self.completed = True
elif event.key >= 32 and event.key <= 127:
k = event.key
kp = pygame.key.get_pressed()
if (kp[pygame.K_LSHIFT] or kp[pygame.K_RSHIFT]) and k > 96 and k < 123:
k -= 32
elif (kp[pygame.K_LSHIFT] or kp[pygame.K_RSHIFT]) and k > 32:
k = ord(self.schars[chr(k)])
self.current_string.insert(self.curpos, chr(k))
self.curpos += 1
elif event.key == pygame.K_LEFT and self.curpos > 0:
self.curpos -= 1
elif event.key == pygame.K_RIGHT and self.curpos < len(self.current_string):
self.curpos += 1
def mouseDown(self, event):
mpos = event.mousepos
self.selected = mpos[0] >= self.pos[0] and mpos[0] < self.pos[0] + self.geometry[0] and mpos[1] >= self.pos[1] and mpos[1] < self.pos[1] + self.geometry[1]