Python解放军VS木马创意射击游戏

import os
from vsmuma.tank import *         # Tank类
from vsmuma.screen import *       # init_screen函数
from vsmuma.shell import *        # Shell类,炮弹类
from vsmuma.alien import *        # Alien类,外星人类 
from vsmuma.laser import *        # Laser类,外星人有时候会发射激光
from vsmuma.audio import *        # 播放背景音乐,返回音效对象元组
from vsmuma.body import *         # 导入Body类,外星人尸体类
from random import choice,randint

def register_gif():
    """加载图片素材"""    
    
    tank_images = [images_folder + "坦克" + str(i) + ".gif" for i in range(7)]     #坦克基座造型列表
    [screen.addshape(image) for image in tank_images]
    
    explosion_images = [images_folder + "造型" + str(i) + ".gif" for i in range(26)]  #爆炸造型列表    
    [screen.addshape(image) for image in explosion_images]
    
    box_images = [images_folder + "箱子" + str(i) + ".gif" for i in range(5)] # 字母箱子造型列表
    [screen.addshape(image) for image in box_images]
    
    body_images = [images_folder + "外星人_小" + str(i) + ".gif" for i in range(4)] #外星人尸体造型列表
    [screen.addshape(image) for image in body_images]

    flags = [images_folder + "flag-" + str(i) + ".gif" for i in range(8)] # 红旗造型列表
    [screen.addshape(image) for image in flags]
    
    p = ((0,0),(-6,0),(-6,80),(6,80),(6,0))                    # 炮筒形状
    screen.addshape("barrel",p)

    return tank_images,explosion_images,box_images,body_images,flags
    
def produce_alien():
    """每隔3秒产生一个外星木马"""    
     
    if not game_over:
        # 参数元组,Body类名,造型列表,炮弹列表,外星人列表,尸体列表...
        ps = Body,box_images,shells,aliens_list,body_images,玻璃碎声,(男惨叫声,女惨叫声)
        alien = Alien(*ps)               # 每个外星人都可以访问炮弹列表和外星人列表
        aliens_list.append(alien)
        screen.ontimer(produce_alien,3000)

                           
def produce_laser():
    """每隔1秒可能发射一次激光"""
    
    if not game_over and aliens_list:   # 游戏没有结束,并且外星人列表不为空
        alien = choice(aliens_list)     # 随机选择一个外星人
        if randint(0,1) ==1:
            h = alien.towards(tank)     # 面向坦克
            laser = Laser(alien.xcor(),alien.ycor(),h,tank,激光声)# 在一个外星人的位置上生成一束激光,它能访问坦克               
        screen.ontimer(produce_laser,1000)


def shoot(x,y):
    """单击鼠标左键发射炮弹"""
    try:炮声.play()
    except:pass
    s = Shell(tank.xcor(),tank.ycor(),tank.heading())
    shells.append(s)
    # 如果碰到边缘就从炮弹列表中移动这颗炮弹
    [shells.remove(shell) for shell in shells if shell.is_to_edge()] 
 
def flag_flutter():
    """封面上面的红旗飘扬"""
    global flag_index
    if start_flag == False:
        flag_index = flag_index + 1
        flag_index = flag_index % len(flag_images)
        flag_turtle.shape(flag_images[flag_index])
        screen.ontimer(flag_flutter,100)
    else:
        flag_turtle.ht()
       
    
def main():

    """进入这个函数后,坦克显示,不断地产生外星木马和激光"""
    
    global start_flag
    start_flag = True    
    screen.bgpic(bgimage)    # 切换到游戏界面       
    tank.show()              # 显示坦克
    produce_alien()          # 不断地产生外星人
    produce_laser()          # 不断地产生激光
    screen.onclick(shoot)    #绑 定鼠标左键单击事件
    
def wait_game_end():
    """每隔一秒检测各个参数,某个条件成立则游戏是否结束"""
    global game_over,end_string
    if not game_over:
        if Alien.beyond == True:             
            end_string = "外星人成功着陆,游戏失败!"
            game_over = True
        elif tank.dead == True:          
            end_string = "光荣牺牲,游戏失败!"
            game_over = True
        elif Alien.counter == Alien.amount:
            end_string = "成功击毁所有外星木马,游戏成功!"
            game_over = True
        screen.ontimer(wait_game_end,1000)
    else:        
        screen.title("游戏结束")
        t = Turtle(visible= False)
        t.write(end_string,align='center',font=("黑体",32,"normal"))
        if tank.dead == False:                                #坦克没有阵亡
          [alien.alt_costume() for alien in aliens_list]    #所有外星木马全爆掉
            
            

if __name__ == "__main__":

    audios_folder = os.getcwd() + os.sep + "audios" + os.sep
    images_folder = os.getcwd() + os.sep + "images" + os.sep
    game_over = False         #游戏结束标志
    end_string = ""           #游戏结束显示的字符串 
    start_flag = False        #开始游戏标志
    
    shells = []               #定义炮弹列表
    aliens_list = []          #外星人列表
    
    gamename = "解放军VS木马_作者:李兴球"
    size = 960,720
    bgimage = images_folder + "蓝天.png"
    coverimage = images_folder + "封面.png"
    screen = init_screen(gamename,size,coverimage)  #显示封面,初始化屏幕

    sounds = "儿童歌曲.wav","榴弹炮.wav","玻璃碎声.wav","激光声.wav","explosion.wav","男人惨叫.wav","女人惨叫.wav"
    sounds = [ audios_folder + wav for wav in sounds]
    炮声,玻璃碎声,激光声,爆炸声,男惨叫声,女惨叫声= init_audio(*sounds)
    
    #坦克基座造型,爆炸效果造型,外星木马造型,外星人尸体造型,红旗造型
    tank_images,explosion_images,box_images ,body_images,flag_images= register_gif()
    
    """红旗飘扬"""
    flag_turtle = Turtle(shape = flag_images[0],visible=False)
    flag_turtle.penup()
    flag_turtle.goto(-80,250)
    flag_turtle.st()
    flag_index = 0
    flag_flutter()      #运行它后,红旗会自动飘扬,直到start_flag的值为True

    
    #新建tank对象,如果坦克碰到激光就切换到下一个造型,一直到切换完了就爆炸.
    tank = Tank(tank_images,explosion_images,爆炸声,0,-300)
    
    screen.onkeypress(main,"space")      #按空格键进入战斗界面
    wait_game_end()                      #检测相关参数,决定游戏是否结束
    
    screen.listen()
    screen.mainloop()