www.pudn.com > MD6__game.rar > Dazi.c, change:1999-09-09,size:8649b


//simpwin.c 
#include <windows.h> 
#include <stdio.h> 
#include <time.h> 
#include <mmsystem.h> 
#include "DAZI.h" 
#include "resource.h" 
/////////GLOBALS///////// 
HINSTANCE hInst; 
HWND hWndMain; 
LETTERBOMB letterbomb[7]; 
UINT  GameSpeed=150; 
static int nCount=1,nMisscnt; 
static float fTimecount=0; 
static BOOL bIsPlaying=	TRUE; 
int   nBACKID=1; 
//The WinMainFunc 
int APIENTRY WinMain(HINSTANCE hInstance, 
                     HINSTANCE hPreInstance, 
					 LPSTR lpCmdLine, 
					 int nCmdShow) 
{ 
     MSG msg; 
	 if(!InitApplication(hInstance)) 
	    return FALSE; 
     if(!InitInstance(hInstance,nCmdShow)) 
	    return FALSE; 
     while(GetMessage(&msg,NULL,0,0)) 
	 { 
	     TranslateMessage(&msg); 
		 DispatchMessage(&msg); 
     } 
	 return(msg.wParam); 
} 
//THE INITWNDCLASS FUNCTION 
 
BOOL InitApplication(HINSTANCE hInstance) 
{ 
	WNDCLASS wcdaziwin; 
	wcdaziwin.style=CS_HREDRAW|CS_VREDRAW; 
	wcdaziwin.lpfnWndProc=(WNDPROC)MainWndProc; 
	wcdaziwin.cbClsExtra=0; 
	wcdaziwin.cbWndExtra=0; 
	wcdaziwin.hInstance=hInstance; 
	wcdaziwin.hIcon=LoadIcon(hInstance,"IDI_ICON1"); 
	wcdaziwin.hCursor=LoadCursor(NULL,IDC_ARROW); 
	wcdaziwin.hbrBackground=GetStockObject(WHITE_BRUSH); 
	wcdaziwin.lpszMenuName="IDR_MENU1"; 
	wcdaziwin.lpszClassName="daziwinclass"; 
	return (RegisterClass(&wcdaziwin)); 
} 
//THE INIT MAINWND FUNCTION 
BOOL InitInstance(HINSTANCE hInstance,int nCmdShow) 
{ 
	hInst=hInstance; 
	hWndMain=CreateWindow( 
		"daziwinclass","打字练习", 
		 WS_DLGFRAME|WS_SYSMENU|WS_MINIMIZEBOX|WS_VISIBLE, 
		 100,100,300,250, 
		 NULL, NULL,hInstance,NULL); 
	if(!hWndMain)return FALSE; 
	return TRUE; 
} 
 
