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

📄 snakeview.cpp

📁 贪吃蛇源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// snakeView.cpp : implementation of the CSnakeView class
//

#include "stdafx.h"
#include "snake.h"
#include "snakeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSnakeView

IMPLEMENT_DYNCREATE(CSnakeView, CView)

BEGIN_MESSAGE_MAP(CSnakeView, CView)
	//{{AFX_MSG_MAP(CSnakeView)
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSnakeView construction/destruction

CSnakeView::CSnakeView()
{
//m_Title.LoadBitmap(IDB_TITLE);
//m_Title.GetObject(sizeof(BITMAP),&m_Bm);
m_Player1brush.CreateSolidBrush(PLAYER1COLOR);
m_Player2brush.CreateSolidBrush(PLAYER2COLOR);
m_Backbrush.CreateSolidBrush(BACKCOLOR);
m_Foodbrush.CreateSolidBrush(FOODCOLOR);
m_Bedgebrush.CreateSolidBrush(BEDGECOLOR);

m_GameMode=FALSE;
m_Hiscore=GetHiscore();
m_GameStart=FALSE;
m_Stage=1;
}

CSnakeView::~CSnakeView()
{
//m_Title.DeleteObject();
m_Player1brush.DeleteObject();
m_Player2brush.DeleteObject();
m_Backbrush.DeleteObject();
m_Foodbrush.DeleteObject();
m_Bedgebrush.DeleteObject();
KillTimer(IDT_MOVE);
}

BOOL CSnakeView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}


/////////////////////////////////////////////////////////////////////////////
// CSnakeView diagnostics

#ifdef _DEBUG
void CSnakeView::AssertValid() const
{
	CView::AssertValid();
}

void CSnakeView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSnakeView message handlers

int CSnakeView::GetHiscore()
{
int score=0;
char filename[256];
::GetSystemDirectory(filename,256);
for(int i=strlen(filename)-1;i>=0;i--)
if(filename[i]=='\\') {filename[i+1]='\0';break;}
strcat(filename,"snake.dat");

CFile file;
char *buffer;
if(file.Open(filename,CFile::modeRead))
{
buffer=new char[file.GetLength()+1];
file.Read(buffer,file.GetLength());
file.Close();
score=atoi(buffer);
delete buffer;
}

return score;
}

void CSnakeView::SaveHiscore()
{
char filename[256];
::GetSystemDirectory(filename,256);
for(int i=strlen(filename)-1;i>=0;i--)
if(filename[i]=='\\') {filename[i+1]='\0';break;}
strcat(filename,"snake.dat");
CFile file;
char temp[255];
if(file.Open(filename,CFile::modeWrite|CFile::modeCreate))
{
sprintf(temp,"%d",m_Hiscore);
file.Write(temp,strlen(temp));
file.Close();
}
}

