#include "stdafx.h"
#include <string.h>
#include <iostream>
#include <windows.h>
#include <list>
using namespace std;
struct Bullet
{
int row;//行
int col;//列
};
struct Enemy//敌人
{
int row;
int col;
int dir;//方向,左或者右
bool bIsLife;//是否是活的
};
int flyplaneRow = 19;
int flyplaneCol = 9;
char map[20][20];
list<Bullet*> g_Bullet;//为什么模板参数要写成指针?
Enemy g_Enemy = {0,9,-1,true};
void UpdataEnemy()
{
if (g_Enemy.bIsLife == false)
{
return;
}
if (l==0||l==19)
{
g_Enemy.dir=-g_Enemy.dir;//把方向取反
}
map[w][l] = ' ';
l+=g_Enemy.dir;
map[w][l] = '@';
}
void GameCtrl()//游戏的控制
{
static unsigned int nBulletTime = GetTickCount();
if (GetKeyState('A')&128)
{
if(flyplaneCol>0)
flyplaneCol--;
}
else if (GetKeyState('D')&128)
{
if(flyplaneCol<19)
flyplaneCol++;
}
else if (GetKeyState('W')&128)
{
if(flyplaneRow>0)
flyplaneRow--;
}
else if (GetKeyState('S')&128)
{
if(flyplaneRow<19)
flyplaneRow++;
}
if (GetTickCount()-nBulletTime>100&&GetKeyState('J')&128)
{
Bullet * pBullet = new Bullet;//这里为什么要new而不直接定义一个对象
pBullet->col = flyplaneCol;//列数和飞机的列数相等
pBullet->row = flyplaneRow;
g_Bullet.push_back(pBullet);//把子弹放入链表
nBulletTime=GetTickCount();
}
}
void UpdataBullet()//更新子弹
{
for (list<Bullet*>::iterator it = g_Bullet.begin();
it != d();)
{
map[(*it)->row][(*it)->col] = ' ';
if ((*it)->row>0)
{
(*it)->row--;//让子弹的行减1,子弹往上走
map[(*it)->row][(*it)->col] = '.';
}
else
{
it = ase(it);
continue;//结束本轮循环执行下一轮循环
}
it++;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
for (int j = 0 ;j < 20 ; j++)
{
for (int q = 0 ;q < 20; q++)
{
map[j][q] = ' ';
}
}
while(true)
{
system("cls");
map[flyplaneRow][flyplaneCol] = ' ';//首先把飞机所在的行和列变为空格
GameCtrl();//游戏的控制,决定飞机在什么位置
map[flyplaneRow][flyplaneCol] = '^';//把飞机所在的行列变为^
UpdataEnemy();//更新敌人
UpdataBullet();//更新子弹
for (int j = 0 ;j < 20 ; j++)
{
for (int q = 0 ;q < 20; q++)
{
cout<<map[j][q];
/
/map[j][q] = ' ';
}
免费游戏代码大全
cout<<endl;
}
}
return 0;
}