高级光能
代码坦克
#include <iostream>
#include <ctime>
#include <windows.h>
#define W 1 //上
#define S 2 //下
#define A 3 //左
#define D 4 //右
#define L 4 // 坦克有4条命
void HideCursor() { //隐藏光标
CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void GoToxy(int x, int y) { //光标移动,X、Y表示横、纵坐标
COORD coord = { x, y };
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//全局变量
int map[50][40];//地图二维数组
int B_num; //子弹编号
int Pos; //敌方坦克生成位置,-1为左边,0为中间,1为右边,2为我的坦克位置
int Speed = 7; //游戏速度
int Enemy; //还未出现的敌人
const char* Tank_Model[3][4] ={
{"◢┃ ◣", "◢╦ ◣", "◢╦◣", "◢╦◣"},
{"╠ █╣", "╠ █╣", "━█╣", "╠█━"},
{"◥╩ ◤", "◥┃ ◤", "◥╩◤", "◥╩◤"}
};
//坦克
class Tank{
public:
int x, y; //中心坐标
int Direction; //方向
int Model; //模型
int Revival; //复活次数
int Num; //敌方坦克编号
bool Type; //我方坦克此参数为1
bool Exist; //存活为1,不存活为0
}AI_tank[6], my_tank;
//子弹
class Bullet{
public:
int x, y; //坐标
int Direction; //方向
bool Exist; //1为存在,0不存在
bool Type; //0为敌方子弹,1为我方子弹
}bullet[50] ;
//基本函数
void GoToxy(int x, int y); //光标移动
void HideCursor(); //隐藏光标
void Key(); //键盘输入
void Init(); //初始化
void Pause(); //暂停
void Show(); //打印框架
void Print_Map(); //打印地图
void Cheak_Game(); //检测游戏胜负
void GameOver(); //游戏结束
//坦克
void Creat_AI_T(Tank* AI_tank); //建立坦克
void Creat_My_T(Tank* my_tank);
void Move_AI_T(Tank* AI_tank);//坦克移动
void Move_My_T(int turn);
void Clear_T(int x, int y); //清除坦克
void Print_T(Tank tank); //打印坦克
bool Cheak_T(Tank tank, int direction); //检测障碍,1阻碍
//子弹
void Creat_AI_B(Tank* tank); //敌方坦克发射子弹
void Creat_My_B(Tank tank);//我方坦克发射子弹
void Move_B(Bullet bullet[50]); //子弹移动
void Break_B(Bullet* bullet); //子弹碰撞
void Print_B(int x, int y);//打印子弹
void Clear_B(int x, int y); //清除子弹
int Cheak_B(int x, int y); //子弹前方情况
void Show() { //打印框架
std::cout << " ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁";
std::cout << "▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁\n";
for (int i = 0; i < 48; i++) {
std::cout << "▕ ▏\n";
}
std::cout << " ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔";
std::cout << "▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔\n";
}
void Print_Map() { // 打印地图
int Map[50][40] = {
//map里的值: 0为可通过陆地,1为砖,6为墙,100~105为敌方坦克,200为我的坦克,
{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,6,6,6,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
{ 4,1,1,1,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,1,1,1,4 },
{ 4,6,6,6,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,6,6,6,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,4,4,1,1,1,4,4,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,0,4,4,1,1,1,4,4,0,0,0,0,1,1,1,0,0,0,1,1,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,9,9,9,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,9,9,9,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,9,9,9,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4 },
{ 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 }
};
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
map[i][j] = Map[i][j];
for (int i = 0; i < 50; i++)
for (int j = 0; j < 40; j++)
if (map[i][j] == 1) {
GoToxy(2 * j, i);
std::cout << "▓";
}
else if (map[i][j] == 6) {
GoToxy(2 * j, i);
std::cout << "■";
}
GoToxy(38, 46);
std::cout << " ◣◢";
GoToxy(38, 47);
std::cout << "◣█ ◢";
GoToxy(38, 48);
std::cout << "◢█ ◣";
}
void Cheak_Game() {
//敌人坦克全部不存活
if (Enemy <= 0 && !AI_tank[0].Exist && !AI_tank[1].Exist && !AI_tank[2].Exist
&& !AI_tank[3].Exist && !AI_tank[4].Exist && !AI_tank[5].Exist)
GameOver();
if (my_tank.Revival >= L)//我复活次数用完
GameOver();//游戏结束
}
void GameOver() {
bool home = 1;
while (home) {
GoToxy(37, 21);
std::cout << "游戏结束!";
if (GetAsyncKeyState(0xD) & 0x8000) { //回车键
system("cls"); //清屏
Show();
Init(); //初始化
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
exit(0);
}
}
void Creat_My_T(Tank* my_tank) {//建立我的坦克
my_tank->x = 15;
my_tank->y = 47;
my_tank->Direction = 1;
// my_tank->Model = 0;
my_tank->Exist = 1;
my_tank->Type = 1;
Print_T(*my_tank); //打印我的坦克
}
void Move_My_T(int turn) {//turn为Key()函数传入的方向值
Clear_T(my_tank.x, my_tank.y);
my_tank.Direction = turn;
if (Cheak_T(my_tank, my_tank.Direction)) //我方坦克当前方向上无障碍
switch (turn) {
case W: my_tank.y--; break; //上
case S: my_tank.y++; break; //下
case A: my_tank.x--; break; //左
case D: my_tank.x++; break; //右
}
Print_T(my_tank);
}
void Print_T(Tank tank) {//打印
for (int i = 0; i < 3; i++) {
GoToxy((tank.x - 1) * 2, tank.y - 1 + i);//在坦克中心坐标的左边,上中下三行打印
std::cout << Tank_Model[i][tank.Direction - 1]; //打印的是地址,地址既字符串
for (int j = 0; j < 3; j++)
if (tank.Type)//若为我的坦克
map[tank.y + j - 1][tank.x + i - 1] = 200;
//在map上敌方值为100~105,我方为200
else
map[tank.y + j - 1][tank.x + i - 1] = 100 +tank.Num;
//这样可以通过map值读取坦克编号
}
}
void Creat_AI_T(Tank* AI_tank) {
AI_tank->x = 19 + 17 * (Pos); //pos为坦克生成位置,-1为左位置,0为中间,1为右,2为我的坦克位置
AI_tank->y = 2;
AI_tank->Direction = 2; //方向朝下
AI_tank->Revival++; //复活次数+1
AI_tank->Exist = 1;//存在
Pos++;
Enemy--;
if (Pos == 2) //循环重置(pos只能为-1,0,1)
Pos = -1;
Print_T(*AI_tank);
return;
}
void Move_AI_T(Tank* AI_tank) {
if (AI_tank->Exist) { //存在
Clear_T(AI_tank->x, AI_tank->y);
if (Cheak_T(*AI_tank, AI_tank->Direction))//前方无障碍
switch (AI_tank->Direction) {
case W: AI_tank->y--; break; //上
case S: AI_tank->y++; break; //下
case A: AI_tank->x--; break; //左
case D: AI_tank->x++; break; //右
}
else {//前方有障碍
for (int i = rand() % 4 + 1; i <= 4; i++)
if (Cheak_T(*AI_tank, i)){ //循环判断,返1可通过
AI_tank->Direction = i;
break;
}
}
Print_T(*AI_tank); //打印敌方坦克
}
}
bool Cheak_T(Tank tank, int direction) { //检测坦克前方障碍,返1为可通过
switch (direction) {
case W:
if (map[tank.y - 2][tank.x] == 0 && map[tank.y - 2][tank.x - 1] == 0 && map[tank.y - 2][tank.x + 1] == 0)
return 1;
else return 0;
case S:
if (map[tank.y + 2][tank.x] == 0 && map[tank.y + 2][tank.x - 1] == 0 && map[tank.y + 2][tank.x + 1] == 0)
return 1;
else return 0;
case A:
if (map[tank.y][tank.x - 2] == 0 && map[tank.y - 1][tank.x - 2] == 0 && map[tank.y + 1][tank.x - 2] == 0)
return 1;
else return 0;
case D:
if (map[tank.y][tank.x + 2] == 0 && map[tank.y - 1][tank.x + 2] == 0 && map[tank.y + 1][tank.x + 2] == 0)
return 1;
else return 0;
default: return 0;
}
}
void Clear_T(int x, int y) { //清除坦克
for (int i = 0; i <= 2; i++)
for (int j = 0; j <= 2; j++) {//将坦克占用的地图清零
map[y + j - 1][x + i - 1] = 0;
GoToxy(2 * x + 2 * j - 2, y + i - 1);
std::cout << " ";
}
}
//键盘输入
void Key() {
//上下左右键
if (GetAsyncKeyState('W') & 0x8000)
Move_My_T(W);
else if (GetAsyncKeyState('S') & 0x8000)
Move_My_T(S);
else if (GetAsyncKeyState('A') & 0x8000)
Move_My_T(A);
else if (GetAsyncKeyState('D') & 0x8000)
Move_My_T(D);
//子弹发射
else if (GetAsyncKeyState('P') & 0x8000) {
Creat_My_B(my_tank);
}
else if (GetAsyncKeyState(0x1B) & 0x8000)// Esc键退出
exit(0);
else if (GetAsyncKeyState(0x20) & 0x8000)//空格暂停
Pause();
}
void Pause() { //暂停
while (1) {
if (GetAsyncKeyState(0xD) & 0x8000) { //回车键继续
break;
}
else if (GetAsyncKeyState(0x1B) & 0x8000) //Esc键退出
exit(0);
}
}
void Creat_AI_B(Tank* tank){ //敌方发射子弹
if (!(rand() % 10)) { //在随后的每个游戏周期中有10分之一的可能发射子弹
Creat_My_B(*tank);
}
}
void Creat_My_B(Tank tank) {
switch (tank.Direction)
{
case W:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y - 2;
bullet[B_num].Direction = 1;//1表示向上
break;
case S:
bullet[B_num].x = tank.x;
bullet[B_num].y = tank.y + 2;
bullet[B_num].Direction = 2;//2表示向下
break;
case A:
bullet[B_num].x = tank.x - 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 3;//3表示向左
break;
case D:
bullet[B_num].x = tank.x + 2;
bullet[B_num].y = tank.y;
bullet[B_num].Direction = 4;//4表示向右
break;
}
bullet[B_num].Exist = 1; //子弹存在
bullet[B_num].Type = tank.Type; //我方坦克发射的子弹bullet.Type=1
B_num++;
if (B_num == 50) //如果子弹编号增长到50号,那么重头开始编号
B_num = 0; //考虑到地图上不可能同时存在50颗子弹,所以数组元素设置50个
}
void Move_B(Bullet bullet[50]) { //子弹移动
for (int i = 0; i < 50; i++) {
if (bullet[i].Exist) {//如果子弹存在
if (map[bullet[i].y][bullet[i].x] == 0) {
Clear_B(bullet[i].x, bullet[i].y);//子弹当前位置无障碍,抹除子弹图形
switch (bullet[i].Direction) {//子弹变到下一个坐标
case W:(bullet[i].y)--; break;
case S:(bullet[i].y)++; break;
case A:(bullet[i].x)--; break;
case D:(bullet[i].x)++; break;
}
}
//判断子弹当前位置情况
if (map[bullet[i].y][bullet[i].x] == 0) //子弹坐标无障碍
Print_B(bullet[i].x, bullet[i].y);//打印
else Break_B(&bullet[i]); //子弹碰撞
for (int j = 0; j < 50; j++)
//子弹间的碰撞判断,若是我方子弹和敌方子弹碰撞则都删除,若为两敌方子弹则无视
if (bullet[j].Exist && j != i && (bullet[i].Type || bullet[j].Type)
&& bullet[i].x == bullet[j].x && bullet[i].y == bullet[j].y)
{ //同样的两颗我方子弹不可能产生碰撞
bullet[j].Exist = 0;
bullet[i].Exist = 0;
Clear_B(bullet[j].x, bullet[j].y);
break;
}
}
}
}
void Break_B(Bullet* bullet) {
int x = bullet->x;
int y = bullet->y; //子弹坐标
int i;
if (map[y][x] == 1) { //子弹碰到砖块
if (bullet->Direction == A || bullet->Direction == D)
//若子弹是横向的
for (i = -1; i <= 1; i++)
if (map[y + i][x] == 1) {
map[y + i][x] = 0;
GoToxy(2 * x, y + i);
std::cout << " ";
}
if (bullet->Direction == W || bullet->Direction == S) //子弹是向上或是向下移动的
for (i = -1; i <= 1; i++)
if (map[y][x + i] == 1) { //如果子弹打中砖块两旁为砖块,则删除砖,若不是则忽略
map[y][x + i] = 0; //砖块碎
GoToxy(2 * (x + i), y);
std::cout << " ";
}
bullet->Exist = 0; //子弹不存在
}
else if (map[y][x] == 4 || map[y][x] == 6) //子弹碰到边框或者不可摧毁方块
bullet->Exist = 0;
else if (bullet->Type ==1 && map[y][x] >= 100 && map[y][x] <= 105) { //我方子弹碰到了敌方坦克
AI_tank[(int)map[y][x] % 100].Exist = 0;
bullet->Exist = 0;
Clear_T(AI_tank[(int)map[y][x] % 100].x, AI_tank[(int)map[y][x] % 100].y); //清除坦克
}
else if (bullet->Type == 0 && map[y][x] == 200) { //若敌方子弹击中我的坦克
my_tank.Exist = 0;
bullet->Exist = 0;
Clear_T(my_tank.x, my_tank.y);
my_tank.Revival++; //我方坦克复活次数加1
}
else if (map[y][x] == 9) { //子弹碰到巢
bullet->Exist = 0;
GoToxy(38, 46); std::cout << " ";
GoToxy(38, 47); std::cout << " ";
GoToxy(38, 48); std::cout << "◢◣ ";
GameOver();
}
}
int Cheak_B(int x, int y) {//子弹当前位置情况
if (map[y][x] == 0)
return 1;
else
return 0;
}
void Print_B(int x, int y){
GoToxy(2 * x, y);
std::cout << "o";
}
void Clear_B(int x, int y){
GoToxy(2 * x, y);
if (Cheak_B(x, y) == 1) {//子弹当前坐标在空地上
std::cout << " ";
}
}
void Init() { //初始化
Enemy = 24;
my_tank.Revival = 0; //我的坦克复活次数为0
Pos = 0;
B_num = 0;
Print_Map();
Creat_My_T(&my_tank);
for (int i = 0; i < 50; i++) {//子弹
bullet[i].Exist = 0;
}
for (int i = 0; i <= 5; i++) {//敌方坦克
AI_tank[i].Revival = 0;
AI_tank[i].Exist = 0; //初始化坦克全是不存活的,用Creat_AI_T()建立不存活的坦克
AI_tank[i].Num = i;
AI_tank[i].Type = 0;
}
}
int main() {
int i;
int gap[16] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }; //间隔数组,用于控制速度
HideCursor(); //隐藏光标
Show(); //打印框架
Init(); //初始化
while(1) {
if (gap[0]++ % Speed == 0) {
//速度调整,
Cheak_Game(); //游戏胜负检测
for (i = 0; i <= 5; i++) {//敌方坦克移动循环
if (gap[i + 7]++ % 3 == 0)
Move_AI_T(&AI_tank[i]);
}
for (i = 0; i <= 5; i++)//建立敌方坦克
if (AI_tank[i].Exist == 0 && AI_tank[i].Revival < 4 && gap[i+1]++ % 50 == 0) { //一个敌方坦克每局只有4条命
//坦克**掉后间隔一段时间建立
Creat_AI_T(&AI_tank[i]);
break;
}
for (i = 0; i <= 5; i++)
if (AI_tank[i].Exist)
Creat_AI_B(&AI_tank[i]);
if (my_tank.Exist && gap[14]++ % 2 == 0)
Key();
if (my_tank.Exist == 0 && my_tank.Revival < L && gap[15]++ % 15 == 0)//我方坦克复活
Creat_My_T(&my_tank);
Move_B(bullet);
}
Sleep(5);
}
return 0;
}
贪吃蛇
#include <iostream>
#include <string>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
int food[2] = { 9, 9 };//初始食物坐标
int snake[1000][2];//蛇身坐标
int length = 1;//初始蛇长
int headX, headY;//蛇头坐标
int speed = 500;//游戏难度
int score = 0;//分数
int level = 1;//难度等级
string name;//玩家姓名
void gotoxy(short x, short y);//移动光标
int setdirection(int x);//确定方向变量
void changesnake(int x);//改变蛇身坐标
void ifchangefood();//判断蛇是否吃到食物
void makefood();//创造新食物
bool judgelife();//判断蛇是否存活
void drawsnake();//画蛇
void drawfood();//画食物
void drawwall();//画墙
void drawscore();//画数据
void draw();//绘图
int main()
{
SetConsoleTitle("贪吃蛇游戏");
int po = 2;//初始方向变量
snake[0][0] = 7;
snake[0][1] = 7;//初始蛇头坐标
headX = snake[0][0];
headY = snake[0][1];
gotoxy(30, 7);
cout << "欢迎来到贪吃蛇游戏";
gotoxy(30, 9);
cout << "作者:陈泽睿";
gotoxy(30, 11);
cout << "请输入你的姓名:";
cin >> name;
system("cls");
gotoxy(30, 7);
cout << "游戏控制方式:";
gotoxy(30, 9);
cout << "W键:向上 S键:向下";
gotoxy(30, 11);
cout << "A键:向左 D键:向右";
gotoxy(30, 13);
cout << "空格键:暂停";
gotoxy(30, 15);
cout << "将游戏窗口最大化之后";
gotoxy(30, 17);
cout << "按回车键开始游戏...";
cin.get();
cin.get();
system("cls");
while (true)
{
po = setdirection(po);
system("cls");
changesnake(po);
ifchangefood();
if (!judgelife())
break;
draw();
Sleep(speed);
}
gotoxy(30, 10);
cout << "Game Over!!!";
Sleep(2000);
gotoxy(28, 12);
system("pause");
return 0;
}
void gotoxy(short x, short y)
{
COORD position = { x, y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOut, position);
}
int setdirection(int x)
{
char ch;
if (_kbhit())
{
ch = _getch();
switch (ch)
{
case 'w':
x = 1;
break;
case 's':
x = 2;
break;
case 'a':
x = 3;
break;
case 'd':
x = 4;
break;
case ' ':
gotoxy(37, 16);
cout << "游 戏 暂 停. . .";
gotoxy(37, 18);
system("pause");
break;
default:
break;
}
}
return x;
}
void changesnake(int x)
{
switch (x)
{
case 1:
headY -= 1;
break;
case 2:
headY += 1;
break;
case 3:
headX -= 1;
break;
case 4:
headX += 1;
break;
default:
break;
}
for (int i = length; i > 0; --i)
{
for (int j = 0; j < 2; ++j)
{
snake[i][j] = snake[i - 1][j];
}
}
snake[0][0] = headX;
snake[0][1] = headY;
}
void ifchangefood()
{
if (snake[0][0] == food[0] && snake[0][1] == food[1])
{
length++;
makefood();
++score;
if (length > 5)
{
speed = 450;
level = 2;
}
if (length > 10)
{
speed = 400;
level = 3;
}
if (length > 15)
{
speed = 350;
level = 4;
}
if (length > 20)
{
speed = 300;
level = 5;
}
if (length > 25)
{
speed = 250;
level = 6;
}
if (length > 30)
{
speed = 200;
level = 7;
}
if (length > 35)
{
speed = 150;
level = 8;
}
if (length > 40)
{
speed = 100;
level = 9;
}
if (length > 45)
{
speed = 50;
level = 10;
}
}
}
void makefood()
{
srand((unsigned)time(NULL));
food[0] = rand() % 30 + 2;
food[1] = rand() % 30 + 4;
for (int m = 0; m < length; ++m)
{
if (food[0] == snake[m][0] && food[1] == snake[m][1])
{
makefood();
break;
}
}
}
bool judgelife()
{
for (int x = 1; x < length; ++x)
{
if (headX == snake[x][0] && headY == snake[x][1])
{
return false;
}
}
if (headX < 1 || headY < 3 || headX > 34 || headY > 34)
return false;
else
return true;
}
void drawsnake()
{
gotoxy(snake[0][0], snake[0][1]);
cout << "@";
for (int n = 1; n < length; ++n)
{
gotoxy(snake[n][0], snake[n][1]);
cout << "#";
}
}
void drawfood()
{
gotoxy(food[0], food[1]);
cout << "$";
}
void drawwall()
{
gotoxy(0, 0);
cout << "---------------------------------------";
gotoxy(16, 1);
cout << "贪吃蛇";
gotoxy(0, 2);
cout << "++++++++++++++++++++++++++++++++++++";
gotoxy(0, 35);
cout << "-------------------------------------------------------------------------------------";
for (int x = 0; x < 35; ++x)
{
gotoxy(0, x);
cout << "|";
gotoxy(35, x);
cout << "|";
}
}
void drawscore()
{
gotoxy(37, 10);
cout << "分数:" << score;
gotoxy(37, 12);
cout << "等级:" << level;
gotoxy(37, 14);
cout << "玩家姓名:" << name;
}
void draw()
{
drawsnake();
drawfood();
drawwall();
drawscore();
}
**方块
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
using namespace std;
int block00[4][4] = { { 10,0,0,0 },{ 1,1,1,1 },{ 0,0,0,0 },{ 0,0,0,0 } };
int block01[4][4] = { { 11,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,1,0 } };
int block02[4][4] = { { 12,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,1,0,0 } };
int block03[4][4] = { { 13,0,0,0 },{ 0,1,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block04[4][4] = { { 14,0,0,0 },{ 0,0,0,0 },{ 0,1,0,0 },{ 1,1,1,0 } };
int block05[4][4] = { { 15,0,0,0 },{ 0,1,0,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block06[4][4] = { { 16,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 1,0,0,0 } };
int block07[4][4] = { { 17,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block08[4][4] = { { 18,0,0,0 },{ 0,0,0,0 },{ 0,0,1,0 },{ 1,1,1,0 } };
int block09[4][4] = { { 19,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 0,1,1,0 } };
int block10[4][4] = { { 20,0,0,0 },{ 0,0,0,0 },{ 1,1,1,0 },{ 0,0,1,0 } };
int block11[4][4] = { { 21,0,0,0 },{ 0,1,0,0 },{ 0,1,0,0 },{ 1,1,0,0 } };
int block12[4][4] = { { 22,0,0,0 },{ 0,0,0,0 },{ 1,0,0,0 },{ 1,1,1,0 } };
int block13[4][4] = { { 23,0,0,0 },{ 0,1,1,0 },{ 0,1,0,0 },{ 0,1,0,0 } };
int block14[4][4] = { { 24,0,0,0 },{ 0,0,0,0 },{ 0,1,1,0 },{ 1,1,0,0 } };
int block15[4][4] = { { 25,0,0,0 },{ 1,0,0,0 },{ 1,1,0,0 },{ 0,1,0,0 } };
int block16[4][4] = { { 26,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 0,1,1,0 } };
int block17[4][4] = { { 27,0,0,0 },{ 0,0,1,0 },{ 0,1,1,0 },{ 0,1,0,0 } };
int block18[4][4] = { { 28,0,0,0 },{ 0,0,0,0 },{ 1,1,0,0 },{ 1,1,0,0 } };
void initialWindow(HANDLE hOut);//初始化窗口
void initialPrint(HANDLE hOut);//初始化界面
void gotoXY(HANDLE hOut, int x, int y);//移动光标
void roundBlock(HANDLE hOut, int block[4][4]);//随机生成方块并打印到下一个方块位置
bool collisionDetection(int block[4][4], int map[21][12], int x, int y);//检测碰撞
void printBlock(HANDLE hOut, int block[4][4], int x, int y);//打印方块
void clearBlock(HANDLE hOut, int block[4][4], int x, int y);//消除方块
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//左移
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//右移
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y);//顺时针旋转90度
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y);//加速下落
void myStop(HANDLE hOut, int block[4][4]);//游戏暂停
void gameOver(HANDLE hOut, int block[4][4], int map[21][12]);//游戏结束
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint);//判断是否能消行并更新分值
int main()
{
int map[21][12];
int blockA[4][4];//候选区的方块
int blockB[4][4];//下落中的方块
int positionX, positionY;//方块左上角的坐标
bool check;//**方块还能不能下落
char key;//用来存储按键
int val;//用来控制下落速度
int fraction;//用来存储得分
int checkpoint;//用来存储关卡
int times;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
initialWindow(hOut);
initial:
gotoXY(hOut, 0, 0);
initialPrint(hOut);
check = true;
val = 50;
fraction = 0;
checkpoint = 1;
times = val;
for (int i = 0; i < 20; ++i)
{
for (int j = 1; j < 11; ++j)
{
map[i][j] = 0;
}
}
for (int i = 0; i < 20; ++i)
{
map[i][0] = map[i][11] = 1;
}
for (int i = 0; i < 12; ++i)
{
map[20][i] = 1;
}
srand((unsigned)time(NULL));
roundBlock(hOut, blockA);
while (true)
{
if (check)
{
eliminateRow(hOut, map, val, fraction, checkpoint);
check = false;
positionX = -3;
positionY = 4;
if (collisionDetection(blockA, map, positionX, positionY))
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
blockB[i][j] = blockA[i][j];
}
}
roundBlock(hOut, blockA);
}
else
{
gameOver(hOut, blockA, map);
goto initial;
}
}
printBlock(hOut, blockB, positionX, positionY);
if (_kbhit())
{
key = _getch();
switch (key)
{
case 72:
myUp(hOut, blockB, map, positionX, positionY);
break;
case 75:
myLeft(hOut, blockB, map, positionX, positionY);
break;
case 77:
myRight(hOut, blockB, map, positionX, positionY);
break;
case 80:
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
break;
case 32:
myStop(hOut, blockA);
break;
case 27:
exit(0);
default:
break;
}
}
Sleep(20);
if (0 == --times)
{
switch (myDown(hOut, blockB, map, positionX, positionY))
{
case 0:
check = false;
break;
case 1:
check = true;
break;
case 2:
gameOver(hOut, blockB, map);
goto initial;
default:
break;
}
times = val;
}
}
cin.get();
return 0;
}
void initialWindow(HANDLE hOut)
{
SetConsoleTitle("**方块");
COORD size = { 80, 25 };
SetConsoleScreenBufferSize(hOut, size);
SMALL_RECT rc = { 0, 0, 79, 24 };
SetConsoleWindowInfo(hOut, true, &rc);
CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };
SetConsoleCursorInfo(hOut, &cursor_info);
}
void initialPrint(HANDLE hOut)
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
for (int i = 0; i < 20; ++i)
{
cout << "■ ■☆ ☆" << endl;
}
gotoXY(hOut, 26, 0);
cout << "☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 0, 20);
cout << "■■■■■■■■■■■■☆☆☆☆☆☆☆☆☆☆☆☆☆";
gotoXY(hOut, 26, 1);
cout << "分 数: ";
gotoXY(hOut, 26, 2);
cout << "关 卡: ";
gotoXY(hOut, 26, 4);
cout << "下一方块:";
gotoXY(hOut, 26, 9);
cout << "操作方法:";
gotoXY(hOut, 30, 11);
cout << "↑:旋转 ↓:速降";
gotoXY(hOut, 30, 12);
cout << "→:右移 ←:左移";
gotoXY(hOut, 30, 13);
cout << "空格键:开始/暂停";
gotoXY(hOut, 30, 14);
cout << "Esc 键:退出";
gotoXY(hOut, 26, 16);
cout << "关 于:";
gotoXY(hOut, 30, 18);
cout << "**方块";
gotoXY(hOut, 35, 19);
cout << "作者:陈泽睿";
}
void gotoXY(HANDLE hOut, int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(hOut, pos);
}
void roundBlock(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
switch (rand() % 19)
{
case 0:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
break;
case 1:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
break;
case 2:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
break;
case 3:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
break;
case 4:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
break;
case 5:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
break;
case 6:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
break;
case 7:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
break;
case 8:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
break;
case 9:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
break;
case 10:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
break;
case 11:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
break;
case 12:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
break;
case 13:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
break;
case 14:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
break;
case 15:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
break;
case 16:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
break;
case 17:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
break;
case 18:
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block18[i][j];
}
}
break;
default:
break;
}
printBlock(hOut, block, 5, 15);
}
bool collisionDetection(int block[4][4], int map[21][12], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (x + i >= 0 && y + j >= 0 && map[x + i][y + j] == 1 && block[i][j] == 1)
{
return false;
}
}
}
return true;
}
void printBlock(HANDLE hOut, int block[4][4], int x, int y)
{
switch (block[0][0])
{
case 10:
case 11:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN);
break;
case 12:
case 13:
case 14:
case 15:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 16:
case 17:
case 18:
case 19:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 20:
case 21:
case 22:
case 23:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 24:
case 25:
SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
break;
case 26:
case 27:
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
break;
case 28:
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
break;
default:
break;
}
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
}
}
void clearBlock(HANDLE hOut, int block[4][4], int x, int y)
{
for (int i = 0; i < 4; ++i)
{
if (i + x >= 0)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
gotoXY(hOut, 2 * (y + j), x + i);
cout << " ";
}
}
}
}
}
void gameOver(HANDLE hOut, int block[4][4], int map[21][12])
{
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 9, 8);
cout << "** OVER";
gotoXY(hOut, 8, 9);
cout << "空格键:重来";
gotoXY(hOut, 8, 10);
cout << "ESC键:退出";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
return;
}
if (key == 27)
{
exit(0);
}
}
}
int myDown(HANDLE hOut, int block[4][4], int map[21][12], int &x, int y)
{
if (collisionDetection(block, map, x + 1, y))
{
clearBlock(hOut, block, x, y);
++x;
return 0;
}
if (x < 0)
{
return 2;
}
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (block[i][j] == 1)
{
map[x + i][y + j] = 1;
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 2 * (y + j), x + i);
cout << "■";
}
}
}
return 1;
}
void myLeft(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
--y;
}
}
void myRight(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
if (collisionDetection(block, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
++y;
}
}
void myUp(HANDLE hOut, int block[4][4], int map[21][12], int x, int &y)
{
switch (block[0][0])
{
case 10:
if (collisionDetection(block01, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block01[i][j];
}
}
}
break;
case 11:
if (collisionDetection(block00, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
}
else if (collisionDetection(block00, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
--y;
}
else if (collisionDetection(block00, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
++y;
}
else if (collisionDetection(block00, map, x, y - 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y - 2;
}
else if (collisionDetection(block00, map, x, y + 2))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block00[i][j];
}
}
y = y + 2;
}
break;
case 12:
if (collisionDetection(block03, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
}
else if (collisionDetection(block03, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
--y;
}
else if (collisionDetection(block03, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block03[i][j];
}
}
++y;
}
break;
case 13:
if (collisionDetection(block04, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
}
else if (collisionDetection(block04, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
--y;
}
else if (collisionDetection(block04, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block04[i][j];
}
}
++y;
}
break;
case 14:
if (collisionDetection(block05, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
}
else if (collisionDetection(block05, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
--y;
}
else if (collisionDetection(block05, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block05[i][j];
}
}
++y;
}
break;
case 15:
if (collisionDetection(block02, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
}
else if (collisionDetection(block02, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
--y;
}
else if (collisionDetection(block02, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block02[i][j];
}
}
++y;
}
break;
case 16:
if (collisionDetection(block07, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
}
else if (collisionDetection(block07, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
--y;
}
else if (collisionDetection(block07, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block07[i][j];
}
}
++y;
}
break;
case 17:
if (collisionDetection(block08, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
}
else if (collisionDetection(block08, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
--y;
}
else if (collisionDetection(block08, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block08[i][j];
}
}
++y;
}
break;
case 18:
if (collisionDetection(block09, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
}
else if (collisionDetection(block09, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
--y;
}
else if (collisionDetection(block09, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block09[i][j];
}
}
++y;
}
break;
case 19:
if (collisionDetection(block06, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
}
else if (collisionDetection(block06, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
--y;
}
else if (collisionDetection(block06, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block06[i][j];
}
}
++y;
}
break;
case 20:
if (collisionDetection(block11, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
}
else if (collisionDetection(block11, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
--y;
}
else if (collisionDetection(block11, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block11[i][j];
}
}
++y;
}
break;
case 21:
if (collisionDetection(block12, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
}
else if (collisionDetection(block12, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
--y;
}
else if (collisionDetection(block12, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block12[i][j];
}
}
++y;
}
break;
case 22:
if (collisionDetection(block13, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
}
else if (collisionDetection(block13, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
--y;
}
else if (collisionDetection(block13, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block13[i][j];
}
}
++y;
}
break;
case 23:
if (collisionDetection(block10, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
}
else if (collisionDetection(block10, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
--y;
}
else if (collisionDetection(block10, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block10[i][j];
}
}
++y;
}
break;
case 24:
if (collisionDetection(block15, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
}
else if (collisionDetection(block15, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
--y;
}
else if (collisionDetection(block15, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block15[i][j];
}
}
++y;
}
break;
case 25:
if (collisionDetection(block14, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
}
else if (collisionDetection(block14, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
--y;
}
else if (collisionDetection(block14, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block14[i][j];
}
}
++y;
}
break;
case 26:
if (collisionDetection(block17, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
}
else if (collisionDetection(block17, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
--y;
}
else if (collisionDetection(block17, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block17[i][j];
}
}
++y;
}
break;
case 27:
if (collisionDetection(block16, map, x, y))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
}
else if (collisionDetection(block16, map, x, y - 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
--y;
}
else if (collisionDetection(block16, map, x, y + 1))
{
clearBlock(hOut, block, x, y);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 4; ++j)
{
block[i][j] = block16[i][j];
}
}
++y;
}
break;
default:
break;
}
}
void myStop(HANDLE hOut, int block[4][4])
{
clearBlock(hOut, block, 5, 15);
SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
gotoXY(hOut, 30, 7);
cout << "游戏暂停";
char key;
while (true)
{
key = _getch();
if (key == 32)
{
gotoXY(hOut, 30, 7);
cout << " ";
printBlock(hOut, block, 5, 15);
return;
}
if (key == 27)
{
exit(0);
}
}
}
void eliminateRow(HANDLE hOut, int map[21][12], int &val, int &fraction, int &checkpoint)
{
SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
for (int i = 19; i >= 0; --i)
{
int x = 0;
for (int j = 1; j < 11; ++j)
{
x += map[i][j];
}
if (x == 10)
{
fraction += 100;
if (val > 1 && fraction / 1000 + 1 != checkpoint)
{
checkpoint = fraction / 1000 + 1;
val -= 5;
}
for (int m = i; m > 0; --m)
{
for (int n = 1; n < 11; ++n)
{
map[m][n] = map[m - 1][n];
gotoXY(hOut, 2 * n, m);
if (map[m][n] == 1)
{
cout << "■";
}
else
{
cout << " ";
}
}
}
++i;
}
}
gotoXY(hOut, 36, 1);
cout << fraction;
gotoXY(hOut, 36, 2);
cout << checkpoint;
}
飞机大战
#include<iostream>
#include<cstring>
#include<windows.h>
#include<cmath>
#include<ctime>
#include<conio.h>
#include<cstdio>
#include<cstdlib>
#define WM_MYMSG WM_USER+100 //线程消息
using namespace std;
const char space=' ';
const int TOP=15;
const int LEFT=50;
const int X=0; // 光标默认横坐标
const int Y=4; // 光标默认纵坐标
int randomFlag = 1; //随机数标志
char arr[TOP][LEFT]; //地图
int gwArrTop[TOP]; //怪物纵坐标
int gwArrLeft[TOP]; //怪物的横坐标
int gwTot; //怪物数
int fjTOP; //飞机的纵坐标
int fjLeft; //飞机的横坐标
int Score=0; //得分
int Life=100;//生命
int oldLife=100;//旧的生命
int oldScore=0; //得分
extern "C"
{
WINBASEAPI HWND WINAPI GetConsoleWindow();
}
void SetPosition(int x,int y) //设置光标位置 (列,行)
{
HANDLE winHandle;//句柄
COORD pos = {x,y};
winHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//设置光标位置
SetConsoleCursorPosition(winHandle,pos);
}
int sjs(int a, int b) { //返回[x,y]的随机数
if (randomFlag) {
randomFlag = 0;
srand((unsigned int)time(NULL));
}
return (int)((double)rand() / ((RAND_MAX + 1.0) / (b - a + 1.0)) + a);
}
void print(){ //打印地图 -边界内的
SetPosition(X,Y);
gwTot=0;//怪物统计
int gw_i=0,gw_j=0;
for(int i=0;i<TOP;i++){
for(int j=0;j<LEFT;j++){
//计算
if(arr[i][j]=='N'){ //击中怪物
if(arr[i-1][j]=='Y'){
Score++;
arr[i-1][j]=space;
gwTot--;
}
}
if(arr[i][j]=='*'){ // arr[t][l]
if(arr[i+1][j]=='1')
Life--;
}else if(arr[i][j]=='O'){ //右斜线
if(arr[i+1][j+1]=='1')
Life--;
}else if(arr[i][j]=='0'){
if(arr[i+1][j-1]=='1')
Life--;
}else if(arr[i][j]=='Y'){
if(arr[i+1][j]=='1'||arr[i+1][j+1]=='1'||arr[i+1][j-1]=='1'||arr[i][j+1]=='1'||arr[i][j-1]=='1'){
arr[i][j]=space;
Life--;
}
}
//颜色控制
if(arr[i][j]=='Y'){
//红字黑底
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED);
gwArrTop[gw_i++]=i; //怪物坐标
gwArrLeft[gw_j++]=j;
gwTot++; //怪物数统计
cout<<arr[i][j];
}else if(arr[i][j]=='O'||arr[i][j]=='0'){
//蓝色黑底
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_BLUE);
cout<<arr[i][j];
}else if(arr[i][j]=='*'){
//黄字黑底
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN);
cout<<arr[i][j];
}
else{
//白色黑底
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
cout<<arr[i][j];
}
}
cout<<endl;
}
}
void print_border(){ //打印地图边界
SetPosition(0,0);
cout<<"得分:"<<Score<<"\t"<<"剩余生命值:"<<Life<<endl;
SetPosition(X,Y-1);
cout<<"--------------------------------------------------"<<endl;
SetPosition(X,Y);
for(int i=0;i<TOP+2;i++){
for(int j=0;j<LEFT+1;j++){
//边界
if(i==TOP||j==LEFT){
cout<<'-';
continue;
}
if(i==TOP+1){
cout<<"说明:上下左右键移动,回车键攻击"<<endl;
break;
}
cout<<arr[i][j];
}
cout<<endl;
}
}
void FJ(){ //飞机坐标初始化
int FJ_TOP=14;
int FJ_LEFT=24;
fjTOP=FJ_TOP;
fjLeft=FJ_LEFT;
arr[FJ_TOP][FJ_LEFT]='1';
arr[FJ_TOP-1][FJ_LEFT]='1';
arr[FJ_TOP][FJ_LEFT+1]='1';
arr[FJ_TOP][FJ_LEFT-1]='1';
}
void GW(){ //随机怪物初始化
int tot=sjs(1,4); //随机一到四个个怪物
int gwTOP;
int gwLEFT;
gwTot=tot;
for(int i=0;i<tot;i++){
gwTOP=sjs(0,5);
gwLEFT=sjs(0,48);
gwArrTop[i]=gwTOP;
gwArrLeft[i]=gwLEFT;
arr[gwTOP][gwLEFT]='Y';
}
}
int gwAct(){ //怪物随机动
for(int i=0;i<gwTot;i++){
int t1=gwArrTop[i]; //TOP
int l1=gwArrLeft[i]; //Left
arr[t1][l1]=space;
int t=sjs(1,5);
switch(t){
case 1:
if(t1<TOP-1){
t1+=1;
break;
}
case 2:
if(l1<LEFT-1){
l1+=1;
break;
}
case 3:
if(l1>0){
l1-=1;
break;
}
case 4:
if(t1<TOP-1&&l1<LEFT-1){
t1++;
l1++;
break;
}
case 5:
if(t1<TOP-1&&l1>0){
t1++;
l1--;
break;
}
default:
if(t1<TOP-1){
t1+=1;}
break;
}
gwArrTop[i]=t1;
gwArrLeft[i]=l1;
arr[t1][l1]='Y';
//怪物移动到边界的处理
if(t1>=TOP-2||l1<=0||l1==LEFT-1){
arr[t1][l1]=space;
gwTot--;
}
}
}
int input(){ //处理键盘输入
int ch;
ch=getch();
switch(ch)
{
case 72:
arr[fjTOP][fjLeft]=space;
arr[fjTOP-1][fjLeft]=space;
arr[fjTOP][fjLeft-1]=space;
arr[fjTOP][fjLeft+1]=space;
fjTOP-=1;
arr[fjTOP][fjLeft]='1';
arr[fjTOP-1][fjLeft]='1';
arr[fjTOP][fjLeft-1]='1';
arr[fjTOP][fjLeft+1]='1';
break;//上
case 80:
arr[fjTOP][fjLeft]=space;
arr[fjTOP-1][fjLeft]=space;
arr[fjTOP][fjLeft-1]=space;
arr[fjTOP][fjLeft+1]=space;
fjTOP+=1;
arr[fjTOP][fjLeft]='1';
arr[fjTOP-1][fjLeft]='1';
arr[fjTOP][fjLeft-1]='1';
arr[fjTOP][fjLeft+1]='1';
break;//下
case 75:
arr[fjTOP][fjLeft]=space;
arr[fjTOP-1][fjLeft]=space;
arr[fjTOP][fjLeft-1]=space;
arr[fjTOP][fjLeft+1]=space;
fjLeft-=1;
arr[fjTOP][fjLeft]='1';
arr[fjTOP-1][fjLeft]='1';
arr[fjTOP][fjLeft-1]='1';
arr[fjTOP][fjLeft+1]='1';
break;//左
case 77:
arr[fjTOP][fjLeft]=space;
arr[fjTOP-1][fjLeft]=space;
arr[fjTOP][fjLeft-1]=space;
arr[fjTOP][fjLeft+1]=space;
fjLeft+=1;
arr[fjTOP][fjLeft]='1';
arr[fjTOP-1][fjLeft]='1';
arr[fjTOP][fjLeft-1]='1';
arr[fjTOP][fjLeft+1]='1';
break;//右
case 13:
arr[fjTOP-2][fjLeft]='|';
break;//enter 飞机子弹
default:
return 0;
}
return 1;
}
void sczd(){ //生成随机子弹
for(int i=0;i<gwTot;i++){
int t1=gwArrTop[i]; //TOP
int l1=gwArrLeft[i]; //Left
int a=sjs(1,7);
switch(a){ //随机子弹
case 1:
arr[t1+1][l1]='*'; //直线
break;
case 2:arr[t1+1][l1+1]='O'; //右斜线
break;
case 3:arr[t1+1][l1-1]='0'; //左斜线
break;
case 4:arr[t1+1][l1]='*'; //直线
arr[t1+1][l1+1]='O'; //右斜线
break;
case 5:arr[t1+1][l1]='*'; //直线
arr[t1+1][l1-1]='0'; //左斜线
break;
case 6:arr[t1+1][l1+1]='O'; //右斜线
arr[t1+1][l1-1]='0'; //左斜线
break;
case 7:arr[t1+1][l1]='*'; //直线
arr[t1+1][l1+1]='O'; //右斜线
arr[t1+1][l1-1]='0'; //左斜线
break;
default:
arr[t1+1][l1]='*'; //直线
break;
}
}
}
void zddel(){ //子弹处理
for(int i=TOP-1;i>=0;i--){
for(int j=LEFT-1;j>=0;j--){
if(arr[i][j]=='*'&&arr[i+1][j]!='Y'){ //怪物直线子弹
if(i<TOP-1&&j>0&&j<LEFT-1){
arr[i][j]=space;
arr[i+1][j]='*';
}else{ //子弹消失
arr[i][j]=space;
}
}else if(arr[i][j]=='O'&&arr[i+1][j+1]!='Y'){ //右斜线子弹
if(i<TOP-1&&j<LEFT-1){
arr[i][j]=space;
arr[i+1][j+1]='O';
}else{ //子弹消失
arr[i][j]=space;
}
}else if(arr[i][j]=='0'&&arr[i+1][j]!='Y') //左斜线子弹
{
if(i<TOP-1&&j>0){
arr[i][j]=space;
arr[i+1][j-1]='0';
}else{ //子弹消失
arr[i][j]=space;
}
}
}
}
for(int i=0;i<TOP;i++){
for(int j=0;j<LEFT;j++){
if(arr[i][j]=='N'){ //飞机子弹
if(i>1&&j>1&&j<LEFT-2){
arr[i][j]=space;
arr[i-1][j]='N';
}else{ //子弹消失
arr[i][j]=space;
}
}
}
}
}
//多线程
DWORD WINAPI func4(LPVOID LpParameter) //即使用户不操作,也产生子弹
{
while(1)
{
sczd();
Sleep(12000); //12秒
}
return 0;
}
DWORD WINAPI func3(LPVOID LpParameter) //怪物、子弹移动
{
while(1)
{
gwAct();//怪物随机动
zddel();//子弹处理
Sleep(800); //怪物、子弹速度控制
}
return 0;
}
DWORD WINAPI func2(LPVOID LpParameter) //捕捉键盘
{
DWORD id = *(int*)LpParameter;
while(1)
{
int ap=0;
ap=input();
if(ap==1) //按键
{
PostThreadMessage(id, WM_MYMSG, 0, 0);
}
}
return 0;
}
DWORD WINAPI func1(LPVOID LpParameter) //怪物、子弹生成
{
MSG msg;
HWND hwnd = GetConsoleWindow();
while(1)
{
GetMessage(&msg,NULL,0,0);
sczd(); //生成子弹
if (msg.message == WM_MYMSG) //按键 添加怪物
{
int t=0;
for(int i=0;i<TOP;i++){
for(int j=0;j<LEFT;j++){
if(arr[i][j]=='Y')
t++;
}
}
if(t<=1){
GW();
}
}
Sleep(1000);
}
return 0;
}
//多线程
int contrl(){
DWORD id;
HANDLE hthrd1 = CreateThread(NULL, 0, func1, NULL, 0, &id);
HANDLE hthrd2 = CreateThread(NULL, 0, func2, &id, 0, 0);
HANDLE hthrd3 = CreateThread(NULL, 0, func3, NULL, 0, 0);
HANDLE hthrd4 = CreateThread(NULL, 0, func4, NULL, 0, 0);
while(1){
print();//打印地图
if(oldScore!=Score){
oldScore=Score;
print_border();
continue;
}
if(oldLife!=Life){
oldLife=Life;
print_border();
continue;
}
if(Life<=0){
break;
}
if(fjTOP<=0||fjTOP>TOP-1||fjLeft<=0||fjLeft>=LEFT-1){
system("cls");//清空屏幕
cout<<"你自杀了!"<<"\t"<<"生命值:"<<Life<<endl;
Sleep(3000);
return 0;
}
Sleep(1); //速度控制
}
}
int main(){
memset(arr,space,sizeof(arr)); //地图初始化
FJ();//飞机坐标初始化
GW(); //随机怪物初始化
sczd();//生成子弹
print_border();//边界
contrl();//总体控制
system("cls");//清空屏幕
cout<<"游戏结束!得分:"<<Score<<endl;
Sleep(6000);
return 0;
}
陈泽睿在2022-04-15 23:01:27追加了内容
//五子棋
#include <iostream>
#include <conio.h>
using namespace std;
char board[16][16],o;
void init(){
for(int i=0;i<=15;i++)
for(int j=0;j<=15;j++)
board[i][j]='-';
for(int i=1;i<=15;i++) board[0][i]=i+64;
for(int j=1;j<=15;j++) board[j][0]=j+64;
board[0][0]='*';
}
void display(){
cout<<" ================================"<<endl;
for(int i=0;i<=15;i++){
cout<<"|";
for(int j=0;j<=15;j++)
cout<<board[i][j]<<" ";
cout<<"|"<<endl;
}
cout<<" ================================"<<endl;
}
void place(bool color){
string colour;
char x,y;
x=y=' ';
system("cls");
display();
if(!color) colour="黑";
if(color) colour="白";
cout<<colour<<"方放置棋子,请输入棋子的行和列(大写字母):";
cin>>x>>y;
if(x-64>16||y-64>16||x-64<1||y-64<1){
cout<<"该位置超出棋盘范围,请重新放置!"<<x<<y<<endl;
system("pause");
place(color);
}
if(board[x-64][y-64]=='b'||board[x-64][y-64]=='w'){
cout<<"该位置已有棋子"<<board[x-64][y-64]<<",请重新放置!"<<endl;
system("pause");
place(color);
}
if(!color) board[x-64][y-64]='b';
if(color) board[x-64][y-64]='w';
}
int compute(){
int num;
for(int i=1;i<=15;i++)
for(int j=1;j<=15;j++)
if(board[i][j]=='b'||board[i][j]=='w') num++;
if(num==225) return 2;
for(int i=1;i<=15;i++)
for(int j=1;j<=15;j++){
if(board[i][j]=='b'&&board[i][j]==board[i+1][j]&&board[i][j]==board[i+2][j]&&board[i][j]==board[i+3][j]&&board[i][j]==board[i+4][j]) return 0;
if(board[i][j]=='w'&&board[i][j]==board[i+1][j]&&board[i][j]==board[i+2][j]&&board[i][j]==board[i+3][j]&&board[i][j]==board[i+4][j]) return 1;
if(board[i][j]=='b'&&board[i][j]==board[i][j+1]&&board[i][j]==board[i][j+2]&&board[i][j]==board[i][j+3]&&board[i][j]==board[i][j+4]) return 0;
if(board[i][j]=='w'&&board[i][j]==board[i][j+1]&&board[i][j]==board[i][j+2]&&board[i][j]==board[i][j+3]&&board[i][j]==board[i][j+4]) return 1;
if(board[i][j]=='b'&&board[i][j]==board[i+1][j+1]&&board[i][j]==board[i+2][j+2]&&board[i][j]==board[i+3][j+3]&&board[i][j]==board[i+4][j+4]) return 0;
if(board[i][j]=='w'&&board[i][j]==board[i+1][j+1]&&board[i][j]==board[i+2][j+2]&&board[i][j]==board[i+3][j+3]&&board[i][j]==board[i+4][j+4]) return 1;
if(board[i][j]=='b'&&board[i][j]==board[i-1][j+1]&&board[i][j]==board[i-2][j+2]&&board[i][j]==board[i-3][j+3]&&board[i][j]==board[i-4][j+4]) return 0;
if(board[i][j]=='w'&&board[i][j]==board[i-1][j+1]&&board[i][j]==board[i-2][j+2]&&board[i][j]==board[i-3][j+3]&&board[i][j]==board[i-4][j+4]) return 1;
}
}
int main(){
game_start:
init();
system("cls");
system("title 双人五子棋");
cout<<"双人五子棋小游戏"<<endl;
cout<<"小提示:棋盘中b代表黑方(black),w代表白方(white)"<<endl;
cout<<"请按任意键开始游戏......";
getch();
while(true){
place(0);
if(compute()==0){
system("cls");
display();
cout<<"黑方胜!"<<endl;
break;
}
if(compute()==2){
system("cls");
display();
cout<<"平局!"<<endl;
break;
}
place(1);
if(compute()==1){
system("cls");
display();
cout<<"白方胜!"<<endl;
break;
}
if(compute()==2){
system("cls");
display();
cout<<"平局!"<<endl;
break;
}
}
cout<<"再来一局?Y/N ";
o=getch();
if(o=='Y'||o=='y') goto game_start;
return 0;
}