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

📄 gsnake.cpp

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

#include "stdafx.h"
#include "resource.h"
#include "Snake.h"
#include "time.h"
//#include ""
#include "GSnake.h"

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

GSnake::GSnake(HWND hWnd)
{
	m_hWnd		= hWnd;
	m_pSnake	= 0;
	m_ptApple.x = 0;
	m_ptApple.y = 0;
	m_iEgg		= 0;
	m_IsON		= false;
	m_iLevel	= LEVEL1;
	m_iTimer	= LEVEL1;

	for(int i = 0; i < EGG_NUM; i++)
	{
		m_ptEgg[i].x = m_ptEgg[i].y = 0;
	}

	srand(time(0));
}

GSnake::~GSnake()
{

	if(m_pSnake) delete m_pSnake;

}

void GSnake::DrawBoard(HDC hdc)
{
	HPEN	hPen,   oldhPen;
	HBRUSH	hBrush, oldhBrush;

	RECT rect;
	GetClientRect(m_hWnd, &rect);


	hBrush = (HBRUSH)CreateSolidBrush(FRM_COLOR);
	oldhBrush = (HBRUSH)SelectObject(hdc,(HBRUSH)hBrush);
	
	//棋盘外框
	Rectangle(hdc, INITPOS_X-QIPAN_HINT,
			INITPOS_Y-QIPAN_HINT,
			QI_KUAN*INTERVAL+INITPOS_X+QIPAN_HINT,
			QI_GAO*INTERVAL+INITPOS_Y+QIPAN_HINT);

	//棋盘边线
	hPen = CreatePen(PS_SOLID,1,RGB(0,0,0));
	oldhPen = (HPEN)SelectObject(hdc,(HPEN)hPen);

	SelectObject(hdc,(HBRUSH)oldhBrush);
	DeleteObject(hBrush);
	hBrush = (HBRUSH)CreateSolidBrush(BK_COLOR);
	oldhBrush = (HBRUSH)SelectObject(hdc,(HBRUSH)hBrush);

	Rectangle(hdc, INITPOS_X,
		INITPOS_Y,
		QI_KUAN*INTERVAL+INITPOS_X,
		QI_KUAN*INTERVAL+INITPOS_Y);

		

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

}

void GSnake::Show(HDC hdc)
{
	DrawBoard(hdc);
	if(m_pSnake) m_pSnake->Show(hdc);
	ShowApple(hdc);
	ShowScore(hdc);
	//	ShowEgg(hdc);

}


void GSnake::ShowApple(HDC hdc)
{
	if(m_ptApple.x && m_ptApple.y )
	{
		HPEN	hPen;
		HBRUSH	hBrush, oldhBrush;
		
		int x = m_ptApple.x;
		int y = m_ptApple.y;
		
		hPen = (HPEN)SelectObject(hdc,(HPEN)GetStockObject(NULL_PEN));

		hBrush	  = CreateSolidBrush(APP_DEFCOL);
		oldhBrush = (HBRUSH)SelectObject(hdc,(HBRUSH)hBrush);
		
		if(PosToPixels(x,y))
			Ellipse(hdc,x,y,x+SNK_KUAN, y+ SNK_KUAN);

		SelectObject(hdc,(HPEN)hPen);
		SelectObject(hdc,(HBRUSH)oldhBrush);
		DeleteObject((HBRUSH)hBrush);
	}
}

bool GSnake::Pause()
{
	if(m_IsON)
		KillTimer(m_hWnd,TIMER_SNAKE);
	else
		SetTimer(m_hWnd,TIMER_SNAKE, m_iLevel, NULL);
	return m_IsON = !m_IsON;

}

void GSnake::ChangeDirect(Direct to)
{
	m_pSnake->ChangeDirect(to);
}

bool GSnake::Move()
{
	if(m_pSnake->Move())
	{
		ComputeScore();
		return true;
	}
	m_IsON = false;
	KillTimer(m_hWnd,TIMER_SNAKE);
	return false;
}

WHAT GSnake::WhatisThat(int x, int y)
{
	if(x == m_ptApple.x && y == m_ptApple.y)
		return APPLE;
	
	if(x < 1 || x >QI_KUAN || y < 1 || y > QI_GAO)
		return WALL;
	
	for(int i = 0; i < m_iEgg; i++)
	{
		if(x == m_ptEgg[i].x && y == m_ptEgg[i].y)
			return EGG;
	}

	if(m_pSnake->IsSnake(x,y)) return SNAKE;

	return EMPTY;
}

void GSnake::Start()
{
	RECT rect;
	if(m_pSnake)
	{
		m_pSnake->Die();
		rect = m_pSnake->GetMinRect();
		InvalidateRect(m_hWnd, &rect,true);
		delete m_pSnake;
	}
	
	HDC hdc = GetDC(m_hWnd);
	ClearBlock(hdc,m_ptApple.x,m_ptApple.y);
	ReleaseDC(m_hWnd,hdc);
	m_IsON = false;
	

	m_pSnake = new Snake(this);
	m_iEgg	 = 0;
	m_iTimer = m_iLevel;
	m_iScore = 0;
	m_iAppleEated = 0;
	


	CreateApple();
	m_IsON = true;
	SetTimer(m_hWnd,TIMER_SNAKE, m_iTimer, NULL);
	rect = m_pSnake->GetMinRect();
	InvalidateRect(m_hWnd,&rect,true);

}

void GSnake::SetLevel(int level)
{

	switch(level)
	{
	case IDM_LEVEL1:
		m_iTimer = m_iLevel = LEVEL1;
		break;
	case IDM_LEVEL2:
		m_iTimer = m_iLevel = LEVEL2;
		break;
	case IDM_LEVEL3:
		m_iTimer = m_iLevel = LEVEL3;
		break;
	default:
		m_iTimer -= level;
		switch(m_iLevel)
		{
		case LEVEL1:
			m_iTimer = Max(m_iTimer, LEVEL2-100);
			break;
		case LEVEL2:
			m_iTimer = Max(m_iTimer, LEVEL3);
			break;
		case LEVEL3:
			m_iTimer = Max(m_iTimer, 50);
			break;
		default:
			m_iTimer = m_iLevel = LEVEL1;
		}

	}
	if(m_IsON)
		SetTimer(m_hWnd,TIMER_SNAKE, m_iTimer,NULL);
	
	ShowText();

}

void GSnake::CreateApple()
{
	int x , y;

	if(!m_IsON)
	{
		x = QI_KUAN/2+1;
		y = QI_GAO/2+1;
	}
	else
	{
		x = rand()%QI_KUAN+1;
		y = rand()%QI_KUAN+1;
		switch(WhatisThat(x, y))
		{
		case APPLE:
		case WALL:
		case SNAKE:
		case EGG:
			FindEmpty(x,y);
		}
	}

	m_ptApple.x = x;
	m_ptApple.y = y;

	HDC hdc =GetDC(m_hWnd);
	ShowApple(hdc);
	ReleaseDC(m_hWnd,hdc);
	
}

bool GSnake::FindEmpty(int &x, int &y)
{
	for(int i = 1; i <= QI_KUAN; i++)
	{
		for(int j = 1; j <= QI_GAO; j++)
			if(WhatisThat(i, j) == EMPTY)
			{
				x = i;
				y = j;
				return true;
			}
	}
	return false;
}

void GSnake::ComputeScore()
{
	int AppleEated = m_pSnake->GetAppleEated();
	if(m_iAppleEated!=AppleEated)
	{
		m_iAppleEated = AppleEated;
		switch(m_iLevel)
		{
		case LEVEL1:
			m_iScore += 100;  break;
		case LEVEL2:
			m_iScore += 500;  break;
		case LEVEL3:
			m_iScore += 1000; break;
		default:
			m_iScore = 0;
		}
	}
	ShowText();
}

void GSnake::ShowScore(HDC hdc)
{
	char str[32];


	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+22, 0*INTERVAL+INITPOS_Y,"Greedy Snake 1.0",16);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 2*INTERVAL+INITPOS_Y,"Level:",6);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 3*INTERVAL+INITPOS_Y,"Apple:",6);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 4*INTERVAL+INITPOS_Y,"Score:",6);

	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 3*INTERVAL+INITPOS_Y,str,wsprintf(str,"%d",m_iAppleEated));
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 4*INTERVAL+INITPOS_Y,str,wsprintf(str,"%d",m_iScore));

	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 11*INTERVAL+INITPOS_Y,"F2 : New Game",13);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 12*INTERVAL+INITPOS_Y,"Space : Pause",13);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 13*INTERVAL+INITPOS_Y,"E : Leve1",9);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 14*INTERVAL+INITPOS_Y,"D : Leve2",9);
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+20, 15*INTERVAL+INITPOS_Y,"C : Leve3",9);


	switch(m_iLevel)
	{
	case LEVEL1:
		TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 2*INTERVAL+INITPOS_Y,"Level1",6);
		break;
	case LEVEL2:
		TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 2*INTERVAL+INITPOS_Y,"Level2",6);
		break;
	case LEVEL3:
		TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 2*INTERVAL+INITPOS_Y,"Level3",6);
		break;
	}

	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 3*INTERVAL+INITPOS_Y,str,wsprintf(str,"%d",m_iAppleEated));
	TextOut(hdc, QI_KUAN*INTERVAL+INITPOS_X+70, 4*INTERVAL+INITPOS_Y,str,wsprintf(str,"%d",m_iScore));


}

void GSnake::ShowText()
{
	RECT rect;
	rect.left	= QI_KUAN*INTERVAL+INITPOS_X+65;
	rect.top	= 2*INTERVAL+INITPOS_Y;
	rect.right	= 550;
	rect.bottom = 5*INTERVAL+INITPOS_Y;
	InvalidateRect(m_hWnd,&rect,true);
}

⌨️ 快捷键说明

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