C语言C艹编程入门经典小游戏坦克大战!

C语言C艹编程入门经典小游戏坦克大战!,第1张

概述本文章向大家介绍C语言/C艹编程入门经典小游戏坦克大战!,主要包括C语言/C艹编程入门经典小游戏坦克大战!使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。



(小编推荐一个学C语言/C++的学习群:788649720,入群即送C/C++全套学习资料,满满的干货!)


game.h下的代码

#ifndef GAME

#define GAME

#include’‘tankdef.h’’

oID importMapGameAreaArry();

oID importHomeGameAreaArry();

oID importMyTankGameAreaArry();

oID DrawGame_Info();

int ConsoleSwtichArrX(int x);

int ConsoleSwtichArrY(int y);

oID TankAdjustUp(Tank *ptank,Dir dir);

oID TankAdjustleft(Tank *ptank);

oID TankAdjustRight(Tank *ptank);

oID TankAdjustDown(Tank *ptank);

oID TankAdjustUpE(Tank *ptank,Dir dir);

//发射炮d,参数为发射改炮d的坦克

SHELLNODE *shot(Tank *ptank);

//炮d向上移动

int ShellMoeUP(SHELLNODE *psn);

int ShellMoeDOWN(SHELLNODE *psn);

int ShellMoeleft(SHELLNODE *psn);

int ShellMoeRIGHT(SHELLNODE *psn);

//检查链表

SHELLNODE *ChecklinkList(int owner,int x,int y);

//线程处理函数

DWORD WINAPI Shelldispes(LPO lpParam);

//oID Writefile1();

oID HitTarget(SHELLNODE *psn,int tagert);

//根据炮d的坐标销毁相应的坦克

oID DestroyEnemy(SHELLNODE *psn);

//清空敌方坦克的数据

oID ClearEnemyTank(Tank *ptank);

//炮d节点的比较函数

int ShellCompareByNum(NODE *pNode1,NODE *pNode2);

//敌方坦克移动函数

DWORD WINAPI EnemyMoe(LPO lpParam);

#endif

game.c下的代码

#define _CRT_SECURE_NO_WARNINGS

#include’‘tankdef.h’’

#include’‘interface.h’’

#include

#include

#include

#include

#include ‘‘game.h’’

int g_start;

oID Game_Start()

