python游戏入门儿童版四,学习加入音效并完成一个游戏

文章介绍了如何在Pygame游戏中添加背景音乐和事件音效,通过加载音乐文件并使用pygame.mixer模块进行播放。同时,为了解决图形更新的残留问题,文章提出了使用screen.fill((0,0,0))清空屏幕的方法,使得图形更新更加干净,代码更简洁。通过这些技巧,可以提升游戏体验并简化编程过程。
摘要由CSDN通过智能技术生成

上一节我们已经可以通过键盘来操控了游戏了

这一节我们加入了背景音乐和事件音效,让游戏更生动

音乐素材已经提供,可自行下载

更多的音乐素材可以去:游戏音效素材_游戏mp3音效下载_熊猫办公

先来看看完整代码:

import pygame
import random

# 画小车的函数
def bus(x, y, color):
    pygame.draw.rect(screen, color, (x, y, 100, 50), 0)  # 画一个长方形做车体
    pygame.draw.circle(screen, color, [x + 15, y + 65], 15)  # 画一个圆形做轮胎
    pygame.draw.circle(screen, color, [x + 85, y + 65], 15)  # 画一个圆形做轮胎

pygame.init()  # 初始化
screen = pygame.display.set_mode((800, 600))  # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")


pygame.mixer.init()# 初始化
track = pygame.mixer.music.load(r'充满童趣的幽默节奏.mp3')# 加载音乐文件
pygame.mixer.music.play()# 开始播放音乐流
hh = pygame.mixer.Sound(r'jie.mp3')
mm = pygame.mixer.Sound(r'mm.mp3')

fclock = pygame.time.Clock()  # 控制时间间隔

x = 300
y = 500
color = 155, 155, 155
going = True
apple_x = 200
apple_y = 0
apple_count = 0

myfont = pygame.font.Font(None, 50)
textImage = myfont.render('apple:' + str(apple_count), True, (255, 255, 255))
screen.blit(textImage, (50, 50))

while going:
    bus(x, y, (0, 0, 0)) #删除之前的小车
    pygame.draw.circle(screen, (0, 0, 0), [apple_x, apple_y], 15)  # 删除掉之前的圆
    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going = False
        if event.type == pygame.KEYDOWN: #判断获取时间是否为按钮按下的类型
            if event.key == pygame.K_RIGHT: #判断是否按了右方向键
                mm.play()
                x += 50
            if event.key == pygame.K_LEFT:  #判断是否按了左方向键
                mm.play()
                x -= 50

    bus(x, y, color)

    apple_y += 1
    if (apple_y>600):#当圆超出界面之后从上边继续掉下来
        apple_y = 0
        apple_x = random.randint(1, 800)

    #更改分数
    myfont = pygame.font.Font(None, 50)
    if(apple_x>x and apple_x<x+100 and apple_y>y-15 and apple_y<y+80):
        apple_x = random.randint(1, 800)
        apple_y = 0
        textImage = myfont.render('apple:' + str(apple_count), True, (0, 0, 0))
        screen.blit(textImage, (50, 50))
        apple_count += 1
        hh.play()
    # 更改分数   
    myfont = pygame.font.Font(None, 50)
    textImage = myfont.render('apple:' + str(apple_count), True, (255, 255, 255))
    screen.blit(textImage, (50, 50))

    pygame.draw.circle(screen, (255, 0, 0), [apple_x, apple_y], 15)  # 画一个圆形做苹果

    pygame.display.update()
    fclock.tick(200)

pygame.quit()

现在发现一个新的问题,我们之前更新图形或移动图形都是使用用黑色颜色在原来基础上再画一个,但是发现这种覆盖不能清除的很干净,看看文字中变动的分数,似乎留有残影。

这次我们使用新的办法,就是再画一个黑色的画布:screen.fill((0,0,0)),相当于清空界面,这样就可以覆盖调所有,所有的图形也不需再重复画一次来覆盖了,用这个方法之后,我们的代码就更加简洁:

import pygame
import random

# 画小车的函数
def bus(x, y, color):
    pygame.draw.rect(screen, color, (x, y, 100, 50), 0)  # 画一个长方形做车体
    pygame.draw.circle(screen, color, [x + 15, y + 65], 15)  # 画一个圆形做轮胎
    pygame.draw.circle(screen, color, [x + 85, y + 65], 15)  # 画一个圆形做轮胎

pygame.init()  # 初始化
screen = pygame.display.set_mode((800, 600))  # 设置窗口的大小
pygame.display.set_caption("这是一个给我们画画用的窗口")

#加入声音
pygame.mixer.init()# 初始化
track = pygame.mixer.music.load(r'充满童趣的幽默节奏.mp3')# 游戏背景音乐
pygame.mixer.music.play()# 开始播放音乐流
hh = pygame.mixer.Sound(r'jie.mp3') #借助的音效
mm = pygame.mixer.Sound(r'mm.mp3') #移动车子的音效

fclock = pygame.time.Clock()  # 控制时间间隔

x = 300
y = 500
color = 155, 155, 155
apple_x = 200
apple_y = 0
apple_count = 0
going = True

while going:
    screen.fill((0,0,0))#将画布设置成黑色并清空所有

    for event in pygame.event.get():  # 遍历事件
        if event.type == pygame.QUIT:  # 退出事件
            going = False
        if event.type == pygame.KEYDOWN: #判断获取时间是否为按钮按下的类型
            if event.key == pygame.K_RIGHT: #判断是否按了右方向键
                mm.play()
                x += 50
            if event.key == pygame.K_LEFT:  #判断是否按了左方向键
                mm.play()
                x -= 50

    #画小车
    bus(x, y, color)

    #画苹果
    apple_y += 1
    if (apple_y>600):#当圆超出界面之后从上边继续掉下来
        apple_y = 0
        apple_x = random.randint(1, 800)
    pygame.draw.circle(screen, (255, 0, 0), [apple_x, apple_y], 15)  # 画一个圆形做苹果

    #当车子接主苹果时,通过苹果和车子的位置以及他们的大小来判断他们是否碰撞
    if(apple_x>x and apple_x<x+100 and apple_y>y-15 and apple_y<y+80):
        apple_x = random.randint(1, 800)
        apple_y = 0
        apple_count += 1
        hh.play()

    #显示文字,显示分数
    myfont = pygame.font.Font(None, 50)
    textImage = myfont.render('apple:' + str(apple_count), True, (255, 255, 255))
    screen.blit(textImage, (50, 50))

    pygame.display.update()
    fclock.tick(200)

pygame.quit()

最后我们来看看效果

现在我们可以根据自己的需求来做一些自己喜欢的小游戏了

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boyxgb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值