c語言游戲代碼大全
① 生命游戲c語言代碼
黑白棋游戲#include"graphics.h"/*圖形系統頭文件*/#defineLEFT0x4b00/*游標左鍵值*/#defineRIGHT0x4d00/*游標右鍵值*/#defineDOWN0x5000/*游標下鍵值*/#defineUP0x4800/*游標上鍵值*/#defineESC0x011b/*ESC鍵值*/#defineENTER0x1c0d/*回車鍵值*/inta[8][8]={0},key,score1,score2;/*具體分數以及按鍵與存放棋子的變數*/charplayone[3],playtwo[3];/*兩個人的得分轉換成字元串輸出*/voidplaytoplay(void);/*人人對戰函數*/voidDrawQp(void);/*畫棋盤函數*/voidSetPlayColor(intx);/*設置棋子第一次的顏色*/voidMoveColor(intx,inty);/*恢復原來棋盤狀態*/intQpChange(intx,inty,intz);/*判斷棋盤的變化*/voidDoScore(void);/*處理分數*/voidPrintScore(intn);/*輸出成績*/voidplayWin(void);/*輸出勝利者信息*//******主函數*********/voidmain(void){intgd=DETECT,gr;initgraph(&gd,&gr,"c:\\tc");/*初始化圖形系統*/DrawQp();/*畫棋盤*/playtoplay();/*人人對戰*/getch();closegraph();/*關閉圖形系統*/}voidDrawQp()/*畫棋盤*/{inti,j;score1=score2=0;/*棋手一開始得分都為0*/setbkcolor(BLUE);for(i=100;i=64-score1-score2)/*如果嘗試超過空格數則停步*/{MoveColor(x,y);fillellipse(x,y,15,15);break;}elsecontinue;/*如果按鍵無效*/}DoScore();/*分數的改變*/break;/*棋盤變化了,則輪對方走棋*/}else/*已經有棋子就繼續按鍵*/continue;}else/*四個方向按鍵的判斷*/if(key==LEFT&&x>120)/*左方向鍵*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x-=40;fillellipse(x,y,15,15);}elseif(key==RIGHT&&x80)/*右方向鍵*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);x+=40;fillellipse(x,y,15,15);}elseif(key==UP&&y>120)/*上方向鍵*/{MoveColor(x,y);fillellipse(x,y,15,15);SetPlayColor(t);y-=40;fillellipse(x,y,15,15);}elseif(key==DOWN&&y1)/*判斷左邊*/{for(k=j-1;k>=0;k--)if(a[i][k]==a[i][j]||!a[i][k])break;if(a[i][k]!=0&&k>=0){for(kk=j-1;kk>k&&k>=0;kk--){a[i][kk]=a[i][j];fillellipse(120+i*40,120+kk*40,15,15);}if(kk!=j-1)yes=1;}}if(i1)/*判斷上邊*/{for(k=i-1;k>=0;k--)if(a[k][j]==a[i][j]||!a[k][j])break;if(a[k][j]!=0&&k>=0){for(kk=i-1;kk>k&&k>=0;kk--){a[kk][j]=a[i][j];fillellipse(120+kk*40,120+j*40,15,15);}if(kk!=i-1)yes=1;}}if(i>1&&j=0&&kk=0&&kkk&&k>=0;ii--,jj++){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(i1)/*左下*/{for(k=i+1,kk=j-1;k=0;k++,kk--)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k=0){for(ii=i+1,jj=j-1;ii1&&j>1)/*左上*/{for(k=i-1,kk=j-1;k>=0&&kk>=0;k--,kk--)if(a[k][kk]==a[i][j]||!a[k][kk])break;if(a[k][kk]!=0&&k>=0&&kk>=0){for(ii=i-1,jj=j-1;ii>k&&k>=0;ii--,jj--){a[ii][jj]=a[i][j];fillellipse(120+ii*40,120+jj*40,15,15);}if(ii!=i-1)yes=1;}}if(iscore1)/*開始判斷最後的結果*/outtextxy(100,50,"blackwin!");elseif(score2#include#include#include#include#defineLEFT0x4b00#defineRIGHT0x4d00#defineDOWN0x5000#defineUP0x4800#defineESC0x011b#defineSPACE0x3920#defineBILI20#defineJZ4#defineJS3#defineN19intbox[N][N];intstep_x,step_y;intkey;intflag=1;voiddraw_box();voiddraw_cicle(intx,inty,intcolor);voidchange();voidjudgewho(intx,inty);voidjudgekey();intjudgeresult(intx,inty);voidattentoin();voidattention(){charch;window(1,1,80,25);textbackground(LIGHTBLUE);textcolor(YELLOW);clrscr();gotoxy(15,2);printf("游戲操作規則:");gotoxy(15,4);printf("PlayRules:");gotoxy(15,6);printf("1、按左右上下方向鍵移動棋子");gotoxy(15,8);printf("1.PressLeft,Right,Up,DownKeytomovePiece");gotoxy(15,10);printf("2、按空格確定落棋子");gotoxy(15,12);printf("2.PressSpacetoplacethePiece");gotoxy(15,14);printf("3、禁止在棋盤外按空格");gotoxy(15,16);printf("3.");gotoxy(15,18);printf("你是否接受上述的游戲規則(Y/N)");gotoxy(15,20);printf("?[Y/N]:");while(1){gotoxy(60,20);ch=getche();if(ch=='Y'||ch=='y')break;elseif(ch=='N'||ch=='n'){window(1,1,80,25);textbackground(BLACK);textcolor(LIGHTGRAY);clrscr();exit(0);}gotoxy(51,12);printf("");}}voiddraw_box(){intx1,x2,y1,y2;setbkcolor(LIGHTBLUE);setcolor(YELLOW);gotoxy(7,2);printf("Left,Right,Up,DownKEYtomove,Spacetoput,ESC-quit.");for(x1=1,y1=1,y2=18;x1=1;i--)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLUE);break;}if(i18)break;else{for(i=step_x+1,j=step_y;i18)break;step_x=i;judgewho(step_x,step_y);break;}caseDOWN:if((step_y+1)>18)break;else{for(i=step_x,j=step_y+1;j18)break;step_y=j;judgewho(step_x,step_y);break;}caseUP:if((step_y-1)=1;j--)if(box[i][j]==0){draw_circle(step_x,step_y,LIGHTBLUE);break;}if(j=1&&step_x=1&&step_y=1;j--){if(box[j][k]==flag)n1++;elsebreak;}/*水平向右數*/for(j=x,k=y;j=5){return(1);break;}/*垂直向上數*/n1=0;n2=0;for(j=x,k=y;k>=1;k--){if(box[j][k]==flag)n1++;elsebreak;}/*垂直向下數*/for(j=x,k=y;k=5){return(1);break;}/*向左上方數*/n1=0;n2=0;for(j=x,k=y;j>=1,k>=1;j--,k--){if(box[j][k]==flag)n1++;elsebreak;}/*向右下方數*/for(j=x,k=y;j=5){return(1);break;}/*向右上方數*/n1=0;n2=0;for(j=x,k=y;j=1;j++,k--){if(box[j][k]==flag)n1++;elsebreak;}/*向左下方數*/for(j=x,k=y;j>=1,k=5){return(1);break;}return(0);break;}}voidmain(){intgdriver=VGA,gmode=VGAHI;clrscr();attention();initgraph(&gdriver,&gmode,"c:\\tc");/*setwritemode(XOR_PUT);*/flag=1;draw_box();do{step_x=0;step_y=0;/*draw_circle(step_x,step_y,8);*/judgewho(step_x-1,step_y-1);do{while(bioskey(1)==0);key=bioskey(0);judgekey();}while(key!=SPACE&&key!=ESC);}while(key!=ESC);closegraph();}
② 誰有一些簡單小游戲的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;i<H;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;i<snk.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==72&&dir!=2)dir=1; //72表示按下了向上方向鍵
if(key==80&&dir!=1)dir=2; //80為向下
if(key==75&&dir!=4)dir=3; //75為向左
if(key==77&&dir!=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("