好玩的c语言代码小游戏(c语言代码小游戏大全)
C语言代码 过河智力游戏
#include stdio.h#define MAX 100typedef enum BOOL{ FALSE = 0, TRUE = 1 }BOOL。
typedef union Items{struct {char boy : 1;char girl : 1;char boys)。
语言特点
主要特点
C语言是一种结构化语言,它有着清晰的层次,可按照模块的方式对程序进行编写,十分有利于程序的调试,且c语言的处理和表现能力都非常的强大,依靠非常全面的运算符和多样的数据类型,可以轻易完成各种数据结构的构建。
通过指针类型更可对内存直接寻址以及对硬件进行直接操作,因此既能够用于开发系统程序,也可用于开发应用软件。通过对C语言进行研究分析,总结出其主要特点如下:
(1)简洁的语言
C语言包含的各种控制语句仅有9种,关键字也只有32个,程序的编写要求不严格且以小写字母为主,对许多不必要的部分进行了精简。
实际上,语句构成与硬件有关联的较少,且C语言本身不提供与硬件相关的输入输出、文件管理等功能,如需此类功能,需要通过配合编译系统所支持的各类库进行编程,故c语言拥有非常简洁的编译系统。
(2)具有结构化的控制语句
C语言是一种结构化的语言,提供的控制语句具有结构化特征,如for语句、if...else语句和switch语句等。可以用于实现函数的逻辑控制,方便面向过程的程序设计。
(3)丰富的数据类型
C语言包含的数据类型广泛,不仅包含有传统的字符型、整型、浮点型、数组类型等数据类型,还具有其他编程语言所不具备的数据类型,其中以指针类型数据使用最为灵活,可以通过编程对各种数据结构进行计算。
以上内容参考:百度百科-C语音
用C语言编写的小游戏代码是什么?
“猜数字小游戏”,每个数字后按空格,最后按回车确认
#includestdio.h
#includestdlib.h
#includetime.h
int a[4],b[4];
int count=0;? //计算猜测次数
void csh( );? //初始化
void start( );? //开始游戏
int main( )
{ csh( );
start( );
}
void csh( )? //初始化
{ printf("\n\n???????? 猜? 数? 字? 小? 游? 戏\n\n");
printf(“? ??猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\n”);
}
void start( )? //开始游戏
{int m,n;? //m是完全猜对的个数,n是顺序不对的个数
while(1)
{srand((unsigned)time(NULL));? //初始化随机数发生器srand( )
while(1) { for(int i=0;i4;i++) a[i]=rand( )%10; ?//rand( )函数每次随机产生一个0-9的数
if( (a[3]!=a[2]a[3]!=a[1]a[3]!=a[0])
(a[2]!=a[1]a[2]!=a[0])a[1]!=a[0] ) break; }? //4个随机数各自不相等
printf("?? ?请依次输入4个一位整数:\n\n? ?");
while(1)
{for(int i=0;i4;i++) scanf(“%d”,b[i]);
printf("?? ?你输入的是:%d? %d? %d? %d ",b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i4;i++)
{for(int j=0;j4;j++)
{ if(b[i]==a[j]i==j)m=m+1; if(b[i]==a[j]i!=j)n=n+1; }
}
count=count+1;
printf("????? %dA? %dB?? 你试了%d次\n? ?",m,n,count);
if(m==4)break;
if(count==8){ count=0; break; }
}
printf("\n");
if(m==4)printf("???? 你猜对了(^-^)! 就是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);
else printf("???? 你输了(T-T)!哈哈!应该是:%d %d %d %d\n",a[0],a[1],a[2],a[3]);
int z;
printf("???? (要继续吗?1或0)\n? ?");
scanf(“%d”,z);
if(z==0) break;
}
}

给我提供个小游戏的C 语言代码
本原代码是基于C语言的原程序。是经典中的小游戏。-primitive code is based on the C language of the original procedure. Classic is a small game.
一个小游戏,用C语言编写的:俄罗斯方块.C原码及应用程序都在里面哦 -a small game using the C language : Russian cubes. The original C code and application procedures inside oh
十全十美游戏原程序,c语言-perfect game program, language c
上数据结构时,自己用C语言做的小游戏,做得不好,请大家原谅。-structure on the data they used C language to the small games, done well, please forgive me.
大家都耍过文曲星中的猜数字的游戏吧 !! 最近我在学习C语言。写了个菜鸟的C语言的猜数字的游戏程序的原代码-rings off the viewing of the game! ! I recently learning C language. Wrote a birdie C language viewing of the games original code procedures
这是我在大学二年级学习C语言课程时,作为“练笔”而编写的一个小程序(当时在我眼里可却是一个大程序!)其主要特点有:1、正真做到了全中文界面(不需要UCDOS操作系支持) 2、大量的图形特技(如图像的显隐技术、图像穿插技术、多任务仿真技术等) 3、纯C语言打造(不含任何C++知识) 4、实现了街机“俄罗斯方块”绝大部分功能(如动画、声音、速度变化) 5、用户可根据自据的习惯自由地调整“游戏操作键” 6、方法独特,全部语句和技术都是我本人原创,没有参考过任何相关代码 7、防“跟踪调试”技术,防“版权篡改”技术 8、……-
这个程序是模仿Windows中的扫雷小游戏制作的,该程序只是实现了扫雷游戏的主体部分,诸如计分、升级部分都没有做。这个程序可以作为初学者学习C语言绘图和游戏的实例。 该程序在Turbo C2.0 下编译通过 由于扫雷游戏是用鼠标操作的,而Turbo C中提供的鼠标驱动程序在Windows xp下不可用,所以我随源程序提供了一个鼠标驱动的头文件,须将将该头文件复制到Turbo C2.0 的安装目录下的“include”文件夹中方可编译或运行,也可自行修改原文件使之包含该投文件。 注:该鼠标驱动程序是我在网上找到的,其出处我已无法考证,如果侵犯了作者的权利还请作者与我联系。 由于在我的电脑上Turbo C图形环境下显示数字会有问题(估计是系统问题),所以程序中雷周围的数字1-8我用a-h代替,看不顺眼的可以自己修改原程序。-
c语言 猜拳游戏的原代码就是这个 已经测试成功了呀-language of the original game is the code has been tested successfully ah
俄罗斯方块对战版c语言原代码。希望大家能喜欢。是比较简单的一个代码,游戏开发高手请指教。-Tetris screen version of the original C language code. Hope you will like. It is a relatively simple code, game development experts please advise.
用linuX 下的C语言 运用CURSES编写的俄罗斯方块游戏,很好,这个是本人原创,值得参考-linuX use the C language CURSES prepared by the Russian box game, well, this is the original, worthy of reference
用C语言编猜单词小游戏
分类: 电脑/网络 操作系统/系统故障
问题描述:
请写一个小游戏程序,程序开始时从文件读入一批单词,文件的名字由命令行得到。程序运行中每次从这些单词中随机地选出一个,要求游戏者猜。做游戏者反复询问某些字母是否出现在单词里,程序给出回答。直至人猜出这个单词(或者放弃)。
解析:
#include stdio.h
#include stdlib.h
#include string.h
#include time.h
#define max 1024
#define min 50
void main()
{
FILE *fp;
char c;
int i=0;
int j=0;
读文件
char str[max][min]={0};
if((fp=fopen("word.txt","r"))==NULL)
{
printf("can not open file word.txt\n");
exit(0);
}
c=fgetc(fp);
while(c!=EOF)
{
if(c!=' ')
{
str[i][j]=c;
j++;
}
else
{
i++;
j=0;
}
c=fgetc(fp);
}
产生随机数,0-i
srand((unsigned int)time(NULL));
int n=rand()/20;
while((n0)||(ni))
{
n=rand()/20;
}
printf("取得的随机数是第%d个单词\n",n);
检查单词
printf("#######guess begin######\n");
printf("\n");
char gs;
char con='y';
while(con=='y')
{
printf("请输入字母,# 号表示结束\n");
scanf("%c",gs);
getchar();
char gue[min]={0};
int flag=0;
while(gs!='#')
{
for(int i=0;str[n][i]!='\0';i++)
{
if(str[n][i]==gs)
{
printf("字母%c在单词中\n",gs);
flag=1;
break;
}
}
if(!flag)
{
printf("字母%不在单词中\n",gs);
}
printf("请输入字母,# 号表示结束\n");
scanf("%c",gs);
getchar();
}
printf("请输入你猜的单词\n");
scanf("%s",gue);
getchar();
if(!(strcmp(str[n],gue)))
{
printf("恭喜你,猜对了!\n");
}
else
{
printf("很遗憾,你猜错了\n");
}
printf("continue?y is guess again,press any key to end \n");
scanf("%c",con);
getchar();
}
fclose(fp);
}
C语言可以写哪些小游戏?
C语言可以编手机游戏. 你叫他去死 不过我这有 贪吃蛇的代码,你倒可以看看 (用TC 编译一定过)
#include
#include
#include
#include
#include
#define Enter 7181
#define ESC 283
#define UP 18432
#define DOWN 20480
#define LEFT 19200
#define RIGHT 19712
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
void interrupt (*oldhandler)(__CPPARGS);
void interrupt newhandler(__CPPARGS);
void SetTimer(void interrupt (*IntProc)(__CPPARGS));
void KillTimer(void);
void Initgra(void);
void TheFirstBlock(void);
void DrawMap(void);
void Initsnake(void);
void Initfood(void);
void Snake_Headmv(void);
void Flag(int,int,int,int);
void GameOver(void);
void Snake_Bodymv(void);
void Snake_Bodyadd(void);
void PrntScore(void);
void Timer(void);
void Win(void);
void TheSecondBlock(void);
void Food(void);
void Dsnkorfd(int,int,int);
void Delay(int);
struct Snake
{int x;int y;int color;}Snk[12];
struct Food
{int x;int y;int color;}Fd;
int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,
checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse;
char Sco[2],Time[6];
void main()
{ Initgra();
SetTimer(newhandler);
TheFirstBlock();
while(1)
{DrawMap();
Snake_Headmv();
GameOver();
Snake_Bodymv();
Snake_Bodyadd();
PrntScore();
Timer();
Win();
if(key==ESC)
break;
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
TheSecondBlock();
Food();
Delay(Snkspeed);
}
closegraph();
KillTimer();
}
void interrupt newhandler(__CPPARGS)
{
TimerCounter++;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)(__CPPARGS))
{
oldhandler=getvect(0x1c);
disable();
setvect(0x1c,IntProc);
enable();
}
void KillTimer()
{
disable();
setvect(0x1c,oldhandler);
enable();
}
void Initgra()
{int gd=DETECT,gm;
initgraph(gd,gm,"d:\\tc");
}
void TheFirstBlock()
{setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The First Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=10;
num=2;
Times=0;
key=0;
TureorFalse=1;
TimerCounter=0;
Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0';
}
else if(key==ESC) cleardevice();
else goto loop;
}
void DrawMap()
{line(10,10,470,10);
line(470,10,470,470);
line(470,470,10,470);
line(10,470,10,10);
line(480,20,620,20);
line(620,20,620,460);
line(620,460,480,460);
line(480,460,480,20);
}
void Initsnake()
{randomize();
num=2;
Snk[0].x=random(440);
Snk[0].x=Snk[0].x-Snk[0].x%20+50;
Snk[0].y=random(440);
Snk[0].y=Snk[0].y-Snk[0].y%20+50;
Snk[0].color=4;
Snk[1].x=Snk[0].x;
Snk[1].y=Snk[0].y+20;
Snk[1].color=4;
}
void Initfood()
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
}
void Snake_Headmv()
{if(bioskey(1))
{key=bioskey(0);
switch(key)
{case UP:Flag(1,0,0,0);break;
case DOWN:Flag(0,1,0,0);break;
case LEFT:Flag(0,0,1,0);break;
case RIGHT:Flag(0,0,0,1);break;
default:break;
}
}
if(flag1)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag2)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].y+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag3)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x-=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
if(flag4)
{checkx=Snk[0].x;
checky=Snk[0].y;
Dsnkorfd(Snk[0].x,Snk[0].y,0);
Snk[0].x+=20;
Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);
}
}
void Flag(int a,int b,int c,int d)
{flag1=a;flag2=b;flag3=c;flag4=d;}
void GameOver()
{int i;
if(Snk[0].x460||Snk[0].y460)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop1:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else
goto loop1;
}
for(i=3;inum;i++)
{if(Snk[0].x==Snk[i].xSnk[0].y==Snk[i].y)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop2:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else
if(key==ESC)
cleardevice();
else goto loop2;
}
}
}
void Snake_Bodymv()
{int i,s,t;
for(i=1;inum;i++)
{Dsnkorfd(checkx,checky,Snk[i].color);
Dsnkorfd(Snk[i].x,Snk[i].y,0);
s=Snk[i].x;
t=Snk[i].y;
Snk[i].x=checkx;
Snk[i].y=checky;
checkx=s;
checky=t;
}
}
void Food()
{if(flag5)
{randomize();
Fd.x=random(440);
Fd.x=Fd.x-Fd.x%20+30;
Fd.y=random(440);
Fd.y=Fd.y-Fd.y%20+30;
Fd.color=random(14)+1;
flag5=0;
}
Dsnkorfd(Fd.x,Fd.y,Fd.color);
}
void Snake_Bodyadd()
{if(Snk[0].x==Fd.xSnk[0].y==Fd.y)
{if(Snk[num-1].xSnk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x+20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].xSnk[num-2].x)
{num++;
Snk[num-1].x=Snk[num-2].x-20;
Snk[num-1].y=Snk[num-2].y;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].ySnk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y+20;
Snk[num-1].color=Fd.color;
}
else
if(Snk[num-1].ySnk[num-2].y)
{num++;
Snk[num-1].x=Snk[num-2].x;
Snk[num-1].y=Snk[num-2].y-20;
Snk[num-1].color=Fd.color;
}
flag5=1;
Score++;
}
}
void PrntScore()
{if(Hscore!=Score)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,100,"SCORE");
setcolor(2);
setfillstyle(1,0);
rectangle(520,140,580,180);
floodfill(530,145,2);
Sco[0]=(char)(Score+48);
Sco[1]='\0';
Hscore=Score;
setcolor(4);
settextstyle(0,0,3);
outtextxy(540,150,Sco);
}
}
void Timer()
{if(TimerCounter18)
{Time[4]=(char)(Time[4]-1);
if(Time[4]'0')
{Time[4]='9';
Time[3]=(char)(Time[3]-1);
}
if(Time[3]'0')
{Time[3]='5';
Time[1]=(char)(Time[1]-1);
}
if(TureorFalse)
{setcolor(11);
settextstyle(0,0,3);
outtextxy(490,240,"TIMER");
setcolor(2);
setfillstyle(1,0);
rectangle(490,280,610,320);
floodfill(530,300,2);
setcolor(11);
settextstyle(0,0,3);
outtextxy(495,290,Time);
TureorFalse=0;
}
if(Time[1]=='0'Time[3]=='0'Time[4]=='0')
{setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"Game Over");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
}
else if(key==ESC) cleardevice();
else goto loop;
}
TimerCounter=0;
TureorFalse=1;
}
}
void Win()
{if(Score==3)
Times++;
if(Times==2)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(160,220,"You Win");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
TheFirstBlock();
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void TheSecondBlock()
{if(Score==3)
{cleardevice();
setcolor(11);
settextstyle(0,0,4);
outtextxy(100,220,"The Second Block");
loop:key=bioskey(0);
if(key==Enter)
{cleardevice();
Initsnake();
Initfood();
Score=0;
Hscore=1;
Snkspeed=8;
num=2;
key=0;
}
else if(key==ESC) cleardevice();
else goto loop;
}
}
void Dsnkorfd(int x,int y,int color)
{setcolor(color);
setfillstyle(1,color);
circle(x,y,10);
floodfill(x,y,color);
}
void Delay(int times)
{int i;
for(i=1;i=times;i++)
delay(15000);
}
有什么好玩的C语言小程序
一个“歼灭敌机”的小游戏,DEVc++编译通过:
#include stdio.h
#include conio.h
#include stdlib.h
#include windows.h
#include time.h
#define zlx 10 ?//增量坐标(x)让游戏框不靠边
#define zly 3?? //增量坐标(y)让游戏框不靠边
#define W 26? //游戏框的宽度
#define H 24 ?//游戏框的高度
int jiem[22][22]={0}, wj=10;? //界面数组, 我机位置(初值为10)
int speed=4,density=30, score=0,death=0; //敌机速度, 敌机密度, 玩家成绩,死亡次数
int m=0,n=0; ?// m,n是控制敌机的变量
void gtxy (int x, int y) ?//控制光标位置的函数
{ COORD pos;
pos.X = x;? pos.Y = y;
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );
}
void?Color(int?a) ?//设定颜色的函数(a应为1-15)
{ SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), a ); }
void yinc(int x=1,int y=0) ? //隐藏光标的函数
{ CONSOLE_CURSOR_INFO ?gb={x,y};? ?//y设为0即隐藏
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), gb);
}
void csh( )? //初始化函数
{ int i;
Color(7);
gtxy(zlx,zly); printf("╔");? gtxy(zlx+W-2,zly); printf("╗");? //左上角和右上角的框角
gtxy(zlx,zly+H-1); printf("╚"); gtxy(zlx+W-2,zly+H-1); printf("╝"); //下边两框角
for(i=2;iW-2;i+=2) {gtxy(zlx+i,zly);? printf("═"); }? ????//打印上横框
for(i=2;iW-2;i+=2) {gtxy(zlx+i,zly+H-1); printf("═"); } ?//打印下横框
for(i=1;iH-1;i++) { gtxy(zlx,zly+i); ?printf("║"); }? ?????//打印左竖框
for(i=1;iH-1;i++) {gtxy(zlx+W-2,zly+i); printf("║"); }? //打印右竖框
Color(14);gtxy(19,2); printf("歼灭敌机"); Color(10);
gtxy(37,5); printf("设置:Esc ");
gtxy(37,7); printf("发射:↑ ");
gtxy(37,9); printf("控制:← → ");
gtxy(37,11);printf("得分:%d",score);
gtxy(37,13); printf("死亡:%d",death);
yinc(1,0);
}
void qcjm( ) ?//清除界面函数
{int i,j;
for(i=0;iH-2;i++)
for(j=0;jW-4;j++){gtxy(zlx+2+j,zly+1+i);printf(" ");}
}
void feiji( ) ?//飞机移动函数
{int i,j;
for(i=21;i=0;i--)? //从底行往上是为了避免敌机直接冲出数组
? for(j=0;j22;j++)
? ? ?{if(i==21jiem[i][j]==3)?jiem[i][j]=0;? //底行赋值0 以免越界
? ? ? ?if(jiem[i][j]==3)?jiem[i][j]=0, jiem[i+1][j]=3;
? ? ?}
if(jiem[20][wj]==3jiem[21][wj]==1) death++;
}
void zidan( ) ?//子弹移动函数
{ int i,j;
for(i=0;i22;i++)
??for(j=0;j22;j++)
? ? ?{if(i==0jiem[i][j]==2) jiem[i][j]=0;
? ???if(jiem[i][j]==2) {?if(jiem[i-1][j]==3) score+=100,printf("\7");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? jiem[i][j]=0,jiem[i-1][j]=2; }
? ? ?}
}
void print( ?) ?//输出界面函数
{int i,j;
qcjm( );
for(i=0;i22;i++)
for(j=0;j22;j++)
{ gtxy(12+j,4+i);
if(jiem[i][j]==3) {Color(13);printf("□");}
if(jiem[i][j]==2) {Color(10);printf(".");}
if(jiem[i][j]==1) {Color(10);printf("■");}
}
gtxy(37,11); Color(10);printf("得分:%d",score);
gtxy(37,13); printf("死亡:%d",death);
}
void setting( )? //游戏设置函数
{ qcjm( );
gtxy(12,4);printf("选择敌机速度:");
gtxy(12,5);printf("? 1.快 2.中 3.慢");
switch(getche( ))
? ? {case '1': speed=2; break;
? ? ?case '2': speed=4; break;
? ? ?case '3': speed=5; break;
? ? ?default: gtxy(12,6);printf("? 错误!默认值");
? ?}
gtxy(12,7);printf("选择敌机密度:");
gtxy(12,8);printf("? 1.大 2.中 3.小");
switch(getche( ))
? ? ?{case '1': density=20; break;
case '2': density=30;?break;
case '3': density=40; break;
? ?? default: gtxy(12,9);printf("? 错误!默认值");
? ???}
for(int i=0;i22;i++)
?for(int j=0;j22;j++)jiem[i][j]=0;
jiem[21][wj=10]=1; jiem[0][5]=3;
gtxy(12,10);printf("? 按任意键保存...");
getch( );
qcjm( );
}
void run( ) ?//游戏运行函数
{ jiem[21][wj]=1;? //值为1代表我机(2则为子弹)
jiem[0][5]=3;? ?//值为3代表敌机
SetConsoleTitle("歼灭敌机"); ?//设置窗口标题
while(1)
{?if (kbhit( ))? //如有键按下,控制我机左右移动、发射或进行设定
? ?? {int key;
? ? ? if((key=getch( ))==224) key=getch( );
? ? ? switch(key)
? ? ??{ case 75: if(wj0) jiem[21][wj]=0,jiem[21][--wj]=1; break;
? ? ? ? case 77: if(wj20)?jiem[21][wj]=0,jiem[21][++wj]=1;?break;
? ? ? ? case 72: jiem[20][wj]=2; break;
? ? ?? case 27: setting( );
? ? ??}
?? }
?? if(++n%density==0)? //控制产生敌机的速度
? ? ?{ n=0;srand((unsigned)time(NULL));
? ? ? ?jiem[0][rand( )%20+1]=3;
? ? ?}
? ??if(++m%speed==0) {feiji( ); m=0;} ?//控制敌机移动速度(相对子弹而言)
? ? zidan( );
?? print( );
Sleep(120); ?//延时120毫秒
? }
}
int main( )
{csh( );
?run( );
?return 0;
}
新手要方便写代码,可以收藏下面几个自编函数:
SetConsoleTitle("俄罗斯方块");? //设置窗口左上角标题栏处出现"俄罗斯方块"5个字
srand( (unsigned) time(NULL) ); ?//初始化随机数发生器
n= rand(? ) % 20; ??//产生随机数0-19中的一个. 如 rand(? )%5 就产生0-4中的一个数
SetConsoleTitle(? )函数在windows.h里,?srand(? )函数与rand(? )函数要配合用,
就是同时要用,在stdlib.h里。如果 rand( )%10+1 就产生1-10之中的一个数。
Sleep(300); ? //延时300毫秒(就是程序暂停300毫秒后继续运行)
system("cls"); ? //清屏(把窗口里的内容全部清除,光标定于(0,0)位置处)
这两个函数都在windows.h里。开头4个自编函数 编写如下:
void gtxy (int x, int y) ?//控制光标位置的函数
{ COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition ( GetStdHandle (STD_OUTPUT_HANDLE), pos );
}
void Color (int a) ?//设定颜色的函数
{ SetConsoleTextAttribute ( GetStdHandle ( STD_OUTPUT_HANDLE ),a ); }
void yinc (int x,int y) ? //隐藏光标的函数
{ CONSOLE_CURSOR_INFO ? gb={ x , y }; ? //gb代表光标
SetConsoleCursorInfo ( GetStdHandle(STD_OUTPUT_HANDLE),? gb );
}
void kou(int w,int h)? //设置窗口大小的函数
{HANDLE? hl=GetStdHandle ( STD_OUTPUT_HANDLE ) ;
COORD? size={ w , h };
SetConsoleScreenBufferSize( hl , size );
SMALL_RECT? rc={ 0, 0, w, h };
SetConsoleWindowInfo( hl, 1, rc );
}
最后这个函数,参数w是宽h是高。里边5行中第一行定义了句柄型变量hl,并给它赋值。
第二行定义了坐标型结构体变量size,它的取值决定了缓冲区的大小。第三行就是使用
size的值设置好缓冲区大小。第四行定义了变量rc,它的值决定当前窗口显示的位置与
大小(不得超过缓冲区的大小)。前两个0,0是从缓冲区左上角0列0行位置处开始,后两
个参数可以小于w和h.比如?rc={0,0,w-10,h-5}; 最后一行使用rc的值设置好窗口,中间
那个参数要为" 1 "或写“ true ”才有效。