### pygame雪地打蝙蝠射击游戏.py

```"""pygame雪地打蝙蝠射击游戏.py,在冰天雪地中,蝙蝠竟然不冬眠全部飞出来了，这是不合情理的，请按左右方向键头操作大炮朝天空打死这些不冬眠的奇怪蝙蝠."""

__author__ = "李兴球"
__date__ = "2018年6月"
__company__ = "风火轮少儿编程"

import time
import pygame
from random import randint
from pygame.locals import *

class Sprite():
def __init__(self,framesRight,framesLeft,x,y,w,h,sound = None):
self.frames_right = framesRight
self.frames_left = framesLeft
self.costume_amounts = len(self.frames_left) # 造型数量
self.costume_index=0                         # 造型索引号
self.造型切换间隔时长=0.2                    # 以秒为单位
self.移动间隔时长=0.03                       # 以秒为单位
self.rect = pygame.Rect(x,y,w,h)
self.xspeed=randint(-5,5)
self.yspeed=0
self.begintime = time.time()
self.begintime2 = time.time()
self.delete = 0                              # 删除标志
self.sound = sound                           # 声音

def next_costume(self):
if (time.time()-self.begintime) >= self.造型切换间隔时长:
self.costume_index = self.costume_index + 1
self.costume_index = self.costume_index % self.costume_amounts
self.begintime = time.time()
def move(self):
if (time.time()-self.begintime2) >= self.移动间隔时长:
self.rect.x += self.xspeed
self.rect.y += self.yspeed
if self.rect.left<0 or self.rect.right>screen_width:
self.xspeed = - self.xspeed
self.begintime2 = time.time()

def 碰到(self,炮弹):
if self.rect.colliderect(炮弹.rect):
self.delete = 1
self.sound.play()                      # 播放声音

def draw(self):
if self.xspeed>0:
screen.blit(self.frames_right[self.costume_index],self.rect)
else:
screen.blit(self.frames_left[self.costume_index],self.rect)

"""大炮类的建立"""
class Canon():
pass     # 大炮类的代码最简单,如果看懂了全部代码,相信读者可以自行写出代码

"""炮弹类"""
class Bomb():
pass

pygame.init()
screen_width,screen_height=480,360
screen = pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption("雪地打蝙蝠--作者:李兴球_风火轮少儿编程")

framesRight = [frame0,frame1]
framesLeft = [pygame.transform.flip(f,True,False) for f in framesRight]

font = pygame.font.Font("c:/windows/fonts/msyh.ttf",30)
textImage =font.render(" ",True,(0,25,255))
(tx,ty,tw,th) = textImage.get_rect()
textpos=(screen_width//2 - tw //2,screen_height//2 - th/2 -100)

pygame.mixer.init()

pygame.mixer.music.play(-1,0)

# 新建一些蝙蝠bat
bat0 = Sprite(framesRight,framesLeft,100,20,40,40,吱声)
bat1 = Sprite(framesRight,framesLeft,130,40,40,40,吱声)
bat2 = Sprite(framesRight,framesLeft,40,80,40,40,吱声)
bat3 = Sprite(framesRight,framesLeft,160,120,40,40,吱声)
bat4 = Sprite(framesRight,framesLeft,320,160,40,40,吱声)
bats = [bat0,bat1,bat2,bat3,bat4 ]

running = True
while running:
# 时不时地生成一只蝙蝠
if randint(0,1000) ==0:
randx = randint(0,screen_width)
randy = randint(0,100)
bats.append(Sprite(framesRight,framesLeft,randx,randy,40,40,吱声))

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

keys = pygame.key.get_pressed()
if keys[K_RIGHT]:大炮.movetoRight()  # 可以加上rect.x大于0及rect.right小于screen_width,让它不能超出边缘.
if keys[K_LEFT]:大炮.movetoLeft()

if keys[K_UP] and 发射间隔 == 100  and 炮弹发射中==False:
炮声.play()
炮弹 = Bomb((255,0,0),大炮.rect.centerx,大炮.rect.centery-20)
炮弹发射中= True
发射间隔 = 0

if 发射间隔<100:发射间隔 = 发射间隔 + 1

for bat in bats:
bat.move()
if '炮弹' in locals():  bat.碰到(炮弹)
bat.next_costume()

if '炮弹' in locals():
if 炮弹.rect.bottom<0:
炮弹发射中=False
else:
炮弹.move()

screen.blit(背景,(0,0))
[bat.draw() for bat in bats if bat.delete==0]
大炮.draw()
if '炮弹' in locals():  炮弹.draw()

screen.blit(textImage,textpos)
pygame.display.update()
[bats.remove(bat) for bat in bats if bat.delete==1]

pygame.quit()
print("游戏结束，谢谢")

```