Python画圣诞树 圣诞快乐!

  • 2019 年 11 月 8 日
  • 筆記

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_42449444/article/details/85248669

写在前面:

昨晚并没有断片,然而!一觉醒来发现11点40啦。不知名的香水,窒息的鬼魅。

ZEPETO这款APP也太好玩了叭。额,走远了 该学就学,该玩就玩。Study hard, play hard嘛!下面还是画棵圣诞树叭?

期末考试快来了,不是很清楚二叉树、AVL树、哈夫曼树、B树··· 额,我这个菜鸡只知道圣诞树。下面就画一棵圣诞树吧。

C++代码:

#include <bits/stdc++.h>  using namespace std;    void Christmas_Tree(int height)  {      int stars = 1;      for (int i = 0; i < height; i++)      {          for (int j = 0; j < height-i; j++)          {              cout << " ";          }          for(int j = 0; j < stars; j++)          {              cout << "*";          }          cout << endl;          stars += 2;      }      for (int i = 0; i < height/4; i++)   //让高一点的圣诞树看起来正常一些      {          for (int j = 0; j < height; j++)   //在正中间画树干          {              cout << " ";          }          cout << "|" << endl;      }    }    int main()  {      int n;   //树叶的高度      cout << "请输入这棵圣诞树的高度:";      cin >> n;      Christmas_Tree(n);      return 0;  }

圣诞树的效果图: (嫩绿配骚粉的界面)

好了,C++圣诞树画完了,可以关闭网页了(手动狗头)。 Life is short, use python! 调用turtle库来骚一波。

import turtle as t  screen = t.Screen()  screen.setup(800,700)  circle = t.Turtle()  circle.shape('circle')  circle.color('red')  circle.speed('fastest')  circle.up()  square = t.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()  t.exitonclick()

参考推文:圣诞节!教你用Python画棵圣诞树