【汇智学堂】-python小游戏(太空阻击之九-添加游戏声音与积分)

5.9添加游戏声音与积分
最后,我们将游戏的声音及用户积分添加进来。下面是我们要做的事情:
1、为游戏添加声音
2、为游戏添加积分

添加声音的代码

pygame.mixer.music.load(‘resources/audio/moonlight.wav’)
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)

每次打中飞碟,就加100分。更新分数。代码如下:
score=0
if enemyrect.colliderect(bulletrect):
score+=100
font1 = pygame.font.SysFont(‘simsunnsimsun’, 16, True)
surface1 = font1.render(u’当前得分:’+str(score), True, [255, 0, 0])
screen.blit(surface1, [20, 20])

将代码整合起来,整合后完整代码如下:

import pygame
from pygame.locals import *
from sys import exit
import math
import random

#添加背景音乐与得分

screen_width, screen_height = 480, 700
screen=pygame.display.set_mode((screen_width, screen_height))
background=pygame.image.load(“resources/images/background2.png”)
pygame.display.set_caption(‘雷雷的太空大战’)
pygame.init()

running=1
flyingsaucers=[]
enemy = pygame.image.load(“resources/images/flyingsaucer.png”)

b=0

bullet = pygame.image.load(“resources/images/bullet1.png”)
player = pygame.image.load(“resources/images/hero1.png”)

positionPlayer=[100,400]
positionBullet=[positionPlayer[0]+50,positionPlayer[1]+40]
bullets=[]

keys = [False, False, False, False]

position=[]

pygame.mixer.music.load(‘resources/audio/moonlight.wav’)
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)

score=0

while running:
screen.blit(background,(0,0))
#screen.blit(player,positionPlayer)

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key==K_w:
            keys[0]=True
        elif event.key==K_a:
            keys[1]=True
        elif event.key==K_s:
            keys[2]=True
        elif event.key==K_d:
            keys[3]=True
    if event.type == pygame.KEYUP:
        if event.key==pygame.K_w:
            keys[0]=False
        elif event.key==pygame.K_a:
            keys[1]=False
        elif event.key==pygame.K_s:
            keys[2]=False
        elif event.key==pygame.K_d:
            keys[3]=False               
                   
    if event.type == pygame.QUIT:
        pygame.quit()
        exit()

    if event.type==pygame.MOUSEBUTTONDOWN:
        position = pygame.mouse.get_pos()
        angle = math.atan2(position[1]-(positionPlayer[1]+50),\

position[0]-(positionPlayer[0]+40))
bullets.append([angle,positionPlayer[0]+50,positionPlayer[1]+40])

if keys[0]:
    positionPlayer[1]-=10
    if positionPlayer[1]<=10:
        positionPlayer[1]=10
elif keys[2]:
    positionPlayer[1]+=10
    if positionPlayer[1]>=600:
        positionPlayer[1]=600
if keys[1]:
    positionPlayer[0]-=10
    if positionPlayer[0]<=10:
        positionPlayer[0]=10
elif keys[3]:
    positionPlayer[0]+=10
    if positionPlayer[0]>=380:
        positionPlayer[0]=380

#screen.blit(player,positionPlayer)

#旋转飞机
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(positionPlayer[1]+50),position[0]-(positionPlayer[0]+40))
#print(angle*57.29)
playerrot = pygame.transform.rotate(player,360-angle*57.29)    
screen.blit(playerrot, positionPlayer)

#射出子弹
for bulletx in bullets:        
    velx=math.cos(bulletx[0])*20
    vely=math.sin(bulletx[0])*20
    #print(velx)
    #print(vely)
    bulletx[1]+=velx
    bulletx[2]+=vely

    index=0

    if bulletx[1]<0 or bulletx[1]>480 or bulletx[2]<0 or bulletx[2]>700:
        bullets.pop(index)
        
    index+=1
    
    bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
    screen.blit(bulletrot,(bulletx[1],bulletx[2]))

    bulletrect=pygame.Rect(bulletrot.get_rect())
    bulletrect.left=bulletx[1]
    bulletrect.top=bulletx[2]
	
    #if enemyrect.colliderect(bulletrect):
        #print('打中飞碟')
     
if b<5:
    flyingsaucers.append([random.randint(0,400),random.randint(20,100)])
b+=1

#击落飞碟

index=0    

for flyingsaucer in flyingsaucers:
    if flyingsaucer[1]<610:
        flyingsaucer[1]+=0.5
    screen.blit(enemy,flyingsaucer)

    enemyrect=pygame.Rect(enemy.get_rect())
    enemyrect.left=flyingsaucer[0]
    enemyrect.top=flyingsaucer[1]

    indexbull=0

    for bulletx in bullets:

        #print(len(bullets))
        
        bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
        bulletrect=pygame.Rect(bulletrot.get_rect())
        bulletrect.left=bulletx[1]
        bulletrect.top=bulletx[2]            

        if enemyrect.colliderect(bulletrect):
            print('打中飞碟')
            print(len(bullets))
            print(len(flyingsaucers))
            print(index)
            print(indexbull)
            
            bullets.pop(indexbull)
            flyingsaucers.pop(index)
            print(index)
            print(indexbull)
            score+=100

            if(len(flyingsaucers)==0):
                b=0#重新生成飞碟

        indexbull+=1

    index+=1

    font1 = pygame.font.SysFont('simsunnsimsun', 16, True)
    surface1 = font1.render(u'当前得分:'+str(score), True, [255, 0, 0])      
    screen.blit(surface1, [20, 20])        
  
    pygame.display.update()
    #pygame.display.flip()

运行这段代码,游戏运行,音乐响起。在画布上的左上角,随时有更新的分数。见下图5-18、5-19、5-20。

【汇智学堂】-python小游戏(太空阻击之九-添加游戏声音与积分)_第1张图片

图5-18
【汇智学堂】-python小游戏(太空阻击之九-添加游戏声音与积分)_第2张图片
图5-19

【汇智学堂】-python小游戏(太空阻击之九-添加游戏声音与积分)_第3张图片
图5-20

你可能感兴趣的:(【汇智学堂】-python小游戏(太空阻击之九-添加游戏声音与积分))