编程编出星星代码怎么编

时间:2025-01-23 22:26:53 游戏攻略

使用Python和turtle库

```python

import turtle

def draw_star(n):

for i in range(n):

print(" " * (n - i - 1) + "*" * (2 * i + 1))

n = int(input("请输入星星行数:"))

draw_star(n)

```

使用Python和pygame库

```python

import pygame

from pygame.math import Vector3

def draw_star():

pygame.init()

display = (800, 600)

pygame.display.set_mode(display, DOUBLEBUF | OPENGL)

gluPerspective(45, (display / display), 0.1, 50.0)

glTranslatef(0.0, 0.0, -5)

num_stars = 1000

stars = np.array([Vector3(random.uniform(-10, 10), random.uniform(-10, 10), random.uniform(-10, 10)) for _ in range(num_stars)])

clock = pygame.time.Clock()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

pygame.quit()

return

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

for star in stars:

glColor3f(1, 1, 1)

glBegin(GL_POINTS)

glVertex3f(star.x, star.y, star.z)

glEnd()

pygame.display.flip()

clock.tick(60)

draw_star()

```

使用C语言

```c

include

int main() {

int rows = 5; // 设置星星行数

for (int i = 1; i <= rows; i++) {

for (int j = 1; j <= rows - i; j++) {

printf(" ");

}

for (int k = 1; k <= 2 * i - 1; k++) {

printf("*");

}

printf("\n");

}

return 0;

}

```

使用Java

```java

public class DrawStars {

public static void main(String[] args) {

int rows = 10; // 星星的行数

int cols = 10; // 星星的列数

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

for (int j = 0; j < cols; j++) {

System.out.print("* "); // 打印星星

}

System.out.println(); // 换行

}

}

}

```

这些代码示例分别使用了不同的编程语言和图形库来绘制星星。你可以根据自己的需求和熟悉程度选择合适的代码进行尝试。