⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cslug.cpp

📁 坦克大战小游戏 控制说明: 玩家1相关控制: A/W/S/D:控制方向 F:开火 1 :玩家1复活 玩家2相关控制: UP/LEFT/RIGHT/DOWN:控制方
💻 CPP
字号:
//----------------------------------------------------------------------------
// 文件名: CSlug.cpp
//
// 描述:用于子弹对象实现
//
// 作者:朱波		创建日期:2007-03-20
//----------------------------------------------------------------------------

#include <windows.h>   // include important windows stuff
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream.h> // include important C/C++ stuff
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#include <sys/timeb.h>
#include <time.h>
#include <vector>
#include <string>
using namespace std;

#include <ddraw.h>
#include <dsound.h>
#include "dxtools.h"
#include "CSlug.h"

CSlug::CSlug()
{
   m_lpMapRegion = NULL;
   m_draw_width = 0;
   m_draw_hight = 0;
   m_draw_lppSurface = NULL;
   m_curDirect = 0;
   m_myTankID = 0;
   m_myGeometryID = 0;
   m_moveStep = 4;

   m_cur_time;
   m_pre_time_update;
}

CSlug::~CSlug()
{

}

int CSlug::Create( int direct, POINT pos, 
			  LPDIRECTDRAWSURFACE7 lpSurface[], const RECT * lpMapRegion, 
			  vector<GEOMETRY> *lpGeometryVec)
{
	int width; 
	int height;

	if ( direct < 0 || direct > SLUG_PIC_CELL_COUNT - 1 )
	{
		direct = abs(direct)%SLUG_PIC_CELL_COUNT;
	}

	width = (direct%2 == 0) ? SLUG_WIDTH : SLUG_HEIGHT;
	height = (direct%2 == 0) ? SLUG_HEIGHT : SLUG_WIDTH;

	m_curDirect = direct;

	if ( pos.x - SLUG_PIC_CELL_WIDTH/2 < 0 )
	{
		 pos.x = SLUG_PIC_CELL_WIDTH/2;
	}

	if ( pos.x + SLUG_PIC_CELL_WIDTH/2 > SCREEN_WIDTH-1)
	{
		 pos.x = SCREEN_WIDTH - SLUG_PIC_CELL_WIDTH/2 - 1;
	}
		 
	if ( pos.y - SLUG_PIC_CELL_HEIGHT/2 < 0)
	{
		 pos.y = SLUG_PIC_CELL_HEIGHT/2;	
	
	}	 
	
	if ( pos.y + SLUG_PIC_CELL_HEIGHT/2 > SCREEN_HEIGHT-1)
	{
		 pos.y = SCREEN_HEIGHT - SLUG_PIC_CELL_HEIGHT/2 - 1;
	}

	MakeBoundingBox( direct, pos, m_boundingBox );

	if ( NULL != lpMapRegion )
	{
		m_lpMapRegion = (RECT *)lpMapRegion;
		
		if ( m_boundingBox.left < lpMapRegion->left )
		{
			m_boundingBox.left = lpMapRegion->left + 1;
			pos.x = m_boundingBox.left + width/2 + 1;
		}

	    if ( m_boundingBox.right > lpMapRegion->right)
		{
			m_boundingBox.right = lpMapRegion->right - 1;
			pos.x = m_boundingBox.right - width/2 + 1;
		}	

		if ( m_boundingBox.top < lpMapRegion->top )
		{
			m_boundingBox.top = lpMapRegion->top + 1;	
			pos.y = m_boundingBox.top + height/2 + 1;
		}	

		if ( m_boundingBox.bottom > lpMapRegion->bottom )
		{
		    m_boundingBox.bottom = lpMapRegion->bottom - 1;
			pos.y = m_boundingBox.bottom - height/2 + 1;
		}

		MakeBoundingBox( direct, pos, m_boundingBox );
	
	} 
	
	m_draw_start.x = pos.x - SLUG_PIC_CELL_WIDTH/2;
	m_draw_start.y = pos.y - SLUG_PIC_CELL_HEIGHT/2;

	m_position.x = pos.x;
	m_position.y = pos.y;

	m_draw_lppSurface = lpSurface;
	m_lpGeometryVec = lpGeometryVec;

	return(1);
}

int CSlug::Draw( void )
{
    DDraw_Draw_Surface(m_draw_lppSurface[m_curDirect], m_draw_start.x, m_draw_start.y, 
				SLUG_PIC_CELL_WIDTH, SLUG_PIC_CELL_HEIGHT, lpddsback,1);	

	return(1);
}

int CSlug::MakeBoundingBox( int direct, POINT pos, RECT & box )
{
    int width = (direct%2 == 0) ? SLUG_WIDTH : SLUG_HEIGHT;
	int height = (direct%2 == 0) ? SLUG_HEIGHT : SLUG_WIDTH;
	
	box.left   = pos.x - width/2;
	box.right  = pos.x + width/2;
	box.top    = pos.y - height/2;
	box.bottom = pos.y + height/2;

	return(1);
}

int CSlug::Move()
{
	int		time;

	m_cur_time=GetTickCount()%100000;

	if(m_cur_time - m_pre_time_update<0)
		 time = 100000 - m_pre_time_update + m_cur_time;
	else
		 time = m_cur_time - m_pre_time_update;   


	if ( time > 20 )
	{
		m_pre_time_update = m_cur_time;

		switch (m_curDirect)
		{
			case 0:
				 m_position.x += m_moveStep;
				 break;

			case 1:
				 m_position.y += m_moveStep;
				break;

			case 2:
				 m_position.x -= m_moveStep;
				 break;

			case 3:
				 m_position.y -= m_moveStep;
				 break;

			default:
				 return(0);

				break;
		}
		
		MakeBoundingBox( m_curDirect, m_position, m_boundingBox );
		m_draw_start.x = m_position.x - SLUG_PIC_CELL_WIDTH/2;
		m_draw_start.y = m_position.y - SLUG_PIC_CELL_HEIGHT/2;	
	}
	
	return(1);
}

int CSlug::TestHitting( GEOMETRY * lpGeometry )
{
	POINT test_pos;
	RECT  test_boundingBox;

	test_pos.x = m_position.x;
	test_pos.y = m_position.y;
	
	test_boundingBox.bottom = m_boundingBox.bottom;
	test_boundingBox.left	= m_boundingBox.left;
	test_boundingBox.right	= m_boundingBox.right;
	test_boundingBox.top	= m_boundingBox.top;

	switch (m_curDirect)
	{
		case 0:
			 test_pos.x += m_moveStep;
			 break;

		case 1:
			 test_pos.y += m_moveStep;
			 break;

		case 2:
			 test_pos.x -= m_moveStep;
			 break;

		case 3:
			 test_pos.y -= m_moveStep;
			 break;

		default:
			 return(0);

			 break;
	}

	MakeBoundingBox( m_curDirect, test_pos, test_boundingBox );	

	if ( NULL != m_lpMapRegion )
	{
		if ( test_boundingBox.left   <= m_lpMapRegion->left 
			|| test_boundingBox.right  >= m_lpMapRegion->right
			|| test_boundingBox.top    <= m_lpMapRegion->top
			|| test_boundingBox.bottom >= m_lpMapRegion->bottom )
		{
			return(2);
		}
	}

	if ( NULL != m_lpGeometryVec )
	{
		int src_left = test_boundingBox.left;
		int src_right = test_boundingBox.right;
		int src_top	= test_boundingBox.top;
		int src_bottom = test_boundingBox.bottom;
		int dest_left, dest_right, dest_top, dest_bottom;

		for (vector<GEOMETRY>::iterator it=m_lpGeometryVec->begin(); 
				it!=m_lpGeometryVec->end(); it++)
		{
			if (it->m_lpBoundingBox != &m_boundingBox)
			{
				dest_left = it->m_lpBoundingBox->left;
				dest_right = it->m_lpBoundingBox->right;
				dest_top = it->m_lpBoundingBox->top;
				dest_bottom = it->m_lpBoundingBox->bottom;

				if ( ( (src_left   >= dest_left && src_left   <= dest_right) 
					|| (src_right  >= dest_left && src_right  <= dest_right)  )
				 && ( (src_top    >= dest_top  && src_top    <= dest_bottom) 
					|| (src_bottom >= dest_top  && src_bottom <= dest_bottom) ) 
				|| ( (dest_left   >= src_left && dest_left   <= src_right) 
					|| (dest_right  >= src_left && dest_right  <= src_right) )
				 && ( (dest_top    >= src_top  && dest_top    <= src_bottom) 
					|| (dest_bottom >= src_top  && dest_bottom <= src_bottom) ) )
				{
					if ( NULL != lpGeometry )
					{
						lpGeometry->m_lpBoundingBox = it->m_lpBoundingBox;
						lpGeometry->m_objectID	= it->m_objectID;
						lpGeometry->m_style = it->m_style;
					}

					return(1);
				}
			}
		}
	}

	return(0);
}

RECT &	CSlug::GetBoundingBox( void )
{
	return m_boundingBox;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -