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

📄 arsview.cpp

📁 小游戏--俄罗斯方块的代码
💻 CPP
字号:
// ArsView.cpp : implementation of the CArsView class
//

#include "stdafx.h"
#include "Ars.h"

#include "ArsDoc.h"
#include "ArsView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CArsView

IMPLEMENT_DYNCREATE(CArsView, CView)

BEGIN_MESSAGE_MAP(CArsView, CView)
	//{{AFX_MSG_MAP(CArsView)
	ON_WM_PAINT()
	ON_WM_TIMER()
	ON_WM_KEYDOWN()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CArsView construction/destruction

CArsView::CArsView()
{
	// TODO: add construction code here

}

CArsView::~CArsView()
{
}

BOOL CArsView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CArsView drawing

void CArsView::OnDraw(CDC* pDC)
{
	CArsDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CArsView printing

BOOL CArsView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CArsView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CArsView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CArsView diagnostics

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

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

CArsDoc* CArsView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CArsDoc)));
	return (CArsDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CArsView message handlers

void CArsView::NewBrick()
{
	m_Count =0;
	count_x = 0;
	Now_state =0;
	BrickNum = pre_BrickNum;
	pre_BrickNum = (::rand())%10;
	if(pre_BrickNum > 5)
		pre_BrickNum-=4;
	UINT medium_buf;
	medium_buf = TotalInfor[BrickNum][Now_state+1];
	int rect = medium_buf/10000;
	CurrentBrick_x = rect%10;
	CurrentBrick_y = rect/10;
	removeFirst = medium_buf%100;
	removeSecond = (medium_buf/100)%100;
	{
		int re_num;
		while((re_num = remove_num()) != -1)
		{
			delete_one(re_num);
		};
	}
	{
		CPoint p(168,32);
		CSize size(100,20);
		CRect rect(p,size);
		InvalidateRect(&rect,false);
	}

}

void CArsView::ADDToContainer()
{
	int l,k;
	for(l=0;l<CurrentBrick_x;l++)
		for(k=0;k<CurrentBrick_y;k++)
		{
			int medium;
			if((medium = (k+1)*10+l+1) != removeSecond && medium != removeFirst)
			{
				{
					PlaceContainer[m_Count+k][count_x+l] = 1;
				}
			}
		}

}

BOOL CArsView::JudgeTouch()
{
	int l,k;
	for(l=0;l<CurrentBrick_x;l++)
		for(k=0;k<CurrentBrick_y;k++)
		{
			int medium;
			if((medium = (k+1)*10+l+1) != removeSecond && medium != removeFirst)
			{
				if (PlaceContainer[m_Count+k+1][count_x+l] == 1)
				{
					return true;
				}
			}
		}
		
    return false;
}

BOOL CArsView::Left_Right_Alert_Touch(int state, int left, int right, int alert)
{
	UINT medium_buf;
	medium_buf = TotalInfor[BrickNum][Now_state+1];
	int rect = medium_buf/10000;
	CurrentBrick_x = rect%10;
	CurrentBrick_y = rect/10;
	removeFirst = medium_buf%100;
	removeSecond = (medium_buf/100)%100;
	
	int l,k;
	for(l=0;l<CurrentBrick_x;l++)
		for(k=0;k<CurrentBrick_y;k++)
		{
			int medium;
			if((medium = (k+1)*10+l+1) != removeSecond && medium != removeFirst)
			{
				if (PlaceContainer[m_Count+k][count_x+l+right-left+alert] == 1)
				{
					return true;
				}
				if (PlaceContainer[m_Count+k][count_x+l+right-left-alert] == 1)
				{
					return true;
				}
			}
		}
		
    return false;
}

int CArsView::remove_num()
{
	int i,j;
	int had_break = 0;
	for(i=23;i>0;i--)
	{
		for (j=0;j<10;j++)
		{
			if (PlaceContainer[i][j] == 0)
			{
				had_break = 1;
				break;
			}
		}
		if (had_break == 0)
		{
			return i;
		}
		had_break = 0;
	}

	return -1;
}

void CArsView::delete_one(int re_num)
{
	int i,j;
	for(i=re_num;i>0;i--)
		for(j=0;j<10;j++)
		{
			PlaceContainer[i][j] = PlaceContainer[i-1][j];
		}
	for (i=0;i<10;i++)
		PlaceContainer[0][i] = 0;
	cents+=100;
	if(cents%1000 == 0 && cents != 0)
	{
		speed-=150;
		SetTimer(1,speed,NULL);
	}

	{
		CPoint p(m_BlockPlace_x,m_BlockPlace_y);
		CSize size(17*10,17*(re_num+1));
		CRect rect(p,size);
		InvalidateRect(&rect,false);
	}

}

void CArsView::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect Client;
	GetClientRect(Client);
	CDC m_hMemoryDC;
	m_hMemoryDC.CreateCompatibleDC(&dc);
	m_hMemoryDC.SelectObject(MyGirl);
	//
	dc.StretchBlt(0,0,Client.Width(),Client.Height(),&m_hMemoryDC,0,0,388,485,SRCCOPY);
	m_hMemoryDC.SelectObject(brick);

	CString str;
	str.Format("%d",cents);
	dc.TextOut(168,32,str);
	//
	int i,j;
	for(i=0;i<24;i++)
		for(j=0;j<10;j++)
		{
			if(PlaceContainer[i][j] == 1)
				dc.StretchBlt(m_BlockPlace_x+j*17,m_BlockPlace_y+i*17,16,16,
				&m_hMemoryDC,0,0,16,16,SRCCOPY);
		}

	//	/
    UINT medium_buf;
	medium_buf = TotalInfor[BrickNum][Now_state+1];
	int rect = medium_buf/10000;
	CurrentBrick_x = rect%10;
	CurrentBrick_y = rect/10;
	removeFirst = medium_buf%100;
	removeSecond = (medium_buf/100)%100;

	int l,k;
	for(l=0;l<CurrentBrick_x;l++)
		for(k=0;k<CurrentBrick_y;k++)
		{
			int medium;
			if((medium = (k+1)*10+l+1) != removeFirst && medium != removeSecond)
			{
				dc.StretchBlt(m_BlockPlace_x+count_x*17+l*17,m_BlockPlace_y+m_Count*17+k*17,16,16,
					&m_hMemoryDC,0,0,16,16,SRCCOPY);
			}
		}
	//
	{
		
	UINT medium_buf;
	medium_buf = TotalInfor[pre_BrickNum][1];
	int rect = medium_buf/10000;
	CurrentBrick_x = rect%10;
	CurrentBrick_y = rect/10;
	removeFirst = medium_buf%100;
	removeSecond = (medium_buf/100)%100;
	
	int l,k;
	for(l=0;l<CurrentBrick_x;l++)
		for(k=0;k<CurrentBrick_y;k++)
		{
			int medium;
			if((medium = (k+1)*10+l+1) != removeFirst && medium != removeSecond)
			{
				dc.StretchBlt(309+l*17,6+k*17,16,16,
					&m_hMemoryDC,0,0,16,16,SRCCOPY);
			}
		}
	}
    //


	// Do not call CView::OnPaint() for painting messages
}

