[C] c语言绘制圣诞树 →→→→→进入此内容的聊天室

来自 Morose Frog, 2024-03-07, 写在 C, 查看 29 次.
URL http://www.code666.cn/view/fc6bb0ed
  1. /*******************************
  2. 圣诞树
  3. byC语言小白入门
  4. *******************************/
  5. #include<stdio.h>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. #include<Windows.h>
  9. #define X 25 //画面长度
  10. int background[20][2 * X] = { 0 };
  11. int SNOW = 30; //雪花密度
  12. /*******************************
  13. 画树
  14. *******************************/
  15. void tree()
  16. {
  17. int i, j, x, y;
  18. for (i = 0; i < 3; i++)
  19. {
  20. y = i;
  21. for (j = 0; j < 2 * i + 1; j++)
  22. {
  23. background[y][(X - i) + j] = 1;
  24. }
  25. }
  26. for (i = 0; i < 5; i++)
  27. {
  28. y++;
  29. for (j = 0; j < 2 * (i + 1) + 1; j++)
  30. {
  31. background[y][(X - (i + 1)) + j] = 1;
  32. }
  33. }
  34. for (i = 0; i < 7; i++)
  35. {
  36. y++;
  37. for (j = 0; j < 2 * (i + 3) + 1; j++)
  38. {
  39. background[y][(X - (i + 3)) + j] = 1;
  40. }
  41. }
  42. for (i = 0; i < 5; i++)
  43. {
  44. y++;
  45. for (j = 0; j < 3; j++)
  46. {
  47. background[y][X + (2 * j - 2)] = 2;
  48. }
  49. }
  50. }
  51. /*******************************
  52. 画雪花
  53. *******************************/
  54. void snow()
  55. {
  56. int i;
  57. srand(time(NULL));
  58. for (i = 0; i < SNOW; i++)
  59. {
  60. int x, y;
  61. x = rand() % (2 * X);
  62. y = rand() % 20;
  63. if ((background[y][x] == 0))
  64. {
  65. background[y][x] = 3;
  66. }
  67. }
  68. }
  69. /*******************************
  70. 打印
  71. *******************************/
  72. void display()
  73. {
  74. int x, y;
  75. for (y = 0; y < 20; y++)
  76. {
  77. for (x = 0; x < 2 * X; x++)
  78. {
  79. if (background[y][x] == 0)
  80. {
  81. printf(" ");
  82. }
  83. if (background[y][x] == 1)
  84. {
  85. printf("0");
  86. }
  87. if (background[y][x] == 2)
  88. {
  89. printf("|");
  90. }
  91. if (background[y][x] == 3)
  92. {
  93. printf("*");
  94. }
  95. }
  96. printf("\n");
  97. }
  98. }
  99. /*******************************
  100. 清除雪花
  101. *******************************/
  102. void clear_snow()
  103. {
  104. int i, j;
  105. for (i = 0; i < 20; i++)
  106. {
  107. for (j = 0; j < 2 * X; j++)
  108. {
  109. if (background[i][j] == 3)
  110. {
  111. background[i][j] = 0;
  112. }
  113. }
  114. }
  115. }
  116. void main()
  117. {
  118. tree();
  119. while (1)
  120. {
  121. snow();
  122. display();
  123. Sleep(1);
  124. system("cls");
  125. clear_snow();
  126. }
  127. }

回复 "c语言绘制圣诞树"

这儿你可以回复上面这条便签

captcha