def test_set_num_channels(self): mixer.init() default_num_channels = mixer.get_num_channels() for i in range(1, default_num_channels + 1): mixer.set_num_channels(i) self.assertEqual(mixer.get_num_channels(), i)
def test_set_num_channels(self): mixer.init() for i in xrange_(1, mixer.get_num_channels() + 1): mixer.set_num_channels(i) self.assert_(mixer.get_num_channels() == i) mixer.quit()
def main(self): global player_health global player_score black = (0, 0, 0) clock = pygame.time.Clock() dt = 0 mixer.music.load("eye_of_the_tiger_8_bit.mp3") mixer.music.set_volume(0.8) mixer.music.play(-1) events = pygame.event.get() self.start_screen_sprites.update(events) self.start_screen_sprites.draw(self.screen) self.DefenceGun.draw(self.screen) while True: events = pygame.event.get() text = self.font.render('Score: ' + str(player_score), True, black) text2 = self.font.render('Health: ' + str(player_health), True, black) textRect = text.get_rect() textRect2 = text2.get_rect() textRect.center = (650, 500) textRect2.center = (650, 530) if mixer.get_num_channels() > 1: mixer.music.set_volume(0.3) if mixer.get_num_channels() == 1: mixer.music.set_volume(0.8) for e in events: if e.type == pygame.QUIT: return if e.type == pygame.MOUSEBUTTONDOWN: self.button_detect() if e.type == pygame.KEYDOWN: self.started = True if not self.started: self.start_screen_sprites.update(events) self.start_screen_sprites.draw(self.screen) self.DefenceGun.draw(self.screen) elif self.started and player_health > 0: self.sprites.update(events) self.collision_detect() self.sprites.draw(self.screen) self.DefenceGun.draw(self.screen) else: self.end_screen_sprites.update(events) self.end_screen_sprites.draw(self.screen) self.DefenceGun.draw(self.screen) self.screen.blit(self.reset.image, self.reset.rect) self.screen.blit(text, textRect) self.screen.blit(text2, textRect2) self.screen.blit(self.quit.image, self.quit.rect) pygame.display.update() dt = clock.tick(60)
def test_set_reserved(self): # __doc__ (as of 2008-08-02) for pygame.mixer.set_reserved: # pygame.mixer.set_reserved(count): return count mixer.init() default_num_channels = mixer.get_num_channels() # try reserving all the channels result = mixer.set_reserved(default_num_channels) self.assertEqual(result, default_num_channels) # try reserving all the channels + 1 result = mixer.set_reserved(default_num_channels + 1) # should still be default self.assertEqual(result, default_num_channels) # try unreserving all result = mixer.set_reserved(0) # should still be default self.assertEqual(result, 0) # try reserving half result = mixer.set_reserved(int(default_num_channels / 2)) # should still be default self.assertEqual(result, int(default_num_channels / 2))
def test_get_num_channels__defaults_eight_after_init(self): mixer.init() num_channels = mixer.get_num_channels() self.assert_(num_channels == 8) mixer.quit()
def set_volume(self, volume=None): "set_volume( volume): sets volume of all channels (0-1)" if volume is None: volume = self.volume else: self.volume = volume for i in range(mixer.get_num_channels()): mixer.Channel(i).set_volume(volume)
def is_playing(sound): #check if sound is already playing on any channels ch = 0 sound_list = [] for _ in range(mixer.get_num_channels()): sound_list.append(mixer.Channel(ch).get_sound()) ch += 1 if not sound in sound_list: return False
def initialize(): passed, failed = PG.init() # make sure nothing weird is going on if failed > 0: print "ERROR: %d Pygame modules failed to initialize" % failed PG.quit() G.Globals.SCREEN = PD.set_mode((800, 650)) PD.set_caption("Gamma Ray Kitten") G.Globals.WIDTH = G.Globals.SCREEN.get_width() G.Globals.HEIGHT = G.Globals.SCREEN.get_height() - G.Globals.HUD_HEIGHT G.Globals.STATE = Title.Title() G.Globals.AMB_MUSIC = PX.Sound("sounds/menu.ogg") if PX.get_num_channels() >= 2: G.Globals.FX_CHANNEL = PX.Channel(0) G.Globals.MUSIC_CHANNEL = PX.Channel(1) else: G.Globals.FX_CHANNEL = PX.Channel(0) G.Globals.MUSIC_CHANNEL = PX.Channel(0)
def __init__(self, screen_size_, channels_: int = 8): """ :param screen_size_: pygame.Rect; Size of the active display :param channels_ : integer; number of channels to reserved for the sound controller :return : None """ if not isinstance(screen_size_, pygame.Rect): raise ValueError( "\n screen_size_ argument must be a pygame.Rect type, got %s " % type(screen_size_)) if not isinstance(channels_, int): raise ValueError( "\n channels_ argument must be a integer type, got %s " % type(channels_)) assert channels_ >= 1, "\nArgument channel_num_ must be >=1" if pygame.mixer.get_init() is None: raise ValueError( "\nMixer has not been initialized." "\nUse pygame.mixer.init() before starting the Sound controller" ) self.channel_num = channels_ # channel to init self.start = mixer.get_num_channels( ) # get the total number of playback channels self.end = self.channel_num + self.start # last channel mixer.set_num_channels( self.end) # sets the number of available channels for the mixer. mixer.set_reserved( self.end) # reserve channels from being automatically used self.channels = [ mixer.Channel(j + self.start) for j in range(self.channel_num) ] # create a channel object for controlling playback self.snd_obj = [None ] * self.channel_num # list of un-initialised objects self.channel = self.start # pointer to the bottom of the stack self.all = list(range( self.start, self.end)) # create a list with all channel number self.screen_size = screen_size_ # size of the display (used for stereo mode)
def test_find_channel(self): # __doc__ (as of 2008-08-02) for pygame.mixer.find_channel: # pygame.mixer.find_channel(force=False): return Channel # find an unused channel mixer.init() filename = example_path(os.path.join("data", "house_lo.wav")) sound = mixer.Sound(file=filename) num_channels = mixer.get_num_channels() if num_channels > 0: found_channel = mixer.find_channel() self.assertIsNotNone(found_channel) # try playing on all channels channels = [] for channel_id in range(0, num_channels): channel = mixer.Channel(channel_id) channel.play(sound) channels.append(channel) # should fail without being forceful found_channel = mixer.find_channel() self.assertIsNone(found_channel) # try forcing without keyword found_channel = mixer.find_channel(True) self.assertIsNotNone(found_channel) # try forcing with keyword found_channel = mixer.find_channel(force=True) self.assertIsNotNone(found_channel) for channel in channels: channel.stop() found_channel = mixer.find_channel() self.assertIsNotNone(found_channel)
def run_game(): # Game parameters SCREEN_WIDTH, SCREEN_HEIGHT = 400, 400 BG_COLOR = 150, 150, 80 CREEP_FILENAMES = [ 'bonehunter2.png', 'skorpio.png', 'tuma.png', 'skrals.png', 'stronius1.png', 'stronius2.png', 'metus_with_guards.png'] TOWER_FILENAMES = [ 'matanui.png', 'malum.png', 'gresh.png', 'gelu.png', 'vastus.png', 'kiina.png', 'ackar.png', 'straak.png' ] SELL_FILENAME = 'Sell.png' RAIN_OF_FIRE = 'rain_of_fire.png' TORNADO = 'tornado.png' TORNADO_BIG = 'tornado_big.png' BUTTON_FILENAMES = TOWER_FILENAMES + [RAIN_OF_FIRE, TORNADO, SELL_FILENAME] money = 643823726935627492742129573207 mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag mixer.init() mixer.set_num_channels(30) print "mix", mixer.get_num_channels() EXPLOSIONS = [mixer.Sound('expl%d.wav'%(i)) for i in range(1, 7)] FIRING = [mixer.Sound('fire%d.wav'%(i)) for i in range(1, 4)] N_CREEPS = 10 N_TOWERS = 0 pygame.init() background = pygame.image.load("Background_Level_1.JPG") path = pygame.image.load("Path_Level_1.png") w, h = background.get_size() assert (w, h) == path.get_size() sc = min(1024.0/w, 1024.0/h) background = pygame.transform.scale(background, (int(w * sc), int(h * sc))) path = pygame.transform.scale(path, (int(w * sc), int(h * sc))) backgroundRect = background.get_rect() starts, paths = create_paths(path) screen = pygame.display.set_mode( background.get_size(), 0, 32) clock = pygame.time.Clock() # Create N_CREEPS random creeps. creeps = [] for i in range(N_CREEPS): creeps.append(Creep(screen, choice(CREEP_FILENAMES), ( choice(starts)[::-1]), # reversed x and y ( choice([-1, 1]), choice([-1, 1])), 0.05, paths, choice(EXPLOSIONS))) towers = [Tower(screen, choice(TOWER_FILENAMES), (randint(0, background.get_size()[0]), randint(0, background.get_size()[1])), (1, 1), 0.0, paths, radius=100, max_attacks=3, firing_sounds=FIRING) for i in range (N_TOWERS)] buttons = [Button(screen, buttonfile, (randint(0, background.get_size()[0]), randint(0, background.get_size()[1])), (1, 1), 0.0, paths) for buttonfile in BUTTON_FILENAMES] rightedge = screen.get_width() - 5 for b in buttons[:len(buttons)//2]: b.pos = vec2d(rightedge - b.image.get_width() / 2, 5 + b.image.get_height() / 2) rightedge -= (b.image.get_width() + 5) rightedge = screen.get_width() - 5 for b in buttons[len(buttons)//2:]: b.pos = vec2d(rightedge - b.image.get_width() / 2, 5 + b.image.get_height() / 2 + 80) rightedge -= (b.image.get_width() + 5) next_tower = TOWER_FILENAMES[0] cursor = Cursor(screen, TOWER_FILENAMES[0], (0, 0), (1,1), 0.0, paths) font = pygame.font.SysFont(pygame.font.get_default_font(), 20, bold=True) tornado_img = pygame.image.load(TORNADO_BIG).convert_alpha() global_attacks = GlobalAttacks(screen, RAIN_OF_FIRE, (randint(0, background.get_size()[0]), randint(0, background.get_size()[1])), (1, 1), 0.0, paths, radius=100, max_attacks=3, firing_sounds=FIRING) # The main game loop # rect = pygame.Rect((1, 1), (10, 10)) selling = False while True: # Limit frame speed to 50 FPS # time_passed = clock.tick(50) for event in pygame.event.get(): if event.type == pygame.QUIT: exit_game() if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: rect.center = pygame.mouse.get_pos() collided = False for b in buttons: if b.rect.colliderect(rect): selling = (b.img_filename == SELL_FILENAME) rain_of_fire = (b.img_filename == RAIN_OF_FIRE) tornado = (b.img_filename == TORNADO) buying = (not selling) and (not rain_of_fire) if buying: next_tower = BUTTON_FILENAMES[buttons.index(b)] cursor.change_image(next_tower) # only one rain of fire available at a time if global_attacks.ready(): if rain_of_fire: for i in range(20): target = choice(creeps) global_attacks.target(target) if tornado: targets = [choice(creeps) for i in range(20)] tornado_attack = MobileAttack(global_attacks, targets, img=tornado_img, pos=vec2d((randint(0, background.get_size()[0]), randint(0, background.get_size()[1])))) tornado_attack.speed = 0.2 global_attacks.add_attack(tornado_attack) collided = True for t in towers: if cursor.rect.colliderect(t.rect): collided = t if not collided and not selling and money >= 100: towers += [Tower(screen, next_tower, pygame.mouse.get_pos(), (1, 1), 0.0, paths, radius=100, max_attacks=3, firing_sounds=FIRING)] money -= 100 if selling and collided: if collided in towers: towers.remove(collided) money += 50 # Redraw the background screen.blit(background, backgroundRect) # Update and redraw all creeps for creep in creeps: creep.update(time_passed) for tower in towers: tower.attack(creeps) money += tower.update(time_passed) money += global_attacks.update(time_passed) cursor.update() for obj in creeps + towers + buttons + [global_attacks, cursor]: obj.blitme() money_text = font.render("%d"%(money), True, pygame.Color(0, 0, 0)) screen.blit(money_text, (rightedge - 5 - money_text.get_width(), 5 + money_text.get_height())) pygame.display.flip()
def test_get_num_channels__defaults_eight_after_init(self): mixer.init() self.assertEqual(mixer.get_num_channels(), 8)