python简单小动画

今天给大家分享几个用python做的小游戏

第一个

import random
import turtle
t = turtle.Pen()
t.speed(0)
turtle.bgcolor("black")
colors = ["red", "green", "yellow", "gray", "blue", "orange", "purple", "white"]
def random_spiral():
    t.pencolor(random.choice(colors))
    size = random.randint(10, 40)
    x = random.randrange(-turtle.window_width()//2,turtle.window_width()//2)
    y = random.randrange(-turtle.window_height()//2,turtle.window_height()//2)
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)
for n in range(50):
    random_spiral()

第二个

# 绘制笑脸
import random
import turtle
t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor("black")
def draw_smiley(x, y):
    t.penup()
    t.setpos(x, y)
    t.pendown()

    # head
    t.pencolor("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    t.circle(50)
    t.end_fill()

    # left eye
    t.setpos(x - 15, y + 60)
    t.fillcolor("blue")
    t.begin_fill()
    t.circle(10)
    t.end_fill()

    # right eye
    t.setpos(x + 15, y + 60)
    t.begin_fill()
    t.circle(10)
    t.end_fill()

    # mouth
    t.setpos(x - 25, y + 40)
    t.pencolor("black")
    t.width(10)
    t.goto(x - 10, y + 20)
    t.goto(x + 10, y + 20)
    t.goto(x + 25, y + 40)
    t.width(1)
for n in range(50):
    x = random.randrange(-turtle.window_width() // 2, turtle.window_width() // 2)
    y = random.randrange(-turtle.window_height()//2, turtle.window_height()//2)

    draw_smiley(x, y)

第三个

# 自己的体重是多少个乒乓球
def covert_in2cm(inches):
    return inches * 2.54
def convert_lb2kg(pounds):
    return pounds / 2.2

height_in = int(input("输入你多高"))
weight_lb = int(input("输入你的体重"))
height_cm = covert_in2cm(height_in)
weight_kg = convert_lb2kg(weight_lb)
ping_pang_tall = round(height_cm / 4)
ping_pang_heavy = round(weight_kg * 1000 / 2.7)
feet = height_in // 12
inch = height_in % 12
print("有", feet, "英尺", inch, "英寸高", weight_lb, "磅重")
print("你的高度是", ping_pang_tall, "个乒乓球")
print("你的体重相当于", ping_pang_heavy, "乒乓球")

第四个

import turtle
t = turtle.Pen()
t.speed(0)
turtle.onscreenclick(t.setpos)

第五个

import turtle
t = turtle.Pen()
t.speed(0)
turtle.onscreenclick(t.setpos)
turtle.bgcolor("blue")
t.pencolor("green")
t.width(99)

第六个

# 用上下键来控制走向
import turtle
t = turtle.Pen()
t.speed(0)
t.turtlesize(2,2,2)
def up():
    t.forward(50)
def left():
    t.left(90)
def right():
    t.right(90)
turtle.onkeypress(up, "Up")
turtle.onkeypress(left, "Left")
turtle.onkeypress(right, "Right")
turtle.listen()

第七个

import random
import turtle
t = turtle.Pen()
t.speed(0)
turtle.bgcolor("black")
colors = ["red", "blue", "yellow", "green", "orange", "purple", "white", "gray"]
def spiral(x,y):
    t.pencolor(random.choice(colors))
    size = random.randint(10,40)
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(91)
turtle.onscreenclick(spiral)

    
import random
import turtle
t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor("black")
def draw_smiley(x,y):
    t.penup()
    t.setpos(x,y)
    t.pendown()

    t.pencolor("yellow")
    t.fillcolor("yellow")
    t.begin_fill()
    t.circle(50)
    t.end_fill()

    # left eye
    t.setpos(x - 15, y + 60)
    t.fillcolor("blue")
    t.begin_fill()
    t.circle(10)
    t.end_fill()

    # right eye
    t.setpos(x + 15, y + 60)
    t.begin_fill()
    t.circle(10)
    t.end_fill()

    mouth
    t.setpos(x - 25, y + 40)
    t.pencolor("black")
    t.width(10)
    t.goto(x - 10, y + 20)
    t.goto(x + 10, y + 20)
    t.goto(x + 25, y + 40)
    t.width(1)

turtle.onscreenclick(draw_smiley)

第八个

 import random
 import turtle
 t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor("black")
colors = ["red", "yellow", "blue","green","orange","purple","white","gray"]
def draw_kaleido(x,y):
    t.pencolor(random.choice(colors))
    size = random.randint(10,40)
    draw_spiral(x,y,size)
    draw_spiral(-x, y, size)
    draw_spiral(-x, -y, size)
    draw_spiral(x, -y, size)
    draw_spiral(x, y, size)
def draw_spiral(x,y,size):
    t.penup()
    t.setpos(x,y)
    t.pendown()
    for m in range(size):
        t.forward(m*2)
        t.left(92)
turtle.onscreenclick(draw_kaleido)

你可能感兴趣的:(Python的和基础操作)