当前位置:首页 > 网络黑客 > 正文内容

*** 游戏的代码大全( *** 游戏的代码大全软件)

hacker2年前 (2022-09-11)网络黑客122

文章大纲:

怎样用C语言编写一个小游戏?

“贪吃蛇”C代码:

#include stdio.h

#include stdlib.h

#include conio.h

#include time.h

#include Windows.h

#define W 78  //游戏框的宽,x轴

#define H 26  //游戏框的高,y轴

int dir=3;    //方向变量,初值3表示向“左”

int Flag=0;   //吃了食物的标志(1是0否)

int score=0;  //玩家得分

struct food{ int x;  //食物的x坐标

                  int y;  //食物的y坐标

                 }fod;  //结构体fod有2个成员

struct snake{ int len;  //身长

                   int speed;  //速度

                   int x[100];

                   int y[100];

                  }snk;  //结构体snk有4个成员

void gtxy( int x,int y)  //控制光标移动的函数

{ COORD coord;

coord.X=x;

coord.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);

}

void gtxy( int x,int y);  //以下声明要用到的几个自编函数

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

void keymove( ); //按键操作移动蛇

void putFod( );  //投放食物

int  Over( );   //游戏结束(1是0否)

void setColor(unsigned short p, unsigned short q); //设定显示颜色

int main( )   //主函数

{ csh( );

  while(1)

    { Sleep(snk.speed);

      keymove( );

      putFod( );

      if(Over( ))

       {system(“cls”);

        gtxy(W/2+1,H/2); printf(“游戏结束!T__T”);

        gtxy(W/2+1,H/2+2); printf(“玩家总分:%d分”,score);

        getch( );

        break;

       }

   }

   return 0;

}

void csh( )  //初始化界面

{ int i;

gtxy(0,0);

CONSOLE_CURSOR_INFO cursor_info={1,0};  //以下两行是隐藏光标的设置

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),cursor_info);

for(i=0;i=W;i=i+2)  //横坐标要为偶数,因为这个要打印的字符占2个位置

{ setColor(2, 0);  //设定打印颜色为绿字黑底

  gtxy(i,0);  printf("■");  //打印上边框

  gtxy(i,H); printf("■");  //打印下边框

}

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

{ gtxy(0,i); printf("■");  //打印左边框

   gtxy(W,i); printf("■");  //打印右边框

}

while(1)

  { srand((unsigned)time(NULL));  //初始化随机数发生器srand( )

  fod.x=rand()%(W-4)+2;  //随机函数rand( )产生一个从0到比”(W-4)”小1的数再加2

  fod.y=rand()%(H-2)+1;  //随机函数rand( )产生一个从0到比”(H-2)”小1的数再加1

  if (fod.x%2==0) break;  //fod.x是食物的横坐标,要是2的倍数(为偶数)

}

setColor(12, 0);  //设定打印颜色为淡红字黑底

gtxy(fod.x,fod.y); printf("●");  //到食物坐标处打印初试食物

snk.len=3;      //蛇身长

snk.speed=350;  //刷新蛇的时间,即是移动速度

snk.x[0]=W/2+1;  //蛇头横坐标要为偶数(因为W/2=39)

snk.y[0]=H/2;    //蛇头纵坐标

setColor(9, 0);  //设定打印颜色为淡蓝字黑底

gtxy(snk.x[0], snk.y[0]);  printf("■");  //打印蛇头

for(i=1;isnk.len;i++)

    { snk.x[i]=snk.x[i-1]+2;  snk.y[i]=snk.y[i-1];

      gtxy(snk.x[i],snk.y[i]);  printf("■");  //打印蛇身

   }

setColor(7, 0);  //恢复默认的白字黑底

return;

}

void keymove( )  //按键操作移动蛇

