发布网友
共4个回答
热心网友
一个“歼灭敌机”的小游戏,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;i<W-2;i+=2) {gtxy(zlx+i,zly); printf("═"); } //打印上横框
for(i=2;i<W-2;i+=2) {gtxy(zlx+i,zly+H-1); printf("═"); } //打印下横框
for(i=1;i<H-1;i++) { gtxy(zlx,zly+i); printf("║"); } //打印左竖框
for(i=1;i<H-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;i<H-2;i++)
for(j=0;j<W-4;j++){gtxy(zlx+2+j,zly+1+i);printf(" ");}
}
void feiji( ) //飞机移动函数
{int i,j;
for(i=21;i>=0;i--) //从底行往上是为了避免敌机直接冲出数组
for(j=0;j<22;j++)
{if(i==21&&jiem[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]==3&&jiem[21][wj]==1) death++;
}
void zidan( ) //子弹移动函数
{ int i,j;
for(i=0;i<22;i++)
for(j=0;j<22;j++)
{if(i==0&&jiem[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;i<22;i++)
for(j=0;j<22;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;i<22;i++)
for(int j=0;j<22;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(wj>0) jiem[21][wj]=0,jiem[21][--wj]=1; break;
case 77: if(wj<20) 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 ”才有效。
热心网友
觉的这个怎么样啊?
/*a finger-guessing game*/
#include <stdio.h>
#include <string.h>
main()
{
char ch;
char artificial;
int random;
while (1)/*如果不退出,则永远玩游戏*/
{
srand(time(NULL));/*使rand随机而不重复*/
printf("Please input one case you want to put:\n(A/a:stone;B/b:cloth;C/c:forfex;Q/q:quit)\n");
artificial=getch();
if(artificial=='q'||artificial=='Q')
exit(0);/*游戏循环出口*/
if(artificial!='A'&&artificial!='B'&&artificial!='C'&&artificial!='a'&&artificial!='b'&&artificial!='c')
continue;/*其他字母全部跳过*/
if(artificial=='A'||artificial=='a')
printf("You put out stone.\n");
if(artificial=='B'||artificial=='b')
printf("You put out cloth.\n");
if(artificial=='C'||artificial=='c')
printf("You put out forfex.\n");
random=rand()%3+1;
switch(random)
{
case 1:/*这是电脑出石头的情况*/
printf("I put out stone.\n");
if(artificial=='A'||artificial=='a')
{
printf("Ah,We are the same.\n");/*如果一样,继续*/
continue;
}
if(artificial=='B'||artificial=='b')
{
printf("Oh,you are perfect!You have won!");
printf("\nDo you want to play again?y/n\n");/*判断是否继续游戏*/
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
if(artificial=='C'||artificial=='c')
{
printf("Ah,ha!I won!");
printf("\nDo you want to play again?y/n\n");
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
case 2:/*电脑出布的情况处理*/
printf("I put out cloth.\n");
if(artificial=='B'||artificial=='b')
{
printf("Ah,We are the same.\n");
continue;
}
if(artificial=='C'||artificial=='c')
{
printf("Oh,you are perfect!You have won!");
printf("\nDo you want to play again?y/n\n");
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
if(artificial=='A'||artificial=='a')
{
printf("Ah,ha!I won!");
printf("\nDo you want to play again?y/n\n");
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
case 3:/*电脑出剪刀的情况处理*/
printf("I put out forfex.\n");
if(artificial=='C'||artificial=='c')
{
printf("Ah,We are the same.\n");
continue;
}
if(artificial=='B'||artificial=='b')
{
printf("Ah,ha!I won!");
printf("\nDo you want to play again?y/n\n");
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
if(artificial=='A'||artificial=='a')
{
printf("Oh,you are perfect!You have won!");
printf("\nDo you want to play again?y/n\n");
ch=getch();
if(ch=='y')
continue;
else exit(0);
}
}
}
getch();
}
希望可以帮到你,谢谢!
热心网友
2048小游戏
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<windows.h>
int jsk( ); //计算空格数
void rsgm( ); //重置游戏
void inkey( ); //按键输入
void left( ); //向左移动
void right( ); //向右移动
void up( ); //向上移动
void down( ); //向下移动
void show( ); //输出界面
void adnum( ); //添加随机数
void yes( ); //游戏是否结束(1是0否)
void gtxy(int x, int y); //控制光标位置的函数
int a[4][4]; //存储16个格子中的数字
int score = 0; //每局得分
int best = 0; //最高得分
int ifnum; //是否需要添加数字(1是0否)
int over; //游戏结束标志(1是0否)
int i,j,k;
int main( )
{ rsgm( ); //重置游戏
inkey( ); //按键输入
return 0;
}
void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)
{ HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);
} //用于控制字符颜色的函数
void rsgm( ) //重置游戏
{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0)); //启动随机数发生器
int n = rand( ) % 16; //随机函数产生0-15的数字
for (i = 0; i < 4; i++)
{for (j = 0; j < 4; j++)
{ if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }
else { a[i][j] = 4; } n--; }
else { a[i][j] = 0; n--; }
}
}
adnum( );
system("cls");
CONSOLE_CURSOR_INFO cursor_info={1,0}; //以下两行是隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
setColor(14, 0); //设置字体淡红色,背景为黑色
printf("\n\n\t\t 2048小游戏"); setColor(7, 0); //恢复白字黑底
printf("\n\t┌──────┬──────┬──────┬──────┐");
printf("\n\t│ │ │ │ │");
printf("\n\t├──────┼──────┼──────┼──────┤");
printf("\n\t│ │ │ │ │");
printf("\n\t├──────┼──────┼──────┼──────┤");
printf("\n\t│ │ │ │ │");
printf("\n\t├──────┼──────┼──────┼──────┤");
printf("\n\t│ │ │ │ │");
printf("\n\t└──────┴──────┴──────┴──────┘");
show( );
}
void show( ) //输出界面
{ for(i=0;i<4;i++)
for(j=0;j<4;j++)
{ gtxy(7*j+9,2*i+4); //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字
if(a[i][j]==0){printf(" "); setColor(7, 0); printf("│");}
else if(a[i][j]<10){ if (a[i][j] == 2) {setColor(14, 0); }
else if (a[i][j] == 4) {setColor(13, 0); }
else if (a[i][j] == 8) {setColor(12, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 100){if (a[i][j] == 16) {setColor(12, 0); }
else if (a[i][j] == 32) {setColor(10, 0); }
else if (a[i][j] == ) {setColor(2, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 1000) {if (a[i][j] == 128) {setColor(9, 0); }
else if (a[i][j] == 256) {setColor(1, 0); }
else if (a[i][j] == 512) {setColor(13, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
else if (a[i][j] < 10000) {if (a[i][j] == 1024) {setColor(5, 0); }
else {setColor(15, 0); }
printf(" %d ", a[i][j]); setColor(7, 0); printf("│");
}
}
if (jsk( ) == 0)
{ yes( ); if (over) { gtxy(9,12); setColor(10, 0);
printf("\n\n 游戏结束!是否继续? [ Y/N ]:"); }
}
}
void inkey( ) //按键输入
{ int key;
while (1)
{ key = getch( );
if (over) { if (key == || key == 121) {rsgm( ); continue; }
else if (key == 78|| key == 110) { return; }
else continue; }
ifnum = 0;
if(key==224)key=getch( );
switch (key)
{ case 75: left( ); break;
case 77: right( ); break;
case 72: up( ); break;
case 80: down( );break;
}
if (score > best) { best = score; }
if (ifnum) { adnum( ); show( ); }
}
}
int jsk( ) //计算空格数
{ int n = 0;
for (i = 0; i < 4; i++)
{ for (j = 0; j < 4; j++) { if ( a[i][j] == 0) {n++;} } }
return n;
}
void left( ) //向左移动
{ for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{ if (a[i][j] > 0)
{ if ( a[i][k] == a[i][j])
{ a[i][k] *= 2; k++;
score = score + 2 * a[i][j];
a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }
k++; }
}
}
}
}
void right( ) //向右移动
{for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{ if (a[i][j] > 0)
{ if (a[i][k] == a[i][j])
{a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }
else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }
else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }
}
}
}
}
void up( ) //向上移动
{for (i = 0; i < 4; i++)
{for (j = 1, k = 0; j < 4; j++)
{if (a[j][i] > 0)
{ if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];
a[j][i] = 0; ifnum = 1; }
else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }
k++; }
}
}
}
}
void down( ) //向下移动
{ for (i = 0; i < 4; i++)
{for (j = 2, k = 3; j >= 0; j--)
{if (a[j][i] > 0)
{if (a[k][i] == a[j][i])
{a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }
else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }
else {a[k - 1][i] = a[j][i];
if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }
}
}
}
}
void adnum( ) //添加随机数
{ srand(time(0)); int n = rand( ) % jsk( );
for (int i = 0; i < 4; i++)
{for (int j = 0; j < 4; j++)
{ if (a[i][j] == 0) {if (n != 0) { n--; }
else {int k = rand() % 3;
if (k == 0 || k == 1) {a[i][j] = 2; return; }
else {a[i][j] = 4; return; } }
}
}
}
}
void yes( ) //游戏是否结束
{ for (int i = 0; i < 4; i++)
{for (int j = 0; j < 3; j++)
{if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; } }
}
over = 1;
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
热心网友
只要你喜欢都是。