与小卡特一起学python 第19章 声音

本文介绍了使用Python的Pygame库来处理游戏中的声音和音乐效果。内容包括基本的声音播放、音量调节、等待歌曲结束的功能实现,以及一个完整的PyPong游戏案例,展示了如何在游戏中集成多种声音效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#19章 声音
#19-1 播放声音
import pygame,sys
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([640,480])
pygame.time.delay(1000)

splat = pygame.mixer.Sound("bg_music.mp3")
splat.play()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#19章 声音
#19-2 播放声音
import pygame,sys
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([640,480])
pygame.time.delay(1000)

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.play()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#19-3 音量调节
import pygame,sys
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([640,480])
pygame.time.delay(1000)

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.set_volume(0.30)
pygame.mixer.music.play()
splat = pygame.mixer.Sound("splat.wav")
splat.set_volume(0.50)
splat.play()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
pygame.quit()

#19-4等待歌曲结束
import pygame,sys
pygame.init()
pygame.mixer.init()

screen = pygame.display.set_mode([640,480])
pygame.time.delay(1000)

pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.set_volume(0.30)
pygame.mixer.music.play(2)#播放次数 -1是永远重复
splat = pygame.mixer.Sound("splat.wav")
splat.set_volume(0.50)
splat.play()
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    if not pygame.mixer.music.get_busy():
        splat.play()
        pygame.time.delay(1000)
        running = False
pygame.quit()


#19-5有声音和音乐的PyPong代码

import pygame,sys

#Ball 类定义
class MyballClass(pygame.sprite.Sprite):
    def __init__(self,image_file,speed,location=[0,0]):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load (image_file)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location
        self.speed = speed
      
    def move(self):#移动球,在顶边和左右两边反弹
        global points,score_text
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right > screen.get_width():
            self.speed[0] = -self.speed[0]
            if self.rect.top < screen.get_height():
                hit_wall.play()

        if self.rect.top <= 0 :
            self.speed[1] = -self.speed[1]
            points = points + 1
            score_text = font.render(str(points),1,(0,0,0))
            get_point.play()
            
#球拍类定义
class MyPaddleClass(pygame.sprite.Sprite):
    def __init__(self,location =[0,0]):
        pygame.sprite.Sprite.__init__(self)
        image_surface = pygame.surface.Surface([100,20])
        image_surface.fill([0,0,0])
        self.image = image_surface.convert()
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = location

#初始化Pygame时钟球和球拍
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("bg_music.mp3")
pygame.mixer.music.set_volume(0.30)
pygame.mixer.music.play(-1)#播放次数 -1是永远重复
hit = pygame.mixer.Sound("hit_paddle.wav")
hit.set_volume(0.50)
new_life = pygame.mixer.Sound("new_life.wav")
new_life.set_volume(0.50)
splat = pygame.mixer.Sound("splat.wav")
splat.set_volume(0.60)
hit_wall = pygame.mixer.Sound("hit_wall.wav")
hit_wall.set_volume(0.4)

get_point = pygame.mixer.Sound("get_point.wav")
hit_wall.set_volume(0.2)
bye = pygame.mixer.Sound("game_over.wav")
bye.set_volume(0.6)

screen = pygame.display.set_mode([640,480])
clock = pygame.time.Clock()
#ball_speed = [10,5]
myBall = MyballClass('wackyball.bmp',[12,6],[50,50])
ballGroup = pygame.sprite.Group(myBall)
paddle = MyPaddleClass([270,400])
lives = 3
points = 0
font = pygame.font.Font(None,50)
score_text= font.render(str(points),1,(0,0,0))
textpos = [10,10]
done = False
running = True
while running:
    clock.tick(30)
    screen.fill([255,255,255])
    for event in pygame.event.get():
        if event.type ==pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEMOTION:#鼠标移动则移动球拍
            paddle.rect.centerx = event.pos[0]

    if pygame.sprite.spritecollide(paddle,ballGroup,False):
        hit.play()
        myBall.speed[1] = -myBall.speed[1]
    myBall.move()#移动球
    if not done:
        screen.blit(myBall.image,myBall.rect)
        screen.blit(paddle.image,paddle.rect)
        screen.blit(score_text,textpos)
        for i in range (lives):
            width = screen.get_width()
            screen.blit(myBall.image,[width - 40 * i, 20])
        pygame.display.flip()
        
    if myBall.rect.top >= screen.get_rect().bottom:
        if not done:
            splat.play()
        lives = lives -1
        if lives <= 0:
            if not done:
                pygame.time.delay(1000)
                bye.play()
            final_text1 = "Game Over"
            final_text2 = "Your final score is :" + str(points)
            ft1_font = pygame.font.Font(None,70)
            ft1_surf = ft1_font.render(final_text1,1,(0,0,0))
            ft2_font = pygame.font.Font(None,50)
            ft2_surf = ft2_font.render(final_text2,1,(0,0,0))
            screen.blit(ft1_surf,[screen.get_width()/2 - ft1_surf.get_width()/2,100])
            screen.blit(ft2_surf,[screen.get_width()/2 - ft2_surf.get_width()/2,200])
            pygame.display.flip()
            done = True
            pygame.mixer.music.fadeout(2000)
        else:
            pygame.time.delay(20000)
            new_life.play()
            myBall.rect.topleft = [50,50]
            screen.blit(myBall.image,myBall.rect)
            pygame.display.flip()
            pygame.time.delay(1000)
            

pygame.quit()


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2079352/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220205/viewspace-2079352/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值