導航:首頁 > 程序命令 > C作品代碼游戲程序員

C作品代碼游戲程序員

發布時間:2022-11-03 23:00:16

『壹』 求用C語言編寫一個簡單游戲的代碼

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

char str[10][10]={0};
int n,i=0,j=0,k=1;

main()
{
for(i=0;i<10;i++){
for(j=0;j<10;j++)
str[i][j]='.';
}
srand((unsigned) time(NULL));
i=0,j=0;
char c='A';
str[0][0]=c;
do{
top:
n=rand()%4;
switch(n){
case 0:
if(i!=0){
i--;
break;
}else
goto top;
case 1:
if(i!=9){
i++;
break;
}else
goto top;
case 2:
if(j!=0){
j--;
break;
}else
goto top;
case 3:
if(j!=9){
j++;
break;
}else
goto top;
}
if(str[i][j]!='.'){
if(n==0){
i++;
goto top;
}
else if(n==1){
i--;
goto top;
}
else if(n==2){
j++;
goto top;
}
else{
j--;
goto top;
}
}else{
str[i][j]=++c;
k++;
}
if((str[i-1>0?i-1:1-i][j]!='.')
&&(str[i+1>9?i-1:1+i][j]!='.')
&&(str[i][j-1>0?j-1:1-j]!='.')
&&(str[i][j+1>9?j-1:1+j]!='.'))
goto end;
}while(k<26);
end:
for(i=0;i<10;i++){
for(j=0;j<10;j++)
printf("%c ",str[i][j]);
printf("\n");
}
}

『貳』 關於用C語言編寫的小游戲的游戲代碼,如黑白棋貪吃蛇等

我這兒有c語言的自寫俄羅斯方塊,請指教:#include
#include
#include
#include
#include
#include
#include

#define ESC 0x011b
#define UP 0x4800
#define DOWN 0x5000
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define SPACE 0x3920
#define Y 0x1579
#define N 0x316e
#define clearkbd(); while(bioskey(1)) bioskey(0); /*清空鍵盤緩沖隊列*/

void update();
void messagebox();
void process();
void initremove();
void initinfo();
void initbox();
void initposition();
void next_shape();

typedef struct shape /*形狀單一狀態的記錄*/
{ int attr;
int co[8];
}shape;

typedef struct RE_AB /*相對,絕對坐標記錄*/
{ int Rx,Ry;
int x1,x2,y1,y2;
}RE_AB;

RE_AB RA;

shape p[19]={ { RED,0,1,1,0,1,1,2,1 }, /*數組中保證y最大的在最後,以便initposition使用*/
{ RED,0,1,1,0,1,1,1,2 },
{ RED,0,0,1,0,2,0,1,1 },
{ RED,0,0,0,1,1,1,0,2 },
{ GREEN,0,0,1,0,2,0,3,0 },
{ GREEN,0,0,0,1,0,2,0,3 },
{ CYAN,0,0,0,1,1,0,1,1 },
{ BROWN,0,0,1,0,1,1,2,1 },
{ BROWN,1,0,0,1,1,1,0,2 },
{ BLUE,1,0,2,0,1,1,0,1 },
{ BLUE,0,0,0,1,1,1,1,2 },
{ MAGENTA,0,0,0,1,0,2,1,2 },
{ MAGENTA,2,0,0,1,1,1,2,1},
{ MAGENTA,0,0,1,0,1,1,1,2 },
{ MAGENTA,0,0,0,1,1,0,2,0 },
{ YELLOW,0,2,1,0,1,1,1,2 },
{ YELLOW,0,0,1,0,2,0,2,1 },
{ YELLOW,1,0,0,0,0,1,0,2},
{ YELLOW,0,0,0,1,1,1,2,1 },
};
int nback,nleft,nright,r_f[12][22],rs1,rs2,xcors,xcorb,ycors,ycorb;
/*檢查方快有沒有左,右,下接觸,游戲區內所有格子有無顏色記錄數組,rs1形狀記錄,rs2為提示框用,記錄小格子在游戲區中的位置,按鍵存儲*/

void interrupt (*oldint)(); /*系統定時中斷*/
int count_down=0,count_other=0; /*中斷記時*/

void interrupt newint() /*設置新的中斷程序*/
{ count_down++;
count_other++;
oldint();
}

void intenable() /*設置中斷向量表,啟動新的中斷程序*/
{ oldint=getvect(0x1c);
disable();
setvect(0x1c,newint);
enable();
}

void intrestore() /*恢復中斷向量*/
{ disable();
setvect(0x1c,oldint);
enable();
}

