예제 #1
0
def main():
    #cause I don't want to pass these around
    global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER, MUSIC

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)

    WINDOW = ext.Window("Tetromino", size=(WINDOWWIDTH, WINDOWHEIGHT))
    REN = ext.Renderer(WINDOW)
    WINDOW.show()

    font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD)
    font_manager = ext.FontManager(font_file, size=18)

    #fontmanager=font_manager will be default_args passed to every sprite creation method
    SPRITE_FACTORY = ext.SpriteFactory(renderer=REN,
                                       fontmanager=font_manager,
                                       free=True)
    SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW)

    sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG)
    sdlmixer.Mix_OpenAudio(22050, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024)
    #BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg")

    showTextScreen("Tetromino")
    while True:
        if random.randint(0, 1) == 0:
            MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisb.mid")
        else:
            MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisc.mid")
        sdlmixer.Mix_PlayMusic(MUSIC, -1)
        runGame()
        sdlmixer.Mix_HaltMusic()
        showTextScreen("Game Over")
예제 #2
0
 def __init__(self, app):
     self.app = app
     # initialize audio
     sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG | sdlmixer.MIX_INIT_MOD)
     sdlmixer.Mix_OpenAudio(self.sample_rate, sdlmixer.MIX_DEFAULT_FORMAT,
                            2, 1024)
     self.reset()
     # sound callback
     # retain handle to C callable even though we don't use it directly
     self.sound_cb = ctypes.CFUNCTYPE(None,
                                      ctypes.c_int)(self.channel_finished)
     sdlmixer.Mix_ChannelFinished(self.sound_cb)
예제 #3
0
    def __enter__(self):
        LOG.debug('Initializing SDL context')
        sdl.SDL_Init(sdl.SDL_INIT_EVERYTHING)

        LOG.debug('Initializing SDL mixer')
        if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024):
            raise RuntimeError(
                'Cannot open mixed audio: {}'.format(sdlmixer.Mix_GetError()))

        if sdlmixer.Mix_Init(0) == -1:
            raise RuntimeError(
                'Cannot initialize mixer: {}'.format(sdlmixer.Mix_GetError()))

        return self
예제 #4
0
    def __init__(self, game):
        super(SoundMixer, self).__init__()
        self.mute = False
        self.sounds = []

        if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) != 0:
            raise RuntimeError("Cannot initialize audio system: {}".format(
                sdl2.SDL_GetError()))

        if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024):
            raise RuntimeError("Cannot open mixed audio: {}".format(
                sdlmixer.Mix_GetError()))

        if sdlmixer.Mix_AllocateChannels(64) != 64:
            raise RuntimeError("Cannot allocate audio channels: {}".format(
                sdlmixer.Mix_GetError()))
예제 #5
0
def init_sdl():
    initialized = getattr(init_sdl, "initialized", False)
    if initialized:
        return True

    sdl2.SDL_Init(0)
    sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_AUDIO)
    sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_JOYSTICK)
    sdl2.SDL_SetHint(sdl2.SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, b"1")

    result = (sdlmixer.Mix_OpenAudio(
        sdlmixer.MIX_DEFAULT_FREQUENCY,
        sdlmixer.MIX_DEFAULT_FORMAT,
        2,
        1024,
    ) != -1)
    if result:
        setattr(init_sdl, "initialized", True)
    return result
예제 #6
0
    def open(self, configuration):
        '''Open and configure audio system

        *Parameters:*

        - `configuration`: Configuration parameters from Application
        '''
        if sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_AUDIO) != 0:
            msg = "Cannot initialize audio system: %s" % sdl2.SDL_GetError()
            logger.critical(msg)
            raise SDL2Error(msg)

        if mixer.Mix_OpenAudio(mixer.MIX_DEFAULT_FREQUENCY,
                               mixer.MIX_DEFAULT_FORMAT, 2, 1024):
            msg = "Cannot open mixed audio: %s" % mixer.Mix_GetError()
            logger.critical(msg)
            raise SDL2Error(msg)

        mixer.Mix_AllocateChannels(configuration.audio_channel)

        logger.info("Audio initialized")
