怎么样编程写圣诞树

时间:2025-01-28 07:25:56 网络游戏

使用Python和Turtle库

```python

import turtle

def draw_christmas_tree(height):

for i in range(height):

打印空格

for j in range(height - i - 1):

turtle.forward(1)

turtle.left(90)

打印星号

for k in range(2 * i + 1):

turtle.forward(1)

turtle.left(90)

turtle.backward(2 * (2 * i + 1) - 1)

turtle.left(90)

turtle.forward(1)

turtle.left(90)

设置树的高度

height = 5

draw_christmas_tree(height)

隐藏turtle

turtle.hideturtle()

结束绘制

turtle.done()

```

使用C语言

```c

include

void draw_christmas_tree(int height) {

int i;

for (i = 0; i < height; i++) {

// 打印树干

printf("%*s", height - i - 1, "");

printf("*");

for (int j = 0; j < 2 * i + 1; j++) {

printf("*");

}

printf("\n");

}

// 打印树干

printf("%*s", height - 1, "");

printf("|");

for (int i = 0; i < 2; i++) {

printf("|");

}

printf("\n");

}

int main() {

int height = 5;

draw_christmas_tree(height);

return 0;

}

```

使用HTML5和CSS3

```html

Christmas Tree