python实现星空效果

以前用DEV C++这个编译器时,看过easyx 这个C++图形库,文档中有范例程序,是模拟星空的,觉得的不错,今天有空,把Crossin用python写的一个雪花模拟程序,修改了一下,效果和easyx的基本一模一样。

使用easyx的C++代码:

// 编译环境:Visual C++ 6.0~2022,EasyX_2023大暑版
// https://easyx.cn
//
#include <graphics.h>
#include <time.h>
#include <conio.h>

#define MAXSTAR 200	// 星星总数

struct STAR
{
	double	x;
	int		y;
	double	step;
	int		color;
};

STAR star[MAXSTAR];

// 初始化星星
void InitStar(int i)
{
	star[i].x = 0;
	star[i].y = rand() % 480;
	star[i].step = (rand() % 5000) / 1000.0 + 1;
	star[i].color = (int)(star[i].step * 255 / 6.0 + 0.5);	// 速度越快,颜色越亮
	star[i].color = RGB(star[i].color, star[i].color, star[i].color);
}

// 移动星星
void MoveStar(int i)
{
	// 擦掉原来的星星
	putpixel((int)star[i].x, star[i].y, 0);

	// 计算新位置
	star[i].x += star[i].step;
	if (star[i].x > 640)	InitStar(i);

	// 画新星星
	putpixel((int)star[i].x, star[i].y, star[i].color);
}

// 主函数
int main()
{
	srand((unsigned)time(NULL));	// 随机种子
	initgraph(640, 480);			// 创建绘图窗口

	// 初始化所有星星
	for(int i = 0; i < MAXSTAR; i++)
	{
		InitStar(i);
		star[i].x = rand() % 640;
	}

	// 绘制星空,按任意键退出
	while(!_kbhit())
	{
		for(int i = 0; i < MAXSTAR; i++)
			MoveStar(i);
		Sleep(20);
	}

	closegraph();					// 关闭绘图窗口
	return 0;
}

效果:

 

 

# -*- coding: utf-8 -*-
 
import pygame
import random
 
# 初始化pygame
pygame.init()  
# 根据背景图片的大小,设置屏幕长宽
SIZE = (640, 480) 
screen = pygame.display.set_mode(SIZE) 
pygame.display.set_caption("star") 
 
# 雪花列表
snow_list = []
 
# 初始化星空:[x坐标, y坐标, x轴速度, y轴速度]
for i in range(200):
    x = random.randrange(0, SIZE[0])
    y = random.randrange(0, SIZE[1])
    sxy=(random.randrange(1,5000)/1000)+1
    colorvalue=int(sxy*255/6+0.5)    
    starcolor=(colorvalue,colorvalue,colorvalue)
    snow_list.append([x, y, sxy, starcolor])
 
clock = pygame.time.Clock()
pixel_array = pygame.PixelArray(screen) 
# 游戏主循环
done = False
while not done:
    # 消息事件循环,判断退出
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
 
    # 黑背景/图片背景
    screen.fill((0, 0, 0))    

    # 雪花列表循环
    for i in range(len(snow_list)):
        # 绘制雪花,颜色、位置、大小
        #第一种方法
        # pygame.draw.circle(screen, snow_list[i][3],(snow_list[i][0],snow_list[i][1]),1,0)
        #第二种方法
        screen.fill(snow_list[i][3], ((snow_list[i][0],snow_list[i][1]), (1, 1)))
        #第三种方法
        #pixel_array[snow_list[i][0], snow_list[i][1]] =snow_list[i][3]
         #pygame.time.wait(20)       
        
        #pixel_array[snow_list[i][0], snow_list[i][1]] =(0,0,0)        
        # 移动雪花位置(下一次循环起效)
        snow_list[i][0] += int(snow_list[i][2])
        
        # 如果雪花落出屏幕,重设位置
        if snow_list[i][0] > SIZE[0]:
            snow_list[i][0]=0

    #pixel_array.close()
    # 刷新屏幕
    pygame.display.flip()
    clock.tick(20)

# 退出 
pygame.quit()

在屏幕上画点,原来的雪花模拟是用的第一种方法,但对于星空来说,有点大了,所以用了第二种方法画点。第三种尝试了一下,放弃了。

效果:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

longsky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值