用C语言编写流星雨程序

用C语言编写流星雨程序,第1张

数字流星雨代码:

//流星雨.cpp:Defines the entry point for the console application.

//

///////////////////////////////////////////////////

//程序名称:数字流星雨

//最后修改:2006-10-15

///////////////////////////////////////////////////

#include&ltwindows.h&gt

#include&lttime.h&gt

#include&ltstdlib.h&gt

#include"graphics.h"

#include&ltconio.h&gt

#include&ltmath.h&gt

/***********************宏定义**********************/

#define PI 3.1415926//圆周率

#define WIDTH 200//屏幕宽度,流星出生区域

#define HEIGHT 150//屏幕兄谈高度,流星出生区域

#define V 20//流星速度,单次移动的像素数

#define LENGTH 20//流星字符

#define DELAY 30//延时

#define NUM 45//流星个数

/******************定义流星结蠢裤构体*******************/

struct meteor

{

int x0

int y0

int yh

char str[LENGTH]

}me[NUM]={0}

/*********************函数声明**********************/

char AsciiRand()

void Move(char*p)

void InitMeteor(struct meteor*me)

int color(int y,int y0,int yh)

void Meteors(struct meteor me[])

/***********************主函数**********************/

///int main(void)

int _tmain(int argc,_TCHAR*argv[]){

char c='\0'//接收键盘输入的变量

initgraph(WIDTH,HEIGHT)//初始化WIDTH*HEIGHT的绘图窗口

HWND hwnd=GetHWnd()//获得窗口句柄

SetWindowText(hwnd,"Gavin Liu数字流星雨")//修改窗口名称

ShowWindow(hwnd,SW_SHOWMAXIMIZED)//最大化显示窗口

MessageBox(hwnd,TEXT("点击【确定】开始演示流星雨带尘简效果,Esc键退出"),TEXT("提示"),MB_OK|MB_ICONWARNING)//d出提示

srand((unsigned)time(NULL))//设置随机种子

for(int i=0i&ltNUMi++){//对NUM个流星体初始化

InitMeteor(&me&lti&gt)

}

while(c!=27){

BeginBatchDraw()//开始批量绘图

Meteors(me)//绘制一帧动画

FlushBatchDraw()//执行未完成的绘制任务

Sleep(DELAY)//延时

cleardevice()//清屏

for(int i=0i&ltNUMi++){

me&lti&gt.yh+=V

Move(me&lti&gt.str)

if(me&lti&gt.yh&gtHEIGHT+LENGTH*V){

InitMeteor(&me&lti&gt)

}

}

if(kbhit()){

c=getch()

}

}

EndBatchDraw()//结束批量绘图

closegraph()//结束绘图环境

return 0

}

/***********************函数体**********************/

char AsciiRand(){//产生随机可见ASCII码

return((char)(rand()%(126-33)+33))

}

void Move(char*p){//字符后移,可以使显示时字符相对屏幕位置不变

char*pt=p+LENGTH

while(pt&gtp){

*(--pt)=*(pt-1)

}

*p=AsciiRand()

}

void InitMeteor(struct meteor*me){//对一颗流星初始化

me-&gtx0=rand()%WIDTH

me-&gtyh=me-&gty0=rand()%HEIGHT

for(int i=0i&ltLENGTHi++)

{

*(me-&gtstr+i)=AsciiRand()

}

}

int color(int y,int y0,int yh){//确定流星的颜色

int color

//出生点之前的流星体置成黑色

if(y&lty0){

color=0

}

//流星颜色自头至尾按照余弦函数递减

else{

//尾迹消失

color=(int)(255*cos((yh-y)*PI/(2*LENGTH*V)))

}

return color

}

//打印一帧流星的画面

