C语言坦克大战中,让坦克移动,需要将每个构成得方块都进行坐标变化,有没有方法把坦克封装成一个块,只

C语言坦克大战中,让坦克移动,需要将每个构成得方块都进行坐标变化,有没有方法把坦克封装成一个块,只,第1张

坦克只有四个方向

你用方向缺橘陪去确定坦克当前的形状,

然后用一个顶点伍敏的坐标来确定大大的那一伏蠢块的位置就可以了吧

至于四个方向的形状,也可以用矩阵变化,但是还不如直接分别列出那些坐标来的快

只是部分代码,太多了,请加分后联系我

// 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, ... )

{

charbuf[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 )


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12542769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存