Everyone can write Python Visualization applet Second: rotation fireworks

interest is the best teacher

Boring programming easy to give up, interest is the best teacher. Whether children or adults, only to discover that this thing is really interesting, we will be very dedicated to do this, such as playing games. If the program can become fun, like playing a game, I believe many people are particularly willing to do about it

Fun of the game is to play

Grasp the key, it is not difficult to adhere to

King of Glory game continued to hit for a few years, the reason is simple, interesting, touching the phone every time, just like my heart has a voice calling, play one, to play a not play, the result of playing playing Its daybreak. If you can learn to have such fun in which the stick is not a difficult thing to believe.

( A brief review of the code on an article, if not understand, you can look and read on one oh, attach address everyone can write a Python visual applets, take you into the world of programming )

# 导入海龟库
import turtle
# 拿到海龟画笔
t = turtle.Pen()
# 循环100次
for x in range(100):
t.forward(x) # 往画笔箭头的方向前进的像素值,x代表具体前进多少
t.left(90) # 画笔箭头左转90度

Use a brush and color

首先我们来修改一下背景颜色,代码是 turtle.bgcolor("black")

Let's modify the color of the brush, where we first declare the four colors  colors = [ 'purple', ' yellow', 'blue', 'red'] names of these colors is the English name, use t.pencolor () when the method turtles library will automatically help us as we want to render this color

Everyone can write Python Visualization applet Second: rotation fireworks

The full code

t.pencolor(colors[x%4])

This line means that what color pen, wherein colors [x% 4] determines which acquires a color in the end. x is the mean value of 4% x 4 taking the remainder, the final result is certainly one of 0,1,2,3. So in other words get colors from one color to this array. colors is an array, the array comprising upper our kinds of colors of each element of the statement corresponds to a position of the subscript 0 represents purple purple (both sequential code numbers starting from 0), represents yellow yellow, represents bule blue, 3 red represents the red. Result code operates as follows:

Everyone can write Python Visualization applet Second: rotation fireworks

 

Here we have completed the basic color manipulation, graphic below we feel a change, this time round we draw four different colors.

Everyone can write Python Visualization applet Second: rotation fireworks

And before the difference is that here the graphic into a circular painting

Everyone can write Python Visualization applet Second: rotation fireworks

Results of code to run

Constantly watching you brush painting, is not there a sense of accomplishment and fun of it.

Using the code so that it fireworks

Everyone can write Python Visualization applet Second: rotation fireworks

Here the added width pen set

Everyone can write Python Visualization applet Second: rotation fireworks

Code running results

Add a variable, so that the same code produces different results Graphics

Here we add a variable sides, it means we want to use several colors and we want to cut several images, of course, the value of the sides must be less than or equal to 6, that is, we set the number of colors present, if large , the code will be thrown. The above figure is equivalent to the sides we set became a result of 6, below us one to experiment with what the sides are set to other values ​​result of it.

  • The value of the sides is set to 5

Everyone can write Python Visualization applet Second: rotation fireworks

sides is equal to the code writing 5

Everyone can write Python Visualization applet Second: rotation fireworks

sides is equal to the result code to run 5

  • The value is set to 4 sides

Everyone can write Python Visualization applet Second: rotation fireworks

4 equal sides writing code

Everyone can write Python Visualization applet Second: rotation fireworks

4 equal sides code running results

  • Sides is set to the value 3

Everyone can write Python Visualization applet Second: rotation fireworks

sides is equal to the code writing 3

Everyone can write Python Visualization applet Second: rotation fireworks

3 runtime code is equal sides Results

  • Sides is set to the value 2

Everyone can write Python Visualization applet Second: rotation fireworks

sides equal to the code writing 2

Everyone can write Python Visualization applet Second: rotation fireworks

code running sides equal 2 results

Write code, just copy taboo Oh do not practice, we must remember that his practice. Write out of their own sense of accomplishment is unparalleled, we can not simply look at it, this is bad, but also not learn, but can not have fun. Oh, I want to have fun will do it. Come quickly hands playing!

If you write code like playing a game, then we are interested in whether there will be a qualitative improvement it?

Guess you like

Origin www.cnblogs.com/lingfengblogs/p/11093303.html