展开全部
只是部分代码,太多了,请加分后联系我
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Game.h"
#include "resource.h"
#define SCREEN_W 640
#define SCREEN_H 480
#define OFFSETX 100
#define OFFSETY 48
#define PLAYER1_STARTX 130
#define PLAYER1_STARTY 386
#define PLAYER2_STARTX 258
#define PLAYER2_STARTY 386
#define SAFE_RELEASE(x) if(x){ x->Release(); x = NULL; }
CGame* g_pGame;
inline int random( int min, int max )
{
return (min + rand() % (max - min + 1));
}
void TRACE( LPCTSTR format, ... )
{
char buf[128];
va_list vl;
va_start(vl, format);
sprintf(buf, format, vl);
OutputDebugString( buf );
va_end(vl);
}
void CGame::OutputText( int x, int y, LPCTSTR string )
{
HDC hdc;
if( m_pddsBackBuffer &&
m_pddsBackBuffer->GetDC(&hdc) == DD_OK )
{
SetBkMode( hdc, TRANSPARENT );
SetTextColor( hdc, RGB(255,255,0) );
TextOut( hdc, x, y, string, lstrlen(string) );
m_pddsBackBuffer->ReleaseDC( hdc );
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame()
{
m_hInst = NULL;
m_hWnd = NULL;
m_bActive = FALSE;
m_bShowStats = FALSE;
m_bFullScreen = TRUE;
m_bSingle = TRUE;
m_pDD = NULL;
m_pddsFrontBuffer = NULL;
m_pddsBackBuffer = NULL;
g_pGame = this;
}
CGame::~CGame()
{
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( g_pGame )
return g_pGame->MsgProc( hWnd, uMsg, wParam, lParam );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
LRESULT CGame::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_ACTIVATEAPP:
if( m_bActive = (BOOL)wParam )
m_DirectInput.Acquire();
return 0;
case WM_SETCURSOR:
if( m_bFullScreen )
{
SetCursor( NULL );
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_MOVE:
m_bActive = TRUE;
GetClientRect( hWnd, &m_rcWindow );
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow );
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow + 1 );
return 0;
case WM_MOVING:
m_bActive = FALSE;
break;
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
if( m_gameState == GS_ACTIVE )
m_gameState = GS_SPLASH;
else if( m_gameState == GS_SPLASH )
PostQuitMessage(0);
else if( m_gameState == GS_OVER )
m_gameState = GS_SPLASH;
break;
case VK_F4:
DDTerm();
m_bFullScreen = !m_bFullScreen;
DDInit();
return 0;
case VK_F5:
m_bShowStats = !m_bShowStats;
return 0;
case VK_DOWN:
case VK_UP:
if( m_gameState == GS_SPLASH )
{
m_bSingle = !m_bSingle;
return 0;
}
break;
case VK_PRIOR:
if( m_gameState == GS_ACTIVE && m_nLevel > 1 )
{
m_nLevel --;
InitLevel();
}
break;
case VK_NEXT:
if( m_gameState == GS_ACTIVE )
{
m_nLevel ++;
InitLevel();
}
break;
case VK_RETURN:
if( m_gameState == GS_SPLASH )
{
ResetGame();
return 0;
}
else if( m_gameState == GS_ACTIVE && !m_bSingle )
{
if( m_player[0].m_nLife > 1 &&
m_player[1].m_nLife <= 0 )
{
m_player[0].m_nLife --;
m_player[1].m_nLife = 1;
ResetPlayer( m_player[1] );
}
else if( m_player[1].m_nLife > 1 &&
m_player[0].m_nLife <= 0 )
{
m_player[1].m_nLife --;
m_player[0].m_nLife = 1;
ResetPlayer( m_player[0] );
}
}
break;
}
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
BOOL CGame::Initialize( HINSTANCE hInst )
{
m_hInst = hInst;
if( !InitApplication() ||
!DDInit() ||
!InitGame() )
{
DDTerm();
return FALSE;
}
return TRUE;
}
BOOL CGame::InitApplication()
{
char szClassName[] = "BATTLECITY";
char szTitle[] = "Battle City";
WNDCLASS wc;
wc.hInstance = m_hInst;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = WndProc;
wc.style = CS_DBLCLKS;
wc.hIcon = LoadIcon( m_hInst, MAKEINTRESOURCE(IDI_TANK) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
if( !RegisterClass( &wc ) )
{
TRACE( "Error In RegisterClassEx\n" );
return FALSE;
}
m_hWnd = CreateWindow( szClassName,
szTitle,
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
646, 505,
NULL,
NULL,
m_hInst,
NULL );
if( !m_hWnd )
{
TRACE( "Error In CreateWindow\n" );
return FALSE;
}
UpdateWindow( m_hWnd );
ShowWindow( m_hWnd, SW_NORMAL );
return TRUE;
}
int CGame::Run()
{
MSG msg;
while( TRUE )
{
if (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage( &msg, NULL, 0, 0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if( m_bActive )
{
UpdateFrame();
}
else
WaitMessage();
}
DDTerm();
return msg.wParam;
}
BOOL CGame::DDInit()
{
HRESULT hr;
hr = DirectDrawCreate( NULL, &m_pDD, NULL );
if( FAILED(hr) )
{
TRACE( "Error Create DirectDraw\n" );
return FALSE;
}
if( m_bFullScreen )
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
else
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_NORMAL );
// Game.cpp: implementation of the CGame class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "Game.h"
#include "resource.h"
#define SCREEN_W 640
#define SCREEN_H 480
#define OFFSETX 100
#define OFFSETY 48
#define PLAYER1_STARTX 130
#define PLAYER1_STARTY 386
#define PLAYER2_STARTX 258
#define PLAYER2_STARTY 386
#define SAFE_RELEASE(x) if(x){ x->Release(); x = NULL; }
CGame* g_pGame;
inline int random( int min, int max )
{
return (min + rand() % (max - min + 1));
}
void TRACE( LPCTSTR format, ... )
{
char buf[128];
va_list vl;
va_start(vl, format);
sprintf(buf, format, vl);
OutputDebugString( buf );
va_end(vl);
}
void CGame::OutputText( int x, int y, LPCTSTR string )
{
HDC hdc;
if( m_pddsBackBuffer &&
m_pddsBackBuffer->GetDC(&hdc) == DD_OK )
{
SetBkMode( hdc, TRANSPARENT );
SetTextColor( hdc, RGB(255,255,0) );
TextOut( hdc, x, y, string, lstrlen(string) );
m_pddsBackBuffer->ReleaseDC( hdc );
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGame::CGame()
{
m_hInst = NULL;
m_hWnd = NULL;
m_bActive = FALSE;
m_bShowStats = FALSE;
m_bFullScreen = TRUE;
m_bSingle = TRUE;
m_pDD = NULL;
m_pddsFrontBuffer = NULL;
m_pddsBackBuffer = NULL;
g_pGame = this;
}
CGame::~CGame()
{
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
if( g_pGame )
return g_pGame->MsgProc( hWnd, uMsg, wParam, lParam );
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
LRESULT CGame::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
switch( uMsg )
{
case WM_ACTIVATEAPP:
if( m_bActive = (BOOL)wParam )
m_DirectInput.Acquire();
return 0;
case WM_SETCURSOR:
if( m_bFullScreen )
{
SetCursor( NULL );
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_MOVE:
m_bActive = TRUE;
GetClientRect( hWnd, &m_rcWindow );
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow );
ClientToScreen( hWnd, (LPPOINT)&m_rcWindow + 1 );
return 0;
case WM_MOVING:
m_bActive = FALSE;
break;
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
if( m_gameState == GS_ACTIVE )
m_gameState = GS_SPLASH;
else if( m_gameState == GS_SPLASH )
PostQuitMessage(0);
else if( m_gameState == GS_OVER )
m_gameState = GS_SPLASH;
break;
case VK_F4:
DDTerm();
m_bFullScreen = !m_bFullScreen;
DDInit();
return 0;
case VK_F5:
m_bShowStats = !m_bShowStats;
return 0;
case VK_DOWN:
case VK_UP:
if( m_gameState == GS_SPLASH )
{
m_bSingle = !m_bSingle;
return 0;
}
break;
case VK_PRIOR:
if( m_gameState == GS_ACTIVE && m_nLevel > 1 )
{
m_nLevel --;
InitLevel();
}
break;
case VK_NEXT:
if( m_gameState == GS_ACTIVE )
{
m_nLevel ++;
InitLevel();
}
break;
case VK_RETURN:
if( m_gameState == GS_SPLASH )
{
ResetGame();
return 0;
}
else if( m_gameState == GS_ACTIVE && !m_bSingle )
{
if( m_player[0].m_nLife > 1 &&
m_player[1].m_nLife <= 0 )
{
m_player[0].m_nLife --;
m_player[1].m_nLife = 1;
ResetPlayer( m_player[1] );
}
else if( m_player[1].m_nLife > 1 &&
m_player[0].m_nLife <= 0 )
{
m_player[1].m_nLife --;
m_player[0].m_nLife = 1;
ResetPlayer( m_player[0] );
}
}
break;
}
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
BOOL CGame::Initialize( HINSTANCE hInst )
{
m_hInst = hInst;
if( !InitApplication() ||
!DDInit() ||
!InitGame() )
{
DDTerm();
return FALSE;
}
return TRUE;
}
BOOL CGame::InitApplication()
{
char szClassName[] = "BATTLECITY";
char szTitle[] = "Battle City";
WNDCLASS wc;
wc.hInstance = m_hInst;
wc.lpszClassName = szClassName;
wc.lpfnWndProc = WndProc;
wc.style = CS_DBLCLKS;
wc.hIcon = LoadIcon( m_hInst, MAKEINTRESOURCE(IDI_TANK) );
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.lpszMenuName = NULL;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
if( !RegisterClass( &wc ) )
{
TRACE( "Error In RegisterClassEx\n" );
return FALSE;
}
m_hWnd = CreateWindow( szClassName,
szTitle,
WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
646, 505,
NULL,
NULL,
m_hInst,
NULL );
if( !m_hWnd )
{
TRACE( "Error In CreateWindow\n" );
return FALSE;
}
UpdateWindow( m_hWnd );
ShowWindow( m_hWnd, SW_NORMAL );
return TRUE;
}
int CGame::Run()
{
MSG msg;
while( TRUE )
{
if (PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE))
{
if (!GetMessage( &msg, NULL, 0, 0))
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else if( m_bActive )
{
UpdateFrame();
}
else
WaitMessage();
}
DDTerm();
return msg.wParam;
}
BOOL CGame::DDInit()
{
HRESULT hr;
hr = DirectDrawCreate( NULL, &m_pDD, NULL );
if( FAILED(hr) )
{
TRACE( "Error Create DirectDraw\n" );
return FALSE;
}
if( m_bFullScreen )
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
else
m_pDD->SetCooperativeLevel( m_hWnd, DDSCL_NORMAL );
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
到C++吧里去找,是贴吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询