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

📄 invaders.c

📁 Space Invaders game
💻 C
📖 第 1 页 / 共 2 页
字号:
				}
				break;

		  case IDOK:

				GetDlgItemText(hDlg, IDD_GUN_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Gun_Delay);
				GetDlgItemText(hDlg, IDD_BULLET_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Bullet_Delay);
				GetDlgItemText(hDlg, IDD_BULLET_FLAT_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Bullet_Flat_Delay);
				GetDlgItemText(hDlg, IDD_EXPLOSION_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Explosion_Delay);
				GetDlgItemText(hDlg, IDD_GREMLIN_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Gremlin_Delay);
				GetDlgItemText(hDlg, IDD_SPACESHIP_LAUNCH_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_SpaceShip_Launch_Delay);
				GetDlgItemText(hDlg, IDD_SPACESHIP_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_SpaceShip_Delay);
				GetDlgItemText(hDlg, IDD_BOMB_LAUNCH_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Bomb_Launch_Delay);
				GetDlgItemText(hDlg, IDD_BOMB_DELAY, buf, 6);
				sscanf(buf, "%d", &Tid_Bomb_Delay);
				GetDlgItemText(hDlg, IDD_SOUND, buf, 6);
				if (strcmp(buf, "FALSE") == 0)
					 Sound = FALSE;
				else
					 Sound = TRUE;

				// Use new timing values

				KillGameTimers();
				SetGameTimers();

				// Terminate this dialog box.

				EndDialog(hDlg, TRUE);
				break;

		  case IDCANCEL:

				// Terminate this dialog box.

				EndDialog(hDlg, FALSE);
				break;

		  default:
				return(FALSE);
		  }

		  break;

	 default:
		  return(FALSE);
	 }

	 return(TRUE);
}


//*******************************************************************
// InvadersWndProc - handles messages for this application
//
// paramaters:
//             hWnd          - The window handle for this message
//             message       - The message number
//             wParam        - The WPARAM parameter for this message
//             lParam        - The LPARAM parameter for this message
//
// returns:
//             depends on message.
//
//*******************************************************************

