C语言 游戏
谁有单纯用C或C++编写的游戏代码,既是在dos环境运行的,不用经过mfc操作的。任何游戏的代码都可以,要完整的。
谁有单纯用C或C++编写的游戏代码,既是在dos环境运行的,不用经过mfc操作的。任何游戏的代码都可以,要完整的。
答案:给你一个:五子棋的代码,在TC下编译通过了的。我试过了的。
#include <graphics.h>
#include <bios.h>
#define ROW 20
#define COL 20
#define SIZE 20
#define HIDE 0
#define SHOW 1
#define SET 2
#define TRUE 1
#define FALSE 0
#define LU 5
#define RD 6
#define LD 7
#define RU 8
#define LEFT 0x4b00 /*光标左键值*/
#define RIGHT 0x4d00 /*光标右键值*/
#define DOWN 0x5000 /*光标下键值*/
#define UP 0x4800 /*光标上键值*/
#define ESC 0x011b /* ESC键值 */
#define ENTER 0x1c0d /* 回车键值 */
#define F2 0x3c00 /* F2值 */
int P1Color,P2Color,BackGround,MapBgColor,LineColor,TextColor;
int MinX,MinY,MaxX,MaxY;
int CurRow,CurCol,CurX,CurY;
int CurFocus, Radius;
int Map[ROW][COL];
char P1Name[20],P2Name[20];
void Init_Graph();
void Init_Data();
void Init_Color();
void DrawMap();
void ShowQi();
void Begin();
void SetFoucs(int focus);
void Move(int dir);
void CheckWin(int row,int col);
void main(){
Init_Graph();
Init_Data();
Init_Color();
DrawMap();
Begin();
closegraph();
}
void Init_Graph(){
int gdriver=DETECT,gmode,errorcode;
initgraph(&gdriver, &gmode, "D:\\turboc2"); /* TC 目录 */
errorcode = graphresult();
if (errorcode != grOk){ /* an error occurred */
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /*terminate with an error code */
}
}
void Init_Color(){
P1Color=15;
P2Color=1;
BackGround=3;
MapBgColor=7;
LineColor=11;
TextColor=11;
strcpy(P1Name,"Player 1");
strcpy(P2Name,"Player 2");
}
void Init_Data(){
int x,y;
x=getmaxx();
y=getmaxy();
MinX=(x-COL*SIZE)/2;
MaxX=MinX+COL*SIZE;
MinY=(y-ROW*SIZE)/2;
MaxY=MinY+ROW*SIZE;
CurRow=ROW/2,CurCol=COL/2;
CurX=MinX+CurCol*SIZE+SIZE/2;
CurY=MinY+CurRow*SIZE+SIZE/2;
Radius=SIZE/2-3 ;
CurFocus=1; /* white 1 black 2*/
}
void DrawMap(){
int i,j,x,y;
setbkcolor(BackGround);
setfillstyle(SOLID_FILL,BackGround);
bar(0,0,getmaxx(),MinY);
setfillstyle(SOLID_FILL,MapBgColor);
setcolor(LineColor);
bar(MinX,MinY,MaxX,MaxY);
x=MinX,y=MinY;
for(i=0;i<=ROW;i++,y+=SIZE)
line(MinX,y,MaxX,y);
for(j=0;j<=COL;j++,x+=SIZE)
line(x,MinY,x,MaxY);
setcolor(TextColor);
settextstyle(0,0,0);
settextjustify(1,1);
outtextxy(MinX-MinX/2,MinY+10,P1Name);
outtextxy(MaxX+MinX/2,MinY+10,P2Name);
outtextxy(getmaxx()/2,MaxY+MinY/2,"Esc:Exit Enter:Set F2:Start ");
}
void ShowQi(int flag){
int color, r=Radius;
if (flag==SET || flag==SHOW){
if(CurFocus==1)
color=P1Color;
else
color=P2Color;
}else
color=MapBgColor;if (flag==SET) {
Map[CurRow][CurCol]=color;
CheckWin(CurRow,CurCol);
}else
{
r-=2;
}
setcolor(color);
circle(CurX,CurY,r);
setfillstyle(SOLID_FILL,color);
floodfill(CurX,CurY,color);
}
void SetFocus(int focus){
int color1,color2;
static x1,y1,x2,y2;
if(!x1){
x1=MinX-MinX/2;
x2=MaxX+MinX/2;
y1=y2=MinY+40;
}
if(focus==1)
color1=P1Color,color2=BackGround;
else
color1=BackGround,color2=P2Color;
setfillstyle(SOLID_FILL,color1);
setcolor(color1);
circle(x1,y1,Radius+2);
floodfill(x1,y1,color1);
setfillstyle(SOLID_FILL,color2);
setcolor(color2);
circle(x2,y2,Radius+2);
floodfill(x2,y2,color2);
CurFocus=focus;
}
void Begin(){
int key,Exit=FALSE;
int i,j;
ShowQi(SHOW);
SetFocus(CurFocus);
while(!Exit) {
key=bioskey(0);
switch(key) {
case ESC:
Exit=TRUE;
break;
case ENTER:
if(!Map[CurRow][CurCol]){
ShowQi(SET);
SetFocus(CurFocus%2+1);
ShowQi(SHOW);
}
break;
case F2:
Init_Data();
for(i=0;i<ROW;i++)
for(j=0;j<COL;j++)
Map[i][j]=0;
DrawMap();
ShowQi(SHOW);
SetFocus(CurFocus);
case DOWN:case UP:case LEFT:case RIGHT:
Move(key);
break;
}
}
}
void Refresh(){
int color=Map[CurRow][CurCol];
setcolor(color);
circle(CurX,CurY,Radius);
setfillstyle(SOLID_FILL,color);
floodfill(CurX,CurY,color);
}
void Move(int dir){
ShowQi(HIDE);
if(Map[CurRow][CurCol]) Refresh();
switch(dir) {
case LEFT:
CurCol--;
CurX-=SIZE;
if( CurCol<0){
CurCol+=COL;
CurX+=COL*SIZE;
}
break;
case RIGHT:
CurCol++;
CurX+=SIZE;
if( CurCol==COL){
CurCol-=COL;
CurX-=COL*SIZE;
}
break;
case DOWN:
CurRow++;
CurY+=SIZE;
if( CurRow==ROW){
CurRow-=ROW;
CurY-=ROW*SIZE;
}
break;
case UP:
CurRow--;
CurY-=SIZE;
if( CurRow<0){
CurRow+=ROW;
CurY+=ROW*SIZE;
}
break;
}
ShowQi(SHOW);
}
int GetNum(int row,int col,int dir,int value)
{
int result=0;
int i,j;
switch(dir)
{
case LEFT:
for(i=col-1;i>=0;i--)
if(Map[row][i]!=value) break;
result=col-1-i;
break;
case RIGHT:
for(i=col+1;i<COL;i++)
if(Map[row][i]!=value) break;
result=i-1-col;
break;
case DOWN:
for(i=row+1;i<ROW;i++)
if(Map[i][col]!=value) break;
result=i-1-row;
break;
case UP:
for(i=row-1;i>=0;i--)
if(Map[i][col]!=value) break;
result=row-1-i;
break;
case LU:
for(i=row-1,j=col-1;i>=0 && j>=0;i--,j--)
if(Map[i][j]!=value) break;
result=row-1-i;
break;
case RD:
for(i=row+1,j=col+1;i<ROW && j<COL;i++,j++)
if(Map[i][j]!=value) break;
result=i-1-row;
break;
case RU:
for(i=row-1,j=col+1;i>=0 && j<COL;i--,j++)
if(Map[i][j]!=value) break;
result=row-1-i;
break;
case LD:
for(i=row+1,j=col-1;i<ROW && j>=0;i++,j--)
if(Map[i][j]!=value) break;
result=i-1-row;
break;
}
return result;
}
void CheckWin(int row,int col)
{
int count,color=Map[row][col];
int winner=0,x,y;
char MsgWin[50];
count=GetNum(row,col,LEFT,color)+GetNum(row,col,RIGHT,color)+1;
if(count>=5)
winner=CurFocus;
if(!winner){
count=GetNum(row,col,UP,color)+GetNum(row,col,DOWN,color)+1;
if(count>=5)
winner=CurFocus