當前位置:首頁 » 編程語言 » c語言製作文字游戲
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言製作文字游戲

發布時間: 2023-01-17 07:46:48

c語言井字游戲

http://www.pudn.com/downloads112/sourcecode/game/49636984jzGame.rar
還有這個
井字游戲:為雙人對弈游戲,雙方輪流放子,單任一行,列,斜線為三個相同的 子時即為勝利!
http://download.pudn.com/downloads63/sourcecode/game/57578890xtkdige.rar
還有
http://download.pudn.com/downloads76/sourcecode/game/55593407402922602.rar
還有這個
一個用c語言寫的「井字游戲」,經過turbo c編譯通過。
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<bios.h>
#include<alloc.h>
#include<stdlib.h>
#define x1 150
#define x2 250
#define x3 350
#define x4 450
#define y1 100
#define y2 200
#define y3 300
#define y4 400
/* 1表示O, 2表示X */

void *buf_yuan,*buf_cha,*buf;
FILE *fp;
int a[4][4];
int flag=0;
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main(void)
{
int gd=DETECT,gm;
int END=0;
int i,j,h,kai=0,restart,key;
int heng=2,lie=2;
int temp=1;
void SystemInitial(void);/*初始化游戲界面*/
void SiJiao(int heng,int lie);/*顯示將要走步的位格的邊框*/
void hua(int heng,int lie,int type);/*走步,即畫圓或畫叉*/
void display(int type);/*顯示誰贏了*/
void hz(int x,int y,int a,int COL,int b,char *s);/*顯漢字,只可以顯示中文*/
void computer(void);/*單人游戲時,電腦走步*/
void hanzi(int x,int y,char *p,int colour);/*也是顯漢字,可以中文與英語混合顯示*/
initgraph(&amt;gd,&amt;gm,"");
if ((fp=fopen("hzk16","rb"))==NULL)
{
printf("Can't open hzk16,Please add it");
getch();
closegraph();
exit(0);
}
cleardevice();
hz(220,100,40,2,YELLOW,"井字游戲");
setcolor(BLUE);
hanzi(400,460,"作者:04計科3班 鄧永華 08號",BLUE);
setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
flag=0;
if(flag==0)/*單人游戲*/
{
circle(195,242,4);
floodfill(195,242,WHITE);
}
else/*雙人游戲*/
{
circle(195,262,4);
floodfill(195,262,WHITE);
}
outtextxy(200,220,"Please select:");
setcolor(RED);
outtextxy(205,240,"Play with computer");
outtextxy(205,260,"Two people play");

hanzi(30,350,"游戲說明:雙方輪流走步,先連成三個符號一直線(橫、豎、斜)就Win",GREEN);

while(temp)
{
switch(bioskey(0))
{
case 0x1c0d:/*回車*/
{
temp=0;
break;
}
case 0x11b:/*Esc*/
exit(0);
case 0x4800:/*上*/
if(flag==0)
break;
else
{
flag=0;
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(195,262,4);
floodfill(195,262,BLACK);

setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
circle(195,242,4);
floodfill(195,242,WHITE);
}
break;
case 0x5000:/*下*/
if(flag==1)
break;
else
{
flag=1;
/*清除*/
setcolor(BLACK);
setfillstyle(SOLID_FILL,BLACK);
circle(195,242,4);
floodfill(195,242,BLACK);

setcolor(WHITE);
setfillstyle(SOLID_FILL,WHITE);
circle(195,262,4);
floodfill(195,262,WHITE);
break;
}
}
}
cleardevice();
/*begin to play*/
SystemInitial();
while(END!=1)
{
restart=0;
switch(bioskey(0))/*按鍵*/
{
case 0x11b:/*Esc退出*/
END=1;
break;
case 0x3920:/*space*/
if(kai==1)
break;
if(a[heng][lie]) break;
kai=1;
hua(heng,lie,1);
a[heng][lie]=1;

if((a[1][1]==1&amt;&amt;a[1][2]==1&amt;&amt;a[1][3]==1)/*判斷是否贏了*/
||(a[2][1]==1&amt;&amt;a[2][2]==1&amt;&amt;a[2][3]==1)
||(a[3][1]==1&amt;&amt;a[3][2]==1&amt;&amt;a[3][3]==1)
||(a[1][1]==1&amt;&amt;a[2][1]==1&amt;&amt;a[3][1]==1)
||(a[1][2]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][2]==1)
||(a[1][3]==1&amt;&amt;a[2][3]==1&amt;&amt;a[3][3]==1)
||(a[1][1]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][3]==1)
||(a[1][3]==1&amt;&amt;a[2][2]==1&amt;&amt;a[3][1]==1))
{
display(1);
END=1;
}
if(!END)
{
h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)/*y的大小寫*/
{
END=0;
restart=1;
break;
}
else if((key==0x316e)||(key==0x314e))/*n的大小寫*/
break;
else continue;
}
}
break;
case 0x5230:/*0*/
if(kai==2) break;
if(a[heng][lie]) break;
kai=2;
hua(heng,lie,2);
a[heng][lie]=20;