void CArsView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (m_Count<=23-CurrentBrick_y && !JudgeTouch())
	{
		m_Count++;
		CPoint p(m_BlockPlace_x+(count_x-right)*17,
			m_BlockPlace_y+(m_Count-1)*17);
		CSize size(CurrentBrick_x*17,CurrentBrick_y*17+17);
		CRect rect(p,size);
		InvalidateRect(&rect,false);
	}
	else
	{
		ADDToContainer();
		NewBrick();
	}

	CView::OnTimer(nIDEvent);
}

void CArsView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	switch(nChar)
	{
	case VK_UP:
		{
			int Next_state = (Now_state+1)%TotalInfor[BrickNum][0];
			if(((TotalInfor[BrickNum][Next_state+1]/10000)%10)*17+m_BlockPlace_x+count_x*17 <= 
				12+10*17 && ((TotalInfor[BrickNum][Next_state+1]/10000)/10)*17+m_BlockPlace_y+
				m_Count*17 <= 68 +24*17 && !Left_Right_Alert_Touch(Next_state,0,0,1))
			{
				{
					CPoint p(m_BlockPlace_x+count_x*17,
						m_BlockPlace_y+m_Count*17);
					CSize size(CurrentBrick_x*17,CurrentBrick_y*17);
					CRect rect(p,size);
					InvalidateRect(&rect,false);
				}
				
				Now_state = Next_state;
				CurrentBrick_x = ((TotalInfor[BrickNum][Now_state+1]/10000)%10);
				CurrentBrick_y = ((TotalInfor[BrickNum][Now_state+1]/10000)/10);
				removeFirst = TotalInfor[BrickNum][Now_state+1]%100;
				removeSecond = (TotalInfor[BrickNum][Now_state+1]/100)%100;
			}
		}
		break;
	case VK_DOWN:
		if(m_Count <= 23-CurrentBrick_y && !JudgeTouch())
		{
			m_Count++;
			down++;
		}
		else
		{
			if(m_Count == 0)
			{
				over = 1;
			}
			ADDToContainer();
			NewBrick();
		}
		break;
	case VK_LEFT:
		if (count_x>0 && !Left_Right_Alert_Touch(Now_state,1,0,0))
		{
			count_x--;
			left++;
		}
		break;
	case VK_RIGHT:
		if (count_x+CurrentBrick_x <= 9 && !Left_Right_Alert_Touch(Now_state,0,1,0))
		{
			count_x++;
			right++;
		}
		break;		
	}//switch end

	if (over == 0)
	{
		{					
			CPoint p(m_BlockPlace_x+(count_x-right)*17,
			m_BlockPlace_y+(m_Count-down)*17);
		    CSize size(CurrentBrick_x*17+(left+right)*17,CurrentBrick_y*17+down*17);
	      	CRect rect(p,size);
		    InvalidateRect(&rect,false);
		}
	}
	else
		if(AfxMessageBox("",MB_YESNO)==IDYES)
		{
			initTostart();
			SetTimer(1,speed,NULL);
			InvalidateRect(NULL,false);
			over = 0;
		}
		else
		{
			::ExitProcess(0);
		}
	left =0;
	right =0;
	down =0;

	CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CArsView::initTostart()
{
	over =0;
	removeSecond = 0;
	removeFirst = 0;
	left = 0;
	right = 0;
	down = 0;
	count_x = 0;
	m_Count = 0;
	m_BlockPlace_x = 12;
	m_BlockPlace_y = 68;
	cents =0;
	speed =600;
	Now_state = 0;
	pre_BrickNum = 0;
	BrickNum = 0;
	CurrentBrick_x = 0;
	CurrentBrick_y = 0;
	TotalInfor[0][0] = 2;
    TotalInfor[0][1] = 140000;
	TotalInfor[0][2] = 410000;
	TotalInfor[0][3] = 0;
	TotalInfor[0][4] = 0;
	TotalInfor[1][0] = 4;
	TotalInfor[1][1] = 322131;
	TotalInfor[1][2] = 231112;
	TotalInfor[1][3] = 321222;
	TotalInfor[1][4] = 232223;
	TotalInfor[2][0] = 4;
	TotalInfor[2][1] = 322232;
	TotalInfor[2][2] = 232122;
	TotalInfor[2][3] = 321121;
	TotalInfor[2][4] = 231213;
	TotalInfor[3][0] = 2;
	TotalInfor[3][1] = 231123;
	TotalInfor[3][2] = 321231;
	TotalInfor[3][3] = 0;
	TotalInfor[3][4] = 0;
	TotalInfor[4][0] = 2;
	TotalInfor[4][1] = 231321;
	TotalInfor[4][2] = 321132;
	TotalInfor[4][3] = 0;
	TotalInfor[4][4] = 0;
	TotalInfor[5][0] = 4;
	TotalInfor[5][1] = 231113;
	TotalInfor[5][2] = 321131;
	TotalInfor[5][3] = 232123;
	TotalInfor[5][4] = 321232;

	BrickNum = (::rand())%10;
	if(BrickNum>5)
		BrickNum-=4;
	pre_BrickNum = (::rand())%10;
	if(pre_BrickNum>5)
		pre_BrickNum-=4;

	CurrentBrick_y = (TotalInfor[BrickNum][Now_state+1]/10000)/10;
	CurrentBrick_x = (TotalInfor[BrickNum][Now_state+1]/10000)%10;

	int i,j;
	for (i=0;i<24;i++)
		for(j=0;j<10;j++)
			PlaceContainer[i][j] = 0;

}

⌨️ 快捷键说明

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