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)

You must log in to read the rest of this article. Please log in or register as a user.