void HZ12(int x0,int y0,int w,int color,char *s) /*根據字模,在dos下顯示漢字*/
/*橫坐標,縱坐標,字間隔,漢字顏色,漢字字元串*/
{ FILE *fp;
register char buffer[24];
register char str[2];
unsigned long fpos;/*fpos為最終偏移動量*/
register int i,j,k;
fp=fopen(hzk12,r);/*打開12*12漢字苦*/
while(*s)/*一直到字元串結束為止*/
{
if(*s<0)/*漢字輸出*/
{ str[0]=(*s)-0xa0;
str[1]=*(s+1)-0xa0;
fpos=((str[0]-1)*94+(str[1]-1))*24L;/*計算漢字在hzk12的偏移量*/
fseek(fp,fpos,SEEK_SET);/*指針移動到當前位置*/
fread(buffer,24,1,fp);/*讀取一個漢字到數組中*/
for(i=0;i<12;i++)/*12行*/
for(j=0;j<2;j++)/*兩個位元組*/
for(k=0;k<8;k++)/*8位*/
if (((buffer[i*2+j]>>(7-k))&0x1)!=NULL)/*是一就畫點*/
putpixel(x0+8*j+k,y0+i,color);
s+=2;/*一個漢字占兩個位元組,現在將指針移動兩個位元組*/
x0+=w;/*顯示坐標也按照間隔移動*/
}
else/*顯示非漢字字元*/
{ settextstyle(0,0,1);
setcolor(color);
str[0]=*s;str[1]=0;
outtextxy(x0,y0+3,str);/*顯示單個字元*/
x0+=w-7;/*顯示單個字元後的x坐標變化*/
s++;/*指針移動到下一個位元組*/
}
}
fclose(fp);
}

void translation() /*把相對坐標解釋為絕對坐標*/
{ if(RA.Rx==1)
{ RA.x1=1; RA.x2=16; }
else
{ RA.x1=16*(RA.Rx-1); RA.x2=16*RA.Rx; }
if(RA.Ry==1)
{ RA.y1=1; RA.y2=16; }
else
{ RA.y1=16*(RA.Ry-1); RA.y2=16*RA.Ry; }
}

int check_b() /*檢查是否到達低部*/
{ int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>=6)
zf+=r_f[x-15][y-6+1];
}
if(zf==0)
return 1;
else
return 0;
}

int finish()
{ int tfull=0,i; /*判斷頂層空間是否有填充*/
for(i=1;i<11;i++)
tfull+=r_f[i][1];
if(tfull!=0)
return 1; /*告訴judge()可以結束了*/
}

int check_l() /*檢查形狀是否與左接觸*/
{ int x,y,i,zf=0;
for(i=0;i<7;i++,i++)
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15-1][y-6];
if(y<=6&&x==16)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

int check_r() /*檢查形狀是否與右接觸*/
{ /*zf為是否有顏色填充記錄*/
int x,y,i,zf=0; /*zf為是否有顏色填充記錄*/
for(i=0;i<7;i++,i++)
{
x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15+1][y-6];
if(y<=6&&x==25)
zf+=1;
}
if(zf==0)
return 1;
else
return 0;
}

void check_touch()
{ nback=check_b();
nleft=check_l();
nright=check_r();
}

