程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

When I drew a Christmas tree for my female classmate in Python

編輯:Python

Preface

hi , Ancestors , It won't be Christmas. I'll be alone ? Today, let's talk about how to use python Let's draw a Christmas tree , Learn to send it to her soon , My circle of friends has let the Christmas tree brush the screen ! I'm here to teach you , After reading it, remember to give it to the third company !



Of course, they are all beautiful , But the code God mo , Is to make something different ,python Let's draw one ! Or get a front end to draw !

Start teaching

This time we used python Medium turtle( Sea turtle ) library , yes python A built-in Library in does not require additional installation

  1. create a window
  2. Set brush
  3. Drawing graphics

establish

setup() function , The parameter is width,height,startx=None,starty=None, Represents the window width respectively , high , And , The abscissa and ordinate of the window on the computer screen .

turtle.setup(800,600)

Set brush

Three attribute functions

pensize(<width>) # thickness
speed(speed) # Speed of motion
color(color) # Set brush color

The plot

This brings us to the coordinate system of sea turtles , Take the center of the window as the origin , Take the right as the positive direction , For the above y Affirmative direction .

Move

  1. forward(distance) # Move forward
  2. backward(distance) # Move backward
  3. goto(x,y=None)# Move to the appropriate location

Angle control

It's similar to the movement above , All are 3 Item control
4. right(degree)# Turn right
5. left(degree)# Turn left
6. seth(degree)# Turn in a certain direction

Graph drawing , Image filling

This won't be used today , I don't think so , Welcome your loving ancestors , See the comments section !

The film begins

from turtle import *
import random # Random , To make dots
import time
n = 100.0
speed("fastest") # Fast
screensize(bg='seashell')
left(90)
forward(3 * n)
color("orange", "yellow")
begin_fill()
left(126)
for i in range(5):
forward(n / 5)
right(144)
forward(n / 5)
left(72)
end_fill()
right(126)
color("dark green")
backward(n * 4.8)
def tree(d, s):
if d <= 0: return
forward(s)
tree(d - 1, s * .8)
right(120)
tree(d - 3, s * .5)
right(120)
tree(d - 3, s * .5)
right(120)
backward(s)
tree(15, n)
backward(n / 2)
for i in range(200):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
up()
forward(b)
left(90)
forward(a)
down()
if random.randint(0, 1) == 0:
color('tomato')
else:
color('wheat')
circle(2)
up()
backward(a)
right(90)
backward(b)
time.sleep(60) # Process delay time

Last

This article belongs to the title party , any similarity , It must not be a code God , Ha ha ha


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved