⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 2 Player Shooting Game In Notepad!
💻 CPP
字号:
#define WIN32_LEAN_AND_MEAN

#include <windows.h>

struct Player
{
	int  XPos;
	int  YPos;
	int  BulletXPos;
	int  BulletYPos;
	BOOL Shooting;
};

Player Player1;
Player Player2;

int WINAPI WinMain(HINSTANCE Instance,
				   HINSTANCE PrevInstance,
				   LPSTR     CommandLine,
				   int       ShowCommand)
{
	HWND Notepad;
	HWND EditBox;

	Notepad = FindWindow("Notepad", NULL);

	if(!Notepad)
	{
		WinExec("Notepad", SW_SHOWNORMAL);

		Notepad = FindWindow("Notepad", NULL);

		if(!Notepad)
			return(0);
	}

	EditBox = FindWindowEx(Notepad, NULL, "Edit", NULL);

	if(!EditBox)
		return(0);

	SetForegroundWindow(Notepad);

PlayAgain:

	SendMessage(Notepad, WM_SETTEXT, NULL, (LPARAM)"A Shooting Game by Brenton Saunders - Notepad");

	Player1.XPos	   = 0;
	Player1.YPos	   = 0;
	Player1.Shooting   = FALSE;
	Player1.BulletXPos = 0;
	Player1.BulletYPos = 0;

	Player2.XPos	   = 0;
	Player2.YPos	   = 10;
	Player2.Shooting   = FALSE;
	Player2.BulletXPos = 0;
	Player2.BulletYPos = 0;

	SendMessage(EditBox, WM_SETTEXT, NULL, (LPARAM)"");

	for(int i = 0; i < Player1.XPos; i ++)
		SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

	for(i = 0; i < 3; i ++)
		SendMessage(EditBox, WM_CHAR, (WPARAM)'-', NULL);

	for(i = 0; i < 10; i ++)
		SendMessage(EditBox, WM_CHAR, (WPARAM)'\n', NULL);

	for(i = 0; i < Player2.XPos; i ++)
		SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

	for(i = 0; i < 3; i ++)
		SendMessage(EditBox, WM_CHAR, (WPARAM)'-', NULL);

	while(TRUE)
	{
		BOOL Redraw = FALSE;

		static DWORD OldTickCount  = 0;
		DWORD        NewTickCount  = GetTickCount();
		static DWORD OldTickCount2 = 0;
		DWORD        NewTickCount2 = GetTickCount();

		if((NewTickCount - OldTickCount) >= 50)
		{
			if(GetAsyncKeyState(VK_ESCAPE) < 0)
			{
				break;
			}
			if(GetAsyncKeyState(VK_RIGHT) < 0)
			{
				if(Player1.XPos < 67)
					Player1.XPos ++;

				Redraw = TRUE;
			}
			if(GetAsyncKeyState(VK_LEFT) < 0)
			{
				if(Player1.XPos > 0)
					Player1.XPos --;

				Redraw = TRUE;
			}
			if(GetAsyncKeyState(VK_DOWN) < 0)
			{
				if(!Player1.Shooting)
				{
					Player1.BulletXPos = Player1.XPos + 1;
					Player1.BulletYPos = Player1.YPos + 1;

					Player1.Shooting = TRUE;
				}

				Redraw = TRUE;
			}
			if(GetAsyncKeyState(VK_F8) < 0)
			{
				if(Player2.XPos <= 67)
					Player2.XPos ++;

				Redraw = TRUE;
			}
			if(GetAsyncKeyState(VK_F6) < 0)
			{
				if(Player2.XPos > 0)
					Player2.XPos --;

				Redraw = TRUE;
			}
			if(GetAsyncKeyState(VK_F7) < 0)
			{
				if(!Player2.Shooting)
				{
					Player2.BulletXPos = Player2.XPos + 1;
					Player2.BulletYPos = Player2.YPos - 1;

					Player2.Shooting = TRUE;
				}

				Redraw = TRUE;
			}

			OldTickCount = NewTickCount;
		}

		if((NewTickCount2 - OldTickCount2) >= 250)
		{	
			if(Player1.Shooting)
			{
				if(Player1.BulletYPos <= 11)
				{
					Player1.BulletYPos ++;

					Redraw = TRUE;
				}
				else
				{
					Player1.Shooting = FALSE;

					Redraw = TRUE;
				}

				if(Player1.BulletYPos == Player2.YPos)
				{
					if(Player1.BulletXPos >= Player2.XPos &&
					   Player1.BulletXPos <= Player2.XPos + 2)
					{
						int Res;

						Res = MessageBox(Notepad, "The winner is: Player 1"
												  "\n"
												  "\n"
												  "Would you like to play again?", "A Shooting Game In Notepad",
												  MB_YESNO | MB_ICONEXCLAMATION);

						switch(Res)
						{
						case IDYES:
							goto PlayAgain;
							break;
						case IDNO:
							goto Quit;
							break;
						}
					}
				}
			}

			if(Player2.Shooting)
			{
				if(Player2.BulletYPos > 0)
				{
					Player2.BulletYPos --;

					Redraw = TRUE;
				}
				else
				{
					Player2.Shooting = FALSE;

					Redraw = TRUE;
				}

				if(Player2.BulletYPos == Player1.YPos)
				{
					if(Player2.BulletXPos >= Player1.XPos &&
					   Player2.BulletXPos <= Player1.XPos + 2)
					{
						int Res;

						Res = MessageBox(Notepad, "The winner is: Player 2"
												  "\n"
												  "\n"
												  "Would you like to play again?", "A Shooting Game In Notepad",
												  MB_YESNO | MB_ICONEXCLAMATION);

						switch(Res)
						{
						case IDYES:
							goto PlayAgain;
							break;
						case IDNO:
							goto Quit;
							break;
						}
					}
				}
			}

			OldTickCount2 = NewTickCount2;
		}


		if(Redraw)
		{
			SendMessage(EditBox, WM_SETTEXT, NULL, (LPARAM)"");

			for(int i = 0; i < Player1.XPos; i ++)
				SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

			for(i = 0; i < 3; i ++)
				SendMessage(EditBox, WM_CHAR, (WPARAM)'-', NULL);

			for(i = 0; i < 10; i ++)
			{
				if(Player1.Shooting)
				{
					if(i == Player1.BulletYPos)
					{
						for(int j = 0; j < Player1.BulletXPos; j ++)
							SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

						SendMessage(EditBox, WM_CHAR, (WPARAM)'|', NULL);
					}
				}

				if(Player2.Shooting)
				{
					if(i == Player2.BulletYPos)
					{
						for(int j = 0; j < Player2.BulletXPos; j ++)
							SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

						SendMessage(EditBox, WM_CHAR, (WPARAM)'|', NULL);
					}
				}


				SendMessage(EditBox, WM_CHAR, (WPARAM)'\n', NULL);
			}

			for(i = 0; i < Player2.XPos; i ++)
				SendMessage(EditBox, WM_CHAR, (WPARAM)' ', NULL);

			for(i = 0; i < 3; i ++)
				SendMessage(EditBox, WM_CHAR, (WPARAM)'-', NULL);
		}
	}

Quit:

	SendMessage(Notepad, WM_CLOSE, 0, 0);

	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -