python画一个星空
时间: 2023-06-07 18:09:41 浏览: 167
好的,请稍等片刻,正在为您生成代码。
下面是Python代码:
```python
from tkinter import Tk, Canvas
from random import randint
# 创建窗口
root = Tk()
root.title('星空')
# 获取窗口尺寸
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
# 创建画布
canvas = Canvas(root, width=width, height=height, bg='black')
canvas.pack()
# 绘制星星
for i in range(500):
x = randint(-width / 2, width / 2)
y = randint(-height / 2, height / 2)
size = randint(1, 3)
canvas.create_oval(width / 2 + x, height / 2 + y, width / 2 + x + size, height / 2 + y + size, fill='white', outline='white')
root.mainloop()
```
运行上面的代码将会生成一个黑色背景的星空画面,其中包含500颗随机分布的白色星星。您可以尝试调整生成星星的数量、大小、颜色等参数,来获得更加逼真的星空效果。
相关问题
Python画一个动态星空
以下是使用Python和Pygame库创建动态星空的示例代码:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 定义窗口的宽度和高度
screen_width = 800
screen_height = 600
# 创建窗口
screen = pygame.display.set_mode((screen_width, screen_height))
# 定义星星的数量
num_stars = 500
# 定义星星的列表
stars = []
# 创建星星
for i in range(num_stars):
# 随机生成星星的位置和大小
x = random.randint(0, screen_width)
y = random.randint(0, screen_height)
size = random.randint(1, 3)
stars.append((x, y, size))
# 定义星星的颜色
star_color = (255, 255, 255)
# 定义移动的速度
speed = 1
# 创建游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# 重新绘制背景
screen.fill((0, 0, 0))
# 移动星星
for i in range(num_stars):
x, y, size = stars[i]
y += speed
# 如果星星超出屏幕范围,则重新生成
if y > screen_height:
x = random.randint(0, screen_width)
y = random.randint(-screen_height, 0)
size = random.randint(1, 3)
stars[i] = (x, y, size)
# 绘制星星
for x, y, size in stars:
pygame.draw.circle(screen, star_color, (x, y), size)
# 刷新屏幕
pygame.display.flip()
```
运行程序,你将会看到一个黑色的背景,上面有许多闪烁的白色星星,这些星星会不断地从屏幕顶部滑落到底部,并且会重新生成。你可以尝试调整代码中的参数,比如星星的数量、移动速度、大小和颜色,来创建不同的星空效果。
用python画一片星空
好的,以下是Python代码,可以用来画一片星空:
```python
import matplotlib.pyplot as plt
import numpy as np
N = 1000
x = np.random.rand(N)
y = np.random.rand(N)
colors = np.random.rand(N)
sizes = 800 * np.random.rand(N)
plt.scatter(x, y, c=colors, s=sizes, alpha=0.5)
plt.show()
```
运行这个代码,你就可以得到一片随机的星空了。希望这个回答能够满足你的需求。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)