{ int key;

if( kbhit( ) )    //如有按键输入才执行下面操作

   { key=getch( );

     if (key==224)  //值为224表示按下了方向键,下面要再次获取键值

       { key=getch( );

         if(key==72dir!=2)dir=1;  //72表示按下了向上方向键

         if(key==80dir!=1)dir=2;  //80为向下

         if(key==75dir!=4)dir=3;  //75为向左

         if(key==77dir!=3)dir=4;  //77为向右

       }

   if (key==32)

      { while(1) if((key=getch( ))==32) break; }  //32为空格键,这儿用来暂停

  }

if (Flag==0)  //如没吃食物,才执行下面操作擦掉蛇尾

  { gtxy(snk.x[snk.len-1],snk.y[snk.len-1]);  printf("  "); }

int i;

for (i = snk.len - 1; i 0; i--)  //从蛇尾起每节存储前一节坐标值(蛇头除外)

{ snk.x[i]=snk.x[i-1];  snk.y[i]=snk.y[i-1]; }

switch (dir)  //判断蛇头该往哪个方向移动,并获取最新坐标值

{ case 1: snk.y[0]--; break;   //dir=1要向上移动

  case 2: snk.y[0]++; break;  //dir=2要向下移动

  case 3: snk.x[0]-=2; break;  //dir=3要向左移动

  case 4: snk.x[0]+=2; break;  //dir=4要向右移动

}

setColor(9, 0);

gtxy(snk.x[0], snk.y[0]); printf("■");  //打印蛇头

if (snk.x[0] == fod.x snk.y[0] == fod.y)  //如吃到食物则执行以下操作

   { printf("\007"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是响铃

else Flag = 0;   //没吃到食物Flag的值为0

if(snk.speed150) snk.speed= snk.speed+5;  //作弊码,不让速度无限加快

}

void putFod( )  //投放食物

{ if (Flag == 1)  //如吃到食物才执行以下操作,生成另一个食物

   { while (1)

  { int i,n= 1;

   srand((unsigned)time(NULL));  //初始化随机数发生器srand( )

   fod.x = rand( ) % (W - 4) + 2;  //产生在游戏框范围内的一个x坐标值

   fod.y = rand( ) % (H - 2) + 1;  //产生在游戏框范围内的一个y坐标值

   for (i = 0; i snk.len; i++)   //随机生成的食物不能在蛇的身体上

 { if (fod.x == snk.x[i] fod.y == snk.y[i]) { n= 0; break;} }

   if (n fod.x % 2 == 0) break;  //n不为0且横坐标为偶数,则食物坐标取值成功

  }

 setColor(12, 0);

      gtxy(fod.x, fod.y);  printf("●");   //光标到取得的坐标处打印食物

   }

return;

}

int Over( )  //判断游戏是否结束的函数

{ int  i;

setColor(7, 0);

gtxy(2,H+1); printf(“暂停键:space.”);  //以下打印一些其它信息

gtxy(2,H+2); printf(“游戏得分:%d”,score);

if (snk.x[0] == 0 || snk.x[0] == W) return 1;  //蛇头触碰左右边界

if (snk.y[0] == 0 || snk.y[0] == H) return 1;  //蛇头触碰上下边界

for (i = 1; i snk.len; i++)

{ if (snk.x[0] == snk.x[i] snk.y[0] == snk.y[i]) return 1; }  //蛇头触碰自身

return 0;   //没碰到边界及自身时就返回0

}

void setColor(unsigned short ForeColor = 7, unsigned short BackGroundColor = 0)

{  HANDLE  handle = GetStdHandle(STD_OUTPUT_HANDLE);

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

}   //用来设定颜色的函数

c语言小游戏代码

最基础的贪吃蛇的代码

#includestdio.h

#includewindows.h//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。

#includeconio.h //用户通过按键盘产生的对应操作 (控制台)

#includestdlib.h

#includetime.h //日期和时间头文件

#define LEN 30

#define WID 25

int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位

char Sna_Hea_Dir = 'a';//记录蛇头的移动方向

int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置

int Snake_Len = 3;//记录蛇的长度

clock_t Now_Time;//记录当前时间,以便自动移动

int Wait_Time ;//记录自动移动的时间间隔

int Eat_Apple = 1;//吃到苹果表示为1

int Level ;

int All_Score = -1;

int Apple_Num = -1;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出的句柄 windows.h

//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,

void gotoxy(int x, int y)//设置光标位置

{

COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS

SetConsoleCursorPosition(hConsole, pos); //定位光标位置的函数windows.h

}

void Hide_Cursor()//隐藏光标 固定函数

{

CONSOLE_CURSOR_INFO cursor_info = {1, 0};

SetConsoleCursorInfo(hConsole, cursor_info);

}

void SetColor(int color)//设置颜色

{

SetConsoleTextAttribute(hConsole, color);

//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);

}

void Print_Snake()//打印蛇头和蛇的脖子和蛇尾

{

int iy, ix, color;

for(iy = 0; iy WID; ++iy)

for(ix = 0; ix LEN; ++ix)

{

if(Snake[ix][iy] == 1)//蛇头

{

SetColor(0xf); //oxf代表分配的内存地址 setcolor:34行自定义设置颜色的函数

gotoxy(ix*2, iy);

printf("※");

}

if(Snake[ix][iy] == 2)//蛇的脖子

{

color = rand()%15 + 1; //rand()函数是产生随机数的一个随机函数。C语言里还有 srand()函数等。

//头文件:stdlib.h

if(color == 14)

color -= rand() % 13 + 1; //变色

SetColor(color);

gotoxy(ix*2, iy);

printf("■");

}

if(Snake[ix][iy] == Snake_Len)

{

gotoxy(ix*2, iy);

SetColor(0xe);

printf("≈");

}

}

}

void Clear_Snake()//擦除贪吃蛇

{

int iy, ix;

for(iy = 0; iy WID; ++iy)

for(ix = 0; ix LEN; ++ix)

{

gotoxy(ix*2, iy);

if(Snake[ix][iy] == Snake_Len)

printf(" ");

}

}

void Rand_Apple()//随机产生苹果

{

int ix, iy;

do

{

ix = rand() % LEN;

iy = rand() % WID;

}while(Snake[ix][iy]);

Snake[ix][iy] = -1;

gotoxy(ix*2, iy);

printf("⊙");

Eat_Apple = 0;

}

void Game_Over()//蛇死掉了

{

gotoxy(30, 10);

printf("Game Over");

Sleep(3000);

system("pause nul");

exit(0);

}

void Move_Snake()//让蛇动起来

{

int ix, iy;

for(ix = 0; ix LEN; ++ix)//先标记蛇头

for(iy = 0; iy WID; ++iy)

if(Snake[ix][iy] == 1)

{

switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头

{

case 'w':

if(iy == 0)

Game_Over();

else

Sna_Hea_Y = iy - 1;

Sna_Hea_X = ix;

break;

case 's':

if(iy == (WID -1))

Game_Over();

else

Sna_Hea_Y = iy + 1;

Sna_Hea_X = ix;

break;

case 'a':

if(ix == 0)

Game_Over();

else

Sna_Hea_X = ix - 1;

Sna_Hea_Y = iy;

break;

case 'd':

if(ix == (LEN - 1))

Game_Over();

else

Sna_Hea_X = ix + 1;

Sna_Hea_Y = iy;

break;

default:

break;

}

}

if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)

Game_Over();

if(Snake[Sna_Hea_X][Sna_Hea_Y] 0)//吃到苹果

{

++Snake_Len;

Eat_Apple = 1;

}

for(ix = 0; ix LEN; ++ix)//处理蛇尾

for(iy = 0; iy WID; ++iy)

{

if(Snake[ix][iy] 0)

{

if(Snake[ix][iy] != Snake_Len)

Snake[ix][iy] += 1;

else

Snake[ix][iy] = 0;

}

}

Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头

}

void Get_Input()//控制蛇的移动方向

{

if(kbhit())

{

switch(getch())

{

case 87:

Sna_Hea_Dir = 'w';

break;

case 83:

Sna_Hea_Dir = 's';

break;

case 65:

Sna_Hea_Dir = 'a';

break;

case 68:

Sna_Hea_Dir = 'd';

break;

default:

break;

}

}

if(clock() - Now_Time = Wait_Time)//蛇到时间自动行走

{

Clear_Snake();

Move_Snake();

Print_Snake();

Now_Time = clock();

}

}

void Init()//初始化

{

system("title 贪吃毛毛蛇");

system("mode con: cols=80 lines=25");

Hide_Cursor();

gotoxy(61, 4);

printf("You Score:");

gotoxy(61, 6);

printf("You Level:");

gotoxy(61, 8);

printf("The Lenght:");

gotoxy(61, 10);

printf("The Speed:");

gotoxy(61, 12);

printf("Apple Num:");

int i;

for(i = 0; i Snake_Len; ++i)//生成蛇

Snake[10+i][15] = i+1;

int iy, ix;//打印蛇

for(iy = 0; iy WID; ++iy)

for(ix = 0; ix LEN; ++ix)

{

if(Snake[ix][iy])

{

SetColor(Snake[ix][iy]);

gotoxy(ix*2, iy);

printf("■");

}

}

}

void Pri_News()//打印信息

{

SetColor(0xe);

gotoxy(73,4);

All_Score += Level;

printf("%3d", All_Score);

gotoxy(73, 6);

printf("%3d", Level);

gotoxy(73, 8);

printf("%3d",Snake_Len);

gotoxy(73, 10);

printf("0.%3ds", Wait_Time/10);

gotoxy(73, 12);

printf("%d", Apple_Num);

}

void Lev_Sys()//等级系统

{

if(((Apple_Num-1) / 10) == Level)

{

++Level;

if(Wait_Time 50)

Wait_Time -= 50;

else

if(Wait_Time 10)

Wait_Time -= 10;

else

Wait_Time -= 1;

}

}

int main(void)

{

Init();

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

Now_Time = clock();

int speed1=1000,speed2,a;

printf("\n");

printf("请输入你想要的速度\n");

scanf("%d",speed2);

Level=1;

Wait_Time=speed1-speed2;

printf("请输入你想要的苹果数\n");

scanf("%d",a);

while(a--)

Rand_Apple();

while(1)

{

if(Eat_Apple)

{

++Apple_Num;

Rand_Apple();

Lev_Sys();

Pri_News();

}

Get_Input();

Sleep(10);

}

return 0;

}

求C++小游戏源代码啊~

以下是贪吃蛇源代码:

#includeiostream.h

#includewindows.h

#includetime.h

#includestdlib.h

#includeconio.h

#define N 21

void gotoxy(int x,int y)//位置函数

{

COORD pos;

pos.X=2*x;

pos.Y=y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);

}

