c/c++/java代码编程 - 爱问答

(爱问答)

c/c++/java代码编程

随便啥c/c++代码
不急
java也可
只要游戏

一般的游戏代码不是这里能贴上的,给你个简单的C++贪吃蛇代码参考下

///23i47238472672418242378547098079641562341535651234654672523543333332323232323232323232323232323232323329573867777777777777777123124234

#include<iostream> #include<windows.h> #include<time.h> #include<conio.h> using namespace std; // 刷新当前屏幕 inline void Refresh(char q[][22], int grade, int gamespeed){    system("cls");     //  清屏 int i,j; cout << endl; for(i=0;i<22;i++){     cout << " ";  for(j=0;j<22;j++)   cout<<q[i][j]<<' ';    //  输出贪吃蛇棋盘        if(i==0) cout << " 等级为:" << grade;        if(i==4) cout << " 自动前进时间";        if(i==6) cout << " 间隔为:" << gamespeed << "ms";  cout<<endl; } } int main(){    char tcsQipan[22][22];     //  贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)    int i,j;    for(i=1;i<=20;i++)        for(j=1;j<=20;j++)            tcsQipan[i][j]=' ';     //     初始化贪吃蛇棋盘中间空白部分    for(i=0;i<=21;i++)        tcsQipan[0][i] = tcsQipan[21][i] = '-';      //初始化贪吃蛇棋盘上下墙壁    for(i=1;i<=20;i++)        tcsQipan[i][0] = tcsQipan[i][21] = '|';      //初始化贪吃蛇棋盘左右墙壁    int tcsZuobiao[2][100];     //蛇的坐标数组    for(i=0; i<4; i++){        tcsZuobiao[0][i] = 1;        tcsZuobiao[1][i] = i + 1;    }    int head = 3,tail = 0;    for(i=1;i<=3;i++)        tcsQipan[1][i]='*';    //蛇身    tcsQipan[1][4]='#';       //蛇头    int x1, y1;           // 随机出米    srand(time(0));    do{  x1=rand()%20+1;  y1=rand()%20+1; }while(tcsQipan[x1][y1]!=' '); tcsQipan[x1][y1]='*'; cout<<" 贪吃蛇游戏即将开始 !"<<endl;//准备开始;; long start;    int grade = 1, length = 4;    int gamespeed = 500;  //前进时间间隔 for(i=3;i>=0;i--){  start=clock();  while(clock()-start<=1000);  system("cls");  if(i>0)   cout << " 进入倒计时:" << i << endl;  else   Refresh(tcsQipan,grade,gamespeed); }    int timeover;    char direction = 77;  // 初始况下,向右运动    int x,y;    while(1){        timeover = 1;        start = clock();        while((timeover=(clock()-start<=gamespeed))&&!kbhit());             //如果有键按下或时间超过自动前进时间间隔则终止循环        if(timeover){            getch();direction = getch();        }        switch(direction){        case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break;     // 向上        case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break;      // 向下        case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break;      // 向左        case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;           // 向右        }        if(!(direction==72||direction==80||direction==75 ||direction==77)){   //  按键非方向键            cout << " Game over!" << endl;return 0;        }        if(x==0 || x==21 ||y==0 || y==21){   // 碰到墙壁            cout << " Game over!" << endl;return 0;        }        if(tcsQipan[x][y]!=' '&&!(x==x1&&y==y1)){ //   蛇头碰到蛇身            cout << " Game over!" << endl;return 0;        }        if(x==x1 && y==y1){      //  吃米,长度加1            length ++;            if(length>=8){                length -= 8;                grade ++;                if(gamespeed>=200)                    gamespeed = 550 - grade * 50; // 改变自动前进时间间隔            }            tcsQipan[x][y]= '#';            tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = '*';            head = (head+1)%100;            tcsZuobiao[0][head] = x;            tcsZuobiao[1][head] = y;            do            {                x1=rand()%20+1;                y1=rand()%20+1;            }while(tcsQipan[x1][y1]!=' ');            tcsQipan[x1][y1]='*';            Refresh(tcsQipan,grade,gamespeed);        }        else{       //  不吃米            tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=' ';            tail=(tail+1)%100;            tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]='*';            head=(head+1)%100;            tcsZuobiao[0][head]=x;            tcsZuobiao[1][head]=y;            tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]='#';            Refresh(tcsQipan,grade,gamespeed);        }    }    return 0; }

程序界面

c/c++/java代码编程

相关标签:java

下一篇:输入一个长度为6的字符串,修改其中的字符。

上一篇:鼠标在鼠标垫上用特别快的速度向一个方向移动时,电脑里鼠标并不会向那个方向移动或者向相反方向

热门标签:
excel 网盘 破解 word dll
最新更新:
微软重新评估新的Outlook的使用时机 联想推出搭载联发科Helio G80芯片组的Tab M9平板 英特尔创新大赛时间确定! 微软Edge浏览器在稳定渠道中推出Workspaces功能 英伟达RTX4060TiGPU推出MaxSun动漫主题! 谷歌地图为用户提供了街景服务! GameSir 在T4 Kaleid中推出了一款出色的控制器! 微软开始在Windows 11 中测试其画图应用程序的新深色模式! LG电子推出全球首款无线OLED电视 英伟达人工智能芯片崭露头角! Steam Deck可以玩什么游戏-Steam Deck价格限时优惠 雷蛇推出CobraPro鼠标 Kindle电子阅读器可以访问谷歌商店吗 Windows10如何加入组策略 window10图片查看器怎么没有了?