导弹发射小游戏——Python源代码

导弹发射小游戏——Python源代码_第1张图片
导弹发射小游戏

# 导弹发射小游戏完整源代码

# Designed by Mcoblack 公众号pythonwanyuxue

import turtle

import random

# 设置屏幕画布的大小背景

wn = turtle.Screen()

wn.setup(600, 600)

wn.bgcolor('black')

wn.bgpic("stars.gif")

# 变量

TARGET_LLEFT_X = 100    # 目标的X坐标

TARGET_LLEFT_Y = 250    # 目标的Y坐标

TARGET_WIDTH = 25      # 目标方块的宽度

FORCE_FACTOR = 30      # 力量因数

PROJECTILE_SPEED = 1    # 导弹速度

NORTH = 90              # 正北的角度

SOUTH = 270            # 正南的角度

EAST = 0                # 正东的角度

WEST = 180              # 正西的角度

# 画导弹要击中的正方形目标

a = turtle.Turtle()

a.hideturtle()

a.speed(0)

a.penup()

a.goto(TARGET_LLEFT_X, TARGET_LLEFT_Y)

a.pendown()

a.fillcolor('orange')

a.begin_fill()

a.pencolor('yellow')

for dir in [EAST,NORTH,WEST,SOUTH]:

    a.setheading(dir)

    a.forward(TARGET_WIDTH)

a.end_fill()

a.penup()

# 画一批星形.

a.pencolor('white')

def stars(turtle):

    turtle.speed(0)

    turtle.up()

    x = random.randint(-300,400)

    y = random.randint(-300,300)

    turtle.goto(x,y)

    turtle.down()

    turtle.dot()

for i in range(50):

    stars(a)

# 画个星球

a.pencolor('black')

a.goto(-129, -270)

a.pendown()

a.fillcolor('green')

a.begin_fill()

a.circle(25)

a.end_fill()

a.penup()

# 再画个星球

a.pencolor('black')

a.goto(-200, 200)

a.pendown()

a.fillcolor('blue')

a.begin_fill()

a.circle(30)

a.end_fill()

a.penup()

# 再画个星球

a.pencolor('black')

a.goto(275, -150)

a.pendown()

a.fillcolor('brown')

a.begin_fill()

a.circle(30)

a.end_fill()

a.penup()

# Draw the ship.

a.pencolor('black')

a.goto(-34, -16)

a.pendown()

a.fillcolor('gray')

a.begin_fill()

a.circle(25)

a.end_fill()

a.penup()

a.goto(-15, -2)

a.pendown()

a.fillcolor('black')

a.begin_fill()

a.circle(10)

a.end_fill()

a.penup()

a.goto(-35,-13)

a.pendown()

a.pencolor('black')

a.setheading(0)

a.forward(55)

a.penup()

# 目标物体写几个字

a.goto(135,270)

a.pendown()

a.pencolor('white')

a.write("That's Something!")

a.penup()

# 显示导弹

a.goto(0, 0)

a.setheading(EAST)

a.pencolor('red')

a.showturtle()

a.speed(PROJECTILE_SPEED)

print()

print('目标指向方形物体,发射导弹击中目标')

print('祝你好运!')

active = True

while active:

    a_angle = input("输入导弹角度: ")

    f_force = input("输入导弹的力度(1-10): ")

    if a_angle == "" and f_force == "":

        active = False

    else:

        angle =float(a_angle)

        force = float(f_force)

        distance = force * FORCE_FACTOR

        a.setheading(angle)

        a.pendown()

        a.forward(distance)

        if (a.xcor() >= TARGET_LLEFT_X and

    a.xcor() <= (TARGET_LLEFT_X + TARGET_WIDTH) and

    a.ycor() >= TARGET_LLEFT_Y and

    a.ycor() <= (TARGET_LLEFT_Y + TARGET_WIDTH)):

            print('击中目标!')

            active = False

        elif (angle > 70):

            print('再小一点点角度!')

            a.undo()

        elif (angle < 63):

            print('再大一点角度!')

            a.undo()

        elif (angle == 70 and force == 10):

            print('你射过头了!')

            print('试着再小点角度,小点力度!')

            a.undo()

        elif (angle == 70 and force <= 9):

            print('好近哦!')

            print('再小点角度!')

            a.undo()

        elif (angle == 70 and force <= 7):

            print('小点角度,更多力度!')

            a.undo()

        elif (angle == 69 and force == 10):

            print('射过头了!')

            print('稍微小点角度,小点力度!')

            a.undo()

        elif (angle == 69 and force <= 9):

            print('有点短有点歪')

            print('再稍小点角度试试!')

            a.undo()

        elif (angle == 68 and force == 10):

            print('刚射过头一丢丢儿!')

            print('再小点力度')

            a.undo()

        elif (angle == 68 and force <= 8):

            print('力度太小!')

            print('大点力度!')

            a.undo()

        elif (angle == 67 and force == 10):

            print('你射过头了!')

            print('更小的力度,合适的角度')

            a.undo()

        elif (angle == 67 and force <= 9):

            print('好近哦!')

            print('试试更大力度,合适的角度!')

            a.undo()

        elif (angle == 66 and  force <= 9):

            print('方向不错!')

            print('试着大点力度!')

            a.undo()

        elif (angle == 65 and force == 10):

            print('你射过头了!')

            print('大点角度小点力度!')

            a.undo()

        elif (angle == 65 and  force <= 9 and  force >= 8):

            print('好近哦,马上快成功了!')

            print('试试更大点的角度和力度!')

            a.undo()

        elif (angle == 65 and force <= 7):

            print('试试更大点的角度和力度!')

            a.undo()

        elif (angle == 64 and force == 10):

            print('你射过头了!')

            print('Try a bit more angle and less force!')

            a.undo()

        elif (angle == 64 and  force <= 9 and  force >= 8):

            print('So close!')

            print('Try more angle and more force!')

            a.undo()

        elif (angle == 64 and force <= 7):

            print('Try more angle and more force!')

            a.undo()

        elif (angle == 63 and force == 10):

            print('You overshot!')

            print('Try a bit more angle and less force!')

            a.undo()

        elif (angle == 63 and force <= 9 and  force >= 8):

            print('So close!')

            print('Try more angle and more force!')

            a.undo()

        elif (angle == 63 and force <= 7):

            print('Try more angle and more force!')

            a.undo()

        else:

            print('You missed the target.')

            a.undo()

你可能感兴趣的:(导弹发射小游戏——Python源代码)