//THE WNDPROC FUNCTION 
LRESULT CALLBACK MainWndProc(HWND hWnd, 
   UINT message,WPARAM wParam,LPARAM lParam) 
{ 
	static HWND hstatic; 
	static BOOL  bmenuchecked=FALSE; 
	static HMENU hmenu; 
	switch(message) 
	{ 
	case WM_CREATE: 
		hmenu=GetMenu(hWnd); 
		hstatic=CreateWindow("STATIC","",WS_CHILD|WS_VISIBLE, 
			1,185,292,20,hWnd,(HMENU)IDS_STATIC,hInst,NULL); 
		OnCreate(hWnd); 
		break; 
	case WM_PAINT: 
		OnPaint(hWnd); 
		break;  
	case WM_TIMER: 
		switch(wParam) 
		{ 
		case 1: 
		OnTimer(hWnd,hstatic); 
		break; 
		case 2: 
			if(nBACKID<2) 
				nBACKID++; 
			else nBACKID=0; 
			break; 
		} 
		break; 
	case WM_CHAR: 
		if(bIsPlaying)OnHitkey(hWnd,wParam); 
		break; 
	case WM_DESTROY: 
		mciSendString("close hit.wav",0,0,0); 
		mciSendString("close backgrd.mid",0,0,0); 
		KillTimer(hWnd,2); 
		KillTimer(hWnd,1); 
		PostQuitMessage(0); 
		break; 
	case WM_COMMAND: 
		switch(LOWORD(wParam)) 
		{ 
		case IDM_PLAY: 
           bIsPlaying=TRUE; 
		   InitGame(); 
		   SetTimer(hWnd,1,GameSpeed,NULL); 
		   break; 
		case IDM_ABOUT: 
			DialogBox(hInst,"IDD_DIALOG1",hWnd,(DLGPROC)About); 
            if(bIsPlaying)SetTimer(hWnd,1,GameSpeed,NULL); 
			break; 
		case IDM_SLOW: 
			GameSpeed=150; 
           if(bIsPlaying)SetTimer(hWnd,1,GameSpeed,NULL); 
 			break; 
		case IDM_NORMAL: 
			GameSpeed=70; 
            if(bIsPlaying)SetTimer(hWnd,1,GameSpeed,NULL); 
			break; 
		case IDM_FAST: 
			GameSpeed=10; 
            if(bIsPlaying)SetTimer(hWnd,1,GameSpeed,NULL); 
			break; 
		case IDM_MUSIC: 
			if(bmenuchecked) 
			{ 
				CheckMenuItem(hmenu,IDM_MUSIC,MF_UNCHECKED); 
				mciSendString("pause back.mid",0,0,0); 
				bmenuchecked=!bmenuchecked; 
			} 
			else 
			{ 
				CheckMenuItem(hmenu,IDM_MUSIC,MF_CHECKED); 
				mciSendString("seek back.mid to start",0,0,0); 
				mciSendString("play back.mid ",0,0,0); 
				bmenuchecked=!bmenuchecked; 
			} 
            if(bIsPlaying)SetTimer(hWnd,1,GameSpeed,NULL); 
			break; 
		case IDM_EXIT: 
		   DestroyWindow(hWnd); 
		} 
	    break; 
	default: 
		return(DefWindowProc(hWnd,message,wParam,lParam)); 
	} 
	return 0; 
} 
///////FUNC ONTIMER/////// 
void OnTimer(HWND hWnd,HWND hstatic) 
{ 
	HDC hdc,hmemdc,htempdc; 
    HBITMAP hbfont,hbmask,holdbmpbgr,holdbmpfont_; 
    HBITMAP  hcurrentbackgrd; 
	static char  windowtext[100]=""; 
	int i,nID; 
 
	fTimecount=fTimecount+(float)((float)GameSpeed/1000); 
	hdc=GetDC(hWnd); 
	hmemdc=CreateCompatibleDC(hdc); 
	htempdc=CreateCompatibleDC(hdc); 
	hcurrentbackgrd=LoadBitmap(hInst,BACKGRD[nBACKID]); 
	holdbmpbgr=SelectObject(hmemdc,hcurrentbackgrd); 
	for(i=0;i<=6;i++) 
		if(letterbomb[i].yPos==125) 
		{ 
			letterbomb[i].exist=FALSE; 
			nMisscnt++; 
		} 
		else letterbomb[i].yPos++;  
		////// 
		sprintf(windowtext,"时间:%3.1f秒  %d错过%d击中:%2d%%  %d字/分" 
		,fTimecount,nCount,nMisscnt,(int)((nCount-nMisscnt)*100/nCount) 
		,(int)(nCount*60/fTimecount)); 
		////// 
		for(i=0;i<=6;i++) 
		if(letterbomb[i].exist==FALSE) 
		{ 
			nID=(33*rand())/RAND_MAX; 
			letterbomb[i].charactor=CHARACTORS[nID]; 
			letterbomb[i].lpbmpsrc=BITMAPS[nID]; 
			letterbomb[i].lpbmpmask=BITMAPS[nID+33]; 
			letterbomb[i].yPos =0; 
			letterbomb[i].exist=TRUE; 
			nCount++; 
		} 
	for(i=0;i<=6;i++) 
	{ 
	    hbmask=LoadBitmap(hInst,letterbomb[i].lpbmpmask); 
		holdbmpfont_=SelectObject(htempdc,hbmask); 
	    BitBlt(hmemdc,10+40*i,letterbomb[i].yPos 
			,35,60,htempdc,0,0,SRCAND); 
	    DeleteObject(SelectObject(htempdc,holdbmpfont_)); 
		hbfont=LoadBitmap(hInst,letterbomb[i].lpbmpsrc); 
	    holdbmpfont_=SelectObject(htempdc,hbfont); 
	    BitBlt(hmemdc,10+40*i,letterbomb[i].yPos 
			,35,60,htempdc,0,0,SRCINVERT); 
	    DeleteObject(SelectObject(htempdc,holdbmpfont_)); 
	} 
	BitBlt(hdc,0,0,300,220,hmemdc,0,0,SRCCOPY); 
	DeleteObject(SelectObject(hmemdc,holdbmpbgr)); 
	DeleteObject(hmemdc); 
	DeleteObject(htempdc); 
	SetWindowText(hstatic,windowtext); 
	if(nMisscnt>=10) 
	{ 
		KillTimer(hWnd,1); 
		bIsPlaying=FALSE; 
		SetWindowText(hstatic,"游戏结束!"); 
	} 
	if(nCount>=300) 
	{ 
		KillTimer(hWnd,1); 
		bIsPlaying=FALSE; 
    	hmemdc=CreateCompatibleDC(hdc); 
		hcurrentbackgrd=LoadBitmap(hInst,BACKGRD[nBACKID]); 
		holdbmpbgr=SelectObject(hmemdc,hcurrentbackgrd); 
		BitBlt(hdc,0,0,300,220,hmemdc,0,0,SRCCOPY); 
		DeleteObject(SelectObject(hmemdc,holdbmpbgr)); 
		SetWindowText(hstatic,"已经过关!"); 
		mciSendString("seek back.mid to start",0,0,0); 
		mciSendString("play back.mid ",0,0,0); 
 
	} 
	ReleaseDC(hWnd,hdc); 
} 
/////////FUNC ON CREATE////// 
void OnCreate(HWND hWnd) 
{ 
	int i,nID;	 
	srand( (unsigned)time( NULL ) );  
	mciSendString("open hit.wav",0,0,0); 
	mciSendString("open back.mid",0,0,0); 
	mciSendString("play back.mid ",0,0,0); 
    SetTimer(hWnd,1,GameSpeed,NULL); 
	SetTimer(hWnd,2,5000,NULL); 
	for(i=0;i<=6;i++) 
	{ 
		letterbomb[i].exist=TRUE; 
        nID=(33*rand())/RAND_MAX; 
		letterbomb[i].charactor=CHARACTORS[nID]; 
		letterbomb[i].lpbmpsrc=BITMAPS[nID]; 
		letterbomb[i].lpbmpmask=BITMAPS[nID+33]; 
		letterbomb[i].yPos =0; 
	} 
} 
//////////FUNC ON PAINT//////// 
void OnPaint(HWND hWnd) 
{ 
    HDC hdc,hmemdc; 
	PAINTSTRUCT ps; 
	HBITMAP hbackgrd1,holdBMP; 
	hdc=BeginPaint(hWnd,&ps); 
    hmemdc=CreateCompatibleDC(hdc); 
	hbackgrd1=LoadBitmap(hInst,BACKGRD[nBACKID]); 
	holdBMP=SelectObject(hmemdc,hbackgrd1); 
	BitBlt(hdc,0,0,300,250,hmemdc,0,0,SRCCOPY); 
	DeleteObject(SelectObject(hmemdc,holdBMP)); 
	DeleteObject(hmemdc); 
	EndPaint(hWnd,&ps);  
} 
//////////FUNC ON HINTKEY////// 
void OnHitkey(HWND hWnd,WPARAM wParam) 
{ 
	int i; 
	HDC hdc ,hmemdc,hmemdcmask; 
	HBITMAP hbmp,hbmpmask,holdbmp,holdbmpmask; 
	hdc=GetDC(hWnd); 
	hmemdc=CreateCompatibleDC(hdc); 
	hmemdcmask=CreateCompatibleDC(hdc); 
	hbmp=LoadBitmap(hInst,"IDB_BOMB_"); 
	hbmpmask=LoadBitmap(hInst,"IDB_BOMB"); 
	holdbmp=SelectObject(hmemdc,hbmp); 
	holdbmpmask=SelectObject(hmemdcmask,hbmpmask); 
	for(i=0;i<=6;i++) 
		if(letterbomb[i].charactor==(TCHAR)wParam) 
		{ 
			letterbomb[i].exist=FALSE; 
		    BitBlt(hdc,10+40*i,letterbomb[i].yPos 
				,35,60,hmemdcmask,0,0,SRCAND); 
			BitBlt(hdc,10+40*i,letterbomb[i].yPos 
				,35,60,hmemdc,0,0,SRCINVERT); 
			mciSendString("play hit.wav from 0 to 1000",0,0,0);	 
		} 
	DeleteObject(SelectObject(hmemdc,holdbmp)); 
	DeleteObject(SelectObject(hmemdcmask,holdbmpmask)); 
	DeleteObject(hmemdc); 
	DeleteObject(hmemdcmask); 
} 
/////////FUNC INITGAME////// 
void InitGame(void) 
{ 
	int i,nID;	 
	for(i=0;i<=6;i++) 
	{ 
		letterbomb[i].exist=TRUE; 
        nID=(33*rand())/RAND_MAX; 
		letterbomb[i].charactor=CHARACTORS[nID]; 
		letterbomb[i].lpbmpsrc=BITMAPS[nID]; 
		letterbomb[i].lpbmpmask=BITMAPS[nID+33]; 
		letterbomb[i].yPos =0; 
	} 
	nCount=1; 
	nMisscnt=0; 
	fTimecount=0; 
} 
///////////////about proc////////// 
BOOL APIENTRY About(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam) 
{ 
	switch(message) 
	{ 
	case WM_INITDIALOG: 
			return (TRUE); 
	case WM_COMMAND: 
		if(LOWORD(wParam)==IDOK) 
		{ 
			EndDialog(hDlg,TRUE); 
			return (TRUE); 
		} 
		break; 
	} 
	return (FALSE); 
}