Python基本语法熟悉,turtle图形函数画图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Victordas/article/details/78925813

1.对Python基本语法的简单运用,小小的文字游戏

import time
print "Welcome to the space"
password = raw_input("Please input your password: \n")
while password != "123456":
    print "The password is worng , please input it again"
    password = raw_input("Please input your password: \n")
print " "
print "Welcome to the ship , you have a lot of choices\n"
print "a-fire cannon"
print "b-break the ship"
print "c-exit"
print " "
time.sleep(1)
conductor = raw_input("please input your conductor\n")
while conductor != "c":
    if conductor == "a":
        T1 = raw_input( "Are you sure? (y/n)\n")
        if T1 == "y":
            print "fire!"
            time.sleep(1)
            print "boom!"
            time.sleep(1)
            conductor = raw_input("ok,please input your another conductor\n")
        elif T1 == "n":
            conductor = raw_input("ok,please input your another conductor\n")
    elif conductor == "b":
        T1 = raw_input( "Are you sure? (y/n)\n")
        if T1 == "y":
            print "dong!"
            time.sleep(1)
            print "dong!"
            time.sleep(1)
            print "bye"
            conductor = "c"
        elif T1 == "n":
            conductor = raw_input("ok,please input your another conductor\n")
    else:
        print "your conductor is invaild"
        conductor = raw_input("please input your conductor\n")
print " "
print "byebye"

2.turtle随机画图小程序

import turtle
import random

def drawshape(sides, length):
    angle = 360.0 / sides
    for sides in range(sides):
        turtle.forward(length)
        turtle.right(angle)

def moveTurtle(x, y):
    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()

def drawsquare(length):
    drawshape(4,length)

def drawtriangle(length):
    drawshape(3,length)

def drawcircle(length):
    drawshape(360, length)

def drawrandom():
    x = random.randrange(-200, 200)
    y = random.randrange(-200, 200)
    length = random.randrange(75)
    shape = random.randrange(1, 4)

    moveTurtle(x, y)

    if shape == 1:
        drawsquare(length)
    elif shape == 2:
        drawtriangle(length)
    elif shape == 3:
        length = length % 4
        drawcircle(length)

for shape in range(100):
    drawrandom()

turtle.done()

猜你喜欢

转载自blog.csdn.net/Victordas/article/details/78925813