void draw(int cb) /*畫形狀,cb=1以填充色畫形狀,cb=2以背景色畫形狀,cb=3以白色畫形狀*/
{ int i,recordx=RA.Rx,recordy=RA.Ry;
for(i=0;i<7;i++,i++)
{ RA.Rx+=p[rs1].co[i];
RA.Ry+=p[rs1].co[i+1];
if(RA.Ry<=6)
{ RA.Rx=recordx;
RA.Ry=recordy;
continue;
}
translation();
if(cb==1)
setfillstyle(1,p[rs1].attr);
else
if(cb==2)
setfillstyle(1,BLACK);
else
if(cb==3)
{ setfillstyle(1,WHITE);
r_f[RA.Rx-15][RA.Ry-6]=1; /*置對應數組標記元素*/
}

bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}

void mov(int key) /*向下,左,右移動方塊*/
{ draw(2);
if(key==LEFT&&nleft)
RA.Rx--;
else
if(key==RIGHT&&nright)
RA.Rx++;
else
RA.Ry++;
nback=check_b();
if(nback) /*判斷形狀有沒有到達底部,有就將其顏色變為白色*/
draw(1);
else
draw(3);
}

void change() /*變換形狀*/
{ int status=rs1,buffer,i,x,y,zf=0;
if(p[rs1].attr==p[rs1+1].attr)
rs1++;
else
while(p[rs1].attr==p[rs1-1].attr)
rs1--;

for(i=0;i<7;i++,i++) /*檢查變化形狀後是否與已存形狀發生沖突*/
{ x=RA.Rx+p[rs1].co[i];
y=RA.Ry+p[rs1].co[i+1];
if(y>6)
zf+=r_f[x-15][y-6];
}
if(zf!=0)
rs1=status;

buffer=rs1;
rs1=status;
status=buffer;
draw(2);

buffer=rs1;
rs1=status;
status=buffer;

nback=check_b(); /*判斷變化後的形狀是不是到達了低部,這個檢查是十分必要的*/
if(nback)
draw(1);
else
draw(3);
}

void accelerate()
{ if(count_down>=1)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}

void drawbox() /*畫方塊所在方框*/
{ int xcor,ycor;
for(xcor=xcors;xcor<=xcorb;xcor++)
for(ycor=ycors;ycor<=ycorb;ycor++)
{ if(xcor==xcors||xcor==xcorb||ycor==ycors||ycor==ycorb)
{ RA.Rx=xcor;
RA.Ry=ycor;
translation();
setfillstyle(1,DARKGRAY);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
}
}
}

void erasure(int k)
{ int i,j,recordx=RA.Rx,recordy=RA.Ry;
{ j=k-1;
for(;j>0;j--)
{ for(i=1;i<11;i++)
{ r_f[i][j+1]=r_f[i][j];
RA.Rx=i+15;
RA.Ry=j+1+6;
translation();
if(r_f[i][j+1]==1)
setfillstyle(1,WHITE);
else
setfillstyle(1,BLACK);
bar(RA.x1+1,RA.y1+1,RA.x2-1,RA.y2-1);
RA.Rx=recordx;
RA.Ry=recordy;
}
}
}
}

void pause()
{ HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,暫停);
for(;;)
if(bioskey(1)&&bioskey(0)==SPACE)
{ clearkbd();
HZ12(450,400,15,BLACK,暫停);
HZ12(450,400,15,RED,正常);
return;
}
}

void judge()
{ int i,j,full=0; /*full等於10說明某一行滿,該消除了*/
if(finish()) /*判斷游戲是否該結束了*/
messagebox(); /*win編程里有這個函數*/
for(j=1;j<21;j++) /*判斷某一行是否滿了*/
{ for(i=1;i<11;i++)
full+=r_f[i][j];
if(full==10)
erasure(j); /*消除這行*/
full=0;
}
}

void update() /*使程序可以重新運行*/
{ cleardevice();
setbkcolor(BLACK);
initinfo(); /*提示信息初始化*/
initbox(); /*游戲框架初始化*/
srand((unsigned)time(NULL)); /*隨機器函數的初始化*/
rs1=random(19);
rs2=random(19);
next_shape();
initposition(); /*方塊最開始的出現位置*/
initremove(); /*記錄每個方格有無顏色填充數組初始化*/
HZ12(450,400,15,RED,正常);
process();
}

void EXIT()
{ closegraph();
intrestore(); /*恢復中斷向量*/
exit(0);
}

void initremove()
{ int i,j;
for(i=0;i<12;i++)
for(j=0;j<22;j++)
if(i==0||i==11||j==0||j==21)
r_f[i][j]=1;
else
r_f[i][j]=0;
}

