isohex9_1.cpp
来自「一個遊戲教程」· C++ 代码 · 共 888 行 · 第 1/2 页
CPP
888 行
/*****************************************************************************
IsoHex9_1.cpp
Ernest S. Pazera
30MAY2000
Start a WIN32 Application Workspace, add in this file
Requires winmm.lib, ddraw.lib, dxguid.lib, dsound.lib
Needs dsound.h, ddraw.h, mmsystem.h
Needs DDFuncs.h/cpp, DSFuncs.h/cpp, GDICanvas.h/cpp, WAVLoader.h/cpp
*****************************************************************************/
//////////////////////////////////////////////////////////////////////////////
//INCLUDES
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <mmsystem.h>
#include "ddraw.h"
#include "dsound.h"
#include "GDICanvas.h"
#include "DDFuncs.h"
#include "DSFuncs.h"
//////////////////////////////////////////////////////////////////////////////
//DEFINES/CONSTANTS
//////////////////////////////////////////////////////////////////////////////
//name for our window class
#define WINDOWCLASS "ISOHEX9"
//title of the application
#define WINDOWTITLE "IsoHex 9-1"
//cound information
const int SOUNDCOUNT=9;//number of sounds in game
const int SOUNDCOPYCOUNT=4;//number of copies of sounds in game
const int SND_BOUNCE=0;
const int SND_LOSE=1;
const int SND_HIT=2;
const int SND_WIN=8;
//ball dimensions
const int BALLWIDTH=8;
const int BALLHEIGHT=8;
//paddle dimensions
const int PADDLEWIDTH=64;
const int PADDLEHEIGHT=16;
//brick dimensions
const int BRICKWIDTH=32;
const int BRICKHEIGHT=16;
const int BRICKCOUNT=6;
//screen dimensions
const int SCREENWIDTH=640;
const int SCREENHEIGHT=480;
const int SCREENBPP=16;
//brick map
const int MAPCOLUMNCOUNT=SCREENWIDTH/BRICKWIDTH;//number of bricks per row
const int MAPROWCOUNT=12;//number of rows
const int MAPOFFSET=64;//pixel offset from top of screen
//game states
enum GAMESTATE {GS_START,GS_STARTWAIT,GS_PLAY,GS_DEAD,GS_RESET,GS_WINLEVEL,GS_LOSEGAME};
//////////////////////////////////////////////////////////////////////////////
//PROTOTYPES
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init();//game data initalizer
void Prog_Loop();//main game loop
void Prog_Done();//game clean up
//initialization functions
void DD_Init();
void DS_Init();
//cleanup functions
void DD_Done();
void DS_Done();
//game functions
void SetUpGame();
void ResetGame();
void ShowBoard();
void DrawBrick(int x, int y, int bricknum);
void DrawPaddle();
void DrawBall();
void MoveBall();
void SoundPlay(int sound);
void ShowScore();
void ShowLives();
//////////////////////////////////////////////////////////////////////////////
//GLOBALS
//////////////////////////////////////////////////////////////////////////////
HINSTANCE hInstMain=NULL;//main application handle
HWND hWndMain=NULL;//handle to our main window
//direct draw variables
LPDIRECTDRAW7 lpdd=NULL;//main controller
LPDIRECTDRAWSURFACE7 lpddsprime=NULL;//primary surface
LPDIRECTDRAWSURFACE7 lpddsback=NULL;//back buffer
LPDIRECTDRAWSURFACE7 lpddsball=NULL;//ball
LPDIRECTDRAWSURFACE7 lpddspaddle=NULL;//paddle
LPDIRECTDRAWSURFACE7 lpddsbricks=NULL;//bricks
//other graphical objects
HFONT hfntMain=NULL;
//direct sound variables
LPDIRECTSOUND lpds=NULL;//main sound controller
LPDIRECTSOUNDBUFFER lpdsb[SOUNDCOUNT][SOUNDCOPYCOUNT];//sounds
DWORD dwSoundCopy[SOUNDCOUNT];//which copy of any given sound is next to be played
LPDIRECTSOUNDBUFFER lpdsbMusic=NULL;
LPDIRECTSOUNDBUFFER lpdsbOoYeah=NULL;
LPDIRECTSOUNDBUFFER lpdsbGroovy=NULL;
LPDIRECTSOUNDBUFFER lpdsbLoveThing=NULL;
//main game state
GAMESTATE GameState=GS_START;
//game variables
int Board[MAPCOLUMNCOUNT][MAPROWCOUNT];
DWORD dwScore=0;
DWORD dwLives=0;
DWORD dwBrickCount=0;
DWORD dwBricksHit=0;
//paddle position
POINT ptPaddle;
//ball position
POINT ptBall;
POINT ptBallNext;
POINT ptBallVel;
bool bHitTop=false;
//////////////////////////////////////////////////////////////////////////////
//WINDOWPROC
//////////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
//which message did we get?
switch(uMsg)
{
case WM_LBUTTONDOWN:
{
switch(GameState)
{
case GS_STARTWAIT:
{
GameState=GS_PLAY;
}break;
default:
{
}break;
}
}break;
case WM_MOUSEMOVE:
{
ptPaddle.x=LOWORD(lParam)-PADDLEWIDTH/2;
if(ptPaddle.x<0) ptPaddle.x=0;
if(ptPaddle.x>SCREENWIDTH-PADDLEWIDTH) ptPaddle.x=SCREENWIDTH-PADDLEWIDTH;
}break;
case WM_DESTROY://the window is being destroyed
{
//tell the application we are quitting
PostQuitMessage(0);
//handled message, so return 0
return(0);
}break;
case WM_PAINT://the window needs repainting
{
//a variable needed for painting information
PAINTSTRUCT ps;
//start painting
HDC hdc=BeginPaint(hwnd,&ps);
/////////////////////////////
//painting code would go here
/////////////////////////////
//end painting
EndPaint(hwnd,&ps);
//handled message, so return 0
return(0);
}break;
}
//pass along any other message to default message handler
return(DefWindowProc(hwnd,uMsg,wParam,lParam));
}
//////////////////////////////////////////////////////////////////////////////
//WINMAIN
//////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
{
//assign instance to global variable
hInstMain=hInstance;
//create window class
WNDCLASSEX wcx;
//set the size of the structure
wcx.cbSize=sizeof(WNDCLASSEX);
//class style
wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
//window procedure
wcx.lpfnWndProc=TheWindowProc;
//class extra
wcx.cbClsExtra=0;
//window extra
wcx.cbWndExtra=0;
//application handle
wcx.hInstance=hInstMain;
//icon
wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
//cursor
wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
//background color
wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
//menu
wcx.lpszMenuName=NULL;
//class name
wcx.lpszClassName=WINDOWCLASS;
//small icon
wcx.hIconSm=NULL;
//register the window class, return 0 if not successful
if(!RegisterClassEx(&wcx)) return(0);
//create main window
hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
//error check
if(!hWndMain) return(0);
//if program initialization failed, then return with 0
if(!Prog_Init()) return(0);
//message structure
MSG msg;
//message pump
for(;;)
{
//look for a message
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
//there is a message
//check that we arent quitting
if(msg.message==WM_QUIT) break;
//translate message
TranslateMessage(&msg);
//dispatch message
DispatchMessage(&msg);
}
//run main game loop
Prog_Loop();
}
//clean up program data
Prog_Done();
//return the wparam from the WM_QUIT message
return(msg.wParam);
}
//////////////////////////////////////////////////////////////////////////////
//INITIALIZATION
//////////////////////////////////////////////////////////////////////////////
bool Prog_Init()
{
//hide the cursor
ShowCursor(FALSE);
//initialize direct draw
DD_Init();
//initialize direct sound
DS_Init();
//set initial gamestate
GameState=GS_START;
return(true);//return success
}
//////////////////////////////////////////////////////////////////////////////
//CLEANUP
//////////////////////////////////////////////////////////////////////////////
void Prog_Done()
{
//clean up direct draw
DD_Done();
//clean up direct sound
DS_Done();
//show the cursor
ShowCursor(TRUE);
}
//////////////////////////////////////////////////////////////////////////////
//MAIN GAME LOOP
//////////////////////////////////////////////////////////////////////////////
void Prog_Loop()
{
switch(GameState)
{
case GS_START:
{
dwScore=0;
dwLives=3;
SetUpGame();
GameState=GS_STARTWAIT;
}break;
case GS_STARTWAIT:
{
ShowBoard();
lpddsprime->Flip(NULL,DDFLIP_WAIT);
}break;
case GS_PLAY:
{
//limit frame time
DWORD dwTimeStart=GetTickCount();
//move ball
MoveBall();
//show board
ShowBoard();
//show frame
lpddsprime->Flip(NULL,DDFLIP_WAIT);
//wait for 10 ms to elapse
while(GetTickCount()-dwTimeStart<10);
}break;
case GS_DEAD:
{
dwLives--;
if(dwLives)
{
GameState=GS_RESET;
}
else
{
GameState=GS_LOSEGAME;
}
}break;
case GS_RESET:
{
ResetGame();
GameState=GS_STARTWAIT;
}break;
case GS_WINLEVEL:
{
SetUpGame();
GameState=GS_STARTWAIT;
}break;
case GS_LOSEGAME:
{
GameState=GS_START;
}break;
}
}
//initialize direct draw vars
void DD_Init()
{
//create direct draw object
lpdd=LPDD_Create(hWndMain,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT);
//set display
lpdd->SetDisplayMode(SCREENWIDTH,SCREENHEIGHT,SCREENBPP,0,0);
//create primary surface, with one back buffer
lpddsprime=LPDDS_CreatePrimary(lpdd,1);
//get back buffer
lpddsback=LPDDS_GetSecondary(lpddsprime);
//add font
AddFontResource("paganini.ttf");
//create font
hfntMain=CreateFont(30,0,0,0,0,0,0,0,0,0,0,0,0,"Paganini");
//create offscreen surfaces
//ball
lpddsball=LPDDS_LoadFromFile(lpdd,"IsoHex9_1-2.bmp");
LPDDS_SetSrcColorKey(lpddsball,0);
//paddle
lpddspaddle=LPDDS_LoadFromFile(lpdd,"IsoHex9_1-3.bmp");
//bricks
lpddsbricks=LPDDS_LoadFromFile(lpdd,"IsoHex9_1-1.bmp");
//clear out back buffer
DDBLTFX ddbltfx;
DDBLTFX_ColorFill(&ddbltfx,0);
lpddsback->Blt(NULL,NULL,NULL,DDBLT_WAIT | DDBLT_COLORFILL,&ddbltfx);
}
//initialize direct sound vars
void DS_Init()
{
//create sound manager
DirectSoundCreate(NULL,&lpds,NULL);
//cooperate with main window
lpds->SetCooperativeLevel(hWndMain,DSSCL_NORMAL);
//load sounds
lpdsb[0][0]=LPDSB_LoadFromFile(lpds,"bounce.wav",0);
lpdsb[1][0]=LPDSB_LoadFromFile(lpds,"lose.wav",0);
lpdsb[2][0]=LPDSB_LoadFromFile(lpds,"hit5.wav",0);
lpdsb[3][0]=LPDSB_LoadFromFile(lpds,"hit4.wav",0);
lpdsb[4][0]=LPDSB_LoadFromFile(lpds,"hit3.wav",0);
lpdsb[5][0]=LPDSB_LoadFromFile(lpds,"hit2.wav",0);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?