#include<Windows.h> #include<bits/stdc++.h> #include<shellapi.h> #include<conio.h> using namespace std; #define kd(vk) (GetAsyncKeyState(vk) & 0x8000 ? 1 : 0) #define KEY_DOWN kd // show #define edge ”@@” #define wall ”##” #define tree “\x05\x05” #define mne “\x13\x13” #define bup ”^^” #define bdn “vv” #define blt ”<<” #define brt ”>>” #define pl1 “你” #define pl2 “敌” #define air ” ”
int Ihp = 300, Imine = 25, Igf = 30, Imf = 100, Imt = 50;
void ReadInit() { FILE *f = fopen(”./init.txt”, “r”); if(f == NULL) { return; } int a, b, c, d, e; if(fscanf(f, “%d%d%d%d%d”, &a, &b, &c, &d, &e) != 5) { return; } if(a > 0) Ihp = a; if(b >= 0) Imine = b; if(c > 0) Igf = c; if(d > 0) Imf = d; if(e >= 0) Imt = e; }
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE }; int SIZ = 20; HANDLE hout = GetStdHandle (STD_OUTPUT_HANDLE); COORD coord; HANDLE hCon; void SetColor (Color c) { if (hCon == NULL) { hCon = GetStdHandle (STD_OUTPUT_HANDLE); } SetConsoleTextAttribute (hCon, c); } SYSTEMTIME sys;
struct PLAYER { int x, y; int hp; int gun; int direct; int mine; } p1, p2; int mp[1005][1005]; int abs (int x) { if (x < 0) { return -x; } return x; } void locate (int x, int y) { coord.X = y - 1; coord.Y = x - 1; SetConsoleCursorPosition (hout, coord); } void print_map() { locate (1, 1); SetColor (GRAY); for (int i = 1; i <= SIZ; i++) { cout << edge; } locate (SIZ, 1); for (int i = 1; i <= SIZ; i++) { cout << edge; } for (int i = 2; i < SIZ; i++) { locate (i, 1); cout << edge; locate (i, SIZ * 2 - 1); cout << edge; } locate (SIZ + 1, 1); SetColor (WHITE); } void create_tree (int x, int y) { mp[x][y] = mp[x + 1][y] = mp[x - 1][y] = mp[x][y + 1] = mp[x][y - 1] = 2; } void use_map (int x) { // if (x == 1) // { // SIZ = 20; // SetColor (DARKGREEN); // mp[16][6] = mp[15][6] = mp[17][6] = mp[16][7] = mp[16][5] = mp[14][13] = mp[13][12] = mp[13][13] = 2; // for (int i = 2; i < SIZ; i++) // { // for (int j = 2; j < SIZ; j++) // { // if (mp[i][j] == 2) // { // locate (i, j * 2 - 1); // cout << tree; // } // } // } // SetColor (GRAY); // for (int i = 5; i <= 15; i++) // { // mp[i][i] = 1; // locate (i, i * 2 - 1); // cout << edge; // } // SetColor (WHITE); // } // else if (x == 2) { SIZ = 30; x = 0; SetColor (GRAY); for (int i = 4; i <= 26; i++) { if (i == 13 || i == 14 || i == 15) { continue; } mp[i][4] = mp[4][i] = mp[26][i] = mp[i][26] = 1; } for (int i = 1; i <= SIZ; i++) { for (int j = 1; j <= SIZ; j++) { if (mp[i][j] == 1) { locate (i, j * 2 - 1); cout << wall; } } } SetColor (DARKGREEN); for (int i = 10; i <= 20; i++) { if (i == 13 || i == 17) { continue; } mp[i][10] = mp[10][i] = mp[20][i] = mp[i][20] = 2; } create_tree (5, 5); create_tree (18, 18); for (int i = 1; i <= SIZ; i++) { for (int j = 1; j <= SIZ; j++) { if (mp[i][j] == 2) { locate (i, j * 2 - 1); cout << tree; } } } SetColor (WHITE); } // else if (x == 3) // { // SIZ = 40; // } } void cleanbody (int x, int y); void putbody (int x, int y, int z); void player_init(int me) { p1.hp = p2.hp = Ihp; p1.gun = p2.gun = 1; p1.mine = p2.mine = Imine; p1.direct = 1; p2.direct = 1; p1.x = p1.y = me ? 2 : SIZ - 1; p2.x = p2.y = !me ? 2 : SIZ - 1; putbody (p1.x, p1.y, 1); putbody (p2.x, p2.y, 2); } void mapinit() { for (int i = 1; i <= SIZ; i++) { mp[i][1] = mp[1][i] = mp[SIZ][i] = mp[i][SIZ] = 1; } } void init(int me) { // printf (“Use Which mp?\n”); // int x; // cin >> x; system (“cls”); use_map (2); mapinit(); print_map(); player_init(me); } void cleanbody (int x, int y) { locate (x, y * 2 - 1); cout << air; } void putbody (int x, int y, int z) { if (z == 1 || z == 8) { SetColor (BLUE); } else if (z == 2 || z == 9) { SetColor (RED); } locate (x, y * 2 - 1); if (z == 1) { cout << pl1; if (mp[x][y] == 9) { Beep (1234, 100); p1.hp -= Imf; mp[x][y] = 0; } } else if (z == 2) { cout << pl2; if (mp[x][y] == 8) { Beep (1234, 100); p2.hp -= Imf; mp[x][y] = 0; } } else if (z == 8 || z == 9) { cout << mne << endl; } SetColor (WHITE); } /* LIST direct: w 1 a 2 s 3 d 4 gun: usp 1 mimigun 2 awp 3 block: void 0 stone 1 tree 2 player 3 clip 4 */ bool judge (int x, int y) { if (mp[x][y] == 1) { return false; } if (mp[x][y] == 2) { return false; } if (mp[x][y] == 3) { return false; } return true; } bool judge_gun (int x, int y) { if (mp[x][y] == 1) { return 0; } if (mp[x][y] == 2) { return 0; } if (mp[x][y] == 3) { if (p1.x == x && p1.y == y) { p1.hp -= Igf, putbody (p1.x, p1.y, 1); //此处暂时不管威力 } else { p2.hp -= Igf, putbody (p2.x, p2.y, 2); } return 0; } return 1; } int cnt; struct Clip { int x, y; int derect; int force; int start; bool flag; } clip[1000000]; void create_clip (int y, int x, int a, int b) { int X, Y; if (y == 1) { if (!judge_gun (a - 1, b)) { return; } X = a - 1; Y = b; } else if (y == 2) { if (!judge_gun (a, b - 1)) { return; } X = a; Y = b - 1; } else if (y == 3) { if (!judge_gun (a + 1, b)) { return; } X = a + 1; Y = b; } else if (y == 4) { if (!judge_gun (a, b + 1)) { return; } X = a; Y = b + 1; } cnt++; GetLocalTime (&sys); clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600; clip[cnt].x = X; clip[cnt].y = Y; if (x == 1) { clip[cnt].derect = p1.direct; } else if (x == 2) { clip[cnt].derect = p2.direct; } } void createclip (int y, int x, int a, int b) { int X = x, Y; if (y == 1) { if (!judge_gun (a - 1, b)) { return; } X = a - 1; Y = b; } else if (y == 2) { if (!judge_gun (a, b - 1)) { return; } X = a; Y = b - 1; } else if (y == 3) { if (!judge_gun (a + 1, b)) { return; } X = a + 1; Y = b; } else if (y == 4) { if (!judge_gun (a, b + 1)) { return; } X = a; Y = b + 1; } cnt++; GetLocalTime (&sys); clip[cnt].start = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600; clip[cnt].x = X; clip[cnt].y = Y; clip[cnt].derect = y; } void shoot (int x) { if (x == 1) { create_clip (p1.direct, 1, p1.x, p1.y); create_clip (p1.direct, 1, p1.x, p1.y); } else if (x == 2) { create_clip (p2.direct, 2, p2.x, p2.y); create_clip (p2.direct, 2, p2.x, p2.y); } } void shootdir (int x, int dir) { if (x == 1) { createclip (dir, 1, p1.x, p1.y); } else if (x == 2) { createclip (dir, 2, p2.x, p2.y); } } void wow (int x) // 地雷 { if (x == 1 && p1.mine > 0) { mp[p1.x][p1.y] = 8, putbody (p1.x, p1.y, 8), p1.mine—; } if (x == 2 && p2.mine > 0) { mp[p2.x][p2.y] = 9, putbody (p2.x, p2.y, 9), p2.mine—; } } void clean_clip (int x, int y) { locate (x, y * 2 - 1); cout << ” ”; locate (1, 1); } void print_clip (int x, int y, int i) { if (clip[i].flag) { clean_clip (x, y); return; } locate (x, y * 2 - 1); SetColor (YELLOW); if (clip[i].derect == 1) { cout << bup; } if (clip[i].derect == 2) { cout << blt; } if (clip[i].derect == 3) { cout << bdn; } if (clip[i].derect == 4) { cout << brt; } locate (1, 1); // system(“pause”); } void clipmove() { GetLocalTime (&sys); int t = sys.wMilliseconds + sys.wSecond * 60 + sys.wHour * 3600; for (int i = 1; i <= cnt; i++) { if (clip[i].flag) { continue; } if (abs (clip[i].start - t) > Imf) { clip[i].start = t; int x = clip[i].x; int y = clip[i].y; if (clip[i].derect == 1) { if (!judge_gun (clip[i].x - 1, clip[i].y)) { clip[i].flag = 1; clean_clip (x, y); continue; } clean_clip (clip[i].x, clip[i].y); clip[i].x—; print_clip (clip[i].x, clip[i].y, i); } else if (clip[i].derect == 2) { if (!judge_gun (clip[i].x, clip[i].y - 1)) { clip[i].flag = 1; clean_clip (x, y); continue; } clean_clip (clip[i].x, clip[i].y); clip[i].y—; print_clip (clip[i].x, clip[i].y, i); } else if (clip[i].derect == 3) { if (!judge_gun (clip[i].x + 1, clip[i].y)) { clip[i].flag = 1; clean_clip (x, y); continue; } clean_clip (clip[i].x, clip[i].y); clip[i].x++; print_clip (clip[i].x, clip[i].y, i); } else if (clip[i].derect == 4) { if (!judge_gun (clip[i].x, clip[i].y + 1)) { clip[i].flag = 1; clean_clip (x, y); continue; } clean_clip (clip[i].x, clip[i].y); clip[i].y++; print_clip (clip[i].x, clip[i].y, i); } } } } bool judge_hp() { int x = p1.hp; int y = p2.hp; if (x < 0 && y < 0 && x > y) { swap (x, y); } if (x <= 0) { locate (1, 1); system (“cls & color f4”); printf (“GAME OVER!\nTHE WINNER IS PEER!”); Sleep (5000); return 1; } else if (y <= 0) { locate (1, 1); system (“cls & color f1”); printf (“GAME OVER!\nTHE WINNER IS YOU!”); Sleep (5000); return 1; } return 0; } //////////////////////////
#define BUF_SIZE 32768 #define perr(b, str) if(b) { cout << str << “\nGLE = ” << GetLastError() << endl; Sleep(10000); exit(GetLastError()); }
void send(HANDLE hWPipe, string s) { DWORD dwReturn; WriteFile(hWPipe, s.data(), s.length(), &dwReturn, NULL); SetConsoleTitleA((“Game - Send message "" + s + "" to parent process.”).data()); } string rece(HANDLE hRPipe) { string s; char szBuffer[BUF_SIZE] = { 0 }; DWORD dwReturn; SetConsoleTitleA(“Game - Receiving message…”); if (ReadFile(hRPipe, szBuffer, BUF_SIZE, &dwReturn, NULL)) { szBuffer[dwReturn] = ‘\0’; SetConsoleTitleA((“Game - Receive message "" + (s = szBuffer) + "" from parent process.”).data()); return s; } return ""; }
string pname; HANDLE hWPipe, hRPipe; void Process();
queue<string> mq;
DWORD WINAPI Listen(LPVOID lpvIgnore) { while(1) { string s = rece(hRPipe); if(s != "") mq.push(s); } }
int main(int argc, char *argv[]) { if (argc != 2) { cout << “Remember, \n\tExecute “gmain.exe”!” << endl; cout << “execute parent” << endl; Sleep(1000); ShellExecuteA(NULL, “open”, “gmain.exe”, NULL, NULL, SW_SHOWDEFAULT); return 0; } pname = argv[1];
perr(!WaitNamedPipe((pname + "R").data(), NMPWAIT_USE_DEFAULT_WAIT), "No Write(main R) Pipe");
perr(!WaitNamedPipe((pname + "W").data(), NMPWAIT_USE_DEFAULT_WAIT), "No Read(main W) Pipe");
cout << "open w(mr) ";
hWPipe = CreateFile((pname + "R").data(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
perr(hWPipe == INVALID_HANDLE_VALUE, "Open Write (mR) Pipe Error");
cout << hWPipe << endl;
cout << "open r(mw) ";
hRPipe = CreateFile((pname + "W").data(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
perr(hRPipe == INVALID_HANDLE_VALUE, "Open Read (mW) Pipe Error");
cout << hWPipe << endl;
SetConsoleTitle("Game");
Process();
CloseHandle(hRPipe);
CloseHandle(hWPipe);
system("pause");
system("pause");}
void ConsoleSettings() { CONSOLE_CURSOR_INFO cci; GetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci); cci.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
DWORD dwMode;
GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &dwMode);
dwMode &= ~ ENABLE_QUICK_EDIT_MODE;
dwMode |= ENABLE_INSERT_MODE;
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), dwMode);}
void Process() { ConsoleSettings(); GetLocalTime(&sys); system(“cls”); int player; sscanf(rece(hRPipe).data(), “%d”, &player); init(player - 1); // child can set pipe state, too ? // DWORD dwst = PIPE_NOWAIT; // SetNamedPipeHandleState(hRPipe, &dwst, NULL, NULL); // dwst = PIPE_NOWAIT; // SetNamedPipeHandleState(hWPipe, &dwst, NULL, NULL);
CreateThread(NULL, 0, Listen, NULL, 0, NULL);
while (1)
{
string r = "";
if(mq.size())
{
r = mq.front();
mq.pop();
}
// dwst = PIPE_WAIT;
// SetNamedPipeHandleState(hRPipe, &dwst, NULL, NULL);
if (r == "peer_leave")
{
MessageBoxA(NULL, "对手退出,被迫离开。", "啊这……", MB_ICONINFORMATION | MB_OK);
return;
}
if (kd('W') && judge (p1.x - 1, p1.y))
{
cleanbody (p1.x, p1.y);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 0;
}
putbody (--p1.x, p1.y, 1);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 3;
}
send(hWPipe, "KEY w");
}
else if (kd('A') && judge (p1.x, p1.y - 1))
{
cleanbody (p1.x, p1.y);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 0;
}
putbody (p1.x, --p1.y, 1);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 3;
}
send(hWPipe, "KEY a");
}
else if (kd('S') && judge (p1.x + 1, p1.y))
{
cleanbody (p1.x, p1.y);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 0;
}
putbody (++p1.x, p1.y, 1);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 3;
}
send(hWPipe, "KEY s");
}
else if (kd('D') && judge (p1.x, p1.y + 1))
{
cleanbody (p1.x, p1.y);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 0;
}
putbody (p1.x, ++p1.y, 1);
if (mp[p1.x][p1.y] != 8)
{
mp[p1.x][p1.y] = 3;
}
send(hWPipe, "KEY d");
}
else if (kd('I'))
{
// p1.direct = 1;
// shoot (1);
shootdir(1, 1);
send(hWPipe, "KEY i");
}
else if (kd('J'))
{
// p1.direct = 2;
// shoot (1);
shootdir(1, 2);
send(hWPipe, "KEY j");
}
else if (kd('K'))
{
// p1.direct = 3;
// shoot (1);
shootdir(1, 3);
send(hWPipe, "KEY k");
}
else if (kd('L'))
{
// p1.direct = 4;
// shoot (1);
shootdir(1, 4);
send(hWPipe, "KEY l");
}
else if (kd(VK_SPACE))
{
wow (1);
send(hWPipe, "KEY ");
}
if (r.find("KEY") != string::npos)
{
char ch = r[4];
if (ch == 'w' && judge (p2.x - 1, p2.y))
{
cleanbody (p2.x, p2.y);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 0;
}
putbody (--p2.x, p2.y, 2);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 3;
}
}
else if (ch == 'a' && judge (p2.x, p2.y - 1))
{
cleanbody (p2.x, p2.y);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 0;
}
putbody (p2.x, --p2.y, 2);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 3;
}
}
else if (ch == 's' && judge (p2.x + 1, p2.y))
{
cleanbody (p2.x, p2.y);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 0;
}
putbody (++p2.x, p2.y, 2);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 3;
}
}
else if (ch == 'd' && judge (p2.x, p2.y + 1))
{
cleanbody (p2.x, p2.y);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 0;
}
putbody (p2.x, ++p2.y, 2);
if (mp[p2.x][p2.y] != 9)
{
mp[p2.x][p2.y] = 3;
}
}
else if (ch == 'i')
{
// p2.direct = 1;
// shoot (2);
shootdir(2, 1);
}
else if (ch == 'j')
{
// p2.direct = 2;
// shoot (2);
shootdir(2, 2);
}
else if (ch == 'k')
{
// p2.direct = 3;
// shoot (2);
shootdir(2, 3);
}
else if (ch == 'l')
{
// p2.direct = 4;
// shoot (2);
shootdir(2, 4);
}
else if (ch == ' ')
{
wow (2);
}
}
clipmove();
if(judge_hp())
{
send(hWPipe, "exit");
return;
}
Sleep (80);
}}