ʹÓõÄÊÇpygame.mixer.SoundÀàÀ´¼ÓÔغ͹ÜÀíÒôƵÎļþ£¬pygameÖ§³ÖÁ½ÖÖÒôƵÎļþ£ºÎ´Ñ¹ËõµÄWAVºÍOGGÒôƵÎļþ£¬Èç¹ûÒª²¥·Å³¤Ê±¼äµÄÒôÀÖ£¬ÎÒÍƼöÄãʹÓÃOGG¸ñʽÒôƵÎļþ£¬ÒòΪËüµÄÌå»ý±È½ÏС£¬Êʺϳ¤Ê±¼äµÄ¼ÓÔغͲ¥·Å¡£µ±ÄãÒª²¥·Å±È½Ï¶ÌµÄÒôƵµÄʱºò¿ÉÒÔÑ¡ÔñWAV¡£
hit_au = pygame.mixer.Sound()
3.²¥·ÅÒôÀÖ£º
ÉÏÃæµÄpygame.mixer.Soundº¯Êý·µ»ØÁËÒ»¸ösound¶ÔÏó£¬ÎÒÃÇ¿ÉÒÔʹÓÃplayºÍstop·½·¨À´²¥·ÅºÍÍ£Ö¹²¥·ÅÒôÀÖ¡£
µ«ÊÇÕâÀïÎÒÃǽéÉÜÒ»ÖÖ¸üΪ¸ß¼¶µÄÓ÷¨£¬Ê¹ÓÃpygame.mixer.Channel£¬Õâ¸öÀàÌṩÁ˱Èsound¶ÔÏó¸üΪ·á¸»µÄ¹¦ÄÜ¡£
Ê×ÏÈÎÒÃÇÏÈÉêÇëÒ»¸ö¿ÉÓõÄÒôƵƵµÀ£º
channel = pygame.mixer.find_channel(True)
Ò»µ©ÓÐÁËƵµÀÖ®ºóÎÒÃǾͿÉÒÔʹÓÃChannel.play()·½·¨À´²¥·ÅÒ»¸ösound¶ÔÏóÁË¡£
channel.play(sound)
ºÃÁËÏÖÔÚÈÃÎÒÃÇÀ´ÊµÏÖһϺÍÒôƵÓйصÄÄ£¿é£º
Ê×Ïȶ¨ÒåÒ»¸ö³õʼ»¯µÄº¯Êý£¬Ëü³õʼ»¯ÁËÒôƵģ¿é£¬²¢ÇÒ¼ÓÔØÁËһЩÒôƵÎļþÒÔ·½±ãÎÒÃÇÔÚ³ÌÐòÖÐʹÓãº
def audio_init(): global hit_au,btn_au,bg_au,bullent_au pygame.mixer.init() hit_au = pygame.mixer.Sound() btn_au = pygame.mixer.Sound() bg_au = pygame.mixer.Sound() bullent_au = pygame.mixer.Sound()
È»ºóÎÒÃÇʵÏÖÁËÒ»¸öMusicÀ࣬Õâ¸öÀà¿ÉÒÔ¿ØÖÆÉùÒôµÄ²¥·ÅºÍÔÝÍ££¨set_volumeº¯ÊýÊÇÓÃÀ´ÉèÖÃÒôÀÖÉùÒô´óСµÄ£©£º
class Music(): def __init__(self,sound): self.channel = None self.sound = sound def play_sound(self): self.channel = pygame.mixer.find_channel(True) self.channel.set_volume(0.5) self.channel.play(self.sound) def play_pause(self): self.channel.set_volume(0.0) self.channel.play(self.sound)
ÌøÔ¾º¯Êý£º
µ±°´Ï¿ոñ¼üµÄʱºò£¬à»´óß÷»áÌøÆð£¬Õâ¸öÊÇÈçºÎʵÏÖµÄÄØ£¿
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() keys = pygame.key.get_pressed() if keys[K_ESCAPE]: pygame.quit() sys.exit() elif keys[K_SPACE]: if not player_jumping: player_jumping = True jump_vel = -12.0
µ±°´Ï¿ոñ¼üµÄʱºò£¬»á½«player_jumping±äÁ¿ÖÃΪTrue ²¢ÇÒ¸øjump_velÒ»¸ö³õËÙ¶È-12.0
È»ºóÔÚÿ´ÎÑ»·µÄʱºò£¬½«jump_vel ¼Ó0.6£¬µ±à»´óß÷»Øµ½ÆðÌøλÖõÄʱºò£¬½«ËÙ¶ÈÖÃΪ0£¬Ê¹ÈËÎï²»ÔÙÔÚy·½ÏòÉÏÓÐÒƶ¯¡£
player_jumping: if jump_vel <0: jump_vel += 0.6 elif jump_vel >= 0: jump_vel += 0.8 player.Y += jump_vel if player.Y > player_start_y: player_jumping = False player.Y = player_start_y jump_vel = 0.0
È»ºóÎÒÃÇ»¹ÐèÒªÒ»¸ö²»¶Ï·¢³öµÄ×Óµ¯£º
game_over: arrow.X -= arrow_vel if arrow.X < -40: reset_arrow()
reset_arrow(): y = random.randint(270,350) arrow.position = 800,y bullent_sound.play_sound()
¹ØÓÚà»´óß÷ºÍ×Óµ¯³åÍ»¼ì²âÎÒÃÇʹÓÃÁË֮ǰѧ¹ýµÄ¾ØÐγåÍ»¼ì²â¼¼Êõ£¬µ±Íæ¼ÒºÍ×Óµ¯²úÉú³åÍ»µÄʱºò£¬ÖØÖÃ×Óµ¯£¬²¥·Å±¬Õ¨¶¯»£¬È»ºó½«ÈËÎïµÄx×ø±êÖµÏò×óÒƶ¯10£¬ÒÔ±íʾÈËÎïÊܵ½É˺¦¡£¶ñÁúºÍ×Óµ¯µÄ³åÍ»ºÍÕâ¸öÊÇÒ»ÑùµÄ£¬ÕâÀï¾Í²»ÔÙ׸ÊöÁË¡£
pygame.sprite.collide_rect(arrow, player): reset_arrow() explosion.position =player.X,player.Y player_hit = True hit_sound.play_sound() if p_first: group_exp.add(explosion) p_first = False player.X -= 10
È»ºóÎÒÃÇ»¹ÐèÒª¿¼ÂÇÒ»ÏÂÍæ¼Ò±»¶ñÁú×·ÉϵÄʱºòµÄÇéÐΣ¬»¹ÊÇÓ¦ÓþØÐμì²â¼¼Êõ£º
if pygame.sprite.collide_rect(player, dragon): game_over = True
ΪÁËʹ¹ûʵÒƶ¯£¬ÎÒÃÇÐèÒª±éÀúgroup_fruitÀïÃæµÄ¹ûʵ£¬È»ºóÒÀ´Î½«ËûÃÇ×óÒÆ5¸öµ¥Î»£¬È»ºóÎÒÃÇ»¹ÐèÒªÅжÏÍæ¼Ò³Ôµ½¹ûʵµÄ³¡¾°£¬¹ûʵ»áÏûʧ£¬È»ºóÍæ¼ÒµÄ»ý·ÖÔö¼Ó¡£
ÕâÀïʹÓÃÁË֮ǰѧ¹ýµÄpygame.sprite.spritecollide(sprite,sprite_group,bool)¡£
µ÷ÓÃÕâ¸öº¯ÊýµÄʱºò£¬Ò»¸ö×éÖеÄËùÓо«Á鶼»áÖð¸öµØ¶ÔÁíÍâÒ»¸öµ¥¸ö¾«Áé½øÐгåÍ»¼ì²â£¬·¢Éú³åÍ»µÄ¾«Áé»á×÷Ϊһ¸öÁÐ±í·µ»Ø¡£
¡¡