简单好玩的编程代码c++语言(简单好玩的编程代码大全)

http://www.itjxue.com  2023-03-05 20:36  来源:未知  点击次数: 

求几个比较有趣,简单的C语言源代码 小白自己敲着练一下手感

最简单的模拟计时器:

#includestdio.h

#includeconio.h

#includewindows.h

int m=0,s=0,ms=0;? //m是分 s是秒 ms是毫秒

//以下是5个自编函数

void csh( );? //初始化界面

void yinc(int x,int y);? //隐藏光标的函数(y值设为0就会隐藏)

void jishi( ); ?//计时器运行(每100毫秒变化一次)

void Color (short x, short y); ?//设定颜色的函数(y设为0就是黑底)

void gtxy (int x, int y); ?//控制光标位置的函数

int main(? )? //主函数

{? csh( );

?? getch( );

?? while(1)

? ? ?? { jishi( );

? ? ? ?? Sleep(100);? //间隔100毫秒

? ? ? ?? if( kbhit( ) )break;? //有键按下就退出循环

? ? ?? }

? ? return 0;

}

void csh( ) ? //初始化界面

{Color(14,0); ?? //设定淡黄字配黑底

printf(“\n\n\t?? ?计时器”);

Color(10,0); ? //设定淡绿字配黑底

printf("\n\t┌───────────┐");

printf("\n\t│????????? ?│");

printf("\n\t└───────────┘");

gtxy(10,4); ? //光标到屏幕第10列4行处输出

Color(7,0); ? //恢复白字黑底

printf(" 00:00:00 ");

yinc(1,0 ); ? //隐藏光标(yinc代表隐藏)

return;

}

void jishi( )? //计时器运行

{ms+=1;

if(ms==10){s+=1;ms=0;}

if(s==60){m+=1;s=0;}

gtxy(10,4);

Color(9,0); ? //设定淡蓝字配黑底

if(m9) printf(" %d:",m);

else printf(" 0%d:",m);

Color(14,0); ? //设定淡黄字配黑底

if(s9) printf("%d:",s);

else printf("0%d:",s);

Color(12,0); ??//设定淡红字配黑底

printf("0%d",ms);

}

void gtxy (int x, int y) ? //控制光标位置的函数

{ COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );

}

void Color (short ForeColor= 7, short BackGroundColor= 0) ? //设定颜色的函数

{ HANDLE? handle = GetStdHandle ( STD_OUTPUT_HANDLE );

SetConsoleTextAttribute ( handle, ForeColor + BackGroundColor * 0x10 );

}

void yinc(int x,int y) ? //隐藏光标的设置(gb代表光标)

{ CONSOLE_CURSOR_INFO ?gb={x,y}; ? //x为1-100,y为0就隐藏光标

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), gb);

}

最简单的C语言代码

最简单的C语言代就是输出“helloWord”,通常是作为初学编程语言时的第一个程序代码。具体代码如下:

#include stdio.h

int main(){

? printf("Hello, World! \n");

? return 0;

}

扩展资料:

1、程序的第一行#include stdio.h是预处理器指令,告诉 C 编译器在实际编译之前要包含 stdio.h 文件。

2、下一行intmain()是主函数,程序从这里开始执行。

3、下一行printf(...)是C中另一个可用的函数,会在屏幕上显示消息"Hello,World!"。

4、下一行return0;终止main()函数,并返回值0。

参考资料来源:百度百科-c语言

c语言编程烟花代码简单

c语言编程烟花代码简单如下:

#include "stdlib.h"

#include "graphics.h"

#include "stdio.h"

#include "math.h"

#include "conio.h "

#define PI? 3.1425926

main()

{

int gdriver=DETECT,gmode,errorcode;

int a[10],b[10],x,y,c,r,i,j,t;

double rad = 0.0;

/* initialize graphics and local variables */

initgraph(gdriver , gmode ,"");

/* read result of initialization */

errorcode = graphresult();

if (errorcode != grOk)? /* an error occurred */

{

printf("Graphics error : %s/n",grapherrormsg(errorcode));

printf("Please any key to halt:");

getch();

exit(1);? /* terminate with an error code */

}

randomize();

for(;!kbhit();)

{

x=rand()%500+100; /*随机中心坐标*/

y=rand()%300+100;

for(r = 0 ;r = 8 ; r++? )? ? /*烟花的大小设定*/

{

for(i = 0,rad = 0.0 ; rad 2*PI; rad += 0.78 )? /*设定坐标*/

{

a[i++] = x + (int)r *10* cos(rad);

b[ i ] = y + (int)r *10* sin(rad);

}

t = i;

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

{

c=rand()%13+1; /*各点的颜色随机*/

setcolor(c);? ? ? ?/*功能:将当前图形屏幕的当前笔画颜色置为color.*/

circle(a[i],b[i],1);/* a[i],b[i] 为圆心 1 为半径 画圆 */

}

delay(10000);

delay(10000);

cleardevice();

函数名: cleardevice? ? ? ? ?

功? 能: 清除图形屏幕?

用? 法: void far cleardevice(void);

}

}

getch();

closegraph();

函数名: closegraph

功? 能: 关闭图形系统

用? 法: void far closegraph(void);

}

初始化烟花参数

void Init( int i )

{

// 分别为:烟花中心到图片边缘的最远距离、烟花中心到图片左上角的距离 (x、y) 两个分量

int r[13] = { 120, 120, 155, 123, 130, 147, 138, 138, 130, 135, 140, 132, 155 };

int x[13] = { 120, 120, 110, 117, 110, 93, 102, 102, 110, 105, 100, 108, 110 };

int y[13] = { 120, 120, 85, 118, 120, 103, 105, 110, 110, 120, 120, 104, 85 };

/**** 初始化烟花 *****/

Fire[i].x = 0; // 烟花中心坐标

Fire[i].y = 0;

Fire[i].width = 240; // 图片宽

Fire[i].height = 240; // 图片高

Fire[i].max_r = r[i]; // 最大半径

Fire[i].cen_x = x[i]; // 中心距左上角距离

Fire[i].cen_y = y[i];

Fire[i].show = false; // 是否绽放

Fire[i].dt = 5; // 绽放时间间隔

Fire[i].t1 = timeGetTime();

Fire[i].r = 0; // 从 0 开始绽放

/**** 初始化烟花弹 *****/

Jet[i].x = -240; // 烟花弹左上角坐标

Jet[i].y = -240;

Jet[i].hx = -240; // 烟花弹发射最高点坐标

Jet[i].hy = -240;

Jet[i].height = 0; // 发射高度

Jet[i].t1 = timeGetTime();

Jet[i].dt = rand() % 10; // 发射速度时间间隔

Jet[i].n = 0; // 烟花弹闪烁图片下标

Jet[i].shoot = false; // 是否发射

}

(责任编辑:IT教学网)

更多