1、猜字
"""猜测代码的含义"""
import random
b=int(random.randint(1,100))
print(b)
a=input("请输入一个数字1-100:")
guess=int(a)
if guess==b:
print("wow,你真棒,居然猜对了")
print("哼,猜对也没有奖励哦")
else:
print("猜错了,笨蛋")
print("游戏结束,不玩了")
#引入生成随机数的模块
import random
#程序设定生成 1-20 之间的一个随机数,让用户猜
secretNum = random.randint(1,20)
print("这是一个位于 1-20 之间的数")
#设定用户只能猜 3 次
for number in range(1,4):
print("请输入猜测的数:")
guess = int(input())
if guess == 0:
break
if guess < secretNum:
print("太小啦")
elif guess > secretNum:
print("太大啦")
else:
break
if(guess == secretNum):
print("真厉害,猜对啦,就是",str(guess))
else:
print("很遗憾,正确的答案应该是",str(secretNum))
2、狮子回家
import pygame
import sys
# 初始化
pygame.init()
size = (500, 500)
pygame.display.set_caption("让狮子回家")
screen = pygame.display.set_mode(size)
white = (255, 255, 255) # 白色
screen.fill(white) # 填充
# 加载图片
# 参数:完整的图片名称
img = pygame.image.load("pic/bg.png")
img2 = pygame.image.load("pic/lion.png")
img = pygame.transform.scale(img, size)
img2 = pygame.transform.scale(img2, (160, 120))
# 变量:狮子的位置
a=0
lion_y=0
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
sys.exit()
if e.type == pygame.KEYDOWN: # 键盘被按下
if e.key == pygame.K_UP: # 如果是上移键
print("在这里填写往上移动的代码")
# 代码...
lion_y-=10
elif e.key == pygame.K_DOWN: # 如果是下移键
print("在这里填写往下移动的代码")
# 代码...
lion_y+=10
elif e.key == pygame.K_LEFT: # 如果是左移键
print("在这里填写往左移动的代码")
# 代码...
a-=10
elif e.key == pygame.K_RIGHT: # 如果是右移键
print("在这里填写往右移动的代码")
# 代码...
a=a+10
# 更新角色位置
screen.fill(white)
screen.blit(img, (0, 0))
screen.blit(img2, (a, lion_y))
pygame.display.flip()
3、海龟画图系列
(1)樱花树
# -*- coding: utf-8 -*-
#@Time:2021/11/18 22:06
#@Author:NWA
#@File:樱花树.py
#@Software:PyCharm
import turtle
import random
from turtle import *
from time import sleep
# 画樱花的躯干(60,t)
turtle.hideturtle()
def tree(branchLen, t):
sleep(0)
if branchLen > 3:
if 8 <= branchLen <= 12:
if random.randint(0, 2) == 0:
t.color('snow') # 白
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 3)
elif branchLen < 8:
if random.randint(0, 1) == 0:
t.color('snow')
else:
t.color('lightcoral') # 淡珊瑚色
t.pensize(branchLen / 2)
else:
t.color('sienna') # 赭(zhě)色
t.pensize(branchLen / 10) # 6
t.forward(branchLen)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
tree(branchLen - 10 * b, t)
t.left(40 * a)
tree(branchLen - 10 * b, t)
t.right(20 * a)
t.up()
t.backward(branchLen)
t.down()
# 掉落的花瓣
def petal(m, t):
for i in range(m):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral') # 淡珊瑚色
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
def main():
# 绘图区域
t = turtle.Turtle()
# 画布大小
w = turtle.Screen()
t.hideturtle() # 隐藏画笔
w.screensize(bg='wheat') # wheat小麦
t.left(90)
t.up()
t.backward(150)
t.down()
t.color('sienna')
# 画樱花的躯干
tree(60, t)
# 掉落的花瓣
petal(200, t)
w.exitonclick()
main()
(2)小猪佩奇
# -*- coding: utf-8 -*-
from turtle import*
def nose(x,y):#鼻子
pu()
goto(x,y)
pd()
seth(-30)
begin_fill()
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
lt(3) #向左转3度
fd(a) #向前走a的步长
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
pu()
seth(90)
fd(25)
seth(0)
fd(10)
pd()
pencolor(255,155,192)
seth(10)
begin_fill()
circle(5)
color(160,82,45)
end_fill()
pu()
seth(0)
fd(20)
pd()
pencolor(255,155,192)
seth(10)
begin_fill()
circle(5)
color(160,82,45)
end_fill()
def head(x,y):#头
color((255,155,192),"pink")
pu()
goto(x,y)
seth(0)
pd()
begin_fill()
seth(180)
circle(300,-30)
circle(100,-60)
circle(80,-100)
circle(150,-20)
circle(60,-95)
seth(161)
circle(-300,15)
pu()
goto(-100,100)
pd()
seth(-30)
a=0.4
for i in range(60):
if 0<=i<30 or 60<=i<90:
a=a+0.08
lt(3) #向左转3度
fd(a) #向前走a的步长
else:
a=a-0.08
lt(3)
fd(a)
end_fill()
def ears(x,y): #耳朵
color((255,155,192),"pink")
pu()
goto(x,y)
pd()
begin_fill()
seth(100)
circle(-50,50)
circle(-10,120)
circle(-50,54)
end_fill()
pu()
seth(90)
fd(-12)
seth(0)
fd(30)
pd()
begin_fill()
seth(100)
circle(-50,50)
circle(-10,120)
circle(-50,56)
end_fill()
def eyes(x,y):#眼睛
color((255,155,192),"white")
pu()
seth(90)
fd(-20)
seth(0)
fd(-95)
pd()
begin_fill()
circle(15)
end_fill()
color("black")
pu()
seth(90)
fd(12)
seth(0)
fd(-3)
pd()
begin_fill()
circle(3)
end_fill()
color((255,155,192),"white")
pu()
seth(90)
fd(-25)
seth(0)
fd(40)
pd()
begin_fill()
circle(15)
end_fill()
color("black")
pu()
seth(90)
fd(12)
seth(0)
fd(-3)
pd()
begin_fill()
circle(3)
end_fill()
def cheek(x,y):#腮
color((255,155,192))
pu()
goto(x,y)
pd()
seth(0)
begin_fill()
circle(30)
end_fill()
def mouth(x,y): #嘴
color(239,69,19)
pu()
goto(x,y)
pd()
seth(-80)
circle(30,40)
circle(40,80)
def body(x,y):#身体
color("red",(255,99,71))
pu()
goto(x,y)
pd()
begin_fill()
seth(-130)
circle(100,10)
circle(300,30)
seth(0)
fd(230)
seth(90)
circle(300,30)
circle(100,3)
color((255,155,192),(255,100,100))
seth(-135)
circle(-80,63)
circle(-150,24)
end_fill()
def hands(x,y):#手
color((255,155,192))
pu()
goto(x,y)
pd()
seth(-160)
circle(300,15)
pu()
seth(90)
fd(15)
seth(0)
fd(0)
pd()
seth(-10)
circle(-20,90)
pu()
seth(90)
fd(30)
seth(0)
fd(237)
pd()
seth(-20)
circle(-300,15)
pu()
seth(90)
fd(20)
seth(0)
fd(0)
pd()
seth(-170)
circle(20,90)
def foot(x,y):#脚
pensize(10)
color((240,128,128))
pu()
goto(x,y)
pd()
seth(-90)
fd(40)
seth(-180)
color("black")
pensize(15)
fd(20)
pensize(10)
color((240,128,128))
pu()
seth(90)
fd(40)
seth(0)
fd(90)
pd()
seth(-90)
fd(40)
seth(-180)
color("black")
pensize(15)
fd(20)
def tail(x,y):#尾巴
pensize(4)
color((255,155,192))
pu()
goto(x,y)
pd()
seth(0)
circle(70,20)
circle(10,330)
circle(70,30)
def setting(): #参数设置
pensize(4)
hideturtle()
colormode(255)
color((255,155,192),"pink")
setup(840,500)
speed(10)
def main():
setting() #画布、画笔设置
nose(-100,100) #鼻子
head(-69,167) #头
ears(0,160) #耳朵
eyes(0,140) #眼睛
cheek(80,10) #腮
mouth(-20,30) #嘴
body(-32,-8) #身体
hands(-56,-45) #手
foot(2,-177) #脚
tail(148,-155) #尾巴
done() #结束
main()
(3)皮卡丘
# -*- coding: utf-8 -*-
import turtle
def getPosition(x, y):
turtle.setx(x)
turtle.sety(y)
print(x, y)
class Pikachu:
def __init__(self):
self.t = turtle.Turtle()
t = self.t
t.pensize(3)
t.speed(9)
t.ondrag(getPosition)
def noTrace_goto(self, x, y):
self.t.penup()
self.t.goto(x, y)
self.t.pendown()
def leftEye(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.seth(0)
t.fillcolor('#333333')
t.begin_fill()
t.circle(22)
t.end_fill()
self.noTrace_goto(x, y + 10)
t.fillcolor('#000000')
t.begin_fill()
t.circle(10)
t.end_fill()
self.noTrace_goto(x + 6, y + 22)
t.fillcolor('#ffffff')
t.begin_fill()
t.circle(10)
t.end_fill()
def rightEye(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.seth(0)
t.fillcolor('#333333')
t.begin_fill()
t.circle(22)
t.end_fill()
self.noTrace_goto(x, y + 10)
t.fillcolor('#000000')
t.begin_fill()
t.circle(10)
t.end_fill()
self.noTrace_goto(x - 6, y + 22)
t.fillcolor('#ffffff')
t.begin_fill()
t.circle(10)
t.end_fill()
def mouth(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.fillcolor('#88141D')
t.begin_fill()
# 下嘴唇
l1 = []
l2 = []
t.seth(190)
a = 0.7
for i in range(28):
a += 0.1
t.right(3)
t.fd(a)
l1.append(t.position())
self.noTrace_goto(x, y)
t.seth(10)
a = 0.7
for i in range(28):
a += 0.1
t.left(3)
t.fd(a)
l2.append(t.position())
# 上嘴唇
t.seth(10)
t.circle(50, 15)
t.left(180)
t.circle(-50, 15)
t.circle(-50, 40)
t.seth(233)
t.circle(-50, 55)
t.left(180)
t.circle(50, 12.1)
t.end_fill()
# 舌头
self.noTrace_goto(17, 54)
t.fillcolor('#DD716F')
t.begin_fill()
t.seth(145)
t.circle(40, 86)
t.penup()
for pos in reversed(l1[:20]):
t.goto(pos[0], pos[1] + 1.5)
for pos in l2[:20]:
t.goto(pos[0], pos[1] + 1.5)
t.pendown()
t.end_fill()
# 鼻子
self.noTrace_goto(-17, 94)
t.seth(8)
t.fd(4)
t.back(8)
# 红脸颊
def leftCheek(self, x, y):
turtle.tracer(False)
t = self.t
self.noTrace_goto(x, y)
t.seth(300)
t.fillcolor('#DD4D28')
t.begin_fill()
a = 2.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
t.lt(3)
t.fd(a)
else:
a += 0.05
t.lt(3)
t.fd(a)
t.end_fill()
turtle.tracer(True)
def rightCheek(self, x, y):
t = self.t
turtle.tracer(False)
self.noTrace_goto(x, y)
t.seth(60)
t.fillcolor('#DD4D28')
t.begin_fill()
a = 2.3
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
t.lt(3)
t.fd(a)
else:
a += 0.05
t.lt(3)
t.fd(a)
t.end_fill()
turtle.tracer(True)
def colorLeftEar(self, x, y):
t = self.t
self.noTrace_goto(x, y)
t.fillcolor('#000000')
t.begin_fill()
t.seth(330)
t.circle(100, 35)
t.seth(219)
t.circle(-300, 19)
t.seth(110)
t.circle(-30, 50)
t.circle(-300, 10)
t.end_fill()
def colorRightEar(self, x, y):
t = self.t
self.noTrace_goto(x, y)
t.fillcolor('#000000')
t.begin_fill()
t.seth(300)
t.circle(-100, 30)
t.seth(35)
t.circle(300, 15)
t.circle(30, 50)
t.seth(190)
t.circle(300, 17)
t.end_fill()
def body(self):
t = self.t
t.fillcolor('#F6D02F')
t.begin_fill()
# 右脸轮廓
t.penup()
t.circle(130, 40)
t.pendown()
t.circle(100, 105)
t.left(180)
t.circle(-100, 5)
# 右耳朵
t.seth(20)
t.circle(300, 30)
t.circle(30, 50)
t.seth(190)
t.circle(300, 36)
# 上轮廓
t.seth(150)
t.circle(150, 70)
# 左耳朵
t.seth(200)
t.circle(300, 40)
t.circle(30, 50)
t.seth(20)
t.circle(300, 35)
# print(t.pos())
# 左脸轮廓
t.seth(240)
t.circle(105, 95)
t.left(180)
t.circle(-105, 5)
# 左手
t.seth(210)
t.circle(500, 18)
t.seth(200)
t.fd(10)
t.seth(280)
t.fd(7)
t.seth(210)
t.fd(10)
t.seth(300)
t.circle(10, 80)
t.seth(220)
t.fd(10)
t.seth(300)
t.circle(10, 80)
t.seth(240)
t.fd(12)
t.seth(0)
t.fd(13)
t.seth(240)
t.circle(10, 70)
t.seth(10)
t.circle(10, 70)
t.seth(10)
t.circle(300, 18)
t.seth(75)
t.circle(500, 8)
t.left(180)
t.circle(-500, 15)
t.seth(250)
t.circle(100, 65)
# 左脚
t.seth(320)
t.circle(100, 5)
t.left(180)
t.circle(-100, 5)
t.seth(220)
t.circle(200, 20)
t.circle(20, 70)
t.seth(60)
t.circle(-100, 20)
t.left(180)
t.circle(100, 20)
t.seth(300)
t.circle(10, 70)
t.seth(60)
t.circle(-100, 20)
t.left(180)
t.circle(100, 20)
t.seth(10)
t.circle(100, 60)
# 横向
t.seth(180)
t.circle(-100, 10)
t.left(180)
t.circle(100, 10)
t.seth(5)
t.circle(100, 10)
t.circle(-100, 40)
t.circle(100, 35)
t.left(180)
t.circle(-100, 10)
# 右脚
t.seth(290)
t.circle(100, 55)
t.circle(10, 50)
t.seth(120)
t.circle(100, 20)
t.left(180)
t.circle(-100, 20)
t.seth(0)
t.circle(10, 50)
t.seth(110)
t.circle(100, 20)
t.left(180)
t.circle(-100, 20)
t.seth(30)
t.circle(20, 50)
t.seth(100)
t.circle(100, 40)
# 右侧身体轮廓
t.seth(200)
t.circle(-100, 5)
t.left(180)
t.circle(100, 5)
t.left(30)
t.circle(100, 75)
t.right(15)
t.circle(-300, 21)
t.left(180)
t.circle(300, 3)
# 右手
t.seth(43)
t.circle(200, 60)
t.right(10)
t.fd(10)
t.circle(5, 160)
t.seth(90)
t.circle(5, 160)
t.seth(90)
t.fd(10)
t.seth(90)
t.circle(5, 180)
t.fd(10)
t.left(180)
t.left(20)
t.fd(10)
t.circle(5, 170)
t.fd(10)
t.seth(240)
t.circle(50, 30)
t.end_fill()
self.noTrace_goto(130, 125)
t.seth(-20)
t.fd(5)
t.circle(-5, 160)
t.fd(5)
# 手指纹
self.noTrace_goto(166, 130)
t.seth(-90)
t.fd(3)
t.circle(-4, 180)
t.fd(3)
t.seth(-90)
t.fd(3)
t.circle(-4, 180)
t.fd(3)
# 尾巴
self.noTrace_goto(168, 134)
t.fillcolor('#F6D02F')
t.begin_fill()
t.seth(40)
t.fd(200)
t.seth(-80)
t.fd(150)
t.seth(210)
t.fd(150)
t.left(90)
t.fd(100)
t.right(95)
t.fd(100)
t.left(110)
t.fd(70)
t.right(110)
t.fd(80)
t.left(110)
t.fd(30)
t.right(110)
t.fd(32)
t.right(106)
t.circle(100, 25)
t.right(15)
t.circle(-300, 2)
##############
# print(t.pos())
t.seth(30)
t.fd(40)
t.left(100)
t.fd(70)
t.right(100)
t.fd(80)
t.left(100)
t.fd(46)
t.seth(66)
t.circle(200, 38)
t.right(10)
t.fd(10)
t.end_fill()
# 尾巴花纹
t.fillcolor('#923E24')
self.noTrace_goto(126.82, -156.84)
t.begin_fill()
t.seth(30)
t.fd(40)
t.left(100)
t.fd(40)
t.pencolor('#923e24')
t.seth(-30)
t.fd(30)
t.left(140)
t.fd(20)
t.right(150)
t.fd(20)
t.left(150)
t.fd(20)
t.right(150)
t.fd(20)
t.left(130)
t.fd(18)
t.pencolor('#000000')
t.seth(-45)
t.fd(67)
t.right(110)
t.fd(80)
t.left(110)
t.fd(30)
t.right(110)
t.fd(32)
t.right(106)
t.circle(100, 25)
t.right(15)
t.circle(-300, 2)
t.end_fill()
# 帽子、眼睛、嘴巴、脸颊
self.cap(-134.07, 147.81)
self.mouth(-5, 25)
self.leftCheek(-126, 32)
self.rightCheek(107, 63)
self.colorLeftEar(-250, 100)
self.colorRightEar(140, 270)
self.leftEye(-85, 90)
self.rightEye(50, 110)
t.hideturtle()
def cap(self, x, y):
self.noTrace_goto(x, y)
t = self.t
t.fillcolor('#CD0000')
t.begin_fill()
t.seth(200)
t.circle(400, 7)
t.left(180)
t.circle(-400, 30)
t.circle(30, 60)
t.fd(50)
t.circle(30, 45)
t.fd(60)
t.left(5)
t.circle(30, 70)
t.right(20)
t.circle(200, 70)
t.circle(30, 60)
t.fd(70)
# print(t.pos())
t.right(35)
t.fd(50)
t.circle(8, 100)
t.end_fill()
self.noTrace_goto(-168.47, 185.52)
t.seth(36)
t.circle(-270, 54)
t.left(180)
t.circle(270, 27)
t.circle(-80, 98)
t.fillcolor('#444444')
t.begin_fill()
t.left(180)
t.circle(80, 197)
t.left(58)
t.circle(200, 45)
t.end_fill()
self.noTrace_goto(-58, 270)
t.pencolor('#228B22')
t.dot(35)
self.noTrace_goto(-30, 280)
t.fillcolor('#228B22')
t.begin_fill()
t.seth(100)
t.circle(30, 180)
t.seth(190)
t.fd(15)
t.seth(100)
t.circle(-45, 180)
t.right(90)
t.fd(15)
t.end_fill()
t.pencolor('#000000')
def start(self):
self.body()
def main():
print('Painting the Pikachu... ')
turtle.screensize(800, 600)
turtle.title('Pikachu')
pikachu = Pikachu()
pikachu.start()
turtle.mainloop()
if __name__ == '__main__':
main()
(4)烟花爆炸
# -*- coding: utf-8 -*-
import turtle
# 海龟初始化
pen = turtle.Pen()
turtle.bgcolor("black") # 背景颜色
turtle.title("烟花")
pen.speed(10)
for x in range(180):
step = 300
if x % 2 == 0:
pen.color("red")
step = 300
elif x % 3 == 0:
pen.color("green")
step = 250
elif x % 5 == 0:
pen.color("yellow")
step = 150
else:
pen.color("pink")
step = 100
pen.forward(step) # 按照步长画直线
# pen.dot()
pen.backward(step) # 原路返回
pen.right(2) # 每次循环向右转2度
pen.forward(step) # 按照步长画直线
pen.dot(1)
pen.backward(step) # 原路返回
pen.right(2) # 每次循环向右转2度
turtle.done()
(5)旗
import turtle
turtle.up()
turtle.goto(-200,200)
turtle.down()
turtle.fillcolor('red')
turtle.pencolor('red')
turtle.begin_fill()
for i in range(2):
turtle.forward(280)
turtle.right(90)
turtle.forward(200)
turtle.right(90)
turtle.end_fill()
#长方形
turtle.up()
turtle.goto(-170,145)
turtle.down()
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(50)
turtle.right(144)
turtle.end_fill()
#1星
turtle.up()
turtle.goto(-100,180)
turtle.down()
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
#2星
turtle.up()
turtle.goto(-70,160)
turtle.down()
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
#3星
turtle.up()
turtle.goto(-70,120)
turtle.down()
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
#4星
turtle.up()
turtle.goto(-100,100)
turtle.down()
turtle.begin_fill()
turtle.pencolor('yellow')
turtle.fillcolor('yellow')
for i in range(5):
turtle.forward(20)
turtle.right(144)
turtle.end_fill()
turtle.hideturtle()
turtle.mainloop()
(6)哆啦a梦
import turtle
t=turtle.Pen()
t.speed(25)
t.pensize(8)
t.hideturtle()
#大圈
t.fillcolor('#00A1E8')
t.begin_fill()
t.circle(120)
t.end_fill()
#小圈
t.pensize(3)
t.fillcolor('white')
t.begin_fill()
t.circle(100)
t.end_fill()
#鼻子大圈
t.penup()
t.goto(0,134)
t.pendown()
t.pensize(4)
t.fillcolor('#ea0014')
t.begin_fill()
t.circle(18)
t.end_fill()
#鼻子小圈
t.penup()
t.goto(7,155)
t.pensize(2)
t.color('white',
'white')
t.pendown()
t.begin_fill()
t.circle(4)
t.end_fill()
#左眼
t.pu()
t.goto(-30,160)
t.pensize(4)
t.pd()
t.color('black','white')
t.begin_fill()
a=0.4
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3)
t.fd(a)
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
#左眼球
t.penup()
t.goto(-30,170)
t.pendown()
t.pensize(4)
t.fillcolor('black')
t.begin_fill()
t.circle(18)
t.end_fill()
t.penup()
t.goto(-25,178)
t.pensize(3)
t.color('white',
'white')
t.pendown()
t.begin_fill()
t.circle(4)
t.end_fill()
#右眼
t.pu()
t.goto(30,160)
t.pensize(4)
t.pd()
t.color('black','white')
t.begin_fill()
for i in range(120):
if 0<=i<30 or 60<=i<90:
a=a+0.08
t.lt(3)
t.fd(a)
else:
a=a-0.08
t.lt(3)
t.fd(a)
t.end_fill()
#右眼球
t.penup()
t.goto(30,170)
t.pendown()
t.pensize(4)
t.fillcolor('black')
t.begin_fill()
t.circle(18)
t.end_fill()
t.penup()
t.goto(25,178)
t.pensize(3)
t.color('white',
'white')
t.pendown()
t.begin_fill()
t.circle(4)
t.end_fill()
#胡须
t.pu()
t.home()
t.goto(0,134)
t.pensize(4)
t.pencolor('black')
t.pd()
t.right(90)
t.forward(40)
#右胡须
t.pu()
t.home()
t.goto(0,124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(10)
t.forward(80)
t.pu()
t.home()
t.goto(0,114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(6)
t.forward(80)
t.pu()
t.home()
t.goto(0,104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(180)
t.forward(80)
t.pu()
t.home()
t.goto(0,104)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(0)
t.forward(80)
#左胡须
t.pu()
t.home()
t.goto(0,124)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(170)
t.forward(80)
t.pu()
t.home()
t.goto(0,114)
t.pensize(3)
t.pencolor('black')
t.pd()
t.left(174)
t.forward(80)
#嘴巴
t.pu()
t.goto(-70,70)
t.pd()
t.color('black','red')
t.pensize(6)
t.seth(-60)
t.begin_fill()
t.circle(80,120)
t.end_fill()
t.pu()
t.home()
t.goto(-80,70)
t.pd()
t.forward(160)
#舌头
t.pu()
t.home()
t.goto(-50,50)
t.pd()
t.pensize(1)
t.fillcolor('#eb6e1a')
t.seth(40)
t.begin_fill()
t.circle(-40,40)
t.circle(-40,40)
t.seth(40)
t.circle(-40,40)
t.circle(-40,40)
t.seth(220)
t.circle(-80,40)
t.circle(-80,40)
t.end_fill()
#领带
t.pu()
t.goto(-70,12)
t.pensize(14)
t.pencolor('red')
t.pd()
t.seth(-20)
t.begin_fill()
t.circle(200,30)
t.circle(200,10)
#铃铛
t.pu()
t.goto(0,-46)
t.pd()
t.pensize(3)
t.color('black','#f8d102')
t.begin_fill()
t.circle(25)
t.end_fill()
t.pu()
t.goto(-5,-40)
t.pd()
t.pensize(2)
t.color('black','#79675d')
t.begin_fill()
t.circle(5)
t.end_fill()
t.pensize(3)
t.right(115)
t.forward(7)
turtle.mainloop()
(7)多边形
# -*- coding: utf-8 -*-
import turtle
turtle.circle(100,360,4)
turtle.circle(100)
turtle.up()
turtle.goto(100,100)
turtle.down()
turtle.circle(100,360,3)
turtle.up()
turtle.goto(-100,-100)
turtle.down()
turtle.circle(100,360,5)
turtle.hideturtle()
turtle.mainloop()
(8)叮当猫2
# -*- coding: utf-8 -*-
from turtle import *
# 无轨迹跳跃
def my_goto(x, y):
penup()
goto(x, y)
pendown()
# 眼睛
def eyes():
fillcolor("#ffffff")
begin_fill()
tracer(False)
a = 2.5
for i in range(120):
if 0 <= i < 30 or 60 <= i < 90:
a -= 0.05
lt(3)
fd(a)
else:
a += 0.05
lt(3)
fd(a)
tracer(True)
end_fill()
# 胡须
def beard():
my_goto(-32, 135)
seth(165)
fd(60)
my_goto(-32, 125)
seth(180)
fd(60)
my_goto(-32, 115)
seth(193)
fd(60)
my_goto(37, 135)
seth(15)
fd(60)
my_goto(37, 125)
seth(0)
fd(60)
my_goto(37, 115)
seth(-13)
fd(60)
# 嘴巴
def mouth():
my_goto(5, 148)
seth(270)
fd(100)
seth(0)
circle(120, 50)
seth(230)
circle(-120, 100)
# 围巾
def scarf():
fillcolor('#e70010')
begin_fill()
seth(0)
fd(200)
circle(-5, 90)
fd(10)
circle(-5, 90)
fd(207)
circle(-5, 90)
fd(10)
circle(-5, 90)
end_fill()
# 鼻子
def nose():
my_goto(-10, 158)
seth(315)
fillcolor('#e70010')
begin_fill()
circle(20)
end_fill()
# 黑眼睛
def black_eyes():
seth(0)
my_goto(-20, 195)
fillcolor('#000000')
begin_fill()
circle(13)
end_fill()
pensize(6)
my_goto(20, 205)
seth(75)
circle(-10, 150)
pensize(3)
my_goto(-17, 200)
seth(0)
fillcolor('#ffffff')
begin_fill()
circle(5)
end_fill()
my_goto(0, 0)
# 脸
def face():
fd(183)
lt(45)
fillcolor('#ffffff')
begin_fill()
circle(120, 100)
seth(180)
# print(pos())
fd(121)
pendown()
seth(215)
circle(120, 100)
end_fill()
my_goto(63.56,218.24)
seth(90)
eyes()
seth(180)
penup()
fd(60)
pendown()
seth(90)
eyes()
penup()
seth(180)
fd(64)
# 头型
def head():
penup()
circle(150, 40)
pendown()
fillcolor('#00a0de')
begin_fill()
circle(150, 280)
end_fill()
# 画哆啦A梦
def Doraemon():
# 头部
head()
# 围脖
scarf()
# 脸
face()
# 红鼻子
nose()
# 嘴巴
mouth()
# 胡须
beard()
# 身体
my_goto(0, 0)
seth(0)
penup()
circle(150, 50)
pendown()
seth(30)
fd(40)
seth(70)
circle(-30, 270)
fillcolor('#00a0de')
begin_fill()
seth(230)
fd(80)
seth(90)
circle(1000, 1)
seth(-89)
circle(-1000, 10)
# print(pos())
seth(180)
fd(70)
seth(90)
circle(30, 180)
seth(180)
fd(70)
# print(pos())
seth(100)
circle(-1000, 9)
seth(-86)
circle(1000, 2)
seth(230)
fd(40)
# print(pos())
circle(-30, 230)
seth(45)
fd(81)
seth(0)
fd(203)
circle(5, 90)
fd(10)
circle(5, 90)
fd(7)
seth(40)
circle(150, 10)
seth(30)
fd(40)
end_fill()
# 左手
seth(70)
fillcolor('#ffffff')
begin_fill()
circle(-30)
end_fill()
# 脚
my_goto(103.74, -182.59)
seth(0)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(-15, 180)
fd(90)
circle(-15, 180)
fd(10)
end_fill()
my_goto(-96.26, -182.59)
seth(180)
fillcolor('#ffffff')
begin_fill()
fd(15)
circle(15, 180)
fd(90)
circle(15, 180)
fd(10)
end_fill()
# 右手
my_goto(-133.97, -91.81)
seth(50)
fillcolor('#ffffff')
begin_fill()
circle(30)
end_fill()
# 口袋
my_goto(-103.42, 15.09)
seth(0)
fd(38)
seth(230)
begin_fill()
circle(90, 260)
end_fill()
my_goto(5, -40)
seth(0)
fd(70)
seth(-90)
circle(-70, 180)
seth(0)
fd(70)
#铃铛
my_goto(-103.42, 15.09)
fd(90)
seth(70)
fillcolor('#ffd200')
# print(pos())
begin_fill()
circle(-20)
end_fill()
seth(170)
fillcolor('#ffd200')
begin_fill()
circle(-2, 180)
seth(10)
circle(-100, 22)
circle(-2, 180)
seth(180-10)
circle(100, 22)
end_fill()
goto(-13.42, 15.09)
seth(250)
circle(20, 110)
seth(90)
fd(15)
dot(10)
my_goto(0, -150)
# 画眼睛
black_eyes()
if __name__ == '__main__':
screensize(800,600, "#f0f0f0")
pensize(3) # 画笔宽度
speed(9) # 画笔速度
Doraemon()
my_goto(100, -300)
write('by dongdong', font=("Bradley Hand ITC", 30, "bold"))
mainloop()
(9)爱心
import turtle
turtle.screensize(800,600,'white')
turtle.title('画一个心')
turtle.color('red','pink')
turtle.hideturtle()
turtle.pensize(3)
turtle.speed(0)
turtle.begin_fill()
turtle.left(135)
turtle.fd(100)
turtle.right(180)
turtle.circle(50,-180)
turtle.left(90)
turtle.circle(50,-180)
turtle.right(180)
turtle.fd(100)
turtle.end_fill()
turtle.up()
turtle.goto(-45,80)
turtle.write('xxxxx',font=('楷体',13))
# align='center'
turtle.done()
(10)基础画图
import turtle as t
a=t.Pen()
a.speed(40)
a.hideturtle()
for i in range(90):
a.forward(i)
a.right(30)
t.mainloop()
import turtle as t
a=t.Pen()
a.hideturtle()
for i in range(1000):
a.fd(i)
a.left(75)
t.mainloop()
import turtle
t=turtle.Pen()
t.hideturtle()
t.pensize(20)
t.pencolor('red')
t.begin_fill()
t.fillcolor('black')
t.circle(100)
t.end_fill()
turtle.mainloop()
import turtle
turtle.color('red')
turtle.fillcolor('yellow')
turtle.pensize(22)
turtle.begin_fill()
turtle.circle(100)
turtle.end_fill()
turtle.hideturtle()
turtle.mainloop()
(11)圣诞树
import turtle
screen = turtle.Screen()
screen.setup(800,600)
circle = turtle.Turtle()
circle.shape('circle')
circle.color('red')
circle.speed('fastest')
circle.up()
square = turtle.Turtle()
square.shape('square')
square.color('green')
square.speed('fastest')
square.up()
circle.goto(0,280)
circle.stamp()
k = 0
for i in range(1, 17):
y = 30*i
for j in range(i-k):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
if i % 4 == 0:
x = 30*(j+1)
circle.color('red')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
k += 2
if i % 4 == 3:
x = 30*(j+1)
circle.color('yellow')
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
square.color('brown')
for i in range(17,20):
y = 30*i
for j in range(3):
x = 30*j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
turtle.exitonclick()
4、计算日期差
import time
import datetime
# 计算两个日期相差天数,自定义函数名,和两个日期的变量名。
def Caltime(date1, date2):
# %Y-%m-%d为日期格式,其中的-可以用其他代替或者不写,但是要统一,同理后面的时分秒也一样;可以只计算日期,不计算时间。
# date1=time.strptime(date1,"%Y-%m-%d %H:%M:%S")
# date2=time.strptime(date2,"%Y-%m-%d %H:%M:%S")
date1 = time.strptime(date1, "%Y-%m-%d")
date2 = time.strptime(date2, "%Y-%m-%d")
# 根据上面需要计算日期还是日期时间,来确定需要几个数组段。下标0表示年,小标1表示月,依次类推...
# date1=datetime.datetime(date1[0],date1[1],date1[2],date1[3],date1[4],date1[5])
# date2=datetime.datetime(date2[0],date2[1],date2[2],date2[3],date2[4],date2[5])
date1 = datetime.datetime(date1[0], date1[1], date1[2])
date2 = datetime.datetime(date2[0], date2[1], date2[2])
# 返回两个变量相差的值,就是相差天数
return date2 - date1
def is_date(str):
try:
time.strptime(str,"%Y-%m-%d")
return True
except:
return False
if __name__=='__main__':
#提示信息请根据实际情况更改
print('请输入较早日期(格式例:xxxx-xx-xx):')
while True:
dt1=input()
if is_date(dt1)==True:
break
else:
print('请输入正确的日期!!!请重新输入!!!')
#print(is_date(dt1))
print('\n请输入较晚日期(格式为:xxxx-xx-xx):')
while True:
dt2=input()
if is_date(dt2)==True:
break
else:
print('请输入正确的日期!!!请重新输入!!!')
#将结果保存到当前目录下的t_day.txt,该txt文档会自动创建,名字可自定义。
with open('./t_day.txt','a') as f:
f.write('较早日期为:'+dt1+' 较晚日期为:'+dt2)
f.write('\n两个日期相差 ')
print (Caltime(dt1,dt2),file=f)
f.write('\n')
f.close
5、抽奖器
import sys
import tkinter as Tkinter
from tkinter import *
import random
data = ['奖品1', '奖品2', '奖品3', '奖品4', '奖品5', '奖品6', '奖品6', '奖品6', '奖品6']
going = True
is_run = False
def lottery_roll(var1, var2):
global going
show_member = random.choice(data)
var1.set(show_member)
if going:
window.after(20, lottery_roll, var1, var2)
else:
var2.set('恭喜你获得{} !!!'.format(show_member))
going = True
return
def lottery_start(var1, var2):
global is_run
if is_run:
return
is_run = True
var2.set('开抽,欧皇就是你!!!')
lottery_roll(var1, var2)
def lottery_end():
global going, is_run
if is_run:
going = False
is_run = False
if __name__ == '__main__':
window = Tkinter.Tk()
window.geometry('405x320+250+15')
window.title('伯乐码奖品抽奖器')
bg_label = Label(window, width=70, height=24, bg='#ECf5FF')
bg_label.place(anchor=NW, x=0, y=0)
var1 = StringVar(value='即 将 开 始')
show_label1 = Label(window, textvariable=var1, justify='left', anchor=CENTER, width=17, height=3, bg='#BFEFFF',
font='楷体 -44 bold', foreground='black')
show_label1.place(anchor=NW, x=21, y=20)
var2 = StringVar(value='开抽,欧皇就是你!!!')
show_label2 = Label(window, textvariable=var2, justify='left', anchor=CENTER, width=38, height=3, bg='#ECf5FF',
font='楷体 -18 bold', foreground='red')
show_label2.place(anchor=NW, x=21, y=240)
button1 = Button(window, text='开始', command=lambda: lottery_start(var1, var2), width=14, height=2, bg='#A8A8A8',
font='宋体 -18 bold')
button1.place(anchor=NW, x=20, y=175)
button2 = Button(window, text='结束', command=lambda: lottery_end(), width=14, height=2, bg='#A8A8A8',
font='宋体 -18 bold')
button2.place(anchor=NW, x=232, y=175)
window.mainloop()
import tkinter
import threading
import time
from tkinter import *
from tkinter import messagebox
from tkinter import messagebox
root = tkinter.Tk()
root.title('啊-Guess the word')
root.minsize(300,330)
label =(root)
label = Label(root,text = '11111111111',font = ('微软雅黑',11),fg = 'red')
label.grid(row =0 ,column =0)
btn1 = tkinter.Button(root,text ='1',width = 5,height =2,bg='blue')
btn1.place(x=20,y=70)
btn2 = tkinter.Button(root,text = '2',width = 5,height =2,bg ='white')
btn2.place(x=90,y=70)
btn3 = tkinter.Button(root,text = '3',width = 5,height =2,bg ='white')
btn3.place(x=160,y=70)
btn4 = tkinter.Button(root,text = '3',width = 5,height =2,bg ='white')
btn4.place(x=230,y=140)
btn5 = tkinter.Button(root,text = '5',width = 5,height =2,bg ='white')
btn5.place(x=230,y=70)
btn6 = tkinter.Button(root,text = '6',width = 5,height =2,bg ='white')
btn6.place(x=230,y=210)
btn7 = tkinter.Button(root,text = '7',width = 5,height =2,bg ='white')
btn7.place(x=230,y=280 )
btn8 = tkinter.Button(root,text = '8',width = 5,height =2,bg ='white')
btn8.place(x=160,y=280)
btn9 = tkinter.Button(root,text = '9',width = 5,height =2,bg ='white')
btn9.place(x=90,y=280 )
btn10 = tkinter.Button(root,text = '10',width = 5,height =2,bg ='white')
btn10.place(x=20,y=280)
btn11 = tkinter.Button(root,text = '11',width = 5,height =2,bg ='white')
btn11.place(x=20,y=210)
btn12 = tkinter.Button(root,text = '12',width = 5,height =2,bg ='white')
btn12.place(x=20,y=140)
herolists = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn11]
isloop = False
stopsing = False
#定义停止id
stopid = None
#定义函数,循环选项 设置背景颜色,选中的为红色,
def round():
global isloop
global stopid
if isloop ==True:
return
i = 1
#判断一个对象是否已经类型 type
#考虑继承关系
if isinstance(stopid,int):
i = stopid
while True:
#
time.sleep(0.05)#延时
for x in herolists:
x['bg'] = 'white'
herolists[i]['bg'] ='blue'
i += 1
if i >= len(herolists):
i = 0
if stopsing == True:
isloop =False
stopid = i
break
#停止
def stop1():
global stopsing
if stopsing == True:
return
stopsing = True
#开始
def newtask():
global isloop
global stopsing
stopsing = False
#建立线程
t = threading.Thread(target = round )
t.start()
isloop = True
btn_start = tkinter.Button(root,text = '开始',bg = 'red',command = newtask)
btn_start.place(x=90,y=175)
#
btn_stop = tkinter.Button(root,text = '结束',bg = 'yellow',command = stop1)
btn_stop.place(x=160,y=175 )
root.mainloop()