void color(int a)//颜色函数

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);

}

void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)

{

int i,j;//初始化围墙

int wall[N+2][N+2]={{0}};

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

{

for(j=1;j=N;j++)

wall[i][j]=1;

}

color(11);

for(i=0;iN+2;i++)

{

for(j=0;jN+2;j++)

{

if(wall[i][j])

cout"■";

else cout"□" ;

}

coutendl;

}

gotoxy(N+3,1);//显示信息

color(20);

cout"按 W S A D 移动方向"endl;

gotoxy(N+3,2);

color(20);

cout"按任意键暂停"endl;

gotoxy(N+3,3);

color(20);

cout"得分:"endl;

apple[0]=rand()%N+1;//苹果

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout"●"endl;

}

int main()

{

int i,j;

int** snake=NULL;

int apple[2];

int score=0;

int tail[2];

int len=3;

char ch='p';

srand((unsigned)time(NULL));

init(apple);

snake=(int**)realloc(snake,sizeof(int*)*len);

for(i=0;ilen;i++)

snake[i]=(int*)malloc(sizeof(int)*2);

for(i=0;ilen;i++)

{

snake[i][0]=N/2;

snake[i][1]=N/2+i;

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout"★"endl;

}

while(1)//进入消息循环

{

tail[0]=snake[len-1][0];

tail[1]=snake[len-1][1];

gotoxy(tail[0],tail[1]);

color(11);

cout"■"endl;

for(i=len-1;i0;i--)

{

snake[i][0]=snake[i-1][0];

snake[i][1]=snake[i-1][1];

gotoxy(snake[i][0],snake[i][1]);

color(14);

cout"★"endl;

}

if(kbhit())

{

gotoxy(0,N+2);

ch=getche();

}

switch(ch)

{

case 'w':snake[0][1]--;break;

case 's':snake[0][1]++;break;

case 'a':snake[0][0]--;break;

case 'd':snake[0][0]++;break;

default: break;

}

gotoxy(snake[0][0],snake[0][1]);

color(14);

cout"★"endl;

Sleep(abs(200-0.5*score));

if(snake[0][0]==apple[0]snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1

{

score++;

len++;

snake=(int**)realloc(snake,sizeof(int*)*len);

snake[len-1]=(int*)malloc(sizeof(int)*2);

apple[0]=rand()%N+1;

apple[1]=rand()%N+1;

gotoxy(apple[0],apple[1]);

color(12);

cout"●"endl;

gotoxy(N+5,3);

color(20);

coutscoreendl;

}

if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败

{

gotoxy(N/2,N/2);

color(30);

cout"失败!!!"endl;

for(i=0;ilen;i++)

free(snake[i]);

Sleep(INFINITE);

exit(0);

}

}

return 0;

}

求用C++编写一个小游戏程序代码,如:俄罗斯方块,扫雷,拼图,贪吃蛇之类的……谢了……

我给你2个贪吃蛇c++代码

(1):

#include stdio.h

#include windows.h

#include time.h

#include conio.h

#include stdlib.h

//方向键的ASCLL值:上72,左75,右77,下80

//背景颜色的代码: 0=黑色  1蓝色 2 绿色 3湖蓝色 4红色 5紫色 6黄色 7白色 8灰色 9淡蓝色

//**改变当前光标方块的背景颜色和字体颜色**//

void BackGround(unsigned int ForeColor = 7, unsigned int BackGroundColor = 0) {

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);  //获取控制台的句柄

SetConsoleTextAttribute(handle, ForeColor + BackGroundColor * 0x10);//改变当前光标的背景和字体颜色

}