LRESULT CALLBACK InvadersWndProc(
HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
	 DLGPROC  lpproc;                  // pointer to thunk for dialog box
	 int      i, x, y;
	 RECT     Rect;

	 switch (message) {
	 case WM_CREATE:

		  return(DefWindowProc(hWnd, message, wParam, lParam));

	 case WM_COMMAND:

		  switch (GET_WM_COMMAND_ID(wParam, lParam)) {

		  case IDM_PAUSE:

				// User pressed Play/Pause

				if (Paused) {
					 Paused = FALSE;
					 if (StartGame) {
						  StartGame = FALSE;
						  Frame = 1;
						  Lives = 3;
						  InitFrame();
					 } else {
						  SetGameText();
                }
				} else {
					 Paused = TRUE;
					 GunMotion = 0;
					 SetGameText();
				}

				break;

		  case IDM_LEFT_DOWN:

				// User pressed move gun left key

				if (!Paused) {
					 LeftDown = TRUE;
					 if (RightDown)
						  GunMotion = 0;
					 else
						  GunMotion = -1;
				}
				break;

		  case IDM_LEFT_UP:

				// User released move gun left key

				LeftDown = FALSE;
				if (RightDown)
					 GunMotion = 1;
				else
					 GunMotion = 0;

				break;

		  case IDM_RIGHT_DOWN:

				// User pressed move gun right key

				if (!Paused) {
					 RightDown = TRUE;
					 if (LeftDown)
						  GunMotion = 0;
					 else {
						  GunMotion = 1;
					 }
				}
				break;

		  case IDM_RIGHT_UP:

				// User released move gun right key

				RightDown = FALSE;
				if (LeftDown)
					 GunMotion = -1;
				else
					 GunMotion = 0;

				break;

		  case IDM_FIRE:

				// User pressed fire bullet key

				if (!Paused && !Stopped && !BulletPresent && !ExplosionPresent) {
					 PlayWAV(SND_FIRE);
					 BullPosX = GunPos;
					 BulletsFired++;
					 if (BulletsFired > 22)
						  BulletsFired = 9;
					 if (
						  VirtScrn[GunPos][y=44] ||
						  VirtScrn[GunPos][y=43] ||
						  VirtScrn[GunPos][y=42]
					 ) {
						  BullPosY = y+1;
						  BulletHit();
					 } else {
						  BulletPresent = TRUE;
						  BullPosY = 42;
						  PaintObject(BullPosX, BullPosY, Bullet);
						  SetTimer(hWndMain, TID_BULLET, Tid_Bullet_Delay, MoveBulletProc);
					 }
				}
				break;

		  case IDM_QUIT:

				// User pressed Quit

				PostMessage(hWnd, WM_CLOSE, 0, 0L);
				break;

		  case IDM_ABOUT:

				// Display about box.

				lpproc = (DLGPROC)MakeProcInstance((FARPROC)About, hInst);
				DialogBox(hInst, MAKEINTRESOURCE(ABOUT), hWnd, lpproc);
				FreeProcInstance((FARPROC)lpproc);
				break;

		  case IDM_VARY:

				// Display vary variablles box.

				lpproc = (DLGPROC)MakeProcInstance((FARPROC)VaryVariablesDlgProc, hInst);
				DialogBox(hInst, MAKEINTRESOURCE(VARY), hWnd, lpproc);
				FreeProcInstance((FARPROC)lpproc);
				break;

		  default:
				break;
		  }

		  break;

	 case WM_MOVE:

	 // Get Window Co-ordinates for profile

		  GetWindowRect(hWndMain, &WndMovedRect);
		  if (
				WndRect.right-WndRect.left == WndMovedRect.right-WndMovedRect.left &&
				WndRect.bottom-WndRect.top == WndMovedRect.bottom-WndMovedRect.top
		  )
				GetWindowRect(hWndMain, &WndRect);

		  return(DefWindowProc(hWnd, message, wParam, lParam));

	 case WM_SIZE:

	 // Get Window Co-ordinates for profile

		  if (wParam != SIZE_MINIMIZED && wParam != SIZE_MAXIMIZED)
				GetWindowRect(hWndMain, &WndRect);
		  if (wParam == SIZE_MAXIMIZED)
				WndMax = TRUE;
		  else
            WndMax = FALSE;
		  if (wParam == SIZE_MINIMIZED) {
				Paused = TRUE;
				SetWindowText(hWndMain, "Space Invaders");
		  } else if (!StartGame)
				SetGameText();

	 // Save size of window client area and compute look up tables.

		  if (lParam) {
				yClient = HIWORD(lParam);       // Height of Client Area
				xClient = LOWORD(lParam);       // Width of Client Area

				for (x = 0; x < 129; x++)
					 RealScrnX[x] = (int)(((long)x*(long)xClient)/128L);
				for (y = 0; y < 49; y++)
					 RealScrnY[y] = (int)(((long)y*(long)yClient)/(long)48L);

				for (x = 0; x < xClient; x++)
					 VScrnX[x] =
						  (int)((((float)x + 0.999999)*128.0)/(float)xClient);
				for (y = 0; y < yClient; y++)
					 VScrnY[y] =
						  (int)((((float)y + 0.999999)*48.0 )/(long)yClient);

				return(DefWindowProc(hWnd, message, wParam, lParam));
		  }
		  break;

	 case WM_KEYDOWN:

		  // Translate various keydown messages to appropriate horizontal
		  // and vertical scroll actions.

		  for (i = 0; i < NUMKEYSDOWN; i++) {

				if (wParam == keysdown[i].wVirtkey) {
					 SendMessage(hWnd, keysdown[i].iMessage,
					 keysdown[i].wRequest, 0L);
					 break;
				}
		  }
		  break;

	 case WM_KEYUP:

		  // Translate various keyup messages to appropriate horizontal
		  // and vertical scroll actions.

		  for (i = 0; i < NUMKEYSUP; i++) {

				if (wParam == keysup[i].wVirtkey) {
					 SendMessage(hWnd, keysup[i].iMessage,
					 keysup[i].wRequest, 0L);
					 break;
				}
		  }
		  break;

	 case WM_PAINT:

		  // Go paint the client area of the window

		  GetUpdateRect(hWndMain, &Rect, TRUE);

		  Rect.left   = VScrnX[Rect.left];
		  Rect.top    = VScrnY[Rect.top];
		  Rect.right  = VScrnX[Rect.right-1];    // We seem to have one over
		  Rect.bottom = VScrnY[Rect.bottom-1];   // because Windows does not
															  // include last line
		  PaintInvaders(&Rect);
		  break;

	 case WM_DESTROY:

		  // This is the end if we were closed by a DestroyWindow call.

		  CloseInvaders();         // take any necessary wrapup action.
		  PostQuitMessage(0);   // this is the end...
		  break;

	 case WM_QUERYENDSESSION:

		  // If we return TRUE we are saying it's ok with us to end the
		  // windows session.

		  CloseInvaders();         // take any necessary wrapup action.
		  return((long) TRUE);  // we agree to end session.

	 case WM_CLOSE:

		  // Tell windows to destroy our window.

		  DestroyWindow(hWnd);
		  break;

	 default:

		  // Let windows handle all messages we choose to ignore.

		  return(DefWindowProc(hWnd, message, wParam, lParam));
	 }

	 return(0L);
}


//*******************************************************************
// PaintInvaders - paint the main window
//
// Paint the client window using VirtScrn array
//
// paramaters:
//             VRect          - The rectangle within VirtScrn
//
//*******************************************************************

void PaintInvaders(
RECT *VRect)
{
	 PAINTSTRUCT  ps;
	 HDC          hDC;
	 int          xleft, x, y;
	 RECT         pixel;
	 HBRUSH       white, black;


	 BeginPaint(hWndMain, (LPPAINTSTRUCT) &ps);
	 hDC = ps.hdc;

	 white = GetStockObject(WHITE_BRUSH);
	 black = GetStockObject(BLACK_BRUSH);

	 xleft = VRect->left;
	 for (y = VRect->top; y <= VRect->bottom; y++) {
		  for (x = xleft; x <= VRect->right; x++) {
				pixel.left   = RealScrnX[x];
				pixel.top    = RealScrnY[y];
				pixel.right  = RealScrnX[x+1];
				pixel.bottom = RealScrnY[y+1];
				if (VirtScrn[x][y])
					 FillRect(hDC, &pixel, white);
				else
					 FillRect(hDC, &pixel, black);
		  }
	 }
	 EndPaint(hWndMain, (LPPAINTSTRUCT) &ps);
}


//*******************************************************************
// PaintObject - paint the object on to main window
//
// Paint the client window with object
//
// paramaters:
//             x, y          - Co-ordinates of object on VirtScrn
//             object        - object to paint
//
//*******************************************************************

void PaintObject(
int x,
int y,
unsigned char *object)
{
	 RECT Rect;
	 int xstart, ystart, xend, yend, xlen, ylen, c;

	 xlen = *object++;
	 ylen = *object++;
	 xend = x+xlen-1;
	 yend = y+ylen-1;
	 xstart = x;
	 ystart = y;
	 if (xstart > 127)
		  return;
	 if (ystart > 47)
		  return;
	 if (xend < 0)
		  return;
	 if (yend < 0)
		  return;
	 for (;y <= yend; y++)
		  for (x = xstart; x <= xend; x++) {
				c = *object++;
				if (x>=0 && x<=127 && y>=0 && y<=47)
					 VirtScrn[x][y] = c;
		  }
	 if (xstart < 0)
		  Rect.left = RealScrnX[0];
	 else
		  Rect.left = RealScrnX[xstart];

	 if (ystart < 0)
		  Rect.top = RealScrnY[0];
	 else
		  Rect.top = RealScrnY[ystart];

	 if (xend > 127)
		  Rect.right = RealScrnX[128];
	 else
		  Rect.right    = RealScrnX[xend+1];

	 if (yend > 47)
		  Rect.bottom = RealScrnY[48];
	 else
		  Rect.bottom   = RealScrnY[yend+1];

	 InvalidateRect(hWndMain, &Rect, TRUE);
	 UpdateWindow(hWndMain);
}
                                   

⌨️ 快捷键说明

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