void Meteors(struct meteor me[]){

//设置格式:背景透明,字符高度,字体粗细,字体

setbkmode(TRANSPARENT)

setfont(12,12,"宋体")

//开始打印一帧图像

int y

for(int n=0n&ltNUMn++){

for(int j=0j&ltLENGTHj++){

//流星中第j个字符的纵坐标

y=me[n].yh-j*V

//设置颜色,流星的头部是白色的

setcolor(RGB(255*(0==j),color(y,me[n].y0,me[n].yh),255*(0==j)))

//打印字符

outtextxy(me[n].x0,y,me[n].str[j])

}

}

}

扩展资料:

include用法:

#include命令预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置;可以标识出只有在特定条件下才会被编译的某一段程序代码;可以定义类似标识符功能的宏,在编译时,预处理器会用别的文本取代该宏。

插入头文件的内容

#include命令告诉预处理器将指定头文件的内容插入到预处理器命令的相应位置。有两种方式可以指定插入头文件:

1、#include&lt文件名&gt

2、#include"文件名"

#include<conio.h>

#include<dos.h>

#include<stdlib.h>

main()

{char screen[24][80],e[10]

int i,j,k,l,m,n,t=0,a[10],b[10],c[10],d[10],f[10]={0}

randomize()

for(i=0i<24i++)

for(j=0j<80j++)

{if(i<19){screen[j]=''printf(" ")}

else{screen[j]='#'textcolor(GREEN)cprintf("%c"纤斗,screen[j])}

}

do{for(i=0i<10i++)

{c=random(4)+1d=random(16)b=0}

a[0]=random(80)j=0

do{k=random(80)j++l=3

for(i=0i<ji++)

if(k==a){j--l=2break}

if(l==2)continuea=k

}while(i<9)

for(i=0i<9i++)

{e=random(26)+'A'gotoxy(a+1,b+1)

textcolor(d)cprintf("%c",e)f=0}

for(j=0j<24j++)

for(i=0i<10i++)

if(f!=1)

{delay(5000)gotoxy(a+1,b+1)printf(" ")b=b+c

l=(c<毁早磨19-b)?c:(19-b)

for(m=1m<lm++)

{n=b+m-cgotoxy(a+1,n+1)textcolor(d)

cprintf("%c",e)gotoxy(a+1,n+1)printf(" ")}

if(b<19){textcolor(d)gotoxy(a+1,b+1)cprintf("%c",e)}

else{for(m=0,k=am<5m++)

if(screen[19+m][k]=='#'睁族)

{screen[19+m][k]=''gotoxy(k+1,20+m)printf(" ")f=1break}

else{gotoxy(k+1,m+20)textcolor(d)cprintf("%c",e)

gotoxy(k+1,m+20)printf(" ")}

}

}

for(k=0k<80k++)

if(screen[23][k]==''){t=1break}

}while(t!=1)

}

我们在做这个,我是这样的

#include <iom16.h>

#include <avr_macros.h>

void Init_IO(void)

 {

   DDRA = 0xFF

   PORTA = 0x00

 }

 /*以上代码51忽略*/

void LED1 (unsigned char x,unsigned char LEDx)

{

  unsigned char Count = 0

  Count = LEDx * 36    //最小0,最大 7 * 36 = 252,char型最大不能超耐察洞过255;

  Count = Count + 1    // 加1的目的是针对LED0不亮

   if(x <= Count)  // <= 昌枯是针对LED0不亮

     PORTA |= (1 << LEDx)  //51怎么改?。 P0 |= (1 << LEDx) PWM正占空

    else

     if(x > Count)

       PORTA &= ~(1 << LEDx) //51怎么改?。 P0 &= ~(1 << LEDx) PWM负占空 

}

void main( void )

{

  unsigned char delay = 0

  unsigned char LEDx = 0

  Init_IO()

  while (1)

   {

      delay ++

      if(delay == 250)

       {

         delay = 0

         LEDx ++

         if(LEDx > 7)

 没空          LEDx = 0

       }

      LED1(delay,LEDx)

   }

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/yw/12501407.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存