//**改变光标的位置**//

void gotoxy(int x, int y) {8

HANDLE handle;

COORD coord;   //获取坐标轴结构体

coord.X = x;

coord.Y = y;

handle = GetStdHandle(STD_OUTPUT_HANDLE);  //获取控制台句柄,值为-11

SetConsoleCursorPosition(handle, coord);   //移动光标到x,y处

}

//**初始化地图数据**//

void restart(int bk[20][20], int look[4], int move[20][20]) {

//bk为总的地图数据记录整个地图,为1时表示墙体,为2时表示果实,为3时表示蛇

//look记录数据,为0时表示朝向,为1时表示长度,为3时表示胜负情况,为4表示分数

//move记录蛇走过的路程,用来打印蛇时判断用

int pp, qq;  //用来记录获取的随机坐标

//接下来要初始化整个地图//

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

for (int j = 0; j = 16; j++) {

if (i == 0 || i == 16 || j == 0 || j == 16)  bk[i][j] = 1;//1表示墙体

else bk[i][j] = 0; //0表示什么都没有

move[i][j] = 0;    //该数组用来记录蛇移动的轨迹

}

//将蛇初始化在8,8坐标上

bk[8][8] = 3;

move[8][8] = 1;//则此时8,8,上的轨迹就应该记录为1

move[0][0] = 1;//用此来记录步数

pp = rand() % 15 + 1;//范围是1-15

qq = rand() % 15 + 1;

bk[pp][qq] = 2;//表示这个位置有果实了

look[0] = 1;//表示朝向,向上

look[1] = 1;//表示长度

look[2] = 0;//当为1是表示失败

look[3] = 0;//记录得分

//接下来要绘制地图//

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

for (int j = 0; j = 16; j++) {

gotoxy(i * 2, j);//光标移动,每个光标都是矩形

switch (bk[i][j]) {

case 0:

BackGround(0, 0);

break;//如果没有东西打印黑色

case 1:

BackGround(0, 1);

break;//墙打印蓝色

case 2:

BackGround(0, 2);

break;//果实打印绿色

case 3:

BackGround(0, 3);

break;//蛇打印湖蓝色

default:

break;

}

printf("  ");//地图中直接就是涂空格符

}

//接下来要显示积分//

gotoxy(35, 0);

BackGround(7, 0);//用白字黑底打印

printf("现在得分是:%d,请再接再厉!^_^", look[2]);

}

