Python历史、发展
Python用途
Python编程环境
Python学习资源:
Python turtle绘图、词云制作
Python tkinter 免费电影
微软免费集成开发(推荐)
IDLE、VS code、pycharm……sublime、anaconda、jupyter
----以上实例可在信息技术博客搜索关键字找到。
x#!/bin/python3
from turtle import *
from time import *
# 介绍字典
# 使用颜色选择器查找并选择新颜色
colours = {
'太空': '#060608',
'月亮灰': '#BCBDEF',
'非常青柠': '#A7E30E',
'深海': '#226363',
'真是树莓': '# FA057F',
'阴沉灰': '#363332',
'魅力橙': '#F37C06',
'炫酷青': '#4FEEF6',
'完美紫': '#6820B0',
'可爱柠檬': '#FBF312',
}
screen = Screen()
screen.setup(400, 400)
screen.bgcolor(colours['太空'])
penup()
goto(0, 100)
color(colours['真是树莓'])
style = ('Arial', 40, 'bold')
write('你好', font=style, align='center')
right(90)
forward(60)
color(colours['魅力橙'])
write('世界', font=style, align='center')
hideturtle()
sleep(3)
goto(0,0)
color(colours['月亮灰'])
dot(350)
setheading(-90)
penup()
hideturtle()
goto(0, 135)
color(colours['非常青柠'])
style=('Verdana', 20, 'bold')
write('一部典型的', font=style, align='center')
forward(40)
color(colours['真是树莓'])
write('智能手机', font=('Verdana', 25, 'bold'), align='center')
forward(40)
color(colours['深海'])
write('比', font=('Verdana', 18, 'bold'), align='center')
forward(40)
color(colours['魅力橙'])
write('着陆在', font=('Verdana', 25, 'bold'), align='center')
forward(40)
color(colours['完美紫'])
write('月球上的', font=style, align='center')
forward(40)
color(colours['炫酷青'])
write('阿波罗11号', font=('Verdana', 25, 'bold'), align='center')
color(colours['可爱柠檬'])
forward(40)
write('拥有更强大的', font=style, align='center')
color(colours['阴沉灰'])
forward(40)
write('计算能力', font=('Verdana', 25, 'bold'), align='center')
color('white')
forward(50)
write('- 南希·吉布斯, 2012', font=('Arial', 14, 'normal'))
xxxxxxxxxx
#!/bin/python3
from turtle import *
from random import *
def randomcolour():
###如果您不使用trinket,请取消下面的注释###
colormode(255)
red = randint(0, 255)
green = randint(0, 255)
blue = randint(0, 255)
color(red, green, blue)
def randomplace():
penup()
x = randint(-100, 100)
y = randint(-100, 100)
goto(x, y)
pendown()
def randomheading():
heading = randint(0, 360)
setheading(heading)
def drawrectangle():
randomcolour()
randomplace()
hideturtle()
length = randint(10, 100)
height = randint(10, 100)
begin_fill()
forward(length)
right(90)
forward(height)
right(90)
forward(length)
right(90)
forward(height)
right(90)
end_fill()
shape("turtle")
speed(0)
for i in range(1, 30):
randomcolour()
randomplace()
randomheading()
stamp()
#挑战-使用内置的dot函数
def drawcircle():
radius = randint(5, 100)
randomcolour()
randomplace()
dot(radius)
def drawstar():
randomcolour()
randomplace()
randomheading()
begin_fill()
size = randint(20, 100)
#绘制星形
for side in range(5):
left(144)
forward(size)
end_fill()
clear()
setheading(0)
for i in range(20):
drawrectangle()
clear()
for i in range(50):
drawcircle()
clear()
for i in range(20):
drawstar()
xxxxxxxxxx
#!/bin/python3
from random import randint
player = input('剪刀(s),石头(r)或布(p)?')
if(player == 'r'):
print('O', end=' ')
elif(player == 'p'):
print('___', end=' ')
elif(player == 's'):
print('>8', end=' ')
else:
print('??')
print('vs', end=' ')
chosen = randint(1,3)
if(chosen == 1):
computer = 'r'
print('O')
elif(chosen == 2):
computer = 'p'
print('___')
else:
computer = 's'
print('>8')
if(player == computer):
print('平手!')
elif(player == 'r' and computer == 's'):
print('玩家赢了!')
elif(player == 'r' and computer == 'p'):
print('电脑赢了!')
elif(player == 'p' and computer == 'r'):
print('玩家赢了!')
elif(player == 'p' and computer == 's'):
print('电脑赢了!')
elif(player == 's' and computer == 'p'):
print('玩家赢了!')
elif(player == 's' and computer == 'r'):
print('电脑赢了!')
else:
print('咦?')
xxxxxxxxxx
import turtle
import math
import random
wn = turtle.Screen()
wn.bgcolor('black')
Albert = turtle.Turtle()
Albert.speed(0)
Albert.color('white')
rotate=int(360)
def drawCircles(t,size):
for i in range(10):
t.circle(size)
size=size-4
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(Albert,100,10)
Steve = turtle.Turtle()
Steve.speed(0)
Steve.color('yellow')
rotate=int(90)
def drawCircles(t,size):
for i in range(4):
t.circle(size)
size=size-10
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(Steve,100,10)
Barry = turtle.Turtle()
Barry.speed(0)
Barry.color('blue')
rotate=int(80)
def drawCircles(t,size):
for i in range(4):
t.circle(size)
size=size-5
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(Barry,100,10)
Terry = turtle.Turtle()
Terry.speed(0)
Terry.color('orange')
rotate=int(90)
def drawCircles(t,size):
for i in range(4):
t.circle(size)
size=size-19
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(Terry,100,10)
Will = turtle.Turtle()
Will.speed(0)
Will.color('pink')
rotate=int(90)
def drawCircles(t,size):
for i in range(4):
t.circle(size)
size=size-20
def drawSpecial(t,size,repeat):
for i in range (repeat):
drawCircles(t,size)
t.right(360/repeat)
drawSpecial(Will,100,10)
xxxxxxxxxx
# A simple drawing program.
# Click on a color then click on the trinket screen
# to draw lines of that color. The gray color will
# allow you to move the turtle without drawing.
#
# Patrick Rodriguez, @stratospark
from turtle import *
screen = Screen()
screenMinX = -screen.window_width()/2
screenMinY = -screen.window_height()/2
screenMaxX = screen.window_width()/2
screenMaxY = screen.window_height()/2
screen.setworldcoordinates(screenMinX,screenMinY,screenMaxX,screenMaxY)
brush_turtle = Turtle()
brush_turtle.goto(0, 0)
brush_turtle.speed(10)
# Set up event handler to have the brush_turtle draw a line
# to the point that the user clicks on
def on_screen_click(x, y):
#print "%d, %d" % (x, y)
if y < screenMaxY - 40: # only draw if clicked below color squares
brush_turtle.goto(x, y)
screen.onclick(on_screen_click)
class ColorPicker(Turtle):
def __init__(self, color="red",num=0):
Turtle.__init__(self)
self.num = num
self.color_name = color
self.speed(0)
self.shape("circle")
self.color("black", color)
self.penup()
# hack to register click handler to instance method
self.onclick(lambda x, y: self.handle_click(x, y))
def draw(self):
self.setx(screenMinX+110+self.num*30)
self.sety(screenMaxY - 20)
def handle_click(self, x, y):
if self.color_name == "#F9F9F9":
brush_turtle.penup()
brush_turtle.color("black")
else:
brush_turtle.pendown()
brush_turtle.color(self.color_name)
# Suppress animations while interface is being drawn
screen.tracer(0)
ui_turtle = Turtle()
ui_turtle.ht()
ui_turtle.penup()
ui_turtle.goto(screenMinX, screenMaxY - 23)
ui_turtle.write("TurtleDraw!", align="left", font=("Courier", 10, "bold"))
# Create color choosing squares at the top of screen
colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet", "black", "#F9F9F9"]
color_pickers = [ColorPicker(color=c, num=i) for i, c in enumerate(colors)]
for picker in color_pickers:
picker.draw()
# Resume animations now that main interface has been drawn
screen.tracer(1)