📄 invaders.c
字号:
#pragma hdrstop
//*******************************************************************
//
// program - Invaders.c
// purpose - a windows program of the arcade game Space Invaders.
//
//*******************************************************************
#define MAIN 1
#include "invaders.h"
KEYS keysdown[] = {
{ VK_ESCAPE, WM_COMMAND, IDM_QUIT },
{ 'Q', WM_COMMAND, IDM_QUIT },
{ 'A', WM_COMMAND, IDM_ABOUT },
{ 'V', WM_COMMAND, IDM_VARY },
{ 'P', WM_COMMAND, IDM_PAUSE },
{ VK_LEFT, WM_COMMAND, IDM_LEFT_DOWN },
{ 'Z', WM_COMMAND, IDM_LEFT_DOWN },
{ VK_RIGHT, WM_COMMAND, IDM_RIGHT_DOWN },
{ 'X', WM_COMMAND, IDM_RIGHT_DOWN },
{ VK_SPACE, WM_COMMAND, IDM_FIRE }
};
# define NUMKEYSDOWN (sizeof keysdown / sizeof keysdown[0])
KEYS keysup[] = {
{ VK_LEFT, WM_COMMAND, IDM_LEFT_UP },
{ 'Z', WM_COMMAND, IDM_LEFT_UP },
{ VK_RIGHT, WM_COMMAND, IDM_RIGHT_UP },
{ 'X', WM_COMMAND, IDM_RIGHT_UP }
};
# define NUMKEYSUP (sizeof keysup / sizeof keysup[0])
//*******************************************************************
// WinMain - Invaders main
//
// paramaters:
// hInstance - The instance of this instance of this
// application.
// hPrevInstance - The instance of the previous instance
// of this application. This will be 0
// if this is the first instance.
// lpszCmdLine - A long pointer to the command line that
// started this application.
// cmdShow - Indicates how the window is to be shown
// initially. ie. SW_SHOWNORMAL, SW_HIDE,
// SW_MIMIMIZE.
//
// returns:
// wParam from last message.
//
//*******************************************************************
int PASCAL WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int cmdShow)
{
MSG msg;
// Go init this application.
InitInvaders(hInstance, hPrevInstance, lpszCmdLine, cmdShow);
// Get and dispatch messages for this applicaton.
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return(msg.wParam);
}
#if !defined(__WIN32__)
//*******************************************************************
// InitInvadersAdded - done only for added instances of Invaders
//
// paramaters:
// hPrevInstance - The instance of the previous instance
// of this application.
//
//*******************************************************************
void InitInvadersAdded(
HINSTANCE hPrevInstance)
{
// get the results of the initialization of first instance
// GetInstanceData(hPrevInstance, (BYTE*) &SetUpData, sizeof(SETUPDATA));
}
#endif
//*******************************************************************
// InitInvaders - init the Invaders application
//
// paramaters:
// hInstance - The instance of this instance of this
// application.
// hPrevInstance - The instance of the previous instance
// of this application. This will be 0
// if this is the first instance.
// lpszCmdLine - A long pointer to the command line that
// started this application.
// cmdShow - Indicates how the window is to be shown
// initially. ie. SW_SHOWNORMAL, SW_HIDE,
// SW_MIMIMIZE.
//
//*******************************************************************
void InitInvaders(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszCmdLine,
int cmdShow)
{
if (! hPrevInstance) // if no previous instance, this is first
InitInvadersFirst(hInstance);
#if !defined(__WIN32__)
else
InitInvadersAdded(hPrevInstance); // this is not first instance
#endif
InitInvadersEvery(hInstance, cmdShow); // initialization for all instances
}
//*******************************************************************
// InitInvadersFirst - done only for first instance of Invaders
//
// paramaters:
// hInstance - The instance of this instance of this
// application.
//
//*******************************************************************
void InitInvadersFirst(
HINSTANCE hInstance)
{
WNDCLASS wcInvadersClass;
// Get string from resource with application name.
LoadString(hInstance, IDS_NAME, (LPSTR) SetUpData.szAppName, 10);
// Define the window class for this application.
wcInvadersClass.lpszClassName = SetUpData.szAppName;
wcInvadersClass.hInstance = hInstance;
wcInvadersClass.lpfnWndProc = InvadersWndProc;
wcInvadersClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wcInvadersClass.hIcon = LoadIcon(hInstance, SetUpData.szAppName);
wcInvadersClass.lpszMenuName = 0; //(LPSTR) SetUpData.szAppName;
wcInvadersClass.hbrBackground = GetStockObject(BLACK_BRUSH);
wcInvadersClass.style = CS_HREDRAW | CS_VREDRAW;
wcInvadersClass.cbClsExtra = 0;
wcInvadersClass.cbWndExtra = 0;
// Register the class
RegisterClass(&wcInvadersClass);
}
//*******************************************************************
// InitInvadersEvery - done for every instance of Invaders
//
// paramaters:
// hInstance - The instance of this instance of this
// application.
// cmdShow - Indicates how the window is to be shown
// initially. ie. SW_SHOWNORMAL, SW_HIDE,
// SW_MIMIMIZE.
//
//*******************************************************************
void InitInvadersEvery(
HINSTANCE hInstance,
int cmdShow)
{
char buf[32];
int i;
hInst = hInstance; // save for use by window procs
// Get Window data from profile
WndMax = FALSE;
GetPrivateProfileString("invaders", "WINDOW_MAXIMIZED", "FALSE", buf, sizeof(buf), "INVADERS.INI");
if (strcmp(buf, "TRUE") == 0) {
cmdShow = SW_MAXIMIZE;
WndMax = TRUE;
}
GetPrivateProfileString("invaders", "WINDOW", "55,55,575,474", buf, sizeof(buf), "INVADERS.INI");
sscanf(buf, "%d,%d,%d,%d", &WndRect.left, &WndRect.top, &WndRect.right, &WndRect.bottom);
// Create applications main window.
StartGame = TRUE; // Stop SIZE message from displaying GameText
hWndMain = CreateWindow(
SetUpData.szAppName, // window class name
"Space Invaders", // window title
WS_OVERLAPPEDWINDOW, // type of window
WndRect.left, // x window location
WndRect.top, // y
WndRect.right-WndRect.left, // cx and size
WndRect.bottom-WndRect.top, // cy
NULL, // no parent for this window
NULL, // use the class menu
hInstance, // who created this window
NULL // no parms to pass on
);
// Update display of main window.
ShowWindow(hWndMain, cmdShow);
UpdateWindow(hWndMain);
// Initialize Game
srand( (unsigned)time( NULL ) );
digit[0] = character_0;
digit[1] = character_1;
digit[2] = character_2;
digit[3] = character_3;
digit[4] = character_4;
digit[5] = character_5;
digit[6] = character_6;
digit[7] = character_7;
digit[8] = character_8;
digit[9] = character_9;
DeviceId = 0;
GetPrivateProfileString(
"invaders", "HI_SCORE", "0", buf, sizeof buf, "INVADERS.INI");
HiScore = 0;
for (i = 0; isdigit(buf[i]); i++)
HiScore = 10L*HiScore+(long)(buf[i]-'0');
Sound = TRUE;
GetPrivateProfileString("invaders", "SOUND", "TRUE", buf, sizeof(buf), "INVADERS.INI");
if (strcmp(buf, "FALSE") == 0)
Sound = FALSE;
Tid_Gun_Delay = GetPrivateProfileInt(
"invaders", "GUN_DELAY", TID_GUN_DELAY, "INVADERS.INI");
Tid_Bullet_Delay = GetPrivateProfileInt(
"invaders", "BULLET_DELAY", TID_BULLET_DELAY, "INVADERS.INI");
Tid_Bullet_Flat_Delay = GetPrivateProfileInt(
"invaders", "BULLET_FLAT_DELAY", TID_BULLET_FLAT_DELAY, "INVADERS.INI");
Tid_Gremlin_Delay = GetPrivateProfileInt(
"invaders", "GREMLIN_DELAY", TID_GREMLIN_DELAY, "INVADERS.INI");
Tid_Explosion_Delay = GetPrivateProfileInt(
"invaders", "EXPLOSION_DELAY", TID_EXPLOSION_DELAY, "INVADERS.INI");
Tid_SpaceShip_Launch_Delay = GetPrivateProfileInt(
"invaders", "SPACESHIP_LAUNCH_DELAY", TID_SPACESHIP_LAUNCH_DELAY, "INVADERS.INI");
Tid_SpaceShip_Delay = GetPrivateProfileInt(
"invaders", "SPACESHIP_DELAY", TID_SPACESHIP_DELAY, "INVADERS.INI");
Tid_Bomb_Launch_Delay = GetPrivateProfileInt(
"invaders", "BOMB_LAUNCH_DELAY", TID_BOMB_LAUNCH_DELAY, "INVADERS.INI");
Tid_Bomb_Delay = GetPrivateProfileInt(
"invaders", "BOMB_DELAY", TID_BOMB_DELAY, "INVADERS.INI");
TimerFrameLength = GetPrivateProfileInt(
"invaders", "TIMER_LENGTH", 60, "INVADERS.INI");
InitGame();
}
//*******************************************************************
// CloseInvaders - done at termination of every instance of Invaders
//
//*******************************************************************
void CloseInvaders()
{
char buf[32];
int i;
if (WndMax)
strcpy(buf, "TRUE");
else
strcpy(buf, "FALSE");
WritePrivateProfileString("invaders", "WINDOW_MAXIMIZED", buf, "INVADERS.INI");
if (Sound)
strcpy(buf, "TRUE");
else
strcpy(buf, "FALSE");
WritePrivateProfileString("invaders", "SOUND", buf, "INVADERS.INI");
sprintf(buf, "%d,%d,%d,%d", WndRect.left, WndRect.top, WndRect.right, WndRect.bottom);
WritePrivateProfileString("invaders", "WINDOW", buf, "INVADERS.INI");
sprintf(buf, "%f", (double)HiScore);
for (i = 0; buf[i] != '.' && i < sizeof buf; i++)
;
buf[i] = '\0';
WritePrivateProfileString(
"invaders", "HI_SCORE", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Gun_Delay);
WritePrivateProfileString(
"invaders", "GUN_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Bullet_Delay);
WritePrivateProfileString(
"invaders", "BULLET_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Bullet_Flat_Delay);
WritePrivateProfileString(
"invaders", "BULLET_FLAT_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Gremlin_Delay);
WritePrivateProfileString(
"invaders", "GREMLIN_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Explosion_Delay);
WritePrivateProfileString(
"invaders", "EXPLOSION_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_SpaceShip_Launch_Delay);
WritePrivateProfileString(
"invaders", "SPACESHIP_LAUNCH_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_SpaceShip_Delay);
WritePrivateProfileString(
"invaders", "SPACESHIP_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Bomb_Launch_Delay);
WritePrivateProfileString(
"invaders", "BOMB_LAUNCH_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", Tid_Bomb_Delay);
WritePrivateProfileString(
"invaders", "BOMB_DELAY", buf, "INVADERS.INI");
sprintf(buf, "%d", TimerFrameLength);
WritePrivateProfileString(
"invaders", "TIMER_LENGTH", buf, "INVADERS.INI");
}
//*******************************************************************
// About - handle about dialog messages
//
// paramaters:
// hDlg - The window handle for this message
// message - The message number
// wParam - The WPARAM parameter for this message
// lParam - The LPARAM parameter for this message
//
//*******************************************************************
BOOL CALLBACK About(
HWND hDlg,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
if (message == WM_INITDIALOG)
return(TRUE);
else if (message == WM_COMMAND) {
switch (GET_WM_COMMAND_ID(wParam, lParam)) {
case IDOK:
EndDialog(hDlg, TRUE);
return(TRUE);
default:
return(TRUE);
}
}
return(FALSE);
}
//*******************************************************************
// VaryVariablesDlgProc - Vary the variables
//
// paramaters:
// hDlg - The window handle of this dialog box
// message - The message number
// wParam - The WPARAM parameter for this message
// lParam - The LPARAM parameter for this message
//
// returns:
// depends on message.
//
//*******************************************************************
BOOL CALLBACK VaryVariablesDlgProc(
HWND hDlg,
UINT iMessage,
WPARAM wParam,
LPARAM lParam)
{
char buf[32];
switch (iMessage) {
case WM_INITDIALOG:
SendDlgItemMessage(hDlg, IDD_GUN_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_BULLET_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_BULLET_FLAT_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_EXPLOSION_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_GREMLIN_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_SPACESHIP_LAUNCH_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_SPACESHIP_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_BOMB_LAUNCH_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_BOMB_DELAY, EM_LIMITTEXT, 6, 0L);
SendDlgItemMessage(hDlg, IDD_SOUND, EM_LIMITTEXT, 6, 0L);
// Save starting variables
sprintf(buf, "%d", Tid_Gun_Delay);
SetDlgItemText(hDlg, IDD_GUN_DELAY, buf);
sprintf(buf, "%d", Tid_Bullet_Delay);
SetDlgItemText(hDlg, IDD_BULLET_DELAY, buf);
sprintf(buf, "%d", Tid_Bullet_Flat_Delay);
SetDlgItemText(hDlg, IDD_BULLET_FLAT_DELAY, buf);
sprintf(buf, "%d", Tid_Explosion_Delay);
SetDlgItemText(hDlg, IDD_EXPLOSION_DELAY, buf);
sprintf(buf, "%d", Tid_Gremlin_Delay);
SetDlgItemText(hDlg, IDD_GREMLIN_DELAY, buf);
sprintf(buf, "%d", Tid_SpaceShip_Launch_Delay);
SetDlgItemText(hDlg, IDD_SPACESHIP_LAUNCH_DELAY, buf);
sprintf(buf, "%d", Tid_SpaceShip_Delay);
SetDlgItemText(hDlg, IDD_SPACESHIP_DELAY, buf);
sprintf(buf, "%d", Tid_Bomb_Launch_Delay);
SetDlgItemText(hDlg, IDD_BOMB_LAUNCH_DELAY, buf);
sprintf(buf, "%d", Tid_Bomb_Delay);
SetDlgItemText(hDlg, IDD_BOMB_DELAY, buf);
if (Sound)
strcpy(buf, "TRUE");
else
strcpy(buf, "FALSE");
SetDlgItemText(hDlg, IDD_SOUND, buf);
return(TRUE);
case WM_COMMAND:
switch (GET_WM_COMMAND_ID(wParam, lParam)) {
case IDD_GUN_DELAY:
case IDD_BULLET_DELAY:
case IDD_BULLET_FLAT_DELAY:
case IDD_EXPLOSION_DELAY:
case IDD_GREMLIN_DELAY:
case IDD_SPACESHIP_LAUNCH_DELAY:
case IDD_SPACESHIP_DELAY:
case IDD_BOMB_LAUNCH_DELAY:
case IDD_BOMB_DELAY:
case IDD_SOUND:
if (GET_WM_COMMAND_CMD(wParam, lParam) == EN_CHANGE) {
EnableWindow(GetDlgItem(hDlg, IDOK),
(BOOL) SendMessage((HWND)lParam,
WM_GETTEXTLENGTH, 0, 0L));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -