用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, 0, 0}},
},
{
{{0, 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 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("