def __init__(self, width, height, fullimage, starting_position=(0, 0)): if not display.get_init(): display.init() self.width = width self.height = height self.image = image.load(fullimage).convert() self.rect = self.image.get_rect()
def test_init(self): """Ensures the module is initialized after init called.""" # display.init() already called in setUp(), so quit and re-init display.quit() display.init() self.assertTrue(display.get_init())
def clearEvents(eventType=None): """Clears all events currently in the event buffer. Optional argument, eventType, specifies only certain types to be cleared. :Parameters: eventType : **None**, 'mouse', 'joystick', 'keyboard' If this is not None then only events of the given type are cleared """ #pyglet if not havePygame or not display.get_init(): #for each (pyglet) window, dispatch its events before checking event buffer wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events()#pump events on pyglet windows if eventType=='mouse': return # pump pyglet mouse events but don't flush keyboard buffer global _keyBuffer _keyBuffer = [] else: #for pygame if eventType=='mouse': junk = evt.get([locals.MOUSEMOTION, locals.MOUSEBUTTONUP, locals.MOUSEBUTTONDOWN]) elif eventType=='keyboard': junk = evt.get([locals.KEYDOWN, locals.KEYUP]) elif eventType=='joystick': junk = evt.get([locals.JOYAXISMOTION, locals.JOYBALLMOTION, locals.JOYHATMOTION, locals.JOYBUTTONUP, locals.JOYBUTTONDOWN]) else: junk = evt.get()
def __init__(self, width, height, fullimage, starting_position=(0, 0)): if not display.get_init(): display.init() self.full_image = image.load(fullimage) self.width = width self.height = height self.x_sprites = self.full_image.get_width() / self.width self.y_sprites = self.full_image.get_height() / self.height self.update_current((0, 0, self.width, self.height)) self.rect = self.image.get_rect() self.rect.topleft = starting_position
def __init__(self, inputCallback, window_size=default_window_size, printing=True): super().__init__(inputCallback) self.printing = printing self.clock = time.Clock() # Make sure display is initialized if not display.get_init(): display.init() self.window = display.set_mode(window_size) display.set_caption('Connect 4') self.window_size = display.get_surface().get_size()
def clearEvents(eventType=None): """Clears all events currently in the event buffer. Optional argument, eventType, specifies only certain types to be cleared. :Parameters: eventType : **None**, 'mouse', 'joystick', 'keyboard' If this is not None then only events of the given type are cleared """ if not havePygame or not display.get_init(): # pyglet # For each window, dispatch its events before # checking event buffer. for win in _default_display_.get_windows(): win.dispatch_events() # pump events on pyglet windows if eventType == 'mouse': pass elif eventType == 'joystick': pass else: # eventType='keyboard' or eventType=None. global _keyBuffer _keyBuffer = [] else: # pygame if eventType == 'mouse': evt.get([ locals.MOUSEMOTION, locals.MOUSEBUTTONUP, locals.MOUSEBUTTONDOWN ]) elif eventType == 'keyboard': evt.get([locals.KEYDOWN, locals.KEYUP]) elif eventType == 'joystick': evt.get([ locals.JOYAXISMOTION, locals.JOYBALLMOTION, locals.JOYHATMOTION, locals.JOYBUTTONUP, locals.JOYBUTTONDOWN ]) else: evt.get()
def test_quit__multiple(self): """Ensures the module is not initialized after multiple quit calls.""" display.quit() display.quit() self.assertFalse(display.get_init())
def test_quit(self): """Ensures the module is not initialized after quit called.""" display.quit() self.assertFalse(display.get_init())
def test_init__multiple(self): """Ensures the module is initialized after multiple init calls.""" display.init() display.init() self.assertTrue(display.get_init())
def test_get_init(self): """Ensures the module's initialization state can be retrieved.""" # display.init() already called in setUp() self.assertTrue(display.get_init())
def getKeys(keyList=None, modifiers=False, timeStamped=False): """Returns a list of keys that were pressed. :Parameters: keyList : **None** or [] Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is `None`, all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn't return timestamps (they are always 0) modifiers : **False** or True If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, modifiers). The modifiers are a dict of keyboard modifier flags keyed by the modifier name (eg. 'shift', 'ctrl'). timeStamped : **False**, True, or `Clock` If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a `core.Clock` is given then the time will be relative to the `Clock`'s last reset. :Author: - 2003 written by Jon Peirce - 2009 keyList functionality added by Gary Strangman - 2009 timeStamped code provided by Dave Britton - 2016 modifiers code provided by 5AM Solutions """ keys = [] if havePygame and display.get_init(): # see if pygame has anything instead (if it exists) for evts in evt.get(locals.KEYDOWN): # pygame has no keytimes keys.append((pygame.key.name(evts.key), 0)) elif havePyglet: # for each (pyglet) window, dispatch its events before checking event # buffer defDisplay = pyglet.window.get_platform().get_default_display() for win in defDisplay.get_windows(): try: win.dispatch_events() # pump events on pyglet windows except ValueError as e: # pragma: no cover # Pressing special keys, such as 'volume-up', results in a # ValueError. This appears to be a bug in pyglet, and may be # specific to certain systems and versions of Python. logging.error(u'Failed to handle keypress') global _keyBuffer if len(_keyBuffer) > 0: # then pyglet is running - just use this keys = _keyBuffer # _keyBuffer = [] # DO /NOT/ CLEAR THE KEY BUFFER ENTIRELY if keyList is None: _keyBuffer = [] # clear buffer entirely targets = keys # equivalent behavior to getKeys() else: nontargets = [] targets = [] # split keys into keepers and pass-thrus for key in keys: if key[0] in keyList: targets.append(key) else: nontargets.append(key) _keyBuffer = nontargets # save these # now we have a list of tuples called targets # did the user want timestamped tuples or keynames? if modifiers == False and timeStamped == False: keyNames = [k[0] for k in targets] return keyNames elif timeStamped == False: keyNames = [(k[0], modifiers_dict(k[1])) for k in targets] return keyNames elif hasattr(timeStamped, 'getLastResetTime'): # keys were originally time-stamped with # core.monotonicClock._lastResetTime # we need to shift that by the difference between it and # our custom clock _last = timeStamped.getLastResetTime() _clockLast = psychopy.core.monotonicClock.getLastResetTime() timeBaseDiff = _last - _clockLast relTuple = [[ _f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1] - timeBaseDiff) if _f ] for k in targets] return relTuple elif timeStamped is True: return [[ _f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1]) if _f ] for k in targets] elif isinstance(timeStamped, (float, int, int)): relTuple = [[ _f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1] - timeStamped) if _f ] for k in targets] return relTuple
def getKeys(keyList=None, modifiers=False, timeStamped=False): """Returns a list of keys that were pressed. :Parameters: keyList : **None** or [] Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is `None`, all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn't return timestamps (they are always 0) modifiers : **False** or True If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, modifiers). The modifiers are a dict of keyboard modifier flags keyed by the modifier name (eg. 'shift', 'ctrl'). timeStamped : **False**, True, or `Clock` If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a `core.Clock` is given then the time will be relative to the `Clock`'s last reset. :Author: - 2003 written by Jon Peirce - 2009 keyList functionality added by Gary Strangman - 2009 timeStamped code provided by Dave Britton - 2016 modifiers code provided by 5AM Solutions """ keys = [] if havePygame and display.get_init(): # see if pygame has anything instead (if it exists) for evts in evt.get(locals.KEYDOWN): # pygame has no keytimes keys.append((pygame.key.name(evts.key), 0)) elif havePyglet: # for each (pyglet) window, dispatch its events before checking event # buffer defDisplay = pyglet.window.get_platform().get_default_display() for win in defDisplay.get_windows(): try: win.dispatch_events() # pump events on pyglet windows except ValueError as e: # pragma: no cover # Pressing special keys, such as 'volume-up', results in a # ValueError. This appears to be a bug in pyglet, and may be # specific to certain systems and versions of Python. logging.error(u'Failed to handle keypress') global _keyBuffer if len(_keyBuffer) > 0: # then pyglet is running - just use this keys = _keyBuffer # _keyBuffer = [] # DO /NOT/ CLEAR THE KEY BUFFER ENTIRELY elif haveGLFW: # 'poll_events' is called when a window is flipped, all the callbacks # populate the buffer if len(_keyBuffer) > 0: keys = _keyBuffer if keyList is None: _keyBuffer = [] # clear buffer entirely targets = keys # equivalent behavior to getKeys() else: nontargets = [] targets = [] # split keys into keepers and pass-thrus for key in keys: if key[0] in keyList: targets.append(key) else: nontargets.append(key) _keyBuffer = nontargets # save these # now we have a list of tuples called targets # did the user want timestamped tuples or keynames? if modifiers == False and timeStamped == False: keyNames = [k[0] for k in targets] return keyNames elif timeStamped == False: keyNames = [(k[0], modifiers_dict(k[1])) for k in targets] return keyNames elif hasattr(timeStamped, 'getLastResetTime'): # keys were originally time-stamped with # core.monotonicClock._lastResetTime # we need to shift that by the difference between it and # our custom clock _last = timeStamped.getLastResetTime() _clockLast = psychopy.core.monotonicClock.getLastResetTime() timeBaseDiff = _last - _clockLast relTuple = [[_f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1] - timeBaseDiff) if _f] for k in targets] return relTuple elif timeStamped is True: return [[_f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1]) if _f] for k in targets] elif isinstance(timeStamped, (float, int, int)): relTuple = [[_f for _f in (k[0], modifiers and modifiers_dict(k[1]) or None, k[-1] - timeStamped) if _f] for k in targets] return relTuple
def getKeys(keyList=None, timeStamped=False): """Returns a list of keys that were pressed. :Parameters: keyList : **None** or [] Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is None all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn't return timestamps (they are always 0) timeStamped : **False** or True or `Clock` If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a `core.Clock` is given then the time will be relative to the `Clock`'s last reset :Author: - 2003 written by Jon Peirce - 2009 keyList functionality added by Gary Strangman - 2009 timeStamped code provided by Dave Britton """ keys=[] if havePygame and display.get_init():#see if pygame has anything instead (if it exists) for evts in evt.get(locals.KEYDOWN): keys.append( (pygame.key.name(evts.key),0) )#pygame has no keytimes elif havePyglet: #for each (pyglet) window, dispatch its events before checking event buffer wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: try: win.dispatch_events()#pump events on pyglet windows except ValueError as e: # Pressing special keys, such as 'volume-up', results in a # ValueError. This appears to be a bug in pyglet, and may be # specific to certain systems and versions of Python. logging.error(u'Failed to handle keypress') global _keyBuffer if len(_keyBuffer)>0: #then pyglet is running - just use this keys = _keyBuffer # _keyBuffer = [] # DO /NOT/ CLEAR THE KEY BUFFER ENTIRELY if keyList==None: _keyBuffer = [] #clear buffer entirely targets=keys # equivalent behavior to getKeys() else: nontargets = [] targets = [] # split keys into keepers and pass-thrus for key in keys: if key[0] in keyList: targets.append(key) else: nontargets.append(key) _keyBuffer = nontargets # save these #now we have a list of tuples called targets #did the user want timestamped tuples or keynames? if timeStamped==False: keyNames = [k[0] for k in targets] return keyNames elif hasattr(timeStamped, 'getLastResetTime'): relTuple = [(k[0],k[1]-timeStamped.getLastResetTime()) for k in targets] return relTuple elif timeStamped==True: return targets
def screen_size(): ''' ''' assert display.get_init() info = display.Info() return (info.current_w, info.current_h)
def getKeys(keyList=None, timeStamped=False): """Returns a list of keys that were pressed. :Parameters: keyList : **None** or [] Allows the user to specify a set of keys to check for. Only keypresses from this set of keys will be removed from the keyboard buffer. If the keyList is None all keys will be checked and the key buffer will be cleared completely. NB, pygame doesn't return timestamps (they are always 0) timeStamped : **False** or True or `Clock` If True will return a list of tuples instead of a list of keynames. Each tuple has (keyname, time). If a `core.Clock` is given then the time will be relative to the `Clock`'s last reset :Author: - 2003 written by Jon Peirce - 2009 keyList functionality added by Gary Strangman - 2009 timeStamped code provided by Dave Britton """ keys = [] if havePygame and display.get_init( ): #see if pygame has anything instead (if it exists) for evts in evt.get(locals.KEYDOWN): keys.append( (pygame.key.name(evts.key), 0)) #pygame has no keytimes elif havePyglet: #for each (pyglet) window, dispatch its events before checking event buffer wins = pyglet.window.get_platform().get_default_display().get_windows() for win in wins: win.dispatch_events() #pump events on pyglet windows global _keyBuffer if len(_keyBuffer) > 0: #then pyglet is running - just use this keys = _keyBuffer # _keyBuffer = [] # DO /NOT/ CLEAR THE KEY BUFFER ENTIRELY if keyList == None: _keyBuffer = [] #clear buffer entirely targets = keys # equivalent behavior to getKeys() else: nontargets = [] targets = [] # split keys into keepers and pass-thrus for key in keys: if key[0] in keyList: targets.append(key) else: nontargets.append(key) _keyBuffer = nontargets # save these #now we have a list of tuples called targets #did the user want timestamped tuples or keynames? if timeStamped == False: keyNames = [k[0] for k in targets] return keyNames elif hasattr(timeStamped, 'timeAtLastReset'): relTuple = [(k[0], k[1] - timeStamped.timeAtLastReset) for k in targets] return relTuple elif timeStamped == True: return targets
def load(width, height, scale=1, fullscreen=False): if (display.get_init()): raise Exception("ALREADY INITIATED") SCREEN_WIDTH = width * scale SCREEN_HEIGHT = height * scale SCALE = scale