if((a[1][1]==20&amt;&amt;a[1][2]==20&amt;&amt;a[1][3]==20)/*判斷是否贏了*/
||(a[2][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[2][3]==20)
||(a[3][1]==20&amt;&amt;a[3][2]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][1]==20&amt;&amt;a[3][1]==20)
||(a[1][2]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][2]==20)
||(a[1][3]==20&amt;&amt;a[2][3]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][3]==20)
||(a[1][3]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][1]==20))

{
display(2);
END=1;
}
if(!END)
{ h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)
{
END=0;
restart=1;
break;
}
if(key==0x316e||key==0x314e)
break;
else continue;
}
}
break;
case 0x4800:
case 0x1177:
heng--;
if(heng<1)
heng=1;
SiJiao(heng,lie);
break;
case 0x5000:
case 0x1f73:
heng++;
if(heng>3)
heng=3;
SiJiao(heng,lie);
break;
case 0x4b00:
case 0x1e61:
lie--;
if(lie<1)
lie=1;
SiJiao(heng,lie);
break;
case 0x4d00:
case 0x2064:
lie++;
if(lie>3)
lie=3;
SiJiao(heng,lie);
break;
}
if(flag==0&amt;&amt;kai==1&amt;&amt;END!=1&amt;&amt;restart!=1)
{
computer();
kai=2;
if((a[1][1]==20&amt;&amt;a[1][2]==20&amt;&amt;a[1][3]==20)/*判斷是否贏了*/
||(a[2][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[2][3]==20)
||(a[3][1]==20&amt;&amt;a[3][2]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][1]==20&amt;&amt;a[3][1]==20)
||(a[1][2]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][2]==20)
||(a[1][3]==20&amt;&amt;a[2][3]==20&amt;&amt;a[3][3]==20)
||(a[1][1]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][3]==20)
||(a[1][3]==20&amt;&amt;a[2][2]==20&amt;&amt;a[3][1]==20))

