import math as m
import random as r
import time
import os

lv = 0
score = 0
g = 9.81
x_max = 200
y_max = 40

def cls():
    os.system('cls')

def wait(t):
    time.sleep(t)

def slowp(s):
    print(s)
    wait(2)

def read1():
    s = input('请输入小鸟的初始速度(格/s,0 < v <= 100)')
    try:
        v = float(s)
    except:
        print('输入错误,请重新输入!')
        return read1()
    if v <= 0 or v > 100:
        print('输入范围错误,请重新输入!')
        return read1()
    return v

def read2():
    s = input('请输入小鸟的发射角度(度,0 < v <= 90)')
    try:
        theta = float(s)
    except:
        print('输入错误,请重新输入!')
        return read2()
    if theta <= 0 or theta > 90:
        print('输入范围错误,请重新输入!')
        return read2()
    return theta

def X(v, theta, t):
    return int(v * m.cos(m.radians(theta)) * t + 0.5)

def Y(v, theta, t):
    return int(v * m.sin(m.radians(theta)) * t - 0.5 * g * t * t + 0.5)

def intro():
    global lv
    slowp('欢迎来到愤怒的小鸟!')
    slowp('本游戏比较考验物理水平和目测能力')
    slowp('你需要在5分钟内摧毁尽可能多的碉堡')
    print('首先请选择难度(1~5)', end = '')
    s = input(':')
    while s not in ('1', '2', '3', '4', '5'):
        slowp('输入错误,请重新输入!')
    lv = 5 - int(s)
    slowp('很好,现在我们开始吧!')
    wait(3)
    cls()

def show(x, y, x_, y_):
    s = ''
    if x <= x_max and y <= y_max:
        for i in range(y_max + 2):
            for j in range(x_max + 1):
                if i == y_max + 1:
                    s += '-'
                elif y_max - y == i and x == j:
                    s += 'o'
                elif abs(y_max - y_ - i) <= lv and abs(x_ - j) <= lv:
                    s += 'x'
                else:
                    s += ' '
            s += '\n'
    else:
        for i in range(y_max + 2):
            for j in range(x_max + 1):
                if i == y_max + 1:
                    s += '-'
                elif abs(y_max - y_ - i) <= lv and abs(x_ - j) <= lv:
                    s += 'x'
                else:
                    s += ' '
            s += '\n'
    print(s)


intro()
slowp('现在你有三次试射机会(不计入时间和积分)')
wait(2)
for i in range(3):
    cls()
    y_ = r.randint(lv, y_max - lv)
    if y_ >= 10:
        x_ = r.randint(lv, x_max - lv)
    else:
        x_ = r.randint(lv, x_max - lv)
    show(0, 0, x_, y_)
    v, theta = read1(), read2()
    t = 0
    x = X(v, theta, t)
    y = Y(v, theta, t)
    while True:
        cls()
        if y <= 0 and t >= 0.2 or abs(x - x_) <= lv and abs(y - y_) <= lv or x > x_ + lv:
            break
        show(x, y, x_, y_)
        wait(0.01)
        t += 0.05
    if abs(x - x_) <= lv and abs(y - y_) <= lv:
        print('打击成功!')
    else:
        print('打击失败!')
slowp('接下来游戏正式开始!')
begin = time.time()
while time.time() - begin <= 300:
    cls()
    y_ = r.randint(lv, y_max - lv)
    if y_ >= 10:
        x_ = r.randint(lv, x_max - lv)
    else:
        x_ = r.randint(lv, x_max - lv)
    show(0, 0, x_, y_)
    v, theta = read1(), read2()
    t = 0
    while True:
        cls()
        x = X(v, theta, t)
        y = Y(v, theta, t)
        if y <= 0 and t >= 0.2 or abs(x - x_) <= lv and abs(y - y_) <= lv or x > x_ + lv:
            break
        show(x, y, x_, y_)
        wait(0.01)
        t += 0.05
    if abs(x - x_) <= lv and abs(y - y_) <= lv:
        score += (8 - abs(x - x_) - abs(y - y_)) ** 2 + (5 - lv) ** 2
        print('打击成功!目前得分:' + str(score))
    else:
        print('打击失败!目前得分:' + str(score))
    wait(1)
slowp('游戏结束,你的最终得分为:' + str(score))
wait(10)