def init(self): flags = 0 if not ANDROID: os.environ['SDL_VIDEO_CENTERED'] = '1' WINSIZE = 480, 800 else: WINSIZE = 0, 0 flags |= pygame.FULLSCREEN pygame.init() mixer.init() # Map the back button to the escape key. if ANDROID: android.init() android.map_key(android.KEYCODE_BACK, pygame.K_b) self.clock = pygame.time.Clock() if not ANDROID: self.icon = pygame.image.load(get_resource('android-icon.png')) pygame.display.set_icon(self.icon) screen = self.screen = pygame.display.set_mode(WINSIZE, flags) self.width, self.height = screen.get_width(), screen.get_height() pygame.display.set_caption('Mazenum') self.score_font = pygame.font.Font(get_resource(join("fonts", "FreeSans.ttf")), 25) self.completed_font = pygame.font.Font(get_resource(join("fonts", "FreeSans.ttf")), 40) self.start_button = Button("Start game") self.playboard = PlayBoard(screen, self.COLUMNS, self.ROWS, self.header_height) self._set_background() self.is_game_over = False
def init(self): flags = 0 if not ANDROID: os.environ['SDL_VIDEO_CENTERED'] = '1' WINSIZE = 480, 800 else: WINSIZE = 0, 0 flags |= pygame.FULLSCREEN pygame.init() if USE_MIXER: mixer.init() # Map the back button to the escape key. if ANDROID: android.init() android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE) self.clock = pygame.time.Clock() if not (ANDROID or PYTHON4ANDROID): self.icon = pygame.image.load(get_resource('android-icon.png')) pygame.display.set_icon(self.icon) screen = self.screen = pygame.display.set_mode(WINSIZE, flags) self.width, self.height = screen.get_width(), screen.get_height() pygame.display.set_caption('Popzi') if USE_MIXER: self.pop_sound = mixer.Sound(get_resource("sfx/pop.ogg")) self.score_font = pygame.font.Font(get_resource(join("fonts", "FreeSans.ttf")), 25) self.completed_font = pygame.font.Font(get_resource(join("fonts", "FreeSans.ttf")), 40) self.start_button = Button("Start game") self.themes_button = Button("Select theme") self.playboard = PlayBoard(screen, self.COLUMNS, self.ROWS, self.header_height) self._read_theme_config()
_platform = platform try: if _platform == 'android': try: import android.mixer as mixer except ImportError: # old python-for-android version import android_mixer as mixer else: from pygame import mixer except: raise # init pygame sound mixer.pre_init(44100, -16, 2, 1024) mixer.init() mixer.set_num_channels(32) class SoundPygame(Sound): # XXX we don't set __slots__ here, to automatically add # a dictionary. We need that to be able to use weakref for # SoundPygame object. Otherwise, it failed with: # TypeError: cannot create weak reference to 'SoundPygame' object # We use our clock in play() method. # __slots__ = ('_data', '_channel') _check_play_ev = None @staticmethod def extensions():
def initialize(experiment=None): """Initialize an experiment. This initializes an experiment defined by 'experiment' as well as the underlying expyriment system. If 'experiment' is None, a new Experiment object will be created and returned. Furthermore, a screen, a clock, a keyboard and a event file are created and added to the experiment. The initialization screen is shown for a short delay to ensure that Python is fully initialized and time accurate. Afterwards, "Preparing experiment..." is presented on the screen. After experiment initialize the following additional properties are available: - experiment.screen -- the current screen - experiment.clock -- the main clock - experiment.keyboard -- the main keyboard - experiment.mouse -- the main mouse - experiment.event -- the main event file Parameters ---------- experiment : design.Experiment, optional the experiment to initialize Returns ------- exp : design.Experiment initialized experiment """ if experiment is None: experiment = design.Experiment() if experiment.log_level is None: experiment.set_log_level(defaults.event_logging) if is_interactive_mode() and not expyriment.control.defaults.window_mode \ and not hasattr(experiment, "testsuite"): print """ Python is running in an interactive shell but Expyriment wants to initialize a fullscreen.""" quest = "Do you want to switch to windows mode?" ans = raw_input(quest + " (Y/n) ").strip().lower() if ans=="" or ans=="y" or ans=="yes": print "Switched to windows mode" expyriment.control.defaults.window_mode = True stdout_logging = defaults.stdout_logging expyriment._active_exp = experiment old_logging = experiment.log_level experiment.set_log_level(0) # switch off for the first screens _keyboard.quit_key = defaults.quit_key _keyboard.pause_key = defaults.pause_key _keyboard.refresh_key = defaults.refresh_key _keyboard.end_function = end _keyboard.pause_function = pause mixer.pre_init(defaults.audiosystem_sample_rate, defaults.audiosystem_bit_depth, defaults.audiosystem_channels, defaults.audiosystem_buffer_size) if defaults.audiosystem_autostart: mixer.init() mixer.init() # Needed on some systems experiment._clock = misc.Clock() experiment._screen = Screen(colour=(0, 0, 0), open_gl=defaults.open_gl, window_mode=defaults.window_mode, window_size=defaults.window_size) # Hack for IDLE: quit pygame and call atexit functions when crashing if is_idle_running() and sys.argv[0] != "": try: import idlelib.run def wrap(orig_func): def newfunc(*a, **kw): pygame.quit() import atexit atexit._run_exitfuncs() idlelib.run.flush_stdout = orig_func return orig_func(*a, **kw) return newfunc idlelib.run.flush_stdout = wrap(idlelib.run.flush_stdout) except ImportError: pass experiment._data = None experiment._subject = None experiment._is_initialized = True # required before EventFile if old_logging> 0: experiment._events = EventFile( additional_suffix=experiment.filename_suffix, time_stamp=True) if stdout_logging: _set_stdout_logging(experiment._events) else: experiment._events = None experiment._keyboard = Keyboard() experiment._mouse = Mouse(show_cursor=False) logo = stimuli.Picture(misc.constants.EXPYRIMENT_LOGO_FILE, position=(0, 100)) logo.scale((0.7, 0.7)) text = stimuli.TextLine("Version {0}".format(expyriment.get_version()), text_size=14, text_colour=misc.constants.C_EXPYRIMENT_ORANGE, background_colour=(0, 0, 0), position=(0, 40)) canvas = stimuli.Canvas((600, 300), colour=(0, 0, 0)) canvas2 = stimuli.Canvas((600, 300), colour=(0, 0, 0)) logo.plot(canvas) text.plot(canvas) hash_ = expyriment.get_experiment_secure_hash() if hash_ is not None: txt = "{0} ({1})".format(os.path.split(sys.argv[0])[1], hash_) if len(expyriment._secure_hash.module_hashes_as_string())>0: txt += ", {0}".format( expyriment._secure_hash.module_hashes_as_string()) text2 = stimuli.TextLine(txt, text_size=14, text_colour=misc.constants.C_EXPYRIMENT_ORANGE, background_colour=(0, 0, 0), position=(0, 10)) text2.plot(canvas) canvas.preload(True) canvas._set_surface(canvas._get_surface().convert()) start = experiment.clock.time r = [x for x in range(256) if x % 5 == 0] stopped = False if defaults.initialize_delay > 0: for x in r: canvas._get_surface().set_alpha(x) canvas2.clear_surface() canvas.plot(canvas2) canvas2.present() experiment.clock.wait(1) key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: stopped = True break duration = experiment.clock.time - start if duration < 2000 and not stopped: start = experiment.clock.time while experiment.clock.time - start < 2000: key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: stopped = True break r = [x for x in range(256)[::-1] if x % 5 == 0] if not stopped: for x in r: canvas._get_surface().set_alpha(x) canvas2.clear_surface() canvas.plot(canvas2) canvas2.present() experiment.clock.wait(1) key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: break stimuli.TextLine("Preparing experiment...", text_size=24, text_colour=misc.constants.C_EXPYRIMENT_PURPLE).present() experiment._screen.colour = experiment.background_colour experiment.set_log_level(old_logging) stimuli._stimulus.Stimulus._id_counter = 0 return experiment
_platform = platform try: if _platform == 'android': try: import android.mixer as mixer except ImportError: # old python-for-android version import android_mixer as mixer else: from pygame import mixer except: raise # init pygame sound mixer.pre_init(44100, -16, 2, 1024) mixer.init() mixer.set_num_channels(32) class SoundPygame(Sound): # XXX we don't set __slots__ here, to automaticly add # a dictionary. We need that to be able to use weakref for # SoundPygame object. Otherwise, it failed with: # TypeError: cannot create weak reference to 'SoundPygame' object # We use our clock in play() method. # __slots__ = ('_data', '_channel') @staticmethod def extensions(): if _platform == 'android': return ('wav', 'ogg', 'mp3', 'm4a')
def initialize(experiment=None): """Initialize an experiment. This initializes an experiment defined by 'experiment' as well as the underlying expyriment system. If 'experiment' is None, a new Experiment object will be created and returned. Furthermore, a screen, a clock, a keyboard and a event file are created and added to the experiment. The initialization screen is shown for a short delay to ensure that Python is fully initialized and time accurate. Afterwards, "Preparing experiment..." is presented on the screen. After experiment initialize the following additional properties are available: - experiment.screen -- the current screen - experiment.clock -- the main clock - experiment.keyboard -- the main keyboard - experiment.mouse -- the main mouse - experiment.event -- the main event file Parameters ---------- experiment : design.Experiment, optional the experiment to initialize Returns ------- exp : design.Experiment initialized experiment """ if experiment is None: experiment = design.Experiment() if experiment.log_level is None: experiment.set_log_level(defaults.event_logging) if is_interactive_mode() and not expyriment.control.defaults.window_mode \ and not hasattr(experiment, "testsuite"): print """ Python is running in an interactive shell but Expyriment wants to initialize a fullscreen.""" quest = "Do you want to switch to windows mode?" ans = raw_input(quest + " (Y/n) ").strip().lower() if ans == "" or ans == "y" or ans == "yes": print "Switched to windows mode" expyriment.control.defaults.window_mode = True stdout_logging = defaults.stdout_logging expyriment._active_exp = experiment old_logging = experiment.log_level experiment.set_log_level(0) # switch off for the first screens _keyboard.quit_key = defaults.quit_key _keyboard.pause_key = defaults.pause_key _keyboard.refresh_key = defaults.refresh_key _keyboard.end_function = end _keyboard.pause_function = pause mixer.pre_init(defaults.audiosystem_sample_rate, defaults.audiosystem_bit_depth, defaults.audiosystem_channels, defaults.audiosystem_buffer_size) if defaults.audiosystem_autostart: mixer.init() mixer.init() # Needed on some systems experiment._clock = misc.Clock() experiment._screen = Screen(colour=(0, 0, 0), open_gl=defaults.open_gl, window_mode=defaults.window_mode, window_size=defaults.window_size) # Hack for IDLE: quit pygame and call atexit functions when crashing if is_idle_running() and sys.argv[0] != "": try: import idlelib.run def wrap(orig_func): def newfunc(*a, **kw): pygame.quit() import atexit atexit._run_exitfuncs() idlelib.run.flush_stdout = orig_func return orig_func(*a, **kw) return newfunc idlelib.run.flush_stdout = wrap(idlelib.run.flush_stdout) except ImportError: pass experiment._data = None experiment._subject = None experiment._is_initialized = True # required before EventFile if old_logging > 0: experiment._events = EventFile( additional_suffix=experiment.filename_suffix, time_stamp=True) if stdout_logging: _set_stdout_logging(experiment._events) else: experiment._events = None experiment._keyboard = Keyboard() experiment._mouse = Mouse(show_cursor=False) logo = stimuli.Picture(misc.constants.EXPYRIMENT_LOGO_FILE, position=(0, 100)) logo.scale((0.7, 0.7)) text = stimuli.TextLine("Version {0}".format(expyriment.get_version()), text_size=14, text_colour=misc.constants.C_EXPYRIMENT_ORANGE, background_colour=(0, 0, 0), position=(0, 40)) canvas = stimuli.Canvas((600, 300), colour=(0, 0, 0)) canvas2 = stimuli.Canvas((600, 300), colour=(0, 0, 0)) logo.plot(canvas) text.plot(canvas) hash_ = expyriment.get_experiment_secure_hash() if hash_ is not None: txt = "{0} ({1})".format(os.path.split(sys.argv[0])[1], hash_) if len(expyriment._secure_hash.module_hashes_as_string()) > 0: txt += ", {0}".format( expyriment._secure_hash.module_hashes_as_string()) text2 = stimuli.TextLine( txt, text_size=14, text_colour=misc.constants.C_EXPYRIMENT_ORANGE, background_colour=(0, 0, 0), position=(0, 10)) text2.plot(canvas) canvas.preload(True) canvas._set_surface(canvas._get_surface().convert()) start = experiment.clock.time r = [x for x in range(256) if x % 5 == 0] stopped = False if defaults.initialize_delay > 0: for x in r: canvas._get_surface().set_alpha(x) canvas2.clear_surface() canvas.plot(canvas2) canvas2.present() experiment.clock.wait(1) key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: stopped = True break duration = experiment.clock.time - start if duration < 2000 and not stopped: start = experiment.clock.time while experiment.clock.time - start < 2000: key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: stopped = True break r = [x for x in range(256)[::-1] if x % 5 == 0] if not stopped: for x in r: canvas._get_surface().set_alpha(x) canvas2.clear_surface() canvas.plot(canvas2) canvas2.present() experiment.clock.wait(1) key = experiment.keyboard.check(pygame.K_ESCAPE, check_for_control_keys=False) if key is not None: break stimuli.TextLine("Preparing experiment...", text_size=24, text_colour=misc.constants.C_EXPYRIMENT_PURPLE).present() experiment._screen.colour = experiment.background_colour experiment.set_log_level(old_logging) stimuli._stimulus.Stimulus._id_counter = 0 return experiment
def __init__(self): mixer.init()