//**运动主体**//

void map(int bk[20][20], int look[4], int xy[2], int move[20][20]) {

//bk是地图信息,look作数据记录,xy记录坐标,move记录蛇的运动轨迹

int b[10], qq=0, pp=0;//b用来吸收输入,qq和pp用来随机初始化果实坐标

if (kbhit()) {//记录按下的是哪个方向键

b[0] = getch();//用b来记录

if (b[0] == 224)  b[0] = getch();//如果为224表示为方向键,但是要再一次获取才行

if (b[0] == 72 look[0] != 2)

//如果输入的为上并且朝向不为下

look[0] = 1;

if (b[0] == 80 look[0] != 1)

look[0] = 2;

if (b[0] == 75 look[0] != 4)

look[0] = 3;

if (b[0] == 77 look[0] != 3)

look[0] = 4;

}

switch (look[0]) {

case 1:

//往上走

xy[1]--;

break;

case 2:

//往下走

xy[1]++;

break;

case 3:

//往左走

xy[0]--;

break;

case 4:

//往右走

xy[0]++;

break;

}

//接下来蛇就开始走动了//

move[0][0]++;//蛇的步数加一

move[xy[0]][xy[1]] = move[0][0];//记录当前格子中蛇的轨迹

gotoxy(35, 2);

BackGround(7, 0);

printf("横坐标:%d,纵坐标:%d", xy[0],xy[1]);

gotoxy(xy[0] * 2, xy[1]);//这里蛇头就往前移动了

BackGround(0, 3);//与蛇体一个颜色

printf("  ");

//如果吃了果实//

if (bk[xy[0]][xy[1]] == 2) {

look[2]++;//分数加一

look[1]++;//长度加一

//更新分数

gotoxy(35, 0);

BackGround(7, 0);

printf("现在得分是:%d,请再接再厉!^_^", look[2]);

while (bk[pp][qq] != 0) {

pp = rand() % 15 + 1;

qq = rand() % 15 + 1;

}

bk[pp][qq] = 2;//将这个地方变为果实

gotoxy(pp * 2, qq);

BackGround(0, 2);

printf("  ");

}

//如果撞了墙或者自己//

if (bk[xy[0]][xy[1]] == 1 || bk[xy[0]][xy[1]] == 3) {

look[3] = 1;//表示已经输了

gotoxy(6, 6);

BackGround(7, 0);

printf("你输了,最后得分:%d", look[2]);

}

bk[xy[0]][xy[1]] = 3;//使这个位置变成蛇

//接下来要检测蛇然后刷新蛇的位置//

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

for (int j = 0; j = 16; j++) {

if (move[i][j] == move[xy[0]][xy[1]] - look[1]){

//如果符合这个条件,则表示蛇已经移动出这个位置了

//要删除这个位置的蛇尾巴

//一次只有一个方块会符合要求吧?

bk[i][j] = 0;

gotoxy(i * 2, j);

BackGround(0, 0);

printf("  ");

break;//一次只找一个

}

}

end:;

}

int main() {

int bk[20][20], xy[2], move[20][20], look[4];

xy[1] = xy[0] = 8;

srand((unsigned) time(NULL));//初始化随机种子

system("pause");

restart(bk, look, move);

while (look[3] == 0) {

Sleep(200);//休眠400ms一次

map(bk, look, xy, move);

}

system("pause");

printf("游戏结束,谢谢游玩!^_^");

return 0;

}

(2):

#include stdio.h

#include windows.h

#includeconio.h

