"""坦克大战,小坦克被一群大坦克包围,情况十分危急。小坦克的优势在于速度快,能连续发射。
"""
#从海龟模块导入所有命令
from turtle import *
import math
from random import randint
def load_sound():
"""加载声音与播放背景音乐"""
sound_normal = True
explode_sound= None
shoot_sound = None
try:
import pygame
pygame.mixer.init()
pygame.mixer.music.load("音效/newgrounds.wav")
pygame.mixer.music.play(-1,0)
explode_sound = pygame.mixer.Sound("音效/Boom.wav")
shoot_sound = pygame.mixer.Sound("音效/榴弹炮.wav")
except:
print("播放背景音乐或加载音频出现错误.")
sound_normal = False
"""返回声音是否正常,爆炸声,射击声三个对象"""
return sound_normal,explode_sound,shoot_sound
def init_screen():
"""初始化屏幕,注册坦克形状"""
screen = Screen()
screen.setup(width,height)
p = ((0,0),(50,0),(50,80),(10,80),(10,150),(-10,150),(-10,80),(-50,80),(-50,0))
screen.addshape("tank",p) #注册tank形状
screen.bgcolor("blue") #屏幕背景色
screen.title(gametitle) #设定屏幕标题
screen.colormode(255) #设定颜色模式
screen.delay(0) #屏幕延时为0
screen.bgpic("封面设计.png") #封面加载
"""添加爆炸造型图片列表到形状列表"""
explosion_images = ["explosion-" + str(i) + ".gif" for i in range(17)]
[screen.addshape(image) for image in explosion_images ] #注册爆炸造型到形状列表
return screen,explosion_images
class Bullet(Turtle):
"""炮弹类,炮弹生成后会自己移动,直到碰到边缘。"""
def __init__(self,x,y,h):
Turtle.__init__(self,visible=False,shape="circle")
self.penup()
self.dead = False
self.goto(x,y)
self.setheadin