{
display(2);
END=1;
}
if(!END)
{ h=0;
for(i=1;i<4;i++)
for(j=1;j<4;j++)
if(a[i][j])
h++;
if(h==9)
{
display(3);
END=1;
}
}
if(END==1)
{
outtextxy(260,450,"play again? Y/N ");
while(1)
{
key=bioskey(0);
if(key==0x1579||key==0x1559)
{
END=0;
restart=1;
break;
}
if(key==0x316e||key==0x314e)
break;
else continue;
}
}
}
if(restart==1)
{
cleardevice();
SystemInitial();
kai=0;
heng=2;
lie=2;
}
}
free(buf_yuan);
free(buf_cha);
free(buf);
fclose(fp);
closegraph();
}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SystemInitial(void)
{
void hz(int x,int y,int a,int COL,int b,char *s);
void SiJiao(int heng,int lie);
int size,i,j;

for(i=1;i<4;i++)
for(j=1;j<4;j++)
a[i][j]=0;

SiJiao(2,2);
hz(240,30,40,2,YELLOW,"井字游戲");
setcolor(GREEN);
outtextxy(10,200,"1P");
setcolor(WHITE);
outtextxy(10,220,"up: w");
outtextxy(10,240,"down: s");
outtextxy(10,260,"left: a");
outtextxy(10,280,"right: d");
outtextxy(10,300,"fill: space");
outtextxy(10,320,"exit: Esc");
if(flag==1)
{
setcolor(GREEN);
outtextxy(520,200,"2P");
setcolor(WHITE);
outtextxy(520,220,"up:");
outtextxy(520,240,"down:");
outtextxy(520,260,"left: ");
outtextxy(520,280,"right: ");
outtextxy(520,300,"fill: 0");
outtextxy(520,320,"exit: Esc");
hz(585,220,25,1,WHITE,"↑");
hz(585,240,25,1,WHITE,"↓");
hz(585,260,25,1,WHITE,"←");
hz(585,280,25,1,WHITE,"→");
}
line(x1,y1,x1,y4);
line(x1,y1,x4,y1);
line(x4,y1,x4,y4);
line(x1,y4,x4,y4);
line(x2,y1,x2,y4); /*shu*/
line(x3,y1,x3,y4);
line(x1,y2,x4,y2); /*heng*/
line(x1,y3,x4,y3);

circle((x2+x3)/2,(y2+y3)/2,(y3-y2)/2-10);/*hua yuan*/
size=imagesize((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9);
buf_yuan=malloc(size);
if(!buf_yuan) exit(1);
getimage((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9,buf_yuan);
setcolor(BLACK);
circle((x2+x3)/2,(y2+y3)/2,(y3-y2)/2-10);

setcolor(WHITE); /*hua cha*/
line(x2+10,y2+10,x3-10,y3-10);
line(x2+10,y3-10,x3-10,y2+10);
buf_cha=malloc(size);
getimage((x2+x3)/2-(y3-y2)/2+9,(y2+y3)/2-(y3-y2)/2+9,(x2+x3)/2+(y3-y2)/2-9,(y2+y3)/2+(y3-y2)/2-9,buf_cha);
setcolor(BLACK);
line(x2+10,y2+10,x3-10,y3-10);
line(x2+10,y3-10,x3-10,y2+10);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void SiJiao(int heng,int lie)
{
int a1,b1,a2,b2;
void huahei(void);/*把原來用白色顯示的將要走步的邊框用黑色覆蓋*/
huahei();

setcolor(WHITE);
switch(heng)
{
case 1:
b1=y1;
b2=y2;
break;
case 2:
b1=y2;
b2=y3;
break;
case 3:
b1=y3;
b2=y4;
break;
}
switch(lie)
{
case 1:
a1=x1;
a2=x2;
break;
case 2:
a1=x2;
a2=x3;
break;
case 3:
a1=x3;
a2=x4;
break;
}
line(a1+3,b1+3,a1+30,b1+3);
line(a1+3,b1+3,a1+3,b1+30);
line(a1+3,b2-3,a1+3,b2-30);
line(a1+3,b2-3,a1+30,b2-3);
line(a2-30,b1+3,a2-3,b1+3);
line(a2-3,b1+3,a2-3,b1+30);
line(a2-30,b2-3,a2-3,b2-3);
line(a2-3,b2-30,a2-3,b2-3);

}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void huahei(void)
{
int i,j,a1,b1,a2,b2;

setcolor(BLACK);
for(i=1;i<4;i++)
for(j=1;j<4;j++)
{
switch(i)
{
case 1:
b1=y1;
b2=y2;
break;
case 2:
b1=y2;
b2=y3;
break;
case 3:
b1=y3;
b2=y4;
break;
}
switch(j)
{
case 1:
a1=x1;
a2=x2;
break;
case 2:
a1=x2;
a2=x3;
break;
case 3:
a1=x3;
a2=x4;
break;
}

line(a1+3,b1+3,a1+30,b1+3);
line(a1+3,b1+3,a1+3,b1+30);
line(a1+3,b2-3,a1+3,b2-30);
line(a1+3,b2-3,a1+30,b2-3);
line(a2-30,b1+3,a2-3,b1+3);
line(a2-3,b1+3,a2-3,b1+30);
line(a2-30,b2-3,a2-3,b2-3);
line(a2-3,b2-30,a2-3,b2-3);
}

}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void hua(int heng,int lie,int type)/*type的值為1表示圓,2表示叉*/
{
int x,y;
setcolor(WHITE);
switch(lie)
{
case 1:
x=(x1+x2)/2-(x2-x1)/2+9;
break;
case 2:
x=(x2+x3)/2-(x3-x2)/2+9;
break;
case 3:
x=(x3+x4)/2-(x4-x3)/2+9;
break;
}

switch(heng)
{
case 1:
y=(y1+y2)/2-(y2-y1)/2+9;
break;
case 2:
y=(y2+y3)/2-(y3-y2)/2+9;
break;
case 3:
y=(y3+y4)/2-(y4-y3)/2+9;
break;
}
switch(type)
{
case 1:
buf=buf_yuan;
a[heng][lie]=1;
break;
case 2:
buf=buf_cha;
a[heng][lie]=20;
break;
}
putimage(x,y,buf,COPY_PUT);
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void display(int type)
{
if(type==1)
outtextxy(270,430,"O Win");
if(type==2)
outtextxy(270,430,"X Win");
if(type==3)
outtextxy(270,430,"draw");

}
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void hz(int x,int y,int a,int COL,int b,char *s)/*x,y為顯示的坐標,a為字與字之間的間隔,b為字的大小,s為指向為顯示的漢字的指針*/
{
int ROW;
char buffer[32];
register m,n,i1,j1,k;
unsigned char qh,wh;
unsigned long offset;
ROW=COL;
while(*s)
{
qh=*(s)-0xa0;/*漢字區位碼*/
wh=*(s+1)-0xa0;
offset=(94*(qh-1)+(wh-1))*32L;/*計算該漢字在字型檔中偏移量*/
fseek(fp,offset,SEEK_SET);
fread(buffer,32,1,fp);/*取出漢字32位元組的點陣字模存入buffer中(一個漢字) */
for (i1=0;i1<16;i1++)/*將32位位元組的點陣按位在屏幕上列印出來(1:列印,0:不列印),顯示漢字 */
for(n=0;n<ROW;n++)
for(j1=0;j1<2;j1++)
for(k=0;k<8;k++)
for(m=0;m<COL;m++)
if (((buffer[i1*2+j1]>>(7-k))&amt;0x1)!=NULL)
putpixel(x+8*j1*COL+k*COL+m,y+i1*ROW+n,b);
s+=2;/*因為一個漢字內碼佔用兩個位元組,所以s必須加2*/
x+=a;
}
}
/**************************************************************************/
void computer(void)
{
int i,j;
for(i=1;i<4;i++)/*橫向判斷*/
if(a[i][1]+a[i][2]+a[i][3]==40)
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}
for(i=1;i<4;i++)/*橫向判斷*/
if(a[i][1]+a[i][2]+a[i][3]==2)
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}

for(i=1;i<4;i++)/*豎向判斷*/
if(a[1][i]+a[2][i]+a[3][i]==40)
{
for(j=1;j<4;j++)
if(a[j][i]==0)
{
hua(j,i,2);
return;
}
}
for(i=1;i<4;i++)/*豎向判斷*/
if(a[1][i]+a[2][i]+a[3][i]==2)
for(j=1;j<4;j++)
if(a[j][i]==0)
{
hua(j,i,2);
return;
}

if(a[1][1]+a[2][2]+a[3][3]==40)/*左上角到右下角判斷*/
{
for(i=1;i<4;i++)
if(a[i][i]==0)
{
hua(i,i,2);
return;
}
}
else if(a[1][1]+a[2][2]+a[3][3]==2)
for(i=1;i<4;i++)
if(a[i][i]==0)
{
hua(i,i,2);
return;
}

if(a[3][1]+a[2][2]+a[1][3]==40)/*右上角到左下角判斷*/
{
for(i=1;i<4;i++)
if(a[i][4-i]==0)
{
hua(i,4-i,2);
return;
}
}
else if(a[3][1]+a[2][2]+a[1][3]==2)
for(i=1;i<4;i++)
if(a[i][4-i]==0)
{
hua(i,4-i,2);
return;
}

for(i=1;i<4;i++)/*隨便找到空位填上去*/
for(j=1;j<4;j++)
if(a[i][j]==0)
{
hua(i,j,2);
return;
}

}
void hanzi(int x,int y,char *p,int colour)
{
FILE *fp;
char buffer[32];
register i,j,k;
unsigned char qh,wh;
unsigned long location;
if((fp=fopen("hzk16","rb"))==NULL)
{
printf("Can't open hzk16!");
getch();
exit(0);
}
while(*p)
{
if(((unsigned char)*p>=0xa1&amt;&amt;(unsigned char)*p<=0xfe)&amt;&amt;((unsigned char)*(p+1)>=0xal&amt;&amt;(unsigned char)*(p+1)<=0xfe))
{
qh=*p-0xa0;
wh=*(p+1)-0xa0;
location=(94*(qh-1)+(wh-1))*32L;
fseek(fp,location,SEEK_SET);
fread(buffer,32,1,fp);
for(i=0;i<16;i++)
for(j=0;j<2;j++)
for(k=0;k<8;k++)
if(((buffer[i*2+j]>>(7-k))&amt;0x1)!=NULL)
putpixel(x+8*j+k,y+i,colour);
p+=2;
x+=18;
if(x>600)
{
x=15;y+=18;
}
}
else
{
char q[2];
moveto(x,y);
*q=*p;
*(q+1)='\0';
outtextxy(x,y+4,q);
x+=8+1;p++;
}
}
fclose(fp);
}
都是井字游戲,C語言的,你參考看看。

❷ 設計一個猜字游戲的C語言程序

把下面的cout是輸出。
用c語言的輸出就行。
#include
<time.h>
#include
<stdlib.h>
int
main()
{
int
x;
cout<<"輸入任意數字開始。"<<endl;
//使用
輸入是否為
數字來判斷是否繼續猜
scanf("%d",&x);
while(x)
{
srand((int)time(0));
int
m
=
rand()%100+1;//生成隨機數m范圍1到100
int
i
=0,n;
for
(;i<10;i++)//判斷是否才對,10次為限,i用來計算才對次數
{
cout<<"input
the
number
you
guess:"<<endl;
scanf("%d",&n);
if(
n
>
m
)
cout<<"wrong,too
high!"<<endl;
if(
n
<
m
)
cout<<"wrong,too
low!"<<endl;
if(
n
==
m)
{cout<<"right,and
the
times
you
have
guessed
is"<<i<<"."<<endl;break;}
}
cout<<"輸入任意數字繼續。"<<endl;
scanf("%d",&x);
}
return
0;
}

❸ C語言編寫打字游戲出問題

請把源代碼發一下, 一般出現這種情況為一下幾種狀況:

  1. 內存出錯(溢出)

  2. 代碼在運行時出現錯誤(例如:scanf("%d", &a)沒有寫&取地址)

  3. 系統不兼容編譯器本身或裡面的一些組件

能不能問的詳細點

❹ C語言編程,猜字游戲,看看我的程序哪裡有錯謝謝!

char word[4] ;你放的是一個字元串,是以'\0'結束的。要定義數組為5個字元,這樣才能get_word()取得到word的長度。
另外scanf的用法也不對。
函數中的if比較少寫了一個=符號

#include<stdio.h>
#include<string.h>

int get_word(char w[]);
int find_lett(char w[],char G,int n);
int main(void)
{
int index;
int size;
char g;
char word[5]; /* **** */
char guess[5]="****";
printf("The Game of Hangman – all caps, 4 different characters\n");
printf("Input a word to be guessed: ");
size=get_word(word);
printf("%s",word);
while(strcmp(word,guess))
{
printf("Guess: %s",guess);
printf("Input letter> ");
fflush(stdin);
scanf("%c",&g); /* **** */
index=find_lett(word,g,size);
if(index!=-1)
guess[index]=word[index];
}
return(0);
}
int get_word(char w[])
{
int i=0;
scanf("%s",w);
while(w[i]!='\0')
{
i++;
}
return(i);
}
int find_lett(char w[],char G,int n)
{
int i;
int Index=-1;
for(i=0;i<n;i++)/* **** */
{
if(w[i]==G)
{
Index=i;
break;
}
}
return(Index);
}

//循環,用goto來實現,修改後代碼:
#include<stdio.h>
#include<string.h>

int get_word(char w[]);
int find_lett(char w[],char G,int n);
int main(void)
{
int index;
int size;
char g;
char word[5]; /* **** */
char guess[5];
loop:
printf("The Game of Hangman – all caps, 4 different characters\n");
printf("Input a word to be guessed: ");
size=get_word(word);
printf("%s\n",word);
strcpy(guess,"****");
while(strcmp(word,guess))
{
printf("Guess: %s",guess);
printf("Input letter> ");
fflush(stdin);
scanf("%c",&g); /* **** */
index=find_lett(word,g,size);
if(index!=-1)
guess[index]=word[index];
}
while(1)
{
printf("\nselect option:Y(continue); N(exit)> ");
fflush(stdin);
g = getchar();

if(g=='y'||g=='Y')
goto loop;
else if(g=='n'||g=='N')
break;

}
return(0);
}
int get_word(char w[])
{
int i=0;
scanf("%s",w);
while(w[i]!='\0')
{
i++;
}
return(i);
}
int find_lett(char w[],char G,int n)
{
int i;
int Index=-1;
for(i=0;i<n;i++)/* **** */
{
if(w[i]==G)
{
Index=i;
break;
}
}
return(Index);
}

❺ 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語言編寫一個簡單的打字母游戲,給出基本思路及完整程序。

# include<stdio.h>
# include <conio.h>
# include <graphics.h>
# include <time.h>
# pragma comment(lib,」Winmm.lib」) //引用Windows Multimedia APT

int score =0; //全局變數********整型的分數
char s[5]; //全局變數****字元型的分數
struct player_ //自定義的結構體,並順便定義一個結構體變數
{
char name[20];
int score_;
}player[11];
/***************函數聲明*****************/
void record_(); //記錄彼此成績函數
void welcome( ); //歡迎界面函數
void instructions( ); //游戲說明界面函數
void gane (int rank2); //游戲函數
void record ( ); //顯示記錄函數
int rank_(char rank1); //等級選擇函數
void goodbye ( ); //退出界面函數

/**************主函數**************/
int main ()
{
char rank1; //rank1接受從鍵盤輸入的字元,根據字元判斷等級
int rank2=15; // 控制延時時間,從而控制速度
intsign=1; // 當做標記,用於標記是否彈出MessageBox對話框

welcome ( ); //調用歡迎界面函數
instructions ( ); //調用游戲什麼函數
record( ); //調用顯示記錄函數
InputBox(player[10].name,20,"請輸入玩家昵稱"; //以對話框形式獲取用戶輸入函數
rank1=getch();//讀入一個字母
rank2=rank_(rank1); //調用等級函數
game (rank2); //調用游戲函數
HWND wnd =GetHWnd();
while (sign) //sign作為標記,執行循環
if (MessageBox(wnd,"游戲結束!!!\n再來一局,挑戰更高分數","詢問",MB_YESNO|MB_ICONQUESTION)==IDYES)
game (rank2);
else
{
sign=0;
goodbye( );
}
closegraph( );
return 0;
}
/***************記錄本次成績函數***************/
void record_()
{
FILE*fp; //文件指針
int i,j;
struct player_t; //定義結構體變數t,冒泡法比較大小時用
player[10].score_=score; //把該局的分數賦給該局用戶的結構體變數中
fp=fopen("記錄.dat","rb"); //打開二進制文件
for (i=0;i<10;i++)
{
fread(player[i].name,20,l,fp); //讀入記錄中的成績和名字
fread (&player[i].score_,4,l,fp);
}
fclose(fp); //關閉二進制文件
for (j=0;j<10;j++) //冒泡法排序
for (i=0;i<10-j;i++)
if (player[i].score_)
{
t=player[i];
player[i]=player[i+1];
player[i+1]=t;
}
fp=fopen("記錄.dat","wb"); //打開文件
for (i=0;i<10;i++) //把排好序列的數據存入記錄文件
{
fwrite(player[i].name,20,l,fp);
fwrite (&player[i].score_,4,l,fp);
}
fclose(fp); //關閉文件
}
/****************歡迎界面函數****************/
void welcome ( )
{
char ch;
mciSendString("open\"res\\Sleep Away.mp3\"alias mymusic",NULL,0,NULL);

initgraph(640,480); //初始化畫圖界面
IMAGE img; //定義IMAGE對象
loadimage (&img,"res\\00.jpg"); //讀取圖片到img對象中
putimage (0,0,&img); //在坐標(0,0)位置顯示IMAGE對象
ch=getch(); //吸收掉一個按鍵
cleardevice(); //清屏
}

/***************游戲說明界面函數**************/
void instructions()
{
int c=255; //控制變色的變數
setcolor(YELLOW); //設置顏色
setfont (30,0,"黑體"); //設置字體
outtextxy(0,0,"游戲說明"); //輸出文字
setcolor(WHITE);
setfont(28,0,"宋體");
outtextxy(0,30,"一、每次游戲下落200個字母"); //輸出文字
outtextxy(0,58,"二、請按數字鍵選擇游戲等級");
outtextxy(0,86,"三、統計被擊中的個數");
setfont(30,0,"華文行楷")
outtextxy (20,220,"等級一二三四 五六七八九");
outtextxy(20,260,「按鍵 1 2 3 4 5 6 7 8 9」);
while (!kbhit()) //kihit()作用是判斷是否有字幕輸入
{
setcolor(RGB(c,255,0));
setfont (80,0,"楷體");
outtextxy(140,130,"請選擇等級");
setcolor(RGB(c,0,255));
setfont(100,0,"Wickenden Cafe NDP");
outtextxy(100,135,"Let's go!!!");
c-=25; //控制顏色的變數自減
if(c<0)c=255; //控制顏色的變數小於0時,再把255賦給c
cleardevice(); //清屏
}
/***************退出界面函數***************/
void goodbye()
{
int c=255; //控制變色變數
cleardevice(); //清屏
setcolor(YELLOW);
setfont (48,0,"黑體");
outtextxy (104,120,"的最終成績是: ");
setfont (48,0,"華文楷體");
setcolor(RGB(255,255,0));
outtextxy(140,320,"按任意鍵退出遊戲");
setcolor(YELLOW);
setfont(48,0,"黑體");
outtextxy (104,120,"的最終成績是: ");
outtextxy(104,70,player[10].name);
record_(); //調用記錄本次成績函數
while(!kbhit())
{
setcolor(RGB(255,0,c-100)); //設置顏色
setfont(100,0,」Wickenden Café NDP」);
outtextxy(400,90,s); //輸出數字
setcolor(RGB(c,0,255));
setfont(48,0,」黑體」);
if (score==200)
outtextxy(100,200,」全部擊中!!!」);
else
if(score>190)
outtextxy(100,200,」不錯!繼續加油!」);
else
outtextxy(100,200,」不要灰心!繼續加油!」);
c-=25;
if(c<0)
c=255;
Sleeep(20); //延時20秒
}
getch( ); //提供給用戶時需要等待輸入後再退出
}
/**************等級選擇函數**************/
int rank_(char rank1)
{
int rank2;
switch(rank1) //判斷字元,確定等級
{
case』0』:rank2=11;break;
case』1』:rank2=10;break;
case』2』:rank2=9;break;
case』3』:rank2=8;break;
case』4』:rank2=7;break;
case』5』:rank2=6;break;
case』6』:rank2=5;break;
case』7』:rank2=4;break;
case』8』:rank2=3;break;
case』9』:rank2=2;break;
default:rank2=15;break;
}
return rank2;
}

/**************顯示記錄函數***********/
void record( )
{
FILE *p; //文件指針
int a[10],j;
char b[10][10];
fp=fopen(「記錄。dat」,」rb」); //打開文件
for (j=0;j<10;j++)
{
fread(player[j].name,20,1,fp); //讀取文件中的信息
fread(&player[j].score_,4,1,fp);
}
fclose(fp); //關閉文件
setfont(60,0,」Arial」);
setcolor(RGB(250,250,0));
outtextxy(250,0,」TOP10」);
for(j=0;j<10;j++)
{
sprintf(「b[j],%d」,player[j].score_); //把整型轉換為字元型
setcolor(RGB(0,100,255));
setfont(40,0,」Arial」);
outtextxy(420,50+40*j,b[j]);
outtextxy(120,50+40*j,player[j].name); //輸出名字

/**********游戲函數******************/
void game (int rank2)
{
cleardevice( ); //清屏
score=0; //清除掉原有的分數
outtextxy(0,0,」玩家:」);
outtextxy(90,0,player[10].name);
setfont(20,0,」Arial」);
outtextxy(100,460,」 人生有享受片刻優雅恬適的權力,讓打字母游戲放鬆你一下吧!」);
int i;
char target; //目標字母
char key; //用戶的按鍵
intx,y; //字母的位置
int flag =1; //標記是否按了退出鍵
int sum=0; //成績計數
int num =200; //規定下落字母的個數
srand (time(NULL)); //隨機時間種子,設置第一個隨機函數的值
setfillstyle(BLACK); //設置下面矩形內圖的顏色
setcolor(YELLOW); //
line(0,450,640,450); //用3條線規定一下工作區
line(0,440,640,440); //用3條線規定一下工作區
line(0,40,640,40); //用3條線規定一下工作區

while (flag&&num)
{
setfont(40,0,」宋體」); //設置字母的字體和大小
i=rand()%2; //rand()函數返回0~32767之間的一個整數
if(i)
target=97+rand()%26; //產生任意小寫字母
else
target=65+rand()%26; //產生任意大寫字母
x=rand()%610; //產生任意下落位置
for (y=41;y<380;y++)
{
setcolor (RGB(255,0,255));
outtextxy(x,y,target); //顯示字母

if(kbhit())
{
key=getch(); //獲取用戶按鍵
if ((key==target)||( key==target-32)||( key==target+32))
{
score++; //分數統計
sprintf(s,」%d」score);
setfont(30,0,」Arial」);
outtextxy(500,0,「分數」);
outtextxy(600,0,s); //輸出分數
bar(x-10,y-10,x+35,y+50); // 擊中用黑框形擦去字母
break; //跳出循環,進行下一個字母
}
else if (key==27) //判斷是否按了
{
flag=0; //若果按ESC,退出遊戲主循環
break;
}
}
Sleep(rank2); //控制清除字母的時間愛你也就是控制下落速度
bar(x,y,x+35,y+60); //擦最下面的字元
}
num--; //計數
}
}

❼ c語言 猜子字游戲

#include "stdio.h"
#include "time.h"
#include "stdlib.h"
int main(int argc, char* argv[])
{
int r;
int g;
srand(time(0)); //設置隨機數種子,使每次運行得到的隨機數都不一樣
r = rand()%100 + 1; //產生一個小於100的隨機數,讓大家猜(加1是為了防止產生的隨機數為 0,也是為了保證可以產生隨機數100)
printf("請輸入你猜的數:");
scanf("%d",&g);
while(g != r)//沒猜中就循環
{
if(g > r)
{
printf("大了,請重新輸入:");
}
else if(g < r)
{
printf("小了,請重新輸入:");
}
scanf("%d",&g);
}
printf("恭喜你,猜對了!\n");
return 0;
}

❽ 急求一份C語言打字游戲代碼,要求單詞從屏幕上方落下,打字正確字母變為綠色。

#include<time.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>

#define R 10
#define ESC 0x011b
#define LOW 0x5000
#define FAST 0x4800
int draw_car(int x,int y,char *ch);
double dalta=20,angle;

void *image;
int step=20;
main()
{

int gdriver=DETECT,gmode;
static int startx=60;
static int starty=20;
int maxy,maxx,l=1,n=1;
char ch,play, str[2];
int size,temch;
int ok,y=50;

clrscr();

printf("\n\n\n\n\n*****************************************\n");

printf(" 1.play only number 0-9 .\n");
printf(" 2.play only char a-z .\n");
printf(" 3.exit\n");
printf("*****************************************\n");
while(1){
printf("\nPls enter choice:");
play=getche();
if((play=='1')||(play=='2'))
break;

if(play=='3')
return;
}

registerbgidriver( EGAVGA_driver ); /*注冊vga驅動*/
initgraph(&gdriver,&gmode,"d:\\tc3\\bgi"); /*初始化圖形界面*/
cleardevice(); /*清除屏幕*/
setbkcolor(BLUE); /*將背景色設置成藍色*/
size=imagesize(startx,starty,startx+60,starty+60); /*計算生成60×60個象素的圖需要的位元組數*/
image=(unsigned char *)malloc(size); /*分配內存*/
maxx=getmaxx();
maxy=getmaxy();
randomize();

printf("Press ESC to exit game! ");
printf(" %c to fast speed,%c to low speed!",24,25);
do{

ok=0;
if(play=='1')
ch=random(9)+48;
if(play=='2')
ch=random(26)+65;
str[0]=ch;
do{

while(bioskey(1)==0) /*如果沒有按鍵就不停的循環*/
{
if(l==1) /*從左到右*/
{
n++;
angle=-1*(n*step)/M_PI*180/R;
if((int)(-1*angle)%360<dalta)
angle-=dalta;
if(n>(maxx-180)/step){
if(starty>maxy-100)
starty=20;
else
starty+=y;

l=0;
}
}
if(l==0) /*從右到左*/
{
--n;
angle=-1*(n*step)/R/M_PI*180;
if((int)(-1*angle)%360<dalta)
angle-=dalta;

if(n==1){
if(starty>maxy-100)
starty=20;
else
starty+=y;

l=1;
}
}

draw_car(startx+n*step,starty,str);
}

temch=bioskey(0);
if(temch==FAST){
step+=10;
y+=10;
}
if(temch==LOW){
step-=10;
y-=10;
}
ok=temch&0x00ff;

if((ok==ch)||(ok==ch+32)){
ok=1;
starty=20;
break;
}

}while(temch!=ESC);
if(ok!=1)
break;
}while(1);
free(image);
closegraph();
return;
}

int draw_car(int x,int y,char *ch){
setcolor(YELLOW); /*設置前景色為黃色*/
rectangle(x,y,x+60,y+40); /*畫車廂*/

pieslice(x+15,y+50,angle,angle-dalta,10); /*畫輪上的小片扇形部分*/
pieslice(x+45,y+50,angle,angle-dalta,10);

setfillstyle(SOLID_FILL, YELLOW); /*設置填充模式為實填充,顏色為黃色*/
circle(x+15,y+50,10); /*畫車輪*/
circle(x+45,y+50,10);
circle(x+15,y+50,3);
circle(x+45,y+50,3);
setcolor(WHITE);
settextstyle(1,0,4);
outtextxy(x+25,y,ch);
getimage(x,y,x+60,y+60,image); /*獲取當前的圖片*/
delay(200);
putimage(x,y,image,XOR_PUT); /*使用異或模式將圖片顯示上去*/

return;
}

❾ c語言編程打字母游戲,怎麼設置規定時間

# include
# include
# include
# include
# pragma comment(lib,」Winmm.lib」) //引用Windows Multimedia APT
int score =0; //全局變數********整型的分數
char s[5]; //全局變數****字元型的分數
struct player_ //自定義的結構體,並順便定義一個結構體變數
{
char name[20];
int score_;
}player[11];
/***************函數聲明*****************/
void record_(); //記錄彼此成績函數
void welcome( ); //歡迎界面函數
void instructions( ); //游戲說明界面函數
void gane (int rank2); //游戲函數
void record ( ); //顯示記錄函數
int rank_(char rank1); //等級選擇函數
void goodbye ( ); //退出界面函數
/**************主函數**************/
int main ()
{
char rank1; //rank1接受從鍵盤輸入的字元,根據字元判斷等級
int rank2=15; // 控制延時時間,從而控制速度
intsign=1; // 當做標記,用於標記是否彈出MessageBox對話框
welcome ( ); //調用歡迎界面函數
instructions ( ); //調用游戲什麼函數
record( ); //調用顯示記錄函數
InputBox(player[10].name,20,"請輸入玩家昵稱"; //以對話框形式獲取用戶輸入函數
rank1=getch();//讀入一個字母
rank2=rank_(rank1); //調用等級函數
game (rank2); //調用游戲函數
HWND wnd =GetHWnd();
while (sign) //sign作為標記,執行循環
if (MessageBox(wnd,"游戲結束!!!
再來一局,挑戰更高分數","詢問",MB_YESNO|MB_ICONQUESTION)==IDYES)

❿ C語言用循環猜字游戲:使用隨機數生成一個三位數作為謎底進行猜字游戲,每次猜的時候給出提示,

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a,b,c=10;
srand(time(NULL));
a=rand()%1000;
printf("系統已經給出了一個0-999的數,請輸入一個數。\n");
while(1)
{ c--;
scanf("%d",&b);
if(b>a)
printf("猜大了,還剩%d次機會,請繼續\n",c);
if(b<a)
printf("猜小了,還剩%d次機會,請繼續\n",c);
if(b==a)
{ printf("恭喜你答對了\n");
printf("系統給的數是:%d",a);
break;}
if(c==0)
{ printf("你已經沒有機會了,請重新開始吧!\n");break;}
}

}