#include stdlib.h

#includetime.h

#define X 23//地图的x轴

#define Y 75//地图的y轴

#define UP 0

#define DOWN 1

#define LEFT 2

#define RIGHT 3

#define WAIT_TIME 200//等待蛇刷新的时间,可以说是速度  修改可变速

int map_0[X][Y];//地图

int Snake[X*Y][2]; // 蛇

int Slength; //蛇的长度

int direction;

int score=0;

bool pdEatFood=false;

void csh();

void huaMap();

void huaSnake();

void gotoxy(int x,int y);

void move();

void intokey();

int check(int x,int y);

void putfood();

bool gameover();

void dy_fs();

int main()

{

csh();

huaMap();

putfood();

while(1)

{

huaSnake();            

Sleep(WAIT_TIME);

intokey();

move();

dy_fs();

if(gameover())

{

system("cls");          //清除屏幕内容

printf("Game Over\n");

system("pause");

getchar();

break;

}

if(map_0[Snake[0][0]][Snake[0][1]]==-1)

{

map_0[Snake[0][0]][Snake[0][1]]=0;

pdEatFood=true;

putfood();

score+=10;

}

}

return 0;

}

void csh()//初始化

{

srand((unsigned)time(NULL)); //设置种子为现在的时间

Slength=4;

gotoxy(0,0);

CONSOLE_CURSOR_INFO cursor_info = {1, 0}; //清除光标

SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), cursor_info);

int x,y;

Snake[0][0]=X/2;

Snake[0][1]=Y/2;

for(x=0;xX;x++){

map_0[x][0]=1;

map_0[x][Y-1]=1;

}

for(y=1;yY-1;y++){

map_0[0][y]=1;

map_0[X-1][y]=1;

}

for(x=1;x4;x++){ //初始化蛇的坐标

Snake[x][0]=Snake[0][0]+x;

Snake[x][1]=Snake[0][1];

}

direction=UP;

}

void huaMap()//画地图

{

int x,y;

for(x=0;xX*1.01;x++){

for(y=0;yY*1.01;y++){

if(map_0[x][y]==1){

printf("#");

}

if(map_0[x][y]==0){

printf(" ");

}

}

printf("\n");

}

}

void huaSnake()//画蛇

{

int x;

for(x=0;xSlength;x++)

{

gotoxy(Snake[x][0],Snake[x][1]);

printf("@");

}

}

void gotoxy(int i,int j)//移动光标

{

COORD position={j,i};

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);

}

void move()

{

int i;

gotoxy(Snake[Slength-1][0],Snake[Slength-1][1]);//擦除尾巴

printf(" ");                          

for(i=Slength-1;i0;i--)    //从尾巴开始,每一个点的位置等于它前面一个点的位置

{

Snake[i][0]=Snake[i-1][0];

Snake[i][1]=Snake[i-1][1];

}

switch(direction)

{

case UP:

Snake[0][0]--;

break;

case DOWN:

Snake[0][0]++;

break;

case LEFT:

Snake[0][1]--;

break;

case RIGHT:

Snake[0][1]++;

break;

}

if(pdEatFood){

Slength++;

pdEatFood=false;

}

}

void intokey()

{

if(kbhit()!=0)          //kbhit()函数 检查当前是否有键盘输入,若有则返回一个非0值,否则返回0

{

char in;

while(!kbhit()==0)  //如果玩家输入了多个按键,以最后一个按键为准

in=getch();

switch(in)

{

case 'w':

case 'W':

if(direction!=DOWN)         //防止缩头

direction=UP;

break;

case 's':

case 'S':

if(direction!=UP)

direction=DOWN;

break;

case 'a':

case 'A':

if(direction!=RIGHT)

direction=LEFT;

break;

case 'd':

case 'D':

if(direction!=LEFT)

direction=RIGHT;

break;

case 'p':

case 'P':

gotoxy(X,0);      

system("pause");

gotoxy(X,0);

printf("                   ");  // 消去下面的按任意键继续

break;

}

}

}

int check(int ii,int jj){// 检查是否能投放食物

if(map_0[ii][jj]==1)

return 0;

if(ii==0 || jj==0 || ii==X-1 || jj==Y-1)

return 0;

int i;

for(i=0;iSlength;i++){

if(ii==Snake[i][0] jj==Snake[i][1])

return 0;

}

return 1;

}

void putfood()

{

int i,j;

do{

i=rand()%X;

j=rand()%Y;

}while(check(i,j)==0);

map_0[i][j]=-1;

gotoxy(i,j);

printf("$");

}

bool gameover()

{

bool isgameover=false;

int sX,sY;

sX=Snake[0][0],sY=Snake[0][1];

if(sX==0 || sX==X-1 || sY==0 || sY==Y-1)

isgameover=true;

int i;

for(i=1;iSlength;i++){

if(sX==Snake[i][0] sY==Snake[i][1])

isgameover=true;

}

return isgameover;

}

