c语言星空特效代码(用c语言编写星星的图片)

http://www.itjxue.com  2023-02-27 17:55  来源:未知  点击次数: 

用C语言输出一个五角星图案

我用VC6实现了。关键代码如下:

1、获得五个外顶点的坐标:

int m_xw[5]; //5个X外顶点坐标

int m_yw[5]; //5个Y外顶点坐标

void CMyTestView::getCoord()

{

int c = 360 / 5; //角度

int r = 150;

for (int i = 0; i 5; i++)

{

m_xw[i] = (int) (cos(i * c * PI / 30 - PI / 2) * (r) + r);

m_yw[i] = (int) (sin(i * c * PI / 30 - PI / 2) * (r) + r);

}

}

2、画线段:

在构造函数中调用求顶点坐标的函数求顶点坐标:

this-getCoord();

然后直接在绘图函数中添加如下代码:

void CMyTestView::OnDraw(CDC* pDC)

{

CMyTestDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

pDC-MoveTo(m_xw[4], m_yw[4]);

pDC-LineTo(m_xw[1], m_yw[1]);

pDC-LineTo(m_xw[3], m_yw[3]);

pDC-LineTo(m_xw[0], m_yw[0]);

pDC-LineTo(m_xw[2], m_yw[2]);

pDC-LineTo(m_xw[4], m_yw[4]);

}

//运行结果

完整的程序给你发到QQ邮箱了!

请教:怎么样能把C语言代码运行后的流星雨效果图保存下来,保存为GIF。

搞定。 我用的C++Builder XE2编译器。 增中代码: #include vcl.h #include GIFimg.hpp TGIFImage *image; 在wmain函数后部分: image = new TGIFImage(); srand((int) GetCurrentTime()); // 初始化随机数发生器 image-Width = Screen-Width; image-Height = Screen-Height; while (GetMessage(msg, NULL, 0, 0)) { TranslateMessage(msg); DispatchMessage(msg); } ShowCursor(TRUE); // 显示鼠标光标 image-SaveToFile("流星雨.gif"); delete image; 在WndProc函数处理WM_TIMER消息后面增加: BitBlt(hdc, 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY); { Graphics::TBitmap *bmp = new Graphics::TBitmap; bmp-Width=Screen-Width; bmp-Height=Screen-Height; BitBlt(HDC(bmp-Canvas-Handle), 0, 0, cxScreen, cyScreen, hdcMem, 0, 0, SRCCOPY); image-Add(bmp); delete bmp; } ReleaseDC(hwnd, hdc); 编译后成功。 保存的GIF文件很大,最好用Ulead GIF Animator优化一下。 需要编译后的文件QQ我:1848447992

记得采纳啊

空心五角星 C语言代码

如果题目没有限制,直接通过打印若干行代表空心五角星的字符串即可。

C语言如何用代码打出星星,如下

既然你这么急,我还是答一下吧,原图没法输出,类似的可以实现,,你看哪个最像原图就写哪个吧:

图形1:

#include stdio.h

int main(void)

{

int i,j;

for(i=1;i=7;i++)

{

for(j=7-i;j--;) printf(" ");

for(j=i;j--;) printf(" *");

printf("\n");

}

return 0;

}

图形2:

#include stdio.h

int main(void)

{

int i,j;

for(i=1;i=7;i++)

{

for(j=7-i;j--;) printf(" ");

for(j=i;j--;) printf("﹡");

printf("\n");

}

return 0;

}

图形3:

#include stdio.h

int main(void)

{

int i,j;

for(i=1;i=7;i++)

{

for(j=7-i;j--;) printf(" ");

for(j=i;j--;) printf("*");

printf("\n");

}

return 0;

}

图形4:

#include stdio.h

int main(void)

{

int i,j;

for(i=1;i=7;i++)

{

for(j=i;j--;) printf("*");

printf("\n");

}

return 0;

}

用C语言编写流星雨程序

数字流星雨代码:

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

//

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

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

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

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

#includelt;windows.hgt;

#includelt;time.hgt;

#includelt;stdlib.hgt;

#include"graphics.h"

#includelt;conio.hgt;

#includelt;math.hgt;

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

#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);//弹出提示

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

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

InitMeteor(melt;igt;);

}

while(c!=27){

BeginBatchDraw();//开始批量绘图

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

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

Sleep(DELAY);//延时

cleardevice();//清屏

for(int i=0;ilt;NUM;i++){

melt;igt;.yh+=V;

Move(melt;igt;.str);

if(melt;igt;.yhgt;HEIGHT+LENGTH*V){

InitMeteor(melt;igt;);

}

}

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(ptgt;p){

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

}

*p=AsciiRand();

}

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

me-gt;x0=rand()%WIDTH;

me-gt;yh=me-gt;y0=rand()%HEIGHT;

for(int i=0;ilt;LENGTH;i++)

{

*(me-gt;str+i)=AsciiRand();

}

}

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

int color;

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

if(ylt;y0){

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=0;nlt;NUM;n++){

for(int j=0;jlt;LENGTH;j++){

//流星中第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、#includelt;文件名gt;

2、#include"文件名"

求C语言闪烁星空语句

在C的文件夹下有个Demo程序,里面就含有你所要的代码。

void

PutPixelDemo(void)

{

int

seed

=

1958;

int

i,

x,

y,

h,

w,

color;

struct

viewporttype

vp;

MainWindow(

"PutPixel

/

GetPixel

Demonstration"

);

getviewsettings(

vp

);

h

=

vp.bottom

-

vp.top;

w

=

vp.right

-

vp.left;

srand(

seed

);

/*

Restart

random

#

function

*/

for(

i=0

;

i5000

;

++i

){

/*

Put

5000

pixels

on

screen

*/

x

=

1

+

random(

w

-

1

);

/*

Generate

a

random

location

*/

y

=

1

+

random(

h

-

1

);

color

=

random(

MaxColors

);

putpixel(

x,

y,

color

);

}

srand(

seed

);

/*

Restart

Random

#

at

same

#

*/

for(

i=0

;

i5000

;

++i

){

/*

Take

the

5000

pixels

off

*/

x

=

1

+

random(

w

-

1

);

/*

Generate

a

random

location

*/

y

=

1

+

random(

h

-

1

);

color

=

getpixel(

x,

y

);

/*

Read

the

color

pixel

*/

if(

color

==

random(

MaxColors

)

)

/*

Used

to

keep

RANDOM

in

sync

*/

putpixel(

x,

y,

);

/*

Write

pixel

to

BLACK

*/

}

Pause();

/*

Wait

for

user's

response

*/

}

(责任编辑:IT教学网)

更多

推荐java认证文章