{

HANDLE hThread;

//初始化临界区资源

system(’‘mode con cols=95 lines=25’’);

SetConsoleTitle(TEXT(’‘坦克大战’’));

InitializeCriticalSection(&g_cs);

srand((unsigned int)time(NulL));

DrawGameBord();

DrawGame_Info();

LoadMap();

DrawHome();

InitMyTnak(&MyselfTanke);

DrawTnak(&MyselfTanke);

importMapGameAreaArry();

importHomeGameAreaArry();

importMyTankGameAreaArry();

PlaySound(TEXT(’'sound/Tnak.wa ‘’),NulL,SND_LOOP);

PlaySound(TEXT(’'sound/TankMoe.wa ‘’),SND_LOOP);

int i;

for (i = 0; i

{

EnemyTank[i] = InitEnemtyTank();

DrawEnmpty(&EnemyTank[i]);

importEnemyTankGameAreaArry(&EnemyTank[i]);

}

while (i–)

{

hThread = CreateThread(NulL,EnemyMoe,&EnemyTank[i],NulL);

CloseHandle(hThread);

}

//初始化炮d链表

InitlinkList(&g_shell_List);

//Writefile1();

}

oID InitMyTnak(Tank *ptank)

{

ptank->belong = MYSELF;

ptank->dir = UP;

ptank->lIEf = 1;

ptank->x = left_top_X 24;

ptank->y = left_top_Y 18;

}

Tank InitEnemtyTank()

{

int row,col,oerlp;

int i,j;

Tank tank;

tank.belong = ENEMY;

tank.dir = rand() % 4;

tank.lIEf = 1;

tank.speed = 400;

tank.y = left_top_Y 1;

while (1)

{

oerlp = 0;

tank.x = rand() % 29 * 2 left_top_X 2;

row = ConsoleSwtichArrY(tank.y);

col = ConsoleSwtichArrX(tank.x);

for (i = row; i

{ //取非为真

for (j = col; j

{

if (g_area_data[i][j])

{

oerlp = 1;

}

}

}

if (!oerlp)

break;

}

return tank;

}

Tank InitEnemtyTankSpeed()

{

int row,j;

Tank tank;

tank.belong = ENEMY;

tank.dir = rand() % 4;

tank.lIEf = 1;

tank.speed = 100;

tank.y = left_top_Y 1;

while (1)

{

oerlp = 0;

tank.x = rand() % 29 * 2 left_top_X 2;

row = ConsoleSwtichArrY(tank.y);

col = ConsoleSwtichArrX(tank.x);

for (i = row; i

{ //取非为真

for (j = col; j

{

if (g_area_data[i][j])

{

oerlp = 1;

}

}

}

if (!oerlp)

break;

}

return tank;

}

//将地图导入到二维数组

oID importMapGameAreaArry()

{

int i,j;

for (i = 0; i

{

for (j = 0; j

{

g_area_data[i 3][j] = map[i][j];

}

}

}

//将老家导入到二维数组

oID importHomeGameAreaArry()

{

int i,j;

int row,col;

row = ConsoleSwtichArrY(left_top_Y 18);

col = ConsoleSwtichArrX(left_top_X 30);

for (i = 0; i

{

for (j = 0; j

{

if (i == 1 && j == 1)

g_area_data[row i][col j] = AREA_HOME;

else

g_area_data[row i][col j] = AREA_WALL;

}

}

}

oID importMyTankGameAreaArry()

{

int i,col;

row = ConsoleSwtichArrY(MyselfTanke.y);

col = ConsoleSwtichArrX(MyselfTanke.x);

for (i = 0; i

{

for (j = 0; j

{

if (tank[MyselfTanke.dir][i][j])

{

g_area_data[i row][j col] = AREA_SELF;

}

else

g_area_data[i row][j col] = AREA_SPACE;

}

}

}

oID importEnemyTankGameAreaArry(Tank *ptank)

{

int i,col;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

for (i = 0; i

{

for (j = 0; j

{

if (tank[ptank->dir][i][j])

{

g_area_data[i row][j col] = AREA_ENEMY;

}

else

g_area_data[i row][j col] = AREA_SPACE;

}

}

}

oID importMyClearTankGameAreaArry()

{

int i,col;

row = ConsoleSwtichArrY(MyselfTanke.y);

col = ConsoleSwtichArrX(MyselfTanke.x);

for (i = 0; i

{

for (j = 0; j

{

if (tank[MyselfTanke.dir][i][j])

{

g_area_data[i row][j col] = AREA_SPACE;

}

}

}

}

int ConsoleSwtichArrX(int x)

{

return (x - (left_top_X 2)) / 2;

}

int ConsoleSwtichArrY(int y)

{

return y - (left_top_Y 1);

}

oID Writefile1()

{

int i,j;

file *fp = fopen(’‘1.txt’’,‘‘w’’);

if (fp == NulL)

return;

for (i = 0; i

{

for (j = 0; j

{

fprintf_s(fp,‘’-’’,g_area_data[i][j]);

}

fprintf_s(fp,’’ ‘’);

}

fclose(fp);

}

oID TankAdjustUp(Tank *ptank,Dir dir)

{

ptank->dir = dir;

DrawTnakClear(ptank);

DrawTnak(ptank);

}

oID TankAdjustleft(Tank *ptank)

{

ptank->dir = left;

DrawTnakClear(ptank);

DrawTnak(ptank);

}

oID TankAdjustRight(Tank *ptank)

{

ptank->dir = RIGHT;

DrawTnakClear(ptank);

DrawTnak(ptank);

}

oID TankAdjustDown(Tank *ptank)

{

ptank->dir = DOWN;

DrawTnakClear(ptank);

DrawTnak(ptank);

}

oID TankAdjustUpE(Tank *ptank,Dir dir)

{

ptank->dir = dir;

DrawTnakClear(ptank);

DrawEnmpty(ptank);

}

SHELLNODE *shot(Tank *ptank)

{

static unsigned int num = 0;

SHELLNODE psn = (SHELLNODE)malloc(sizeof(SHELLNODE));

if (psn == NulL)

{

return NulL;

}

psn->node.next = NulL;

psn->shell.belong = ptank->belong;

psn->shell.dir = ptank->dir;

psn->shell.isshow = 0;

psn->shell.speed = 70;

psn->shell.left = 1;

psn->shell.num = num ;

switch (ptank->dir)

{

case UP:

psn->shell.x = ptank->x 2;

psn->shell.y = ptank->y;

break;

case DOWN:

psn->shell.x = ptank->x 2;

psn->shell.y = ptank->y 2;

break;

case left:

psn->shell.x = ptank->x;

psn->shell.y = ptank->y 1;

break;

case RIGHT:

psn->shell.x = ptank->x 4;

psn->shell.y = ptank->y 1;

break;

}

//放入链表中

AddNode(g_shell_List,(NODE*)psn);

return psn;

}

int ShellMoeUP(SHELLNODE *psn)

{

int x,y;

SHELLNODE *ps;

x = ConsoleSwtichArrX(psn->shell.x);

y = ConsoleSwtichArrY(psn->shell.y);

if (psn->shell.isshow)//如果炮d之前已经运行

DrawShell(&psn->shell,‘’ ‘’);//擦除炮d

if (y

return OER_LOW;//越界

//如果上方有墙壁或坦克,就返回

if (g_area_data[y - 1][x] != AREA_SPACE)//撞到其它物体

{

psn->shell.isshow = 0;//停止运行

psn->shell.y -= 1;//调整炮d坐标

return g_area_data[y - 1][x];

}

//如果上方有对方炮d,就返回

//遍历链表,查找有无对方炮d存在

if (ps = ChecklinkList(!psn->shell.belong,psn->shell.x,psn->shell.y - 1))

{

ps->shell.left = 0;//让对方的炮d的生命结束

/*sprintf_s(buf,100,‘‘Owner %d Shell Num %d dIEd of x:%d,y:%d’’,ps->shell.owner,ps->shell.num,ps->shell.x,ps->shell.y);

Writetofile(buf);*/

return AREA_SHELL;

}

//其它情况,则显示该炮d

psn->shell.y -= 1;//调整炮d坐标

if (psn->shell.left)

DrawShell(&psn->shell,SHELL_LETTER);

psn->shell.isshow = 1;

return 0;

}

int ShellMoeDOWN(SHELLNODE *pShell)

{

int x,y;

SHELLNODE *ps;

x = ConsoleSwtichArrX(pShell->shell.x);

y = ConsoleSwtichArrY(pShell->shell.y);

if (pShell->shell.isshow)//如果炮d之前已经运行

DrawShell(&pShell->shell,‘’ ‘’);//擦除炮d

if (y >= Gmae_Arr_Height - 1)

return OER_LOW;//越界

//如果上方有墙壁或坦克,就返回

if (g_area_data[y 1][x] != AREA_SPACE)//撞到其它物体

{

pShell->shell.isshow = 0;//停止运行

pShell->shell.y = 1;//调整炮d坐标

return g_area_data[y 1][x];

}//如果上方有对方炮d,就返回

//遍历链表,查找有无对方炮d存在

if (ps = ChecklinkList(!pShell->shell.belong,pShell->shell.x,pShell->shell.y 1))

{

ps->shell.left = 0;//让对方的炮d的生命结束

/*sprintf_s(buf,ps->shell.y);

Writetofile(buf);*/

return AREA_SHELL;

}

//其它情况,则显示该炮d

pShell->shell.y = 1;//调整炮d坐标

if (pShell->shell.left)

DrawShell(&pShell->shell,SHELL_LETTER);

pShell->shell.isshow = 1;

return 0;

}

int ShellMoeleft(SHELLNODE *pShell)

{

int x,‘’ ‘’);//擦除炮d

if (x

return OER_LOW;//越界

//如果上方有墙壁或坦克,就返回

if (g_area_data[y][x - 1] != AREA_SPACE)//撞到其它物体

{

pShell->shell.isshow = 0;//停止运行

pShell->shell.x -= 2;//调整炮d坐标

return g_area_data[y][x - 1];

}//如果上方有对方炮d,就返回

//遍历链表,查找有无对方炮d存在

if (ps = ChecklinkList(!pShell->shell.belong,pShell->shell.x - 2,pShell->shell.y))

{

ps->shell.left = 0;//让对方的炮d的生命结束

/*sprintf_s(buf,ps->shell.y);

Writetofile(buf);*/

return AREA_SHELL;

}

//其它情况,则显示该炮d

pShell->shell.x -= 2;//调整炮d坐标

if (pShell->shell.left)

DrawShell(&pShell->shell,SHELL_LETTER);

pShell->shell.isshow = 1;

return 0;

}

int ShellMoeRIGHT(SHELLNODE *pShell)

{

int x,‘’ ‘’);//擦除炮d

if (x >= Game_Arr_WIDth - 1)

return OER_LOW;//越界

//如果右方有墙壁或坦克,就返回

if (g_area_data[y][x 1] != AREA_SPACE)//撞到其它物体

{

pShell->shell.isshow = 0;//停止显示

pShell->shell.x = 2;//调整炮d坐标

return g_area_data[y][x 1];

}//如果上方有对方炮d,就返回

//遍历链表,查找有无对方炮d存在

if (ps = ChecklinkList(!pShell->shell.belong,pShell->shell.x 2,ps->shell.y);

Writetofile(buf);*/

return AREA_SHELL;

}

//其它情况,则显示该炮d

pShell->shell.x = 2;//调整炮d坐标

if (pShell->shell.left)

DrawShell(&pShell->shell,SHELL_LETTER);

pShell->shell.isshow = 1;

return 0;

}

//查找链表中有没有敌方的炮d

SHELLNODE *ChecklinkList(int owner,int y)

{

SHELLNODE *psn = NulL;

int i;

for (i = 0; i

{

psn = (SHELLNODE *)GetNode(g_shell_List,i);

if (psn->shell.x == x && psn->shell.y == y

&& psn->shell.belong == owner)

{

return psn;

}

}

return NulL;

}

DWORD WINAPI Shelldispes(LPO lpParam)

{

SHELLNODE *psm = (SHELLNODE *)lpParam;

int ret; //返回值

while (psm->shell.left == 1)

{

if (g_start == 1)

continue;

EnterCriticalSection(&g_cs);

switch (psm->shell.dir)

{

case UP:

if (ret = ShellMoeUP(psm))

{

HitTarget(psm,ret);

}

break;

case DOWN:

if (ret = ShellMoeDOWN(psm))

HitTarget(psm,ret);

break;

case left:

if (ret = ShellMoeleft(psm))

HitTarget(psm,ret);

break;

case RIGHT:

if (ret = ShellMoeRIGHT(psm))

HitTarget(psm,ret);

break;

}

LeaeCriticalSection(&g_cs);

Sleep(psm->shell.speed);

}

//为了保护链表删除时要同步

EnterCriticalSection(&g_cs);

if (psm->shell.isshow)

DrawShell(&psm->shell,‘’ ‘’);

if (psm->shell.belong == MYSELF)

–g_self_shell_cout;

if (DeleteNode(g_shell_List,(NODE*)psm,ShellCompareByNum))

free(psm);

LeaeCriticalSection(&g_cs);

return 0;

}

int g_dIE_Enemy = 0;

oID HitTarget(SHELLNODE *psn,int tagert)

{

switch (tagert)

{

case AREA_SHELL:

case OER_LOW:

psn->shell.left = 0;

break;

case AREA_WALL:

HitWall(&psn->shell);

psn->shell.left = 0;

PlaySound(TEXT(’'sound/击碎.wa ‘’),SND_ASYNC );

break;

case AREA_HOME:

HitHome(&psn->shell);

psn->shell.left = 0;

break;

case AREA_ENEMY:

if (psn->shell.belong == MYSELF)

{

g_dIE_Enemy;

psn->shell.left = 0;

DestroyEnemy(psn);

PlaySound(TEXT(’'sound/爆炸.wa ‘’),SND_ASYNC );

DrawGame_Info();

}

break;

case AREA_SELF:

if (psn->shell.belong == ENEMY)

{

psn->shell.left = 0;

ClearEnemyTank(&MyselfTanke);

DrawTnakClear(&MyselfTanke);

g_dIE_shellf;

DrawGame_Info();

if (g_dIE_shellf == 3)

{

music = 0;

PrintGameOer();

}

else

{

InitMyTnak(&MyselfTanke);

DrawTnak(&MyselfTanke);

importMyTankGameAreaArry();

PlaySound(TEXT(’'sound/爆炸.wa ‘’),SND_ASYNC);

}

}

break;

}

}

oID DestroyEnemy(SHELLNODE *psn)

{

int i;//用于遍历数组

Tank t; //一个临时的坦克

for (i = 0; i

{

t = EnemyTank[i];

if (psn->shell.x >= t.x && psn->shell.x

psn->shell.y >= t.y && psn->shell.y

{

//清除坦克

DrawTnakClear(&t);

//修改坦克的生命值

EnemyTank[i].lIEf = 0;

//清空敌方坦克的数据

ClearEnemyTank(&t);

}

}

}

oID ClearEnemyTank(Tank *ptank)

{

int row,col;

int i,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

for (i = row; i

{

for (j = col; j

{

g_area_data[i][j] = AREA_SPACE;

}

}

}

//炮d节点比较函数

int ShellCompareByNum(NODE *pNode1,NODE *pNode2)

{

SHELLNODE p1 = (SHELLNODE)pNode1;

SHELLNODE P2 = (SHELLNODE)pNode2;

return p1->shell.num == P2->shell.num ? 0 : 1;

}

int x = 0;

DWORD WINAPI EnemyMoe(LPO lpParam)

{

Tank * pTank = (Tank*)lpParam;

SHELLNODE *psn = NulL;

HANDLE hThread;

unsigned int nStep = 0;

unsigned int nShot = 0;

nShot = 2;

int nDir = rand() % 5 5;

while (pTank->lIEf)

{

if(g_start == 1)

continue;

EnterCriticalSection(&g_cs);

nStep;

if(nStep % nDir == 0)

TankAdjustUpE(pTank,rand()%4);

if (nStep % nShot == 0) //改发射炮d了

{

if (psn = shot(pTank))

{

hThread = CreateThread(NulL,Shelldispes,psn,NulL);

CloseHandle(hThread);

}

}

switch (pTank->dir)

{

case UP:

if (TankMoeUpE(pTank))

{

TankAdjustUpE(pTank,(pTank->dir 1) % 4);

}

break;

case DOWN:

if(TankMoeDownE(pTank))

TankAdjustUpE(pTank,(pTank->dir 1) % 4);

break;

case left: if(TankMoeleftE(pTank))

TankAdjustUpE(pTank,(pTank->dir 1) % 4);

break;

case RIGHT: if(TankMoeRightE(pTank))

TankAdjustUpE(pTank,(pTank->dir 1) % 4);

break;

}

LeaeCriticalSection(&g_cs);

Sleep(pTank->speed);

}

DIE_Enemy_Tank_Count;

// The number of tanks on the map Number of dead tanks

if (ENEMY_Tank DIE_Enemy_Tank_Count

{

//Create a new enemy tank

HANDLE hThread;

EnterCriticalSection(&g_cs);

if (DIE_Enemy_Tank_Count % 1 == 0)

{

*pTank = InitEnemtyTankSpeed();

importEnemyTankGameAreaArry(pTank);

}

else

{

*pTank = InitEnemtyTank();

importEnemyTankGameAreaArry(pTank);

}

hThread = CreateThread(NulL,pTank,NulL);

CloseHandle(hThread);

LeaeCriticalSection(&g_cs);

}

else if(DIE_Enemy_Tank_Count == ENEMY_Z_ALL_TANK)

{

EnterCriticalSection(&g_cs);

Printictory();

LeaeCriticalSection(&g_cs);

}

return 0;

}

oID DrawGame_Info()

{

EnterCriticalSection(&g_cs);

SetConsoleTextAttribute(g_hout,FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);

COORD pos;

pos.X = left_top_X Game_Arr_WIDth * 2 8;

pos.Y = left_top_Y 1;

SetConsoleCursorposition(g_hout,pos);

printf(’‘C语言坦克大战项目’’);

pos.Y = 2;

SetConsoleCursorposition(g_hout,pos);

printf(’‘信息窗口:’’);

pos.Y;

if (g_start == 1)

{

SetConsoleCursorposition(g_hout,pos);

printf(’‘游戏运行状态: Pause’’);

}

if(g_start == 0)

{

SetConsoleCursorposition(g_hout,pos);

printf(’'游戏运行状态: Run ‘’);

}

pos.Y = 2;

SetConsoleCursorposition(g_hout,pos);

printf(’‘敌方坦克总数:%d’’,ENEMY_Z_ALL_TANK);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘敌方坦克阵亡数量:%d’’,g_dIE_Enemy);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’'敌方坦克剩余数量:%d ‘’,ENEMY_Z_ALL_TANK - g_dIE_Enemy);

pos.Y = 2;

SetConsoleCursorposition(g_hout,pos);

printf(’‘我方坦克总数:%d’’,AREA_SELF);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘我方坦克阵亡数量:%d’’,g_dIE_shellf);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘我方坦克剩余数量:%d’’,AREA_SELF - g_dIE_shellf);

pos.Y = 2;

SetConsoleCursorposition(g_hout,pos);

printf(’‘游戏暂停或开始:P’’);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘坦克向上移动:W’’);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘坦克向左移动:A’’);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘坦克向右移动:D’’);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘坦克向下移动:S’’);

pos.Y ;

SetConsoleCursorposition(g_hout,pos);

printf(’‘坦克发射炮d:J’’);

LeaeCriticalSection(&g_cs);

}

interface.h

#include

#include’‘tankdef.h’’

#ifndef INTERFACE

#define INTERFACE

//开始游戏

oID Game_Start();

C语言/C艹编程入门经典小游戏坦克大战!

//画游戏界面

oID DrawGameBord();

oID LoadMap();

oID DrawHome();

oID InitMyTnak(Tank *ptank);

oID DrawTnak(Tank *ptank);

Tank InitEnemtyTank();

oID DrawEnmpty(Tank *pEtank);

oID DrawTnakClear(Tank *ptank);

oID importEnemyTankGameAreaArry();

oID importMyClearTankGameAreaArry();

int TankMoeUp(Tank *ptank);

int TankMoeleft(Tank *ptank);

int TankMoeRight(Tank *ptank);

int TankMoeDown(Tank *ptank);

oID DrawShell(SHELL *Pshell,const char *liter);

oID Writefile1();

oID HitWall(SHELL *pShell);

oID HitHome(SHELL *pShell);

oID PrintGameOer();

int TankMoeUpE(Tank *ptank);

int TankMoeDownE(Tank *ptank);

int TankMoeleftE(Tank *ptank);

int TankMoeRightE(Tank *ptank);

oID Printictory();

#endif

interface.c

#include’‘tankdef.h’’

#include’‘interface.h’’

#include

#include’‘game.h’’

HANDLE g_hout;

HWND hwnd = NulL;

CONSolE_SCREEN_BUFFER_INFO csbi;

int music = 1;

oID DrawGameBord()

{

g_hout = GetStdHandle(STD_OUTPUT_HANDLE);

CONSolE_CURSOR_INFO c;

GetConsoleCursorInfo(g_hout,&c);

c.bisible = 0;

SetConsoleCursorInfo(g_hout,&c);

int row,col;

DWORD count;

COORD pos;

pos.X = left_top_X;

pos.Y = left_top_Y;

for (row = 0; row

{

SetConsoleCursorposition(g_hout,pos);

for (col = 0; col

{

if (row == 0 || row == Gmae_Arr_Height 1 ||

col == 0 || col == Game_Arr_WIDth 1)

{

GetConsoleScreenBufferInfo(g_hout,&csbi);

printf(’’’’);

FillConsoleOutputAttribute(g_hout,border_color,2,csbi.DWCursorposition,&count);

}

else

printf(’’ ‘’);

}

pos.Y ;

}

pos.X = 34;

SetConsoleCursorposition(g_hout,pos);

GetConsoleScreenBufferInfo(g_hout,&csbi);

printf(’‘坦克大战’’);

FillConsoleOutputAttribute(g_hout,TANKTEXT,8,&count);

}

oID LoadMap()

{

int row,col;

COORD pos;

DWORD count;

pos.X = left_top_X 2;

pos.Y = left_top_Y 4;

for (row = 0; row

{

SetConsoleCursorposition(g_hout,pos);

for (col = 0; col

{

if (map[row][col])

{

GetConsoleScreenBufferInfo(g_hout,&csbi);

printf(’’%s’’,TANKLE_LETTER);

FillConsoleOutputAttribute(g_hout,border_color1,&count);

}

else

printf(’’ ‘’);

}

pos.Y ;

}

}

oID DrawHome()

{

int row,col;

COORD pos;

DWORD count;

pos.X = left_top_X 30;

pos.Y = left_top_Y 18;

for (row = 0; row

{

SetConsoleCursorposition(g_hout,pos);

for (col = 0; col

{

if (row == 1 && col == 1)

{

GetConsoleScreenBufferInfo(g_hout,HOME_LETTER);

FillConsoleOutputAttribute(g_hout,HOME_color,&count);

}

else

printf(’’%s’’,border_leftER);

}

pos.Y ;

}

}

oID DrawTnak(Tank *ptank)

{

COORD pos;

DWORD count;

int row,col;

pos.X = ptank->x;

pos.Y = ptank->y;

for (row = 0; row

{

SetConsoleCursorposition(g_hout,pos);

for (col = 0; col

{

if (tank[ptank->dir][row][col] == 1)

{

GetConsoleScreenBufferInfo(g_hout,Tank_color,&count);

}

else

{

if (tank[0][row][col] == 0 || tank[1][row][col] == 0)

printf(’‘│’’);

if (tank[ptank->dir][row][col] == 2)

{

printf(’’’’);

}

if (tank[3][row][col] == 0 || tank[2][row][col] == 0)

printf(’‘─’’);

}

}

pos.Y ;

}

}

oID DrawEnmpty(Tank *pEtank)

{

COORD pos;

DWORD count;

pos.X = pEtank->x;

pos.Y = pEtank->y;

int row,col;

for (row = 0; row

{

SetConsoleCursorposition(g_hout,pos);

for (col = 0; col

{

if (tankE[pEtank->dir][row][col])

{

GetConsoleScreenBufferInfo(g_hout,FOREGROUND_GREEN | FOREGROUND_INTENSITY,&count);

}

else

printf(’’ ‘’);

}

pos.Y ;

}

}

oID DrawTnakClear(Tank *ptank)

{

COORD pos;

int row,pos);

for (col = 0; col

{

if (tank[ptank->dir][row][col])

{

printf(’’ ‘’);

}

else

printf(’’ ‘’);

}

pos.Y ;

}

}

int TankMoeUp(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (row

return OER_LOW;

//判断是否撞墙

for (i = col; i

{

if (g_area_data[row - 1][i])

return g_area_data[row - 1][i];

}

for (i = row - 1; i

{

for (j = col; j

{

if (i == row 2)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i 1][j];

}

}

DrawTnakClear(ptank);

–ptank->y;

DrawTnak(ptank);

return 0;

}

int TankMoeleft(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (col

return OER_LOW;

//判断是否撞墙

for (i = row; i

{

if (g_area_data[i][col - 1])

return g_area_data[i][col - 1];

}

for (i = row; i

{

for (j = col - 1; j

{

if (j == col 2)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i][j 1];

}

}

DrawTnakClear(ptank);

ptank->x -= 2;

DrawTnak(ptank);

return 0;

}

int TankMoeRight(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (col 2 >= Game_Arr_WIDth - 1)

return OER_LOW;

//判断是否撞墙

for (i = row; i

{

if (g_area_data[i][col 3])

return g_area_data[i][col 3];

}

for (i = row; i

{

for (j = col 3; j >= col; --j)

{

if (j == col)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i][j - 1];

}

}

DrawTnakClear(ptank);

ptank->x = 2;

DrawTnak(ptank);

return 0;

}

int TankMoeDown(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (row 2 >= Gmae_Arr_Height - 1)

return OER_LOW;

//判断是否撞墙

for (i = col; i

{

if (g_area_data[row 3][i])

return g_area_data[row 3][i];

}

for (i = row 3; i >= row; --i)

{

for (j = col; j

{

if (i == row)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i - 1][j];

}

}

DrawTnakClear(ptank);

ptank->y;

DrawTnak(ptank);

return 0;

}

int TankMoeUpE(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (row

return OER_LOW;

//判断是否撞墙

for (i = col; i

{

if (g_area_data[row - 1][i])

return g_area_data[row - 1][i];

}

for (i = row - 1; i

{

for (j = col; j

{

if (i == row 2)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i 1][j];

}

}

DrawTnakClear(ptank);

–ptank->y;

if (x == 0)

DrawEnmpty(ptank);

/*if (x == 1)

DrawEnmptySpeed(ptank);*/

return 0;

}

int TankMoeDownE(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (row 2 >= Gmae_Arr_Height - 1)

return OER_LOW;

//判断是否撞墙

for (i = col; i

{

if (g_area_data[row 3][i])

return g_area_data[row 3][i];

}

for (i = row 3; i >= row; --i)

{

for (j = col; j

{

if (i == row)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i - 1][j];

}

}

DrawTnakClear(ptank);

ptank->y;

if(x == 0)

DrawEnmpty(ptank);

//if (x == 1)

//DrawEnmptySpeed(ptank);

return 0;

}

int TankMoeRightE(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (col 2 >= Game_Arr_WIDth - 1)

return OER_LOW;

//判断是否撞墙

for (i = row; i

{

if (g_area_data[i][col 3])

return g_area_data[i][col 3];

}

for (i = row; i

{

for (j = col 3; j >= col; --j)

{

if (j == col)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i][j - 1];

}

}

DrawTnakClear(ptank);

ptank->x = 2;

if (x == 0)

DrawEnmpty(ptank);

/* if (x == 1)

DrawEnmptySpeed(ptank);*/

return 0;

}

int TankMoeleftE(Tank *ptank)

{

int row,j;

row = ConsoleSwtichArrY(ptank->y);

col = ConsoleSwtichArrX(ptank->x);

//判断是否越界

if (col

return OER_LOW;

//判断是否撞墙

for (i = row; i

{

if (g_area_data[i][col - 1])

return g_area_data[i][col - 1];

}

for (i = row; i

{

for (j = col - 1; j

{

if (j == col 2)

g_area_data[i][j] = 0;

else

g_area_data[i][j] = g_area_data[i][j 1];

}

}

DrawTnakClear(ptank);

ptank->x -= 2;

if (x == 0)

DrawEnmpty(ptank);

/* if (x == 1)

DrawEnmptySpeed(ptank);*/

return 0;

}

oID DrawShell(SHELL *Pshell,const char *litter)

{

DWORD cout;

COORD pos = { Pshell->x,Pshell->y };

SetConsoleCursorposition(g_hout,pos);

printf(’’%s’’,litter);

FillConsoleOutputAttribute(g_hout,FOREGROUND_RED | FOREGROUND_INTENSITY,pos,&cout);

}

oID HitWall(SHELL *pShell)

{

int i;

int row,col;

row = ConsoleSwtichArrY(pShell->y);

col = ConsoleSwtichArrX(pShell->x);

if (pShell->dir == UP || pShell->dir == DOWN)

{

COORD pos = { pShell->x - 2,pShell->y };

for (i = col - 1; i

{

SetConsoleCursorposition(g_hout,pos);

if(g_area_data[row][i] == AREA_WALL)

{

printf(’’ ‘’);

g_area_data[row][i] = AREA_SPACE;

}

pos.X = 2;

}

}

else

{

COORD pos = { pShell->x,pShell->y - 1 };

for (i = row - 1; i

{

SetConsoleCursorposition(g_hout,pos);

if(g_area_data[i][col] == AREA_WALL)

{

printf(’’ ‘’);

g_area_data[i][col] = AREA_SPACE;

}

pos.Y ;

}

}

}

oID HitHome(SHELL *pShell)

{

COORD pos = { pShell->x,pShell->y };

SetConsoleCursorposition(g_hout,pos);

printf(’’ ‘’);

PrintGameOer();

}

oID PrintGameOer()

{

system(’‘cls’’);

DWORD count;

COORD pos = { 32,10};

SetConsoleCursorposition(g_hout,pos);

printf(’‘Game Oer!’’);

FillConsoleOutputAttribute(g_hout,FOREGROUND_GREEN | FOREGROUND_INTENSITY |

BACKGROUND_RED | BACKGROUND_INTENSITY,10,&count);

PlaySound(TEXT(’'sound/Dath.wa ‘’),SND_LOOP);

DeleteCriticalSection(&g_cs);

DestroyList(&g_shell_List);

exit(0);

}

oID Printictory()

{

PlaySound(TEXT(’'sound/胜利.wa ‘’),SND_LOOP);

system(’‘cls’’);

DWORD count;

COORD pos = { 32,10 };

SetConsoleCursorposition(g_hout,pos);

printf(’’ octiory ‘’);

FillConsoleOutputAttribute(g_hout,&count);

DeleteCriticalSection(&g_cs);

DestroyList(&g_shell_List);

exit(0);

}

#ifndef liKList

#define liKList

//定义节点结构

typedef struct node

{

struct node *next; //指向下一个节点

}NODE;

typedef struct linkList

{

NODE head; //头节点

int size; //大小

}linkList;

//链表 *** 作函数

//链表初始化

oID InitlinkList(linkList **List);

//销毁链表

oID DestroyList(linkList **List);

//添加一个节点的链表到尾部

oID AddNode(linkList *List,NODE *pNode);

//删除一个指定的节点,删除成功返回成功节点的指针,失败返回NulL

NODE *DeleteNode(linkList *List,NODE *pNode,int(*compare)(NODE *,NODE *));

//返回链表中节点的个数

int CountOflinkList(linkList *List);

//返回指定位置的节点

NODE *GetNode(linkList *List,int pos);

#endif

linkList.c 下的代码

#include’‘linkList.h’’

#include

#include

//链表 *** 作函数

//链表初始化

oID InitlinkList(linkList **List)

{

List = (linkList)malloc(sizeof(linkList));

if (*List == NulL)

return;

(*List)->head.next = NulL;

(*List)->size = 0;

}

//销毁链表

oID DestroyList(linkList **List)

{

if (List && *List)

{

free(*List);

*List = NulL;

}

}

//添加一个节点的链表到尾部

oID AddNode(linkList *List,NODE *pNode)

{

NODE *p = &List->head;

while (p->next)

p = p->next;

p->next = pNode;

List->size ; //节点的大小 1

}

//删除一个指定的节点,删除成功返回成功节点的指针,失败返回NulL

NODE *DeleteNode(linkList *List,NODE *))

{

NODE *p = &List->head;

NODE *pfree = NulL;

while (p->next)

{

if (compare(p->next,pNode) == 0)

{

pfree = p->next;

p->next = p->next->next;

List->size–;

break;

}

p = p->next;

}

return pfree;

}

//返回链表中节点的个数

int CountOflinkList(linkList *List)

{

return List->size;

}

NODE *GetNode(linkList *List,int pos)

{

int i;

NODE *p = List->head.next;

if (pos

return NulL;

for (i = 0; i

{

p = p->next;

}

return p;

}

tankdef.h

#include

#include ‘‘linkList.h’’

#include’‘game.h’’

#ifndef TANKDEFH

#define TANKDEFH

#define Game_Arr_WIDth 31 //游戏区域的宽度

#define Gmae_Arr_Height 20 //游戏区域的高度

#define left_top_X 4 //初始x坐标

#define left_top_Y 1 //初始y坐标

#define border_leftER ‘’’’ //游戏边框字符

#define TANKLE_LETTER ‘’’’ //坦克字符

#define HOME_LETTER ‘‘★’’ //老家字符

#define SHELL_LETTER ‘’’’ //炮d字符

#define MYSELF 1 //我方坦克

#define ENEMY 0 //敌方坦克

#define border_color FOREGROUND_BLUE | FOREGROUND_INTENSITY //窗口的颜色

#define border_color1 FOREGROUND_GREEN | FOREGROUND_INTENSITY | FOREGROUND_BLUE//关卡颜色

#define TANKTEXT FOREGROUND_RED | FOREGROUND_INTENSITY //文字

#define HOME_color FOREGROUND_RED | FOREGROUND_INTENSITY //老家颜色

#define Tank_color FOREGROUND_RED | FOREGROUND_INTENSITY | FOREGROUND_BLUE | FOREGROUND_GREEN //坦克颜色

#define ENEMY_Tank 4

#define ENEMY_Z_ALL_TANK 10

#define AREA_SPACE 0 //空

#define AREA_WALL 1 //墙

#define AREA_HOME 2 //老家

#define AREA_SELF 3 //自己

#define AREA_ENEMY 4 //敌人

#define AREA_SHELL 5 //炮d

#define OER_LOW -1 //越界

#define Game_Oer 0 //游戏结束

#define Myself_Shell_Count 3 //我方炮d的数量

typedef enum { UP,DOWN,left,RIGHT }Dir;

typedef struct

{

int lIEf; //声明

int x; //x坐标

int y; //y坐标

Dir dir; //方向

int belong; //属于

int speed; //速度

}Tank;

typedef struct

{

int x;

int y;

Dir dir;

int left;

int speed;

int belong;

int isshow; //是否显示

int num;

}SHELL;

//炮d节点

typedef struct

{

NODE node;

SHELL shell;

}SHELLNODE;

//***********声明全局变量

extern HANDLE g_hout;

extern char map[14][Game_Arr_WIDth];

extern char tank[4][3][3];

extern Tank MyselfTanke; //我方坦克

extern Tank EnemyTank[ENEMY_Tank]; //敌方坦克

extern char g_area_data[Gmae_Arr_Height][Game_Arr_WIDth];

extern linkList * g_shell_List;

extern char tankE[4][3][3];

extern char tankSpeed[4][3][3];

extern int g_self_shell_cout;

extern CRITICAL_SECTION g_cs;

extern int DIE_Enemy_Tank_Count;

extern int x;

extern int g_dIE_shellf;

extern int g_start;

extern int g_dIE_Enemy;

extern int music;

#endif

gamedata.c 下的文件

#include’‘tankdef.h’’

#include’‘interface.h’’

char map[14][Game_Arr_WIDth] = {

{ 0,1,0 },

{ 0,

{ 1,1 },0 }

};

char tank[4][3][3] = {

{

1,

1,1

},

{

1,

0,1

}

};

char tankE[4][3][3] = {

{

0,0

},

{

0,0

}

};

char tankSpeed[4][3][3] = {

{

0,3,

3,0

}

};

char g_area_data[Gmae_Arr_Height][Game_Arr_WIDth];

Tank MyselfTanke; //我方坦克

Tank EnemyTank[ENEMY_Tank]; //敌方坦克

//炮d链表

linkList * g_shell_List;

// 我放炮d的数量

int g_self_shell_cout = 0;

int DIE_Enemy_Tank_Count = 0; //The number of enemy tank deaths

int g_dIE_shellf = 0; // 敌方阵亡的坦克数量

CRITICAL_SECTION g_cs;

main.c 下的文件

#include ‘‘tankdef.h’’

#include ‘‘interface.h’’

#include

#include

#include ‘‘game.h’’

#include

#pragma comment(lib,’‘winmm.lib’’)

int main()

{

char ch;

HANDLE hThread;

SHELLNODE *psn = NulL;

Game_Start();

//PlaySound(TEXT(’'G:s2015坦克大战坦克大战soundBack.wa ‘’),SND_ASYNC | SND_LOOP | SND_filename);

/*PlaySound(TEXT(’'G:s2015坦克大战坦克大战hit.wa ‘’),SND_LOOP | SND_filename);

PlaySound(TEXT(’'G:s2015坦克大战坦克大战Bang.wa ‘’),SND_LOOP | SND_filename);

PlaySound(TEXT(’'G:s2015坦克大战坦克大战Fanfare.wa ‘’),SND_LOOP | SND_filename);*/

mciSendString(TEXT(’‘open soundBack.wa alias music’’),NulL);

mciSendString(TEXT(’‘play music’’),NulL);

while (1)

{

if (_kbhit())

{

ch = _getch();

if(ch == ‘p’)

while (1)

{

g_start = 1;

DrawGame_Info();

ch = _getch();

if (ch == ‘p’)

{

g_start = 0;

DrawGame_Info();

break;

}

}

switch (ch)

{

case ‘w’:

EnterCriticalSection(&g_cs);

if (MyselfTanke.dir != UP)

TankAdjustUp(&MyselfTanke,UP);

else

{

TankMoeUp(&MyselfTanke);

}

LeaeCriticalSection(&g_cs);

break;

case ‘s’:

EnterCriticalSection(&g_cs);

if (MyselfTanke.dir != DOWN)

TankAdjustDown(&MyselfTanke);

else

{

TankMoeDown(&MyselfTanke);

}

LeaeCriticalSection(&g_cs);

break;

case ‘a’:

EnterCriticalSection(&g_cs);

if (MyselfTanke.dir != left)

TankAdjustleft(&MyselfTanke);

else

{

TankMoeleft(&MyselfTanke);

}

LeaeCriticalSection(&g_cs);

break;

case ‘d’:

EnterCriticalSection(&g_cs);

if (MyselfTanke.dir != RIGHT)

TankAdjustRight(&MyselfTanke);

else

{

TankMoeRight(&MyselfTanke);

}

LeaeCriticalSection(&g_cs);

break;

case ‘j’:

if (g_self_shell_cout

{

PlaySound(TEXT(’'sound/hit.wa ‘’),SND_ASYNC | SND_NowAIT);

psn = shot(&MyselfTanke);

g_self_shell_cout;

hThread = CreateThread(NulL,NulL);

CloseHandle(hThread);

}

break;

}

}

}

mciSendString(TEXT(’‘close music’’),NulL);

while(CountOflinkList(g_shell_List))

{

EnterCriticalSection(&g_cs);

psn = (SHELLNODE *)GetNode(g_shell_List,0);

if (psn)

psn->shell.left = 0;

LeaeCriticalSection(&g_cs);

}

return 0;

}

总结

以上是内存溢出为你收集整理的C语言/C艹编程入门经典小游戏坦克大战!全部内容,希望文章能够帮你解决C语言/C艹编程入门经典小游戏坦克大战!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264571.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存