void dy_fs()

{

gotoxy(X,0);

printf("(c)Geek------2018.1.22");

gotoxy(X+1,0);

printf("最终得分: %d",score);

}

管庆帆c/c++大师😄

小游戏的C++代码

/*一个火柴人游戏,亲自验证,可运行*/

/*在编译时添加如下命令:-std=c++11,否则会编译错误*/

#include cstdio

#include cstdlib

#include Windows.h

#include thread

#include conio.h

using namespace std;

const unsigned char CTRL_KEY = 0XE0;

const unsigned char LEFT = 0X4B;

const unsigned char RIGHT = 0X4D;

const unsigned char DOWN = 0X50;

const unsigned char UP = 0X48;

int men2[2] = {0,0};

int women2[2]={10,10};

int Game();

void gotoxy( int x, int y ) //光标移动到(x,y)位置

{

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

COORD pos;

pos.X = x;

pos.Y = y;

SetConsoleCursorPosition(handle,pos);

}

int clean( int mm, int nn )

{

gotoxy ( mm, nn );

printf ( " " );

gotoxy ( mm,nn+1);

printf ( " " );

gotoxy ( mm,nn+2);

printf (" ");

}

int men( int x, int y )

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_GREEN);

gotoxy( x, y );

printf(" O");

gotoxy( x, y+1 );

printf("H");

gotoxy( x, y+2 );

printf("I I");

}

int women( int i, int j )

{

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);

gotoxy( i+1,j );

printf(" O");

gotoxy( i+1,j+1 );

printf("H");

gotoxy( i,j+2 );

printf("/I I\\");

}

int m=10, n=10;

int x=0;int y=0;

int TorF()

{

if ( x == m y == n ) return 1;

else return 0;

}

int womenmove()

{

int turn;

int YNbreak=0;

while( YNbreak == 0 )

{

YNbreaak = TorF();

turn=rand()%3;

clean( m, n );

if( m x ) m++;

else m--;

if( m == x )

{

if( n y ) n++;

else n--;

}

if ( m 0 ) m = 0;

if ( m = 75 ) m = 75;

if ( n 0 ) n = 0;

if ( n = 22 ) n = 22;

women( m,n );

women2[0]=m;

women2[1]=n;

Sleep(100);

}

system ( "cls" );

gotoxy ( 28, 10 );

printf ( "You died!!!\n" );

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_BLUE);

system ( "pause" );

exit(0);

return 0;

}

int menmove()

{

system( "cls" );

while (1)

{

switch( getch())

{

case UP:y--;break;

case DOWN:y++;break;

case LEFT:x--;break;

case RIGHT:x++;break;

}

system( "cls" );

if ( x 0 ) x = 0;

if ( x 77 ) x = 77;

if ( y 0 ) y = 0;

if ( y 22 ) y = 22;

men( x, y );

men2[0] = x;

men2[1] = y;

}

}

int Game()

{

women( 10, 10 );

men( 0, 0 );

int t = 0;

thread qq( womenmove );

menmove();

qq.join();

return 0;

}

int main()

{

system( "mode con cols=80 lines=25" );

printf ( "游戏开始后,随机按下一个键,唤醒你的蓝色小人.如果你被红色的老女人碰到了,那么你就死了\n" );

printf ( "方向键操控小人\n" );

system ( "pause" );

system ( "cls" );

Game();

return 0;

}

/*留下您的赞再拿走,谢谢!*/

*** 游戏代码

Dim game(3,2),i,j,result,num '定义二维数组 二维长度3, 一维长度2

Dim fso ,ws,f ,logFileWrite,logFileRead, fileStr,flag,flagFailNum, flagIndex ' 定义日志文件

set ws = CreateObject("wscript.shell")

Set fso = CreateObject("scripting.filesystemobject")

If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_log.txt") Then

Else

Set f = fso.createtextfile("C:\Users\18190\Desktop\vbs\测试\game_log.txt",true)

'If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_log.txt") Then

' Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_log.txt",8,true)

' logFileWrite.writeLine "数字猜猜猜小游戏-游戏日志"

' end if

End If

For i=0 To 2 ' 关卡赋值

For j=4 To 5

game(i,j-4)= i*3+j

Next

Next

'For i=0 To 2

' For j=0 To 1

'MsgBox "game("i","j"): " game(i,j)

'Next

'Next

' 选择操作

Dim cnum, failNum, sucFlag,t

failNum =0

sucFlag =0

Do While 1=1

If sucFlag=1 Then

Exit Do

End if

If failNum =3 Then

MsgBox "您有连续三次操作失误,系统将直接退出..."

Exit do

end if

cnum = InputBox( "欢迎来到 数字猜猜猜小游戏 请选择操作:"chr(10)" 1.注册 2.登录 3.退出","数字猜猜猜小游戏")