void CSnakeView::InitGameDat()
{
m_Pause=FALSE;
m_GameOver=FALSE;
m_PassStage=FALSE;
for(int i=0;i<50;i++)
for(int j=0;j<38;j++)
m_Box[i][j]=BACKCOLOR;

//初始玩家一蛇
if(m_Stage==1)
{
m_player1.life=3;
m_player1.score=0;
m_tempplayer1score=0;
}
if(m_player1.life>=1)
{
m_player1.snake.position=UP_POS;
m_player1.snake.len=3;
m_player1.snake.pos[0].x=10;
m_player1.snake.pos[0].y=30;
m_Box[10][30]=PLAYER1COLOR;
m_player1.snake.pos[1].x=10;
m_player1.snake.pos[1].y=31;
m_Box[10][31]=PLAYER1COLOR;
m_player1.snake.pos[2].x=10;
m_player1.snake.pos[2].y=32;
m_Box[10][32]=PLAYER1COLOR;
}
//初始玩家二蛇
if(m_GameMode)
{
if(m_Stage==1)
{
m_player2.life=3;
m_player2.score=0;
m_tempplayer2score=0;
}
if(m_player2.life>=1)
{
m_player2.snake.position=UP_POS;
m_player2.snake.len=3;
m_player2.snake.pos[0].x=39;
m_player2.snake.pos[0].y=30;
m_Box[39][30]=PLAYER2COLOR;
m_player2.snake.pos[1].x=39;
m_player2.snake.pos[1].y=31;
m_Box[39][31]=PLAYER2COLOR;
m_player2.snake.pos[2].x=39;
m_player2.snake.pos[2].y=32;
m_Box[39][32]=PLAYER2COLOR;
}

}

//**************
if(m_GameMode)
m_LeftFood=(m_Stage*2+6)%71;
else
m_LeftFood=(m_Stage+3)%36;
m_Bedges=(m_Stage+3)%36;
m_Speed=133-m_Stage*4;

//初始化蚕及障碍物
int k=::GetTickCount()%(50*38);
int num=0;
int x,y;

while(num<m_LeftFood)
{
x=k/38;y=k%38;
while(m_Box[x][y]!=BACKCOLOR)
{
x++;y++;
x=x%50;y=y%38;
}
m_Box[x][y]=FOODCOLOR;
num++;
k+=50*38/m_LeftFood;
k=k%(50*38);
}


k=(k*k)%(50*38);
num=0;
while(num<m_Bedges)
{
x=k/38;y=k%38;
x=x%50;y=y%38;
while(m_Box[x][y]!=BACKCOLOR || (x==10 && y>10) ||(x==39 && y>10))
{
x++;y++;
x=x%50;y=y%38;
}
m_Box[x][y]=BEDGECOLOR;
num++;
k+=50*38/m_Bedges;
k=k%(50*38);
}
}
//************
void CSnakeView::DrawTitle()
{
CClientDC dc(this);
CFont font;
CRect rect;
GetWindowRect(&rect);
CSnakeView::ScreenToClient(&rect);
dc.FillSolidRect(&rect,RGB(0,0,0));
font.CreateFont( 0,           //字体字符的逻辑高度
			     0,                //字符平均宽度取默认值
				 0,                //文本行角度为0,水平
				 0,                //字符角度为0,正立
				 FW_NORMAL,        //正常字体
				 FALSE,            //不倾斜 
				 FALSE,            //不加下划线
				 FALSE,            //不加删除线
				 ANSI_CHARSET,       
			     OUT_DEFAULT_PRECIS,
				 CLIP_DEFAULT_PRECIS,
				 DEFAULT_QUALITY,
				 DEFAULT_PITCH|FF_MODERN,
				 "Times New Roman");
LOGFONT lf;
font.GetLogFont(&lf);

dc.SetTextColor(RGB(255,255,128));
dc.SelectObject(&font);
dc.TextOut(240,28,"HISCORE:");

dc.SetTextColor(RGB(0,0,255));
lf.lfHeight=80;lf.lfWeight=80;
font.CreateFontIndirect(&lf);
dc.SelectObject(&font);
dc.TextOut(200,90,"SNAKE");

dc.SetTextColor(RGB(192,192,192));
lf.lfHeight=30;lf.lfWeight=30;
font.CreateFontIndirect(&lf);
dc.SelectObject(&font);
dc.TextOut(240,210,"1    PLAYER");
dc.TextOut(240,250,"2    PLAYERS");

dc.SetTextColor(RGB(192,255,192));
lf.lfHeight=0;lf.lfWeight=0;
font.CreateFontIndirect(&lf);
dc.SelectObject(&font);
dc.TextOut(230,300,"Http://www.fxstudio.xiloo.com");
dc.TextOut(230,320,"gamemake_boy@163.net");

}

//重画游戏数据显示
#define VIEWCOLOR RGB(0,128,128)
void CSnakeView::DrawView()
{
CClientDC dc(this);
char temp[50];
dc.SetBkColor(RGB(192,192,192));
dc.SetTextColor(RGB(255,0,0));
dc.TextOut(505,30,"PLAYER1:");
dc.TextOut(505,50,"SCORE:");
dc.TextOut(505,70,"LIFE");
dc.TextOut(505,170,"STATE     :");
dc.TextOut(505,200,"CONTROL:");
dc.TextOut(505,220,"PAUSE:");
dc.TextOut(505,240,"EXIT:");
dc.TextOut(505,260,"UP:");
dc.TextOut(505,280,"DOWN:");
dc.TextOut(505,300,"LEFT:");
dc.TextOut(505,320,"RIGHT:");

dc.SetTextColor(VIEWCOLOR);
sprintf(temp,"%d  ",m_player1.score);
dc.TextOut(575,50,temp);
sprintf(temp,"%d  ",m_player1.life);
dc.TextOut(575,70,temp);
sprintf(temp,"%d  ",m_Stage);
dc.TextOut(575,170,temp);
dc.TextOut(565,220,"ENTER");
dc.TextOut(565,240,"ESC");
dc.TextOut(565,260,"W   UP");
dc.TextOut(565,280,"S   DOWN");
dc.TextOut(565,300,"A   LEFT");
dc.TextOut(565,320,"D   RIGHT");
dc.SelectObject(&m_Foodbrush);
dc.Rectangle(505,343,515,353);
dc.TextOut(520,340,":          FOOD");
dc.SelectObject(&m_Bedgebrush);
dc.Rectangle(505,363,515,373);
dc.TextOut(520,360,":          BEDGE");


if(m_GameMode)
{
dc.SetTextColor(RGB(255,0,0));
dc.TextOut(505,100,"PLAYER2:");
dc.TextOut(505,120,"SCORE:");
dc.TextOut(505,140,"LIFE:");
dc.SetTextColor(VIEWCOLOR);
sprintf(temp,"%d  ",m_player2.score);
dc.TextOut(575,120,temp);
sprintf(temp,"%d  ",m_player2.life);
dc.TextOut(575,140,temp);
}

}