예제 #7
0
def main():
	#cause I don't want to pass these around
	global REN, SPRITE_FACTORY, SPRITE_RENDERER
	global CLICKEDBUTTON, BEEP1, BEEP2, BEEP3, BEEP4

	sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO|sdl2.SDL_INIT_AUDIO)

	window = sdl2.ext.Window("Simulate", size=(WINDOWWIDTH, WINDOWHEIGHT))
	REN = sdl2.ext.Renderer(window)
	REN.blendmode = sdl2.SDL_BLENDMODE_BLEND
	
	window.show()

	font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD)
	font_manager = sdl2.ext.FontManager(font_file, size=16)

	#fontmanager=font_manager will be default_args passed to every sprite creation method
	SPRITE_FACTORY = sdl2.ext.SpriteFactory(renderer=REN, fontmanager=font_manager, free=True)
	SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(window)



	sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG)
	sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024)
	BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg")
	BEEP2 = sdlmixer.Mix_LoadWAV(b"beep2.ogg")
	BEEP3 = sdlmixer.Mix_LoadWAV(b"beep3.ogg")
	BEEP4 = sdlmixer.Mix_LoadWAV(b"beep4.ogg")

	#channel = sdlmixer.Mix_PlayChannel(-1, BEEP1, 0)

	# Initialize some variables for a new game
	pattern = [] # stores the pattern of colors
	currentStep = 0 # the color the player must push next
	lastClickTime = 0 # timestamp of the player's last button push
	score = 0
	# when False, the pattern is playing. when True, waiting for the player to click a colored button:
	waitingForInput = False

	#directions text sprite
	info_text = make_text(SPRITE_FACTORY, "Match the pattern by clicking on the button or using the Q, W, A, S keys.", 10, WINDOWHEIGHT-25)

	CLICKEDBUTTON = []
	while True:
		REN.fill((0, 0, WINDOWWIDTH, WINDOWHEIGHT), BGCOLOR)
		drawButtons()

		score_text = make_text(SPRITE_FACTORY, "Score: "+str(score), WINDOWWIDTH - 100, 10)
		SPRITE_RENDERER.render([score_text, info_text])

		handle_events()

		if not waitingForInput:
			# play the pattern
			sdl2.SDL_Delay(1000)
			pattern.append(random.choice((YELLOW, BLUE, RED, GREEN)))
			for button in pattern:
				handle_events()
				flashButtonAnimation(button)
				sdl2.SDL_Delay(FLASHDELAY)
			waitingForInput = True
		else:
			# wait for the player to enter buttons
			if CLICKEDBUTTON and CLICKEDBUTTON[0] == pattern[currentStep]:
				# pushed the correct button
				flashButtonAnimation(CLICKEDBUTTON[0])
				currentStep += 1
				lastClickTime = sdl2.SDL_GetTicks()
				
				#could replace with collections.deque but premature optimizations and all that
				CLICKEDBUTTON.pop(0)

				if currentStep == len(pattern):
					# pushed the last button in the pattern
					changeBackgroundAnimation()
					score += 1
					waitingForInput = False
					currentStep = 0 # reset back to first step
					#CLICKEDBUTTON.clear() clear added in 3.3! ... I'm surprised it hasn't been there forever since it's way better than del l[:] or l[:] = []
					#and it parallels other collection clear functions
					del CLICKEDBUTTON[:]


			elif (CLICKEDBUTTON and CLICKEDBUTTON[0] != pattern[currentStep]) or (currentStep != 0 and sdl2.SDL_GetTicks() - TIMEOUT*1000 > lastClickTime):
				# pushed the incorrect button, or has timed out
				gameOverAnimation()
				# reset the variables for a new game:
				pattern = []
				#CLICKEDBUTTON.clear()
				del CLICKEDBUTTON[:]
				currentStep = 0
				waitingForInput = False
				score = 0
				sdl2.SDL_Delay(1000)
				changeBackgroundAnimation()


		sdl2.SDL_Delay(1000//FPS)

	
	shutdown()
예제 #8
0
 def __init__(self, model):
     sdlmixer.Mix_Init(0)
     sdlmixer.Mix_OpenAudio(22050, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024)
     self.model = model
     self.loadaudiofx()
     self.player = self.play()
예제 #9
0
except ImportError:
    import traceback
    traceback.print_exc()
    sys.exit(1)

if __name__ == "__main__":
    RESOURCES = sdl2.ext.Resources(__file__, "")

    # Audio
    if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) != 0:
        raise RuntimeError("Cannot initialize audio system: {}".format(
            sdl2.SDL_GetError()))

    # int Mix_OpenAudio(int frequency, Uint16 format, int channels, int chunksize)
    if sdlmixer.Mix_OpenAudio(44100, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024):
        raise RuntimeError("Cannot open mixed audio: {}".format(
            sdlmixer.Mix_GetError()))
    sdlmixer.Mix_AllocateChannels(64)

    sound_file = RESOURCES.get_path("Cat.wav")
    sample = sdlmixer.Mix_LoadWAV(sdl2.ext.compat.byteify(sound_file, "utf-8"))
    if sample is None:
        raise RuntimeError("Cannot open audio file: {}".format(
            sdlmixer.Mix_GetError()))

    count = 5
    while count > 0:
        channel = sdlmixer.Mix_PlayChannel(channel=-1, chunk=sample, loops=0)
        sdlmixer.Mix_SetPosition(channel, 270, 100)
        print(channel)