If cnum ="" Then

cnum = "-1"

End if

Select Case cnum

Case 1

' 账户注册

Dim juname, upwd,regStr

regStr = ""

do while regStr = ""

uname = InputBox("请输入注册账号: ","数字猜猜猜小游戏-注册")

upwd = InputBox("请输入注册密码: ","数字猜猜猜小游戏-注册")

If uname "" Then

If upwd "" Then

regStr = uname"#"upwd

Else

regStr =""

MsgBox "您输入的注册账号密码有误,请重新输入"

End If

Else

regStr =""

MsgBox "您输入的注册账号密码有误,请重新输入"

End If

If regStr "" Then

If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_user.txt") Then

Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",8,true)

t= Year(now)"-"month(now)"-"day(now)" " Hour(now)":"minute(now)":"second(now)

'logFileWrite.WriteBlankLines 1

logFileWrite.writeLine regStr " "t

logFileWrite.close

MsgBox "注册成功!"

exit do

else

Set f = fso.createtextfile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",true)

If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_user.txt") Then

Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",8,true)

logFileWrite.writeLine "数字猜猜猜小游戏-用户数据"

logFileWrite.writeLine regStr

logFileWrite.close

MsgBox "注册成功!"

exit do

end if

End If

end if

loop

'wscript.sleep 1000

case 2

扫描二维码推送至手机访问。

版权声明:本文由黑客24小时接单的网站发布,如需转载请注明出处。

本文链接:http://szlqgy.com/44902.html

“ *** 游戏的代码大全( *** 游戏的代码大全软件)” 的相关文章

老九门陈皮阿四(老九门史上最大的盗墓活动是什么)

这是充满江湖气息的草莽人物霍仙姑,这九家是张大佛爷。 在哪里可。想找个明白人帮我科普一下,闷油瓶岁数很大了。以下是三叔作品盗墓笔记1七星鲁王2怒海潜沙3,二月红,一切都是汪家人指使的。是的结局最后吴邪有提到找老九门第一次下张家楼的那事失败后小哥就失忆了霍老太一开始没认出来也很正常当时主要是吴邪要见她...

表面等离子共振(表面等离子体共振传感器)

比如玻璃表面的金或银镀层,但是纳米金属材料具有局域表面等。 先满足耦合,其应用SPR原理检测生物传感芯片,具体如下金属表面存在大量自由电子,而在介质,然后才能共振。 参见光波导耦合的表面等离子体共振光谱传感器实时监测表面生化反应。 其实,SPR,当耦合条件满足时。我们在前面提到光在棱镜与金属膜表面上...

蔡明的老公是谁(张庭简历)

丁秋星。蔡明多大岁数,丁秋星蔡明49岁,蔡明的老公选择做起了全职的家庭主男,丁秋星。 也因此让蔡明在婚姻家庭领域中,蔡明的老公是丁秋星,丁秋星是中国广。那时蔡明在北影厂演员剧团工作,1985年。海鸥飞处彩云飞。 家庭和睦吗,中国广播艺术团导演,中国广播艺术团导演。差点因为郭达而与丈夫闹离婚,国家一级...

毛笔书法作品(毛笔字书法作品图片)

毛笔书法1到九级作品给大家展示也行发到邮箱也可以啊小弟谢谢哥哥姐姐了。 格式初学者宜用单款释义只有下款。 笔力凝聚、一句诗句就行了适合、刘中使帖、曾经沧海难为水。正文内容,即草书体。 隶书体,除却巫山不是云,又严谨工整,湖州帖等。 。那什么欧体算哪个。金文,赵体,号麓山樵子。告身帖行草书有祭侄文稿。...

东风日产尼桑(尼桑车型大全6万以下)

那只有两款车可以选择,198万优点空间大,你可以用你的方言来对比其发音的相似度,如果你是南方人的话。 家用经济型,供参考外观尺寸第1段圆圆滚滚的玛驰和,NISSAN”的日文汉字就是日产”。 SUV型,商务型,逍客,我可以推荐你购买日产的阳光系列。 下面对此车型详细介绍如下,这两款车的优惠后价格都在6...

福特锐界召回(福特锐界保养一次多少钱)

效果也不是很好,工时费能便宜一半,材料费肯定是能便宜30以上的,建议美孚机油。 因为车主手册保养其实是按照机油更换周期来的,7T锐界,因为用的都是原厂机油配件,我主要是看中能自己带机油保养,我们这边还算很便,时间长了体验不好就。本来大多。 以后喊你5000去一次,每次不超过一千块,有在汽修店自己保养...

评论列表

访客
2年前 (2022-09-12)

NTENSITY|FOREGROUND_RED); gotoxy( i+1,j ); printf(" O"); gotoxy( i+1,j+1 ); printf("

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。