Python implements the example code for drawing python Python.

Source: Internet
Author: User

Python implements the example code for drawing python Python.

This article mainly studies the example of the Python programming turtle and draws a colorful Python .. The details are as follows.

In the 2nd-week after-school exercise, there was a question that required to modify the "Python rendering" program and use different colors for each part of the Python to draw a color Python.

The original Python rendering program is as follows:

Because I was not familiar with Python at the beginning, I added some comments for easy understanding.

# Python draw import turtledef drawSnake (rad, angle, len, neckrad): for I in range (len): turtle. circle (rad, angle) # draw circle rad: circle radius angle: arc degree turtle. circle (-rad, angle) # Draw a circle turtle far from rad on the right of the running track. circle (rad, angle/2) turtle. fd (rad) # indicates crawling in a straight line, forward. The parameter indicates the crawling distance from turtle. circle (neckrad + 1,180) turtle. fd (rad * 2/3) def main (): turtle. setup (1300,800,) # Set the window width + height + start position pythonsize = 30 turtle. pensize (pythonsize) # Set the pen width to turtle. pencolor ("blue") # Set the pen color turtle. seth (-40) # Set the start angle of the PEN drawSnake (, 5, pythonsize/2)

After carefully analyzing the code, we found that the color of the paint brush in this code is set by pencolor (). If you want to change the color to color, you obviously cannot use this method, therefore, I thought of using the list to solve this problem, so I created a list variable named colors in drawSnake and placed six colors in the list, as shown below:

colors = [“red”,”orange”,”yellow”,”green”,”cyan”,”blue”]

Before that, because the len value in the original program is 5, the figure shows that the python has only five parts. In order to display the result as colorful, I changed the len value to 6.

In the for loop, a statement is added before painting to set the color of the paint brush:

turtle.color(colors[i])

In colors [I], the I and for loops have the same variable. When I changes from 0 to 5, the corresponding color () select a color from the color name list named colors added in the previous step and set it to the color of the paint brush.

The Python head uses turtle. color ("purple") to set it to 7th colors.

After running it, I found that every drawing in turtle started from the center of the drawing board. I thought it was not very nice, so I wanted to find a way to change the starting point of the drawing, the current method I have found is to use the penup (), goto (), pendown () commands to change the position of the starting point of the paint brush, as shown below:

turtle.penup()
turtle.goto(-350,0)
turtle.pendown()

If a better method is found later, update it.

Just draw a colorful python ~

# Import turtledef drawSnake (rad, angle, len, neckrad): colors = ["red", "orange", "yellow", "green", "cyan ", "blue"] for I in range (len): turtle. color (colors [I]) turtle. circle (rad, angle) turtle. circle (-rad, angle) turtle. color ("purple") turtle. circle (rad, angle/2) turtle. fd (rad) turtle. circle (neckrad + 1,180) turtle. fd (rad * 2/3) def main (): turtle. setup (1300,800,) turtle. penup () turtle. goto (-350,0) turtle. pendown () pythonsize = 30 turtle. pensize (pythonsize) turtle. seth (-40) drawSnake (40, 80, 6, pythonsize/2) main ()

Result Display:

Summary

The above is all the content of this article on Python's implementation of the example code of colorful pythons, and I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.