上一节我们已经可以通过键盘来操控了游戏了
这一节我们加入了背景音乐和事件音效,让游戏更生动
音乐素材已经提供,可自行下载
更多的音乐素材可以去:游戏音效素材_游戏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()
最后我们来看看效果
现在我们可以根据自己的需求来做一些自己喜欢的小游戏了