void initinfo()
{ char aStr[2];
setcolor(RED);
outtextxy(450,100,This game's writer is:);
HZ12(450,140,15,RED,該程序作者:NULL);
outtextxy(525,110,NULL);
outtextxy(450,180,FUNCTION FOR KEYS:);
outtextxy(450,200,UP:change the shape);
outtextxy(450,210,DOWN:accelerate);
outtextxy(450,220,LEFT:move left);
outtextxy(450,230,RIGHT:move right);
outtextxy(450,240,ESC:exit this game);
outtextxy(450,250,SPACE:pause);
HZ12(450,260,20,RED,上:);
HZ12(450,280,20,RED,下:);
HZ12(450,300,20,RED,左:);
HZ12(450,320,20,RED,右:);
HZ12(450,340,20,RED,ESC:退出);
HZ12(450,360,15,RED,空格: 暫停/開始);
HZ12(450,380,15,RED,目前狀態:);
HZ12(20,200,15,RED,下一個形狀);

aStr[0]=24;
aStr[1]=0;
aStr[6]=0;
HZ12(480,260,12,GREEN,aStr);
HZ12(500,260,12,GREEN,( 變形 ));
aStr[0]=25;
aStr[1]=0;
HZ12(480,280,12,GREEN,aStr);
HZ12(500,280,12,GREEN,( 加速 ));
aStr[0]=27;
aStr[1]=0;
HZ12(480,300,12,GREEN,aStr);
HZ12(500,300,12,GREEN,向左);
aStr[0]=26;
aStr[1]=0;
HZ12(480,320,12,GREEN,aStr);
HZ12(500,320,12,GREEN,向右);
}

void messagebox()
{ int key;
setcolor(GREEN);
setfillstyle(1,DARKGRAY);
rectangle(220,200,420,300);
bar(221,201,419,299);
HZ12(280,210,15,GREEN,GAME OVER);
HZ12(275,230,15,GREEN,重新游戲: Y);
HZ12(275,270,15,GREEN,退出遊戲: N);
HZ12(450,400,15,BLACK,正常);
HZ12(450,400,15,GREEN,GAME OVER);
for(;;)
if(bioskey(1))
{ key=bioskey(0);
if(key==Y)
{ clearkbd();
update();
}
else
if(key==N)
{ clearkbd();
EXIT();
}
else
clearkbd();
}
}

void initbox()
{ xcors=15; /*畫游戲框*/
xcorb=26;
ycors=6;
ycorb=27;
drawbox();

xcors=2; /*畫提示框*/
xcorb=7;
ycors=6;
ycorb=11;
drawbox();
}

void initposition()
{ RA.Rx=18;
RA.Ry=6-p[rs1].co[7];;
RA.x1=0;
RA.x2=0;
RA.y1=0;
RA.y2=0;
}

void next_shape() /*畫下一形狀提示框*/
{ int recordx=RA.Rx,recordy=RA.Ry,buffer;
RA.Rx=3;
RA.Ry=7;
draw(2);

buffer=rs1;
rs1=rs2;
rs2=buffer;

draw(1);
RA.Rx=recordx;
RA.Ry=recordy;

buffer=rs1;
rs1=rs2;
rs2=buffer;
}

void process() /*游戲過程*/
{ for(;;)
{ check_touch();
if(!nback)
{ rs1=rs2;
rs2=random(19); /*產生另一種方塊的碼數*/
initposition();
judge(); /*判斷某一行是否滿了和這個游戲是否可以結束了*/
draw(1);
next_shape();
}

if(count_other>=1)
{ count_other=0;
if(bioskey(1)) /*對按鍵的處理*/
{ int key=bioskey(0);
clearkbd(); /*清除鍵盤緩沖隊列*/
if(key==ESC)
EXIT();
if(key==LEFT&&nleft&&nback)
mov(LEFT);
if(key==RIGHT&&nright&&nback)
mov(RIGHT);
if(key==UP&&nback)
change();
if(key==SPACE)
pause();
if(key==DOWN)
accelerate();
}
}

if(count_down>=4)
{ check_touch(); /*消除上一步動作對方塊狀態的影響*/
count_down=0;
if(nback) /*0表示到達底部,1表示沒有到達*/
mov(DOWN);
}
}/*for*/
}

main()
{ int gdriver=DETECT,gmode=0;
initgraph(&gdriver,&gmode,d:turboc); /*啟動圖形與中斷部分*/
intenable();
update();
}

『叄』 用C語言編寫的小游戲代碼是什麼

「猜數字小游戲」,每個數字後按空格,最後按回車確認

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int a[4],b[4];

int count=0; //計算猜測次數

void csh( ); //初始化

void start( ); //開始游戲

int main( )

{ csh( );

start( );

}

void csh( ) //初始化

{ printf(" 猜 數 字 小 游 戲 ");

printf(「 猜四個數字,如數字與順序都正確記為A,數字正確位置不對記為B. 」);

}

void start( ) //開始游戲

{int m,n; //m是完全猜對的個數,n是順序不對的個數

while(1)

{srand((unsigned)time(NULL)); //初始化隨機數發生器srand( )

while(1) { for(int i=0;i<4;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個一位整數: ");

while(1)

{for(int i=0;i<4;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;i<4;i++)

{for(int j=0;j<4;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次 ",m,n,count);

if(m==4)break;

if(count==8){ count=0; break; }

}

printf(" ");

if(m==4)printf(" 你猜對了(^-^)! 就是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

else printf(" 你輸了(T-T)!哈哈!應該是:%d %d %d %d ",a[0],a[1],a[2],a[3]);

int z;

printf(" (要繼續嗎?1或0) ");

scanf(「%d」,&z);

if(z==0) break;

}

}

『肆』 求幾C語言個小游戲代碼,簡單的,要注釋、、謝謝了、

// Calcu24.cpp : Defines the entry point for the console application.
//
/*
6-6
24點游戲
*/
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"/*
從一副撲克牌中,任取4張。
2-10 按其點數計算(為了表示方便10用T表示),J,Q,K,A 統一按 1 計算
要求通過加減乘除四則運算得到數字 24。
本程序可以隨機抽取紙牌,並用試探法求解。
*/void GivePuzzle(char* buf)
{
char card[] = {'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i] = card[rand() % 13];
}
}
void shuffle(char * buf)
{
for(int i=0; i<5; i++){
int k = rand() % 4;
char t = buf[k];
buf[k] = buf[0];
buf[0] = t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0' && c<='9') return c - '0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return '+';
case 1:
return '-';
case 2:
return '*';
case 3:
return '/';
} return ' ';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1 + op2;
case 1:
return op1 - op2;
case 2:
return op1 * op2;
case 3:
if(fabs(op2)>0.0001)
return op1 / op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i] == 'T' )
strcpy(p[i], "10");
else
sprintf(p[i], "%c", question[i]);
}

switch(type)
{
case 0:
sprintf(answer, "%s %c (%s %c (%s %c %s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer, "%s %c ((%s %c %s) %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer, "(%s %c %s) %c (%s %c %s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer, "((%s %c %s) %c %s) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer, "(%s %c (%s %c %s)) %c %s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
// 等待考生完成
int type[5]={0,1,2,3,4};//計算類型
double p[4];
double sum=0;
//
for(int i=0; i<4; i++) //循環取得點數
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper); //獲取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]); //A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
採用隨機試探法:就是通過隨機數字產生 加減乘除的 組合,通過大量的測試來命中的解法
提示:
1. 需要考慮用括弧控制計算次序的問題 比如:( 10 - 4 ) * ( 3 + A ), 實際上計算次序的數目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2. 需要考慮計算結果為分數的情況:( 3 + (3 / 7) ) * 7
3. 題目中牌的位置可以任意交換
*/
bool TryResolve(char* question, char* answer)
{
int oper[3]; // 存儲運算符,0:加法 1:減法 2:乘法 3:除法
for(int i=0; i<1000 * 1000; i++)
{
// 打亂紙牌順序
shuffle(question);

// 隨機產生運算符
for(int j=0; j<3; j++)
oper[j] = rand() % 4; if( TestResolve(question, oper, answer) ) return true;
} return false;
}
int main(int argc, char* argv[])
{
// 初始化隨機種子
srand( (unsigned)time( NULL ) ); char buf1[4]; // 題目
char buf2[30]; // 解答
printf("***************************\n");
printf("計算24\n");
printf("A J Q K 均按1計算,其它按牌點計算\n");
printf("目標是:通過四則運算組合出結果:24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1); // 出題
printf("題目:");
for(int j=0; j<4; j++){
if( buf1[j] == 'T' )
printf("10 ");
else
printf("%c ", buf1[j]);
} printf("\n按任意鍵參考答案...\n");
getch(); if( TryResolve(buf1, buf2) ) // 解題
printf("參考:%s\n", buf2);
else
printf("可能是無解...\n"); printf("按任意鍵出下一題目,x 鍵退出...\n");
if( getch() == 'x' ) break;
} return 0;
}

『伍』 怎樣用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;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("07"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //007是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) 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語言 游戲 代碼

「坑人的無限」(一):

#include<iostream>
#include<windows.h>
#include<ctime>
#include<cstdlib>
#include<conio.h>
using namespace std;
int a;
class Screen
{
private:
int n;
public:
Screen()
{
n=5;
}
void move1()//注意只是循環輸出各個數字,不能對循環輸出再進行循環(如果對循環輸出0123456789再進行循環,則move1就變成一個無限循環的函數,則下面的screen循環就進行不下去了)
{
for(int i=0;i<10;++i)
{
cout<<char(1)<<" ";
}
}
void move2()
{
char i;
for(i='a';i<='z';++i)
{
cout<<char(1)<<" ";
}
}
void screen()
{
int t;
while(!kbhit())
{
t=time(0)%(2*n);//如果是放在循環外面的話,time(0)的值就一直不變,放在循環裡面,一秒鍾進行一次判斷,一秒鍾進行一次循環
if(t<n)
move1();
else
move2();
}
}
};
int main(){
cout<<"歡迎來到「無限 」游戲"<<char(1)<<endl;
cout<<"下面會輸出無限個笑臉"<<char(1)<<endl;
cout<<"按'enter'取消"<<endl;
Sleep(4000);
Screen s;
s.screen();
cout<<endl<<"哈哈!!控制不住了吧!"<<char(1)<<endl;
cout<<"接下來會更讓你喪心病狂的!"<<char(1)<<endl;
cout<<"但是堅持過後必有彩蛋!!!!!!加油!!";
cout<<endl<<endl<<endl<<endl<<endl<<endl<<endl<<endl;
Sleep(10000);
for(int as=0;as<=50;as++){
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(2)<<" ";
}
cout<<endl;
}
for(int i=0;i<=100;i++){
for(int j=0;j<=10;j++){
cout<<char(1)<<" ";
}
cout<<endl;
}
}
cout<<"你居然堅持下來了!"<<char(1)<<" "<<char(2)<<endl<<"不可思議呀!"<<endl;
for(int i=0;i<=10;i++){
cout<<"-----------------------------------------------"<<endl;
}
cout<<"敬請期待!等待無限游戲(二)!";
return 0;
}

『柒』 c語言游戲編程,下落的小鳥 求代碼

下落的小鳥

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<Windows.h>
int Grade = 1, Score = 0, Max_blank = 9, Distance = 18;
struct Birds{int x; int y;}; //定義一種Birds數據類型(含3個成員)
Birds *Bird = (Birds*)malloc(sizeof(Birds)); //定義Birds類型 指針變數Bird並賦初值
struct Bg{int x, y; int l_blank; Bg *pri; Bg *next;}; //定義一種Bg數據類型(含5個成員)
Bg *Bg1 = (Bg*)malloc(sizeof(Bg)); //定義Bg類型 指針變數Bg1並賦初值

void Position(int x, int y) //游標定位函數(用於指定位置輸出)
{COORD pos = { x - 1, y - 1 };
HANDLE Out = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(Out, pos);
}

void Csh( ) //初始化界面

{

printf("══════════════════════════════════════ ");

printf(" ■■ ■■ C語言版 Flappy Bird ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 瞎搞人:yyposs原創 ");

printf(" ■■ ■■ 瞎搞日期:2014.2 ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ 改編:鳴蟬百2021.7 ");

printf(" ■■ ■■ 操作:按向上方向鍵讓小鳥起飛 ");

printf(" ■■ ");

printf(" ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ ");

printf(" ■■ ■■ DEVc++運行通過 ");

printf("══════════════════════════════════════ ");

printf(" 按鍵繼續…");

getch( );

system("cls");

}

void PrFK( ) //輸出方框(游戲范圍區)

{int i;

Position(1, 1); printf("╔"); Position(79, 1); printf("╗");

Position(1, 24); printf("╚"); Position(79, 24); printf("╝");

for (i = 3; i <= 78; i += 2){Position(i, 1); printf("═"); Position(i, 24); printf("═");}

for(i=2;i<=23;i++)

{ Position(1,i); printf("║");if(i<11)printf("0%d",i-1);else printf("%d",i-1);

Position(79,i); printf("║");

}

Position(4, 25); printf("小鳥即將出現,請准備按鍵起飛… ");

getch( );

Position(4, 25); printf(" ");

}

void CreatBg( ) //創建障礙物坐標(便於列印輸出)
{Bg *Bg2 = (Bg*)malloc(sizeof(Bg));
Bg1->x = 90; Bg1->y = 8; //確定障礙物的一對基本坐標(此時值是在游戲框之外)
Bg2->x = Bg1->x + Distance; Bg2->y = 9; //下一障礙物的基本坐標x、y
Bg1->l_blank = Max_blank - Grade; //障礙物上下兩部分之間的空白距離l_blank
Bg2->l_blank = Max_blank - Grade;
Bg1->next = Bg2; Bg1->pri = Bg2;
Bg2->next = Bg1; Bg2->pri = Bg1;
}


void InsertBg(Bg *p) //隨機改變障礙物的y坐標,讓空白通道有上下變化
{int temp;
Bg *Bgs = (Bg*)malloc(sizeof(Bg));
Bgs->x = p->pri->x + Distance;
Bgs->l_blank = Max_blank - Grade;
srand((int)time(0)); //啟動隨機數發生器
temp = rand( ); //產生一個隨機數並賦值給temp
if (temp % 2 == 0)
{if ((temp % 4 + p->pri->y + Max_blank - Grade)<21)
Bgs->y = p->pri->y + temp % 4;
else Bgs->y = p->pri->y;
}
else
{if ((p->pri->y - temp % 4)>2)Bgs->y = p->pri->y - temp % 4;
else Bgs->y = p->pri->y;
}
Bgs->pri = p->pri; Bgs->next = p;
p->pri->next = Bgs; p->pri = Bgs;
}

void CreatBird( ) //建立小鳥的坐標(初始列印輸出小鳥的位置)
{Bird->x = 41; Bird->y = 10;}

int CheckYN(Bg *q) //判斷游戲結束與否(值為0是要結束,為1沒有要結束)
{Bg *p = q; int i = 0;
while (++i <= 5)
{if (Bird->y>23)return 0;
if (Bird->x == p->x&&Bird->y <= p->y)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y)return 0;
if (Bird->x == p->x&&Bird->y>p->y + p->l_blank)return 0;
if ((Bird->x == p->x || Bird->x == p->x + 1 || Bird->x == p->x + 2) && Bird->y == p->y + p->l_blank)
return 0;
p = p->next;
}
return 1;
}


void Check_Bg(Bg *q) //核查開頭的障礙物坐標是否在游戲區內
{Bg *p = q; int i = 0, temp;
while (++i <= 5)
{if (p->x>-4)p = p->next;
else
{srand((int)time(0)); temp = rand();
if (temp % 2 == 0)
{if ((temp % 4 + p->y + Max_blank - Grade)<21)p->y = p->y + temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
else
{if ((p->y - temp % 4)>2)p->y = p->y - temp % 4;
else p->y = p->y; p->x = p->pri->x + Distance;
p->l_blank = Max_blank - Grade;
}
}
}
}

void Prt_Bg(Bg *q) //列印輸出障礙物(依據其x、y坐標進行相應輸出)
{Bg *p = q; int i = 0, k, j;
while (++i <= 5)
{if (p->x>0 && p->x <= 78)
{for (k = 2; k<p->y; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
Position(p->x, p->y);
printf("■"); printf("■"); printf("■"); printf(" ");
Position(p->x, p->y + p->l_blank);
printf("■"); printf("■"); printf("■"); printf(" ");
k = k + p->l_blank + 1;
for (k; k <= 23; k++){Position(p->x + 1, k); printf("■"); printf("■"); printf(" ");}
}
p = p->next;
if (p->x == 0)
{for (j = 2; j<p->y; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
Position(p->x + 1, p->y);
printf(" "); printf(" "); printf(" ");
Position(p->x + 1, p->y + Max_blank - Grade);
printf(" "); printf(" "); printf(" ");
j = j + Max_blank - Grade + 1;
for (j; j <= 22; j++){Position(p->x + 1, j); printf(" "); printf(" ");}
}
}
}

void PrtBird( ) //列印輸出小鳥
{Position(Bird->x, Bird->y - 1); printf(" ");
Position(Bird->x, Bird->y); printf("Ю");
Position(38, 2); printf("Score:%d", Score);
}
void Loop_Bg(Bg *q) //障礙物x坐標左移,並記錄成績
{Bg *p = q; int i = 0;
while (++i <= 5)
{p->x = p->x - 1; p = p->next;
if (Bird->x == p->x)
{Score += 1;
if (Score % 4 == 0 && Grade<4)Grade++;
}
}
}

int main( )
{int i = 0; int t;

while (1)

{
Csh( );PrFK( );CreatBg( );
InsertBg(Bg1);InsertBg(Bg1);InsertBg(Bg1);
CreatBird( );
while (1)
{if (!CheckYN(Bg1))break;
Check_Bg(Bg1);Prt_Bg(Bg1);
PrtBird( );Loop_Bg(Bg1);
Bird->y = Bird->y + 1;
if (GetAsyncKeyState(VK_UP))
//按下了向上方向鍵
{Position(Bird->x, Bird->y - 1);printf(" ");
Bird->y = Bird->y - 4;
}
Sleep(200);
//程序延時200毫秒(數值大小決定游戲速度快慢)
i = 0;
}
Position(6, 25);
printf("游戲結束! 請輸入:0.退出 1.重玩");

scanf("%d",&t);

if (t==0)break;

system("cls"); Score = 0;

}
return 0;
}

『捌』 c語言小游戲代碼

「貪吃蛇」C代碼,在dev C++試驗通過(用4個方向鍵控制)

#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]; //蛇身某節x坐標

int y[100]; //蛇身某節y坐標

}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 Color(int a); //設定顯示顏色的函數

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個位置

{Color(2); //設定列印顏色為綠色

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的倍數(為偶數)

}

Color(12); //設定列印顏色為淡紅

gtxy(fod.x,fod.y); printf("●"); //到食物坐標處列印初試食物

snk.len=3; //蛇身長初值為3節

snk.speed=350; //刷新蛇的時間,即移動速度初值為350毫秒

snk.x[0]=W/2+1; //蛇頭橫坐標要為偶數(因為W/2=39)

snk.y[0]=H/2; //蛇頭縱坐標

Color(9); //設定列印顏色為淡藍

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("■"); //列印蛇身

}

Color(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要向右移動

}

Color(9);

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

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

{ printf("7"); snk.len++; score += 100; snk.speed -= 5; Flag = 1; } //7是響鈴

else Flag = 0; //沒吃到食物Flag的值為0

if(snk.speed<150) 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且橫坐標為偶數,則食物坐標取值成功

}

Color(12); //設定字元為紅色

gtxy(fod.x, fod.y); printf("●"); //游標到取得的坐標處列印食物

}

return;

}

int Over( ) //判斷游戲是否結束的函數

{ int i;

Color(7);

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 Color(int a) //設定顏色的函數

{ SetConsoleTextAttribute(GetStdHandle( STD_OUTPUT_HANDLE ),a ); }

『玖』 C語言簡易文字冒險游戲源代碼

記憶游戲

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#include<windows.h>

#defineN10

intmain()

{inti,k,n,a[N],b[N],f=0;

srand(time(NULL));

printf("按1開始 按0退出:_");

scanf("%d",&n);

system("cls");

while(n!=0)

{for(k=0;k<N;k++)a[k]=rand()%N;

printf(" [請您牢記看到顏色的順序] ");

for(k=0;k<N;k++)

{switch(a[k])

{case0:system("color90");printf("0:淡藍色 ");break;//淡藍色

case1:system("colorf0");printf("1:白色 ");break;//白色

case2:system("colorc0");printf("2:淡紅色 ");break;//淡紅色

case3:system("colord0");printf("3:淡紫色 ");break;//淡紫色

case4:system("color80");printf("4:灰色 ");break;//灰色

case5:system("colore0");printf("5:黃色 ");break;//黃色

case6:system("color10");printf("6:藍色 ");break;//藍色

case7:system("color20");printf("7:綠色 ");break;//綠色

case8:system("color30");printf("8:淺綠色 ");break;//淺綠色

case9:system("color40");printf("9:紅色 ");break;//紅色

}

Sleep(1500);

system("colorf");//單個控制文字顏色

Sleep(100);

}

system("cls");

printf("0:淡藍色,1:白色,2:淡紅色,3:淡紫色,4:灰色,5:黃色,6:藍色7:綠色,8:淺綠色,9:紅色 ");

printf(" 請輸入顏色的順序:");

for(k=0;k<N;k++)scanf("%d",&b[k]);

for(k=0;k<N;k++)if(a[k]==b[k])f++;

if(f==0)printf("你的記憶弱爆了0 ");

elseif(f==1)printf("你的記憶有點弱1 ");

elseif(f<5)printf("你的記憶一般<5 ");

elseprintf("你的記憶力很強! ");

Sleep(2000);

system("cls");

printf(" 按0退出 按任意鍵繼續游戲: ");

scanf("%d",&n);

system("cls");

}

return0;

}

註:DEVc++運行通過,每輸入一個數字要加入一個空格。

閱讀全文

與C作品代碼游戲程序員相關的資料

熱點內容
app專屬流量過月租怎麼不更新 瀏覽:654
王者程序員都有誰 瀏覽:73
給牛換腳掌解壓 瀏覽:386
圍棋有多少種演算法 瀏覽:600
unity資源包在哪個文件夾 瀏覽:704
阿里雲伺服器遠程鏈接不成功 瀏覽:482
文件系統pdf 瀏覽:762
原神安卓區服什麼意思 瀏覽:34
貝殼app怎麼線上發布 瀏覽:157
如何挑選安卓系統機頂盒 瀏覽:53
安卓快充使用有什麼注意事項 瀏覽:909
黑馬程序員的雲計算網課 瀏覽:946
endnotestyle文件夾怎麼導入 瀏覽:460
講解少兒編程演講會開頭 瀏覽:424
思科交換機基礎命令 瀏覽:497
便簽可以設置加密嗎 瀏覽:339
免費漫畫app怎麼看書 瀏覽:27
華為筆記本電腦怎麼安裝抖音app 瀏覽:412
阿里雲國際版試用的伺服器怎麼搞 瀏覽:895
java正則表達式工具 瀏覽:160