void CSnakeView::DrawBox(CClientDC *pDC,const i,const j,const COLORREF color)
{
switch(color)
{
case BACKCOLOR:
	pDC->SelectObject(&m_Backbrush);
	break;
case PLAYER1COLOR:
	pDC->SelectObject(&m_Player1brush);
	break;
case PLAYER2COLOR:
	pDC->SelectObject(&m_Player2brush);
	break;
case FOODCOLOR:
	pDC->SelectObject(&m_Foodbrush);
	break;
case BEDGECOLOR:
	pDC->SelectObject(&m_Bedgebrush);
	break;
default:
	pDC->SelectObject(&m_Backbrush);
	break;
}
pDC->Rectangle(i*10,j*10,i*10+10,j*10+10);
}
/////////////////////////////////////////////////////////////////////////////
// CSnakeView drawing

void CSnakeView::OnDraw(CDC* pDC)
{
    if(m_GameStart)
     
 	//游戏开始****************************************************************
	{
    CRect rect;
	GetWindowRect(&rect);   
    pDC->SelectObject(&m_Backbrush);
	pDC->Rectangle(0,0,500,rect.Height());
	CBrush brush;
	brush.CreateSolidBrush(RGB(192,192,192));
	pDC->SelectObject(&brush);
	pDC->Rectangle(501,0,rect.Width(),rect.Height());
	brush.DeleteObject();
    DrawView();
	
	//重画游戏各方格
	CClientDC dc(this);
	
	for(int i=0;i<50;i++)
	for(int j=0;j<38;j++)
	DrawBox(&dc,i,j,m_Box[i][j]);

    if(m_Pause)
	{
	    pDC->SetBkColor(RGB(0,0,0));
		pDC->SetTextColor(RGB(255,0,0));
		pDC->TextOut(230,180,"PAUSE...");
	}
    if(m_GameOver)
	{
        pDC->SetBkColor(RGB(0,0,0));
		pDC->SetTextColor(RGB(255,0,0));
		pDC->TextOut(210,180,"GAME OVER !");
		m_Pause=TRUE;
	}
	if(m_PassStage)
	{
        pDC->SetBkColor(RGB(0,0,0));
		pDC->SetTextColor(RGB(255,0,0));
		char temp[80];
		if(m_Stage!=33)
		sprintf(temp,"STAGE %d CLEAR !",m_Stage-1);
		else
		sprintf(temp,"The End !         ");

		pDC->TextOut(200,180,temp);
		m_Pause=TRUE;
	}
	}
   
	//***************************************************************
	else
	{
	/*
    CDC memDC;
	CRect rect;
	GetWindowRect(&rect);
    memDC.CreateCompatibleDC(pDC);
    memDC.SelectObject(&m_Title);
	//pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,m_Bm.bmWidth,m_Bm.bmHeight,SRCCOPY);
	pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memDC,0,0,SRCCOPY);
	*/
	DrawTitle();
	if(m_GameMode)
	{
	m_GameMode=FALSE;
	OnKeyDown(VK_DOWN,0,0);
	}
	else
	{
	m_GameMode=TRUE;
	OnKeyDown(VK_UP,0,0);
	}
    char temp[20];
	sprintf(temp,"%d",m_Hiscore);
	pDC->SetBkMode(TRANSPARENT);
    pDC->SetTextColor(RGB(0,255,0));
    pDC->TextOut(HISCOREX,HISCOREY,temp);
//	memDC.DeleteDC();
	}
}

void CSnakeView::InitPlayer1()
{
m_player1.snake.position=UP_POS;
m_player1.snake.len=3;
m_player1.snake.pos[0].x=10;
m_player1.snake.pos[0].y=30;
m_Box[10][30]=PLAYER1COLOR;
m_player1.snake.pos[1].x=10;
m_player1.snake.pos[1].y=31;

⌨️ 快捷键说明

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