我们从Python开源项目中,提取了以下44个代码示例,用于说明如何使用pygame.event()。
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 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]
def main(): init() if use_fast_events: fastevent.init() c = time.Clock() win = display.set_mode((640, 480), RESIZABLE) display.set_caption("fastevent Workout") poster = post_them() t1 = pytime.time() poster.start() going = True while going: # for e in event.get(): #for x in range(200): # ee = event.Event(USEREVENT) # r = event_module.post(ee) # print (r) #for e in event_module.get(): event_list = [] event_list = event_module.get() for e in event_list: if e.type == QUIT: print (c.get_fps()) poster.stop.append(1) going = False if e.type == KEYDOWN: if e.key == K_ESCAPE: print (c.get_fps()) poster.stop.append(1) going = False if poster.done: print (c.get_fps()) print (c) t2 = pytime.time() print ("total time:%s" % (t2 - t1)) print ("events/second:%s" % (NUM_EVENTS_TO_POST / (t2 - t1))) going = False if with_display: display.flip() if slow_tick: c.tick(40) pygame.quit()
def __init__(self, mainwindow, inner_evb): '''Initialise the Translator with the windows to which to listen''' self._mainwindow = mainwindow self._inner_evb = inner_evb # Enable events # (add instead of set here because the main window is already realized) self._mainwindow.add_events( Gdk.EventMask.KEY_PRESS_MASK | \ Gdk.EventMask.KEY_RELEASE_MASK \ ) self._inner_evb.set_events( Gdk.EventMask.POINTER_MOTION_MASK | \ Gdk.EventMask.POINTER_MOTION_HINT_MASK | \ Gdk.EventMask.BUTTON_MOTION_MASK | \ Gdk.EventMask.BUTTON_PRESS_MASK | \ Gdk.EventMask.BUTTON_RELEASE_MASK ) self._mainwindow.set_can_focus(True) self._inner_evb.set_can_focus(True) # Callback functions to link the event systems self._mainwindow.connect('unrealize', self._quit_cb) self._inner_evb.connect('key_press_event', self._keydown_cb) self._inner_evb.connect('key_release_event', self._keyup_cb) self._inner_evb.connect('button_press_event', self._mousedown_cb) self._inner_evb.connect('button_release_event', self._mouseup_cb) self._inner_evb.connect('motion-notify-event', self._mousemove_cb) self._inner_evb.connect('draw', self._draw_cb) self._inner_evb.connect('configure-event', self._resize_cb) # Internal data self.__stopped = False self.__keystate = [0] * 323 self.__button_state = [0,0,0] self.__mouse_pos = (0,0) self.__repeat = (None, None) self.__held = set() self.__held_time_left = {} self.__held_last_time = {} self.__tick_id = None
def process_event(self, event): # perf: local for faster access io = self.io if event.type == pygame.MOUSEMOTION: io.mouse_pos = event.pos if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: io.mouse_down[0] = 1 if event.button == 2: io.mouse_down[1] = 1 if event.button == 3: io.mouse_down[2] = 1 if event.type == pygame.MOUSEBUTTONUP: if event.button == 1: io.mouse_down[0] = 0 if event.button == 2: io.mouse_down[1] = 0 if event.button == 3: io.mouse_down[2] = 0 if event.button == 4: io.mouse_wheel = .5 if event.button == 5: io.mouse_wheel = -.5 if event.type == pygame.KEYDOWN: for char in event.unicode: code = ord(char) if 0 < code < 0x10000: io.add_input_character(code) io.keys_down[event.key] = True if event.type == pygame.KEYUP: io.keys_down[event.key] = False if event.type in (pygame.KEYDOWN, pygame.KEYUP): io.key_ctrl = ( io.keys_down[pygame.K_LCTRL] or io.keys_down[pygame.K_RCTRL] ) io.key_alt = ( io.keys_down[pygame.K_LALT] or io.keys_down[pygame.K_RALT] ) io.key_shift = ( io.keys_down[pygame.K_LSHIFT] or io.keys_down[pygame.K_RSHIFT] ) io.key_super = ( io.keys_down[pygame.K_LSUPER] or io.keys_down[pygame.K_LSUPER] ) if event.type == pygame.VIDEORESIZE: io.display_size = event.size