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

📄 snake.cpp

📁 一个自己编写的贪食蛇游戏.
💻 CPP
字号:
// Snake.cpp: implementation of the Snake class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "GSnake.h"
#include "Snake.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

GSnake*	Snake::m_pGSnake;

Snake::Snake(GSnake * theGSnake, int iLen, COLORREF rCol)
{
	m_pGSnake = theGSnake;
	if(iLen < 1 || iLen > iLen)
		iLen = 6;
	m_iLength	= iLen;
	m_iAppleEat = 0;
	m_bIsDie	= false;
	m_bIsEat	= false;
	m_rColor	= rCol;
	m_to		= SNK_RIGHT;
	for(int i = 1; i <= TOTALLEN; i++)
	{	
		if(i <= iLen)
		{
			m_pos[i].x = iLen - i + 1 ;
			m_pos[i].y = QI_GAO;
		}
		else
		{
			m_pos[i].x = 0;
			m_pos[i].y = 0;
		}
	}

	ComputeRect();

}

Snake::~Snake()
{
	m_pGSnake = 0;
}

void Snake::Show(HDC hdc, bool isMove, bool isAdd)
{
	HPEN	hPen,	oldhPen;
	HBRUSH	hBrush, oldhBrush;
	
	hPen	= CreatePen(PS_INSIDEFRAME,1,RGB(0,0,0));
	oldhPen = (HPEN)SelectObject(hdc,(HPEN)hPen);
	hBrush	= (HBRUSH)CreateSolidBrush(m_rColor);

	oldhBrush = (HBRUSH)SelectObject(hdc,(HBRUSH)hBrush);
	
	if(isAdd || isMove)
	{
		
		int x = m_pos[1].x;
		int y = m_pos[1].y;

		if(PosToPixels(x, y))
		{
			Rectangle(hdc, x, y, x+SNK_KUAN, y+SNK_KUAN);
		}

		if(isMove) ClearBlock(hdc,m_pos[0].x,m_pos[0].y);

	}
	else
	{
		for(int i = 1; i <= m_iLength; i++)
		{	
			int x = m_pos[i].x;
			int y = m_pos[i].y;

			if(PosToPixels(x, y))
			{
				Rectangle(hdc, x, y, x+SNK_KUAN, y+SNK_KUAN);
			}
		}
	}

	SelectObject(hdc, oldhPen);
	SelectObject(hdc, oldhBrush);
	DeleteObject(hPen);
	DeleteObject(hBrush);

}

bool Snake::Move()
{
	if(CanMove())
	{
		switch(m_to)
		{
		case SNK_RIGHT:
			m_pos[0].x = m_pos[1].x + 1;
			m_pos[0].y = m_pos[1].y;
			break;
		case SNK_LEFT:
			m_pos[0].x = m_pos[1].x - 1;
			m_pos[0].y = m_pos[1].y;
			break;
		case SNK_UP:
			m_pos[0].x = m_pos[1].x;
			m_pos[0].y = m_pos[1].y - 1;
			break;
		case SNK_DOWN:
			m_pos[0].x = m_pos[1].x;
			m_pos[0].y = m_pos[1].y + 1;
			break;
		default:
			return false;
		}
		
		int x = m_pos[m_iLength].x;
		int y = m_pos[m_iLength].y;

		for(int i = m_iLength; i >= 2; i--)
		{	
			m_pos[i].x = m_pos[i-1].x ;
			m_pos[i].y = m_pos[i-1].y ;
		}
		
		m_pos[1].x = m_pos[0].x;
		m_pos[1].y = m_pos[0].y;

		m_pos[0].x = x;
		m_pos[0].y = y;
		
		if(m_bIsEat)
		{
			m_iLength++;
			m_pos[m_iLength].x = x ;
			m_pos[m_iLength].y = y ;
			m_pGSnake->SetLevel();
		}
		
		HDC hdc = GetDC(m_pGSnake->m_hWnd);
		Show(hdc,true,m_bIsEat);
		m_bIsEat = false;
		ReleaseDC(m_pGSnake->m_hWnd,hdc);
		ComputeRect();



		return true;
	}
	return false;

}

bool Snake::CanMove()
{
	int	x = m_pos[1].x;
	int y = m_pos[1].y;

	switch(m_to)
	{
	case SNK_RIGHT:	x++;	break;
	case SNK_LEFT:	x--;	break;
	case SNK_UP:	y--;	break;
	case SNK_DOWN:	y++;	break;
	default:
		return false;
	}
	
	switch(m_pGSnake->WhatisThat(x,y))
	{
	case APPLE:
		m_pGSnake->CreateApple();
		m_bIsEat = true;
		m_iAppleEat++;
	case EMPTY:
		break;
	case WALL:
	case EGG:
	case SNAKE:
		m_bIsDie = true;
	default:
		return false;


	}
	return true;
}

void Snake::ComputeRect()
{
	RECT rect;
	rect.left = rect.top = rect.right =rect.bottom =0;

	if(m_iLength>0)
	{
		int l = m_pos[1].x;
		int r = m_pos[1].x;
		int t = m_pos[1].y;
		int b = m_pos[1].y;

		for(int i = 2; i <= m_iLength; i++)
		{	
			l = Min(l, m_pos[i].x);
			r = Max(r, m_pos[i].x);
			t = Min(t, m_pos[i].y);
			b = Max(b, m_pos[i].y);
		}
		if(PosToPixels(l,t)&&PosToPixels(r,b))
		{
				rect.left   = l ;
				rect.top    = t ;
				rect.right  = r + SNK_KUAN;
				rect.bottom = b + SNK_KUAN;
		}
	}

	m_rect = rect;

}


bool Snake::IsSnake(int x, int y)
{
	for(int i = 1; i <= m_iLength; i++)
	{
		if(x == m_pos[i].x && y == m_pos[i].y)
			return true;
	}
	return false;
}

void Snake::ChangeDirect(Direct to)
{
	switch(m_to)
	{
	case SNK_DOWN:
	case SNK_UP:
		if(to==SNK_LEFT || to==SNK_RIGHT)
			m_to = to;
		break;
	case SNK_LEFT:
	case SNK_RIGHT:
		if(to==SNK_UP || to==SNK_DOWN)
			m_to = to;
		break;
	}
}



⌨️ 快捷键说明

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