用c語言編寫的小游戲
/*也不知道你是什麼級別的,我是一個
新手
,剛接觸
編程語言
,以下是我自己變得一個小
程序
,在所有c語言的編譯器(vc++6.0、turbo…………)上都能運行,你還可以進一步改進。這是一個類似
貪吃蛇
的
小游戲
。
祝你好運
*/
/*貪吃蛇*/
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<stdlib.h>
int
head=3
,tail=0;
int
main()
{
int
i,j,k=0;
int
zuobiao[2][80];
long
start;
int
direction=77;
int
gamespeed;
int
timeover;
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction);
zuobiao[0][tail]=1;zuobiao[1][tail]=1;zuobiao[0][1]=1;zuobiao[1][1]=2;zuobiao[0][2]=1;zuobiao[1][2]=3;zuobiao[0][head]=1;zuobiao[1][head]=4;
/*處理棋盤*/
char
qipan[20][80];//
定義
棋盤
for(i=0;i<20;i++)
for(j=0;j<80;j++)
qipan[i][j]='
';//
初始化
棋盤
for(i=0;i<80;i++)
qipan[0][i]='_';
for(i=0;i<20;i++)
qipan[i][0]='|';
for(i=0;i<20;i++)
qipan[i][79]='|';
for(i=0;i<80;i++)
qipan[19][i]='_';
qipan[1][1]=qipan[1][2]=qipan[1][3]='*';//初始化蛇的位置
qipan[1][4]='#';
printf("This
is
a
game
of
a
SNAKE.
GOOD
LUCK
TO
YOU
!
");
printf("Input
your
game
speed,please.(e.g.300)
");
scanf("%d",&gamespeed);
while(direction!='q')
{
system("cls");
for(i=0;i<20;i++)//列印出棋盤
for(j=0;j<80;j++)
printf("%c",qipan[i][j]);
timeover=1;
start=clock();
while(!kbhit()&&(timeover=clock()-start<=gamespeed));
if(timeover)
{
getch();
direction=getch();
}
else
direction=direction;
if(!(direction==72||direction==80||direction==75||direction==77))
{
return
0;
system("cls");
printf("GAME
OVER!
");
}
if(!change(qipan,zuobiao,direction))
{
direction='q';
system("cls");
printf("GAME
OVER!
");
}
}
return
0;
}
int
change(char
qipan[20][80],int
zuobiao[2][80],char
direction)
{
int
x,y;
if(direction==72)
x=zuobiao[0][head]-1;y=zuobiao[1][head];
if(direction==80)
x=zuobiao[0][head]+1;y=zuobiao[1][head];
if(direction==75)
x=zuobiao[0][head];y=zuobiao[0][head]-1;
if(direction==77)
x=zuobiao[0][head];y=zuobiao[1][head]+1;
if(x==0||x==18||y==78||y==0)
return
0;
if(qipan[x][y]!='
')
return
0;
qipan[zuobiao[0][tail]][zuobiao[1][tail]]='
';
tail=(tail+1)%80;
qipan[zuobiao[0][head]][zuobiao[1][head]]='*';
head=(head+1)%80;
zuobiao[0][head]=x;
zuobiao[1][head]=y;
qipan[zuobiao[0][head]][zuobiao[1][head]]='#';
return
1;
}
② C語言編寫小游戲
如果以後你的主攻方向不是C語言,
那我建議你不要嘗試用C寫游戲, 頂多也就看看專那些源碼屬.
用C寫游戲, 哪怕是很簡單的一個游戲也是很麻煩的, 現在的C大部分人都只是把它作為了一個編程入門的語言, 真正想搞嵌入式, 搞驅動開發之類的底層才會真正深入...
如果你要向那方面發展, 那你就繼續, 如果不是, 那還是看看源碼然後轉向其他高級語言吧...
③ 用C語言編寫一個小游戲要在vc++6.0里運行
//c語言的俄羅斯方塊游戲
//不能運行再來找我!
#include<dos.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<bios.h>
#define LEFT 0x4b
#define RIGHT 0x4d
#define DOWN 0x50
#define CHANGE 0x20
#define ESC 0x1b
#define INTR 0x1C
#define DefaultX 5
#define DefaultY 1
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif
unsigned counter=0;
unsigned shape[7][4][4][4]={
{
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
{{0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 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, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
char ShapeColor[7]={8, 10, 11, 12, 13, 14, 15};
char DigitalShape[10]={128+119, 3, 62, 31, 128+75, 128+93, 128+125, 19, 128+127, 128+95};
char ZodiacBack[11]={4, 6, 12, 13, 11, 10, 2, 3, 9, 1, 8};
char ZodiacSoft[18][14]={
{ 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 0, 60, 0},
{255, 248, 0, 0, 96, 0, 0, 0, 0, 248, 0, 0, 124, 0},
{255, 248, 0, 0, 97, 128, 0, 0, 3, 248, 0, 0, 192, 0},
{ 0, 112, 0, 0, 97, 128, 0, 0, 7, 0, 0, 1, 128, 48},
{ 0, 224, 0, 0, 96, 0, 0, 0, 14, 0, 0, 1, 128, 48},
{ 1, 192, 0, 0, 96, 0, 0, 0, 12, 0, 0, 1, 128, 48},
{ 3, 128, 120, 15, 97, 128, 240, 60, 12, 0, 15, 15, 249, 255},
{ 7, 0, 252, 31, 225, 131, 248, 127, 14, 0, 31, 143, 249, 255},
{ 7, 1, 142, 48, 225, 135, 24, 227, 7, 240, 49, 193, 128, 48},
{ 14, 3, 134, 96, 97, 142, 24, 192, 3, 252, 112, 193, 128, 48},
{ 8, 3, 6, 96, 97, 140, 25, 192, 0, 28, 96, 193, 128, 48},
{ 56, 3, 6, 96, 97, 140, 25, 128, 0, 6, 96, 193, 128, 48},
{ 56, 3, 6, 96, 97, 140, 25, 128, 0, 6, 96, 193, 128, 48},
{120, 3, 6, 96, 97, 140, 25, 128, 0, 6, 96, 193, 128, 48},
{224, 1, 140, 48, 225, 142, 25, 195, 24, 14, 49, 129, 128, 48},
{255, 249, 252, 63, 225, 135, 252, 255, 28, 28, 63, 129, 128, 48},
{255, 248, 240, 15, 97, 131, 236, 60, 15, 248, 30, 1, 128, 48},
{ 0, 0, 0, 0, 0, 0, 0, 0, 7, 224, 0, 1, 128, 0}};
unsigned long TotalMark=0;
unsigned int Erasered=0, ETimes=0;
int Speed=0;
int CEr=0;
int NumOfLev[5]={0, 0, 0, 0, 0};
int TimeDelay[10]={21, 18, 17, 15, 13, 11, 9, 7, 5, 3};
char NewShape[2];
unsigned PerOnce[4];
void interrupt (*oldhandler)(__CPPARGS);
void interrupt handler(__CPPARGS)
{
++counter;
oldhandler();
}
void DrawBackground()
{
}
void DrawDigital(int x, int y, int a)
{
int i=7;
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x, y+1, x, y+15);
line(x+1, y+2, x+1, y+14);
line(x+2, y+3, x+2, y+13);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x, y+17, x, y+31);
line(x+1, y+18, x+1, y+30);
line(x+2, y+19, x+2, y+29);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x, y+17, x, y+31);
line(x+1, y+18, x+1, y+30);
line(x+2, y+19, x+2, y+29);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x+1, y, x+16, y);
line(x+2, y+1, x+15, y+1);
line(x+3, y+2, x+14, y+2);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x+2, y+15, x+15, y+15);
line(x+1, y+16, x+16, y+16);
line(x+2, y+17, x+15, y+17);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x+3, y+30, x+14, y+30);
line(x+2, y+31, x+15, y+31);
line(x+1, y+32, x+16, y+32);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
i--;
line(x+15, y+3, x+15, y+13);
line(x+16, y+2, x+16, y+14);
line(x+17, y+1, x+17, y+15);
setcolor(((DigitalShape[a]>>i)&1)*7+4);
line(x+15, y+19, x+15, y+29);
line(x+16, y+18, x+16, y+30);
line(x+17, y+17, x+17, y+31);
}
void ArtClear()
{
for(int i=480; i>=0; i-=2)
{
setcolor(3);
line(0, i-6, 639, i-6);
setcolor(9);
line(0, i-4, 639, i-4);
setcolor(1);
line(0, i-2, 639, i-2);
setcolor(getbkcolor());
line(0, i, 639, i);
delay(1);
}
for(i=1; i<480; i+=2)
{
setcolor(3);
line(0, i+6, 639, i+6);
setcolor(9);
line(0, i+4, 639, i+4);
setcolor(1);
line(0, i+2, 639, i+2);
setcolor(getbkcolor());
line(0, i, 639, i);
delay(1);
}
}
void end()
{
for(int j=0; j<4; j++)
{
for(int i=j; i<480; i+=4)
{
setcolor(3);
line(0, i+8, 639, i+8);
setcolor(9);
line(0, i+4, 639, i+4);
setcolor(1);
line(0, i, 639, i);
setcolor(getbkcolor());
line(0, i, 639, i);
delay(1);
}
delay(5);
}
setcolor(YELLOW);
outtextxy(260, 200, "[ Game Over ]");
gotoxy(14, 16);
printf("This freeware is written by Daniel.Qu ZodiacSoft 2000");
gotoxy(12, 17);
printf("Please e-mail to [email protected] or visit my homepage at");
gotoxy(12, 18);
printf("http://grocery.2699.com if you have question.");
gotoxy(14, 20);
printf("I will soon provide something new, please wait...");
gotoxy(14, 22);
printf("We hold these truths to be self-evident, that all codes");
gotoxy(12, 23);
printf("are created FREE.");
getch();
clrscr();
closegraph();
exit(0);
}
void ShowTitle()
{
int gdriver=DETECT, gmode, errorcode, i, j, k;
initgraph(&gdriver, &gmode, "C:\\JMSOFT\\DRV");
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error: %s\n\007", grapherrormsg(errorcode));
exit(1);
}
setbkcolor(9);
for(i=0; i<11; i++)
{
setcolor(ZodiacBack[i]);
line(200, i*8+100, 440, i*8+100);
}
delay(1000);
for(j=0; j<17; j++)
for(i=0; i<11; i++)
{
setcolor(ZodiacBack[(i+j)%16]);
line(200, i*8+100, 440, i*8+100);
delay(4);
}
for(j=0; j<18; j++)
{
for(i=0; i<14; i++)
{
for(k=7; k>=0; k--)
if((ZodiacSoft[j][i]>>k)&1)
{
putpixel(i*8-k+271, j+122, 14);
}
}
delay(10);
}
for(j=17; j>=0; j--)
{
for(i=0; i<14; i++)
for(k=7; k>=0; k--)
{
if((ZodiacSoft[j][i]>>k)&1)
putpixel(i*8-k+271, 158-j, 5);
}
delay(10);
}
setcolor(15);
outtextxy(295, 171, "Present");
getch();
ArtClear();
}
void DrawShape(int x, int y, int color)
{
setcolor(color);
for(int i=0; i<15; i++)
line(x<<4, (y<<4)+i, (x<<4)+14, (y<<4)+i);
setcolor(8);
line((x<<4)+15, (y<<4)+1, (x<<4)+15, (y<<4)+15);
line((x<<4)+1, (y<<4)+15, (x<<4)+15, (y<<4)+15);
setcolor(color-8);
line((x<<4)+2, (y<<4)+2, (x<<4)+12, (y<<4)+2);
line((x<<4)+2, (y<<4)+2, (x<<4)+2, (y<<4)+12);
}
void DrawNull(int x, int y)
{
setcolor(getbkcolor());
for(int i=0; i<16; i++)
line(x<<4, (y<<4)+i, (x<<4)+15, (y<<4)+i);
}
void show(int x, int y, int CurrentShape, int Status)
{
int temp;
for(int i=0; i<20; i++)
for(int j=2; j<12; j++)
{
if(back[i][j]==1)
DrawShape(6+j, 4+i, 8);
else
DrawNull(6+j, 4+i);
}
for(int l=0; l<4; l++)
for(int p=0; p<4; p++)
{
if(l+x<2||x+l>11||p+y>19)
continue;
if(shape[CurrentShape][Status][p][l]==1)
DrawShape(6+l+x, 4+p+y, ShapeColor[CurrentShape]);
if(back[y+p][x+l]==1)
DrawShape(6+l+x, 4+p+y, 8);
}
}
void DrawScore()
{
int temp=TotalMark;
for(int i=0; i<7; i++)
{
DrawDigital(477-i*20, 65, temp%10);
temp/=10;
}
temp=Erasered;
for(i=0; i<4; i++)
{
DrawDigital(491-i*20, 111, temp%10);
temp/=10;
}
if(Erasered==0)
return;
for(i=1; i<5; i++)
{
setfillstyle(1, 8);
bar(378, 163+i*14, 508, 170+i*14);
setfillstyle(1, 5);
bar(378, 163+i*14, 378+130*NumOfLev[i]/ETimes, 170+i*14);
}
DrawDigital(466, 293, Speed);
return;
}
int CreateNewShape()
{
NewShape[1]=NewShape[0];
NewShape[0]=rand()%7;
setfillstyle(1, 0);
bar(447, 240, 514, 288);
for(int l=0; l<4; l++)
for(int p=0; p<4; p++)
if(shape[NewShape[0]][0][p][l]==1)
DrawShape(28+l, 15+p, ShapeColor[7]);
return NewShape[1];
}
int Possible(int x, int y, int CurrentShape, int Status)
{
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(shape[CurrentShape][Status][i][j]+back[y+i][x+j]==2)
return 0;
return 1;
}
void GoLeft(int&x, int&y, int&CurrentShape, int&Status)
{
if(Possible(x-1, y, CurrentShape, Status))
x--;
show(x, y, CurrentShape, Status);
}
void GoRight(int&x, int&y, int&CurrentShape, int&Status)
{
if(Possible(x+1, y, CurrentShape, Status))
x++;
show(x, y, CurrentShape, Status);
}
void GoDown(int&x, int&y, int&CurrentShape, int&Status)
{
int AllAreOne=1, temp=0;
if(Possible(x, y+1, CurrentShape, Status))
{
y++;
show(x, y, CurrentShape, Status);
return;
}
TotalMark+=3*CurrentShape;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
back[i+y][j+x]+=shape[CurrentShape][Status][i][j];
for(int r=1; r<20; r++)
{
for(int l=2; l<13; l++)
if(back[r][l]==0)
AllAreOne=0;
if(AllAreOne==1)
{
for(int rr=r; rr>0; rr--)
for(int ll=2; ll<13; ll++)
back[rr][ll]=back[rr-1][ll];
Erasered++;
CEr++;
temp++;
}
AllAreOne=1;
}
CurrentShape=CreateNewShape();
x=DefaultX; y=DefaultY; Status=0;
NumOfLev[temp]++;
TotalMark+=5*temp;
show(x, y, CurrentShape, Status);
counter=0;
if(!Possible(x, y, CurrentShape, Status))
{
getch();
end();
}
if(CEr>=30)
{
Speed=(Speed+1)%10;
CEr/=30;
}
if(temp)
ETimes++;
DrawScore();
return;
}
void ChageShape(int&x, int&y, int&CurrentShape, int&Status)
{
if(Possible(x, y, CurrentShape, (Status+1)%4))
Status=(Status+1)%4;
show(x, y, CurrentShape, Status);
}
void DrawFace()
{
int temp;
setcolor(15);
line(351, 59, 519, 59);
line(351, 60, 518, 60);
line(351, 59, 351, 156);
line(352, 59, 352, 155);
setcolor(7);
line(518, 61, 518, 154);
line(519, 60, 519, 154);
line(353, 155, 519, 155);
line(352, 156, 519, 156);
setcolor(8);
for(int i=61; i<155; i++)
line(353, i, 517, i);
setcolor(7);
line(427, 106, 512, 106);
line(428, 107, 511, 107);
line(427, 106, 427, 147);
line(428, 106, 428, 146);
setcolor(15);
line(512, 106, 512, 147);
line(511, 107, 511, 146);
line(428, 146, 511, 146);
line(427, 147, 512, 147);
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
setcolor(14);
outtextxy(357, 119, "Erasered");
outtextxy(370, 130, "Level:");
setcolor(15);
line(352, 167, 519, 167);
line(352, 168, 518, 168);
line(352, 169, 352, 407);
line(353, 169, 353, 406);
setcolor(7);
line(519, 167, 519, 407);
line(518, 168, 518, 406);
line(352, 407, 519, 407);
line(353, 406, 519, 406);
line(524, 55, 89, 55);
line(524, 55, 524, 411);
line(89, 411, 524, 411);
line(89, 55, 89, 411);
line(524, 56, 89, 56);
line(525, 55, 525, 411);
line(89, 410, 524, 410);
line(88, 55, 88, 411);
setfillstyle(8, 7);
bar(94, 59, 336, 406);
for(i=0; i<8; i++)
DrawDigital(357+i*20, 65, 0);
for(i=0; i<4; i++)
DrawDigital(431+i*20, 111, 0);
setfillstyle(1, 8);
setcolor(12);
for(i=0; i<4; i++)
{
temp=i+'1';
outtextxy(364, 177+14*i, (char*)&temp);
bar(378, 177+i*14, 508, 184+i*14);
}
setcolor(14);
outtextxy(366, 250, "Next Shape");
setcolor(10);
outtextxy(366, 294, "Current");
outtextxy(382, 309, "Level");
setcolor(6);
outtextxy(366, 334, "Message:");
bar(362, 348, 509, 398);
DrawDigital(466, 293, Speed);
}
void WannaQuit()
{
int choise=0, temp;
setfillstyle(1, 8);
bar(362, 348, 509, 398);
setcolor(15);
outtextxy(378, 355, "Are you sure to");
outtextxy(368, 368, "quit game?");
outtextxy(410, 388, "Yes");
outtextxy(470, 388, "No");
setcolor(14);
circle(399, 392, 4);
while((temp=bioskey(0))!=7181)
{
if(temp==19200)
{
setcolor(8);
circle(459, 392, 4);
choise=0;
setcolor(14);
circle(399, 392, 4);
}
if(temp==19712)
{
setcolor(8);
circle(399, 392, 4);
choise=1;
setcolor(14);
circle(459, 392, 4);
}
}
if(choise==0)
end();
setfillstyle(1, 8);
bar(362, 348, 509, 398);
return;
}
void main()
{
ShowTitle();
unsigned c;
int CurrentShape, x=6, y=0, Status=0;
oldhandler=getvect(INTR);
setvect(INTR, handler);
randomize();
NewShape[0]=rand()%7;
DrawFace();
CurrentShape=CreateNewShape();
show(x, y, CurrentShape, Status);
while(1)
{
if(kbhit())
{
c=getch();
if(c==0)
c=getch();
switch(c)
{
case 's' :Speed=(Speed+1)%10; DrawDigital(466, 293, Speed); break;
case DOWN :GoDown(x, y, CurrentShape, Status); break;
case LEFT :GoLeft(x, y, CurrentShape, Status); break;
case RIGHT :GoRight(x, y, CurrentShape, Status); break;
case CHANGE :ChageShape(x, y, CurrentShape, Status); break;
case ESC :WannaQuit();
}
}
if((counter%=TimeDelay[Speed])==(TimeDelay[Speed]-1))
{
GoDown(x, y, CurrentShape, Status);
counter=0;
}
}
}
④ 如何使用C語言編寫簡單小游戲
C語言是計算機專業都要學習的一門基礎學科。一般來說,是比較枯燥的.那麼,我們能不能通過編一些小游戲來提高它的趣味性呢?這樣學習程序設計,就不會是一件艱苦 ,枯燥的事,它變得象電腦游戲一樣充滿好奇,富有樂趣。
例如2048這款游戲:
方法/步驟:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函數
int main()
{
int i,j;
int n[SIZE][SIZE];
char c=' ';
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
n[i][j]=0;
}
}
printf( "*********************** "
" 2048(%dX%d) "
" control:W/A/S/D "
"press any key to begin "
"*********************** ",SIZE,SIZE);
getch();
system("cls");
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)&&isfull(n))
break;
sleep(200);
c=getch();
while(c!='w'&&c!='a'&&c!='s'&&c!='d')
c=getch();
math(n,c);
system("cls");
}
printf(" Game Over! ",score);
return 0;
}
//函數
void putn(int n[][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
printf("| ");
printf("| ");
for(j=0;j<SIZE;j++)
{
if(n[i][j]==0)
printf("| ");
else
printf("|%4d ",n[i][j]);
}
printf("| ");
for(j=0;j<SIZE;j++)
printf("|_____");
printf("| ");
}
printf("score: %d",score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
{
for(j=1;j<SIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++;
}
}
for(j=0;j<SIZE;j++)
{
for(i=1;i<SIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count>0?0:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case 'w':tow(n);break;
case 'a':toa(n);break;
case 's':tos(n);break;
case 'd':tod(n);break;
default :;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=0;j<SIZE;j++)
{
for(a=0;a<SIZE;a++)
{
for(i=0;i<SIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;j<SIZE;j++)
{
for(a=0,i=0;i<SIZE;i++)
{
if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;i<SIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=0;i<SIZE;i++)
{
for(a=0;a<SIZE;a++)
{
for(j=0;j<SIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;i<SIZE;i++)
{
for(a=0,j=0;j<SIZE;j++)
{
if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;j<SIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1;a>=0;a--)
{
for(i=SIZE-1;i>0;i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1,i=SIZE-1;i>=0;i--)
{
if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a--]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i>=0;i--)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1;a>=0;a--)
{
for(j=SIZE-1;j>0;j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1,j=SIZE-1;j>=0;j--)
{
if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a--]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j>=0;j--)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
⑤ 利用C語言編寫的小游戲
我有一些例子,有其中的大部分例子,是一些程序代碼來的,希望對你有幫助吧。。 已發到郵箱,注意查收啦
⑥ 使用C語言編寫小游戲的方法是什麼
||//聲明
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函數
int main()
{
int i,j;
int n[SIZE][SIZE];
char c=' ';
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
n[i][j]=0;
}
}
printf( "***********************
"
" 2048(%dX%d)
"
" control:W/A/S/D
"
"press any key to begin
"
"***********************
",SIZE,SIZE);
getch();
system("cls");
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)&&isfull(n))
break;
sleep(200);
c=getch();
while(c!='w'&&c!='a'&&c!='s'&&c!='d')
c=getch();
math(n,c);
system("cls");
}
printf(" Game Over!
",score);
return 0;
}
//函數
void putn(int n[][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
printf("| ");
printf("|
");
for(j=0;j<SIZE;j++)
{
if(n[i][j]==0)
printf("| ");
else
printf("|%4d ",n[i][j]);
}
printf("|
");
for(j=0;j<SIZE;j++)
printf("|_____");
printf("|
");
}
printf("score: %d",score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
{
for(j=1;j<SIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||[i][j]==n[i][j-1])
count++;
}
}
for(j=0;j<SIZE;j++)
{
for(i=1;i<SIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count>0?0:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case 'w':tow(n);break;
case 'a':toa(n);break;
case 's':tos(n);break;
case 'd':tod(n);break;
default :;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=0;j<SIZE;j++)
{
for(a=0;a<SIZE;a++)
{
for(i=0;i<SIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;j<SIZE;j++)
{
for(a=0,i=0;i<SIZE;i++)
{
if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;i<SIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=0;i<SIZE;i++)
{
for(a=0;a<SIZE;a++)
{
for(j=0;j<SIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;i<SIZE;i++)
{
for(a=0,j=0;j<SIZE;j++)
{
if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;j<SIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1;a>=0;a--)
{
for(i=SIZE-1;i>0;i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1,i=SIZE-1;i>=0;i--)
{
if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a--]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i>=0;i--)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1;a>=0;a--)
{
for(j=SIZE-1;j>0;j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1,j=SIZE-1;j>=0;j--)
{
if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a--]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j>=0;j--)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
需要注意的是:
srand((unsigned) time(0);rand();是固定形式,不要更改任何一個字元!
如果不能編譯,請把sleep(200);注釋掉,如果提示不能找到system("cls");請把system("cls")更換為clrscr()。
c語言中沒有類,只有結構,也可以像類一樣編寫,用結構,但其成員都是公開訪問的,C++才有真正的類。
⑦ 僅用c語言能編出哪些小游戲
這個,。用c語言只要你願意折騰,有人陪你折騰,啥游戲都能寫吧,管它大還是小
⑧ 怎樣用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("