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

📄 gamedlg.cpp

📁 很好玩的俄罗斯方块
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// gameDlg.cpp : implementation file
//

#include "stdafx.h"
#include "game.h"
#include "gameDlg.h"

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


#include "Gameover.h"

#define M 13
#define N 20
#define V 34

int G[M][N];
int Run_Time=500;

#define Clear_away  one(0,object.x, object.y)
int X=47,Y=40,r=25;
COLORREF BackColor=RGB(118,4,118);//RGB(223,194,108);

OBJECT   object;
int a0,a1,a2,a3,a4,a5,a6,a7; int ZZ;

OBJECT   object2;
int b0,b1,b2,b3,b4,b5,b6,b7; int ZZ2;

OBJECT   object3;
int c0,c1,c2,c3,c4,c5,c6,c7; int ZZ3;



int Runing=0,Score=0;
int X1=250,X2=300;int OK_X=960,OK_Y=20,sizeZ=40;

int J_x=430,J_y=245;
int m_a=435,m_b=95;
int Back;
   
 CPoint pointo,poinfrom; 	  
 

		CPoint p1,pp;
		CRect rect;  
		CRect desktopRect;
		int Read=1;
		int m_W,m_H;int LW,LH;

	CPoint ooo;
	CRect rect0;//桌面大小
	CRect rectO;//窗口大小
	CPoint rectp;//鼠标位置
	CPoint recto;//窗口位置

	CRect So,Si,Sj; float R=0.15; int T=30,Set=0;

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGameDlg dialog

CGameDlg::CGameDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGameDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGameDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGameDlg)
	DDX_Control(pDX, IDC_COMBO1, m_str);
	DDX_Control(pDX, IDC_OUT, m_Out);
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGameDlg, CDialog)
	//{{AFX_MSG_MAP(CGameDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BACKCOLOR, OnBackcolor)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_DOWN, OnDown)
	ON_BN_CLICKED(IDC_LEFT, OnLeft)
	ON_BN_CLICKED(IDC_RIGHT, OnRight)
	ON_BN_CLICKED(IDC_UP, OnUp)
	ON_WM_TIMER()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_MYSELF, OnMyself)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGameDlg message handlers

BOOL CGameDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	


	m_str.AddString("低级");	
	m_str.AddString("高级");
	m_str.AddString("中级");
	m_str.SetCurSel(0);

	ooo.x=ooo.y=0;
	recto=rectp=ooo;//桌面大小

	ClientToScreen(&recto);		
	GetCursorPos(&rectp);
	
	GetClientRect(&rect);
	m_W=rect.Width();m_H=rect.Height()+30;
	GetDesktopWindow()->GetWindowRect(desktopRect);
	LW=desktopRect.Width ();
	LH=desktopRect.Height ();
		
	So.bottom=int(LH-R*LH);So.top=int(R*LH);
	So.right=int(LW-R*LW); So.left=int(R*LW);

	SetTimer(10,1,NULL);
	SetTimer(9,1,NULL);


	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGameDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CGameDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CGameDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



//=========================================================================================


//=======================================================================================
void CGameDlg::one(OBJECT object)		//以组形式画出
{	Clear_away;
	one(object.Type, object.x, object.y);
}

void CGameDlg::one(OBJECT object,int a,int b)
{	
	one(object.Type, object.x+b, object.y+a);
}
void CGameDlg::Clear(int a,int b)
{	
	one(0,object.x+a, object.y+b);
}
int CGameDlg::check1()				//走动条件
{
	if(object.x+a2>M-1||object.x+a4>M-1||object.x+a6>M-1
		||object.x+a2<0||object.x+a4<0||object.x+a6<0)
	return 1;
	else if( G[object.x+a0][object.y+a1]||G[object.x+a2][object.y+a3]
			||G[object.x+a4][object.y+a5]||G[object.x+a6][object.y+a7]) 
	return 1;
	else return 0;
}
int CGameDlg::check2()				//走动条件
{
	if(object.y+a3>N-1||object.y+a5>N-1||object.y+a7>N-1
		||object.y+a3<0||object.y+a5<0||object.y+a7<0)
		return 1;
	else if( G[object.x+a0][object.y+a1]||G[object.x+a2][object.y+a3]
			||G[object.x+a4][object.y+a5]||G[object.x+a6][object.y+a7]) 
		return 1;
	else return 0;
}
int CGameDlg::check3()				//走动条件
{
	if(object.y>N-1||object.y+a3>N-1||object.y+a5>N-1||object.y+a7>N-1)
		return 1;
	else if(G[object.x+a0][object.y+a1]||G[object.x+a2][object.y+a3]
			||G[object.x+a4][object.y+a5]||G[object.x+a6][object.y+a7] )		
		return 1;	
	else return 0;
}

void CGameDlg::cout(int a1,int x,int y)
{	char ch[10];
	CDC *pDC=GetDC();  ASSERT(pDC!=NULL);

	if(a1==-1) pDC->TextOut(x,y,"-1");
	else if (a1==0) pDC->TextOut(x,y,"0");
	else {gcvt(a1,10,ch);pDC->TextOut(x,y,ch);}
	ReleaseDC(pDC);
}
/////////////////////////////////////////////////////////////////////////////
// CMyView message handlers

void CGameDlg::OnDraw(CDC* pDC)			//开始进入
{

	// TODO: add draw code for native data here
	Shape(); int i,j;
	for(i=0;i<M;i++)for(j=0;j<N;j++)G[i][j]=0;   Runing=1;Score=0;
	object.Type=3;object.x=M/2;object.y=0;object.group=3; ZZ=1;
	
	PreView();
	Show(object);
	ChangeShape(object);
	ObjectGroup(object);


	switch(m_str.GetCurSel())
	{
		case 0: Run_Time=700;break;
		case 1: Run_Time=100;break;
		case 2: Run_Time=400;break;
	}
	SetTimer(11,Run_Time,NULL);


	pointo.x=pointo.y=0;
	ClientToScreen(&pointo);
	poinfrom=pointo;
	

}


void CGameDlg::Shape()					//外部框架
{	
	int line=18;int rr=4; 
	CDC *pDC=GetDC();
	ASSERT(pDC!=NULL);	
	
	CPen pen;
	pen.CreatePen (PS_SOLID,1,RGB(0,0,0));	
	CBrush brush1,brush2,brush3;
	brush1.CreateSolidBrush (RGB(60,60,60));
	brush3.CreateSolidBrush (RGB(0,170,0));
	brush2.CreateSolidBrush (BackColor);

	pDC->SelectObject (pen);
	pDC->SelectObject (brush1);
	pDC->Rectangle(X-line,Y-line,X+M*r+line,Y+N*r+line);

	int b=line*2/3,c=b-rr; 
	pDC->SelectObject (brush3);
	pDC->Rectangle(X-b,Y-b,X+M*r+b,Y+N*r+b);
	pDC->SelectObject (brush1);
	pDC->Rectangle(X-c,Y-c,X+M*r+c,Y+N*r+c);

	pDC->SelectObject (brush2);
	pDC->Rectangle(X-2,Y-2,X+M*r+2,Y+N*r+2);
	DeleteObject(pen);DeleteObject(brush1);
	DeleteObject(brush2);DeleteObject(brush3);

	ReleaseDC(pDC);
}

void CGameDlg::one(int Type, int i, int j)  //某元格颜色 位置
{	
	if(Runing==0){OnDestroy();return;}
	CDC *pDC=GetDC();  ASSERT(pDC!=NULL);	

	COLORREF color,L_color=RGB(0,0,0);
	switch(Type)
	{	case 0: color=L_color=BackColor;break;
		case 1: color=RGB(131,129,222); break;
		case 2: color=RGB(250,90,130);  break;
		case 3: color=RGB(40,220,250);  break;
		case 4: color=RGB(40,120,50);   break;
		case 5: color=RGB(0,250,0);     break;
		case 6: color=RGB(131,19,222);  break;
		case 7: color=RGB(120,190,30);  break;
		case 8: color=RGB(140,20,150);  break;
		case 9: color=RGB(40,120,150);  break;
		case 10: color=RGB(23,62,180);  break;
		case 11: color=RGB(131,129,22); break;
		case 12: color=RGB(20,190,130); break;
		case 13: color=RGB(40,234,220); break;
		case 14: color=RGB(140,70,150); break;
		case 15: color=RGB(30,150,40);  break;
		case 16: color=RGB(78,203,30);	break;
		case 17: color=RGB(120,100,30); break;
		case 18: color=RGB(240,70,20);  break;
		case 19: color=RGB(40,180,90);  break;
		case 20: color=RGB(123,62,180); break;
		case 21: color=RGB(120,0,20);	break;
		case 22: color=RGB(0,0,250);	break;
		case 23: color=RGB(0,250,0);	break;
		case 24: color=RGB(0,250,250);	break;
		case 25: color=RGB(250,0,0);	break;
		case 26: color=RGB(250,0,250);  break;
		case 27: color=RGB(250,250,0);	break;
		case 28: color=RGB(250,250,250);break;
	
		case 29: color=RGB(rand()%250,rand()%250,rand()%250);    break;
		case 30: color=RGB(rand()%250,rand()%250,rand()%250);    break;
		case 31: color=RGB(rand()%250,rand()%250,rand()%250);    break;
		case 32: color=RGB(rand()%250,rand()%250,rand()%250);    break;
		case 33: color=RGB(rand()%250,rand()%250,rand()%250);    break;
		case 34: color=RGB(rand()%250,rand()%250,rand()%250);    break;
	}
	CPen pen;
	CBrush brush;
	pen.CreatePen (PS_SOLID,1,L_color);		
	brush.CreateSolidBrush (color);
	pDC->SelectObject (pen);
	pDC->SelectObject (brush);

	pDC->Rectangle(X+i*r,Y+j*r,X+(i+1)*r,Y+(j+1)*r);
	DeleteObject(pen);DeleteObject(brush);
	
	ReleaseDC(pDC);
}


void CGameDlg::ObjectGroup(OBJECT object)//画组
{		if(Runing==0){OnDestroy();return;}
		one(object,a1,a0);
		one(object,a3,a2);
		one(object,a5,a4);
		one(object,a7,a6);
}

void CGameDlg::DeleteGroup(OBJECT object)//删组
{		if(Runing==0){OnDestroy();return;}
		Clear(a0,a1);
		Clear(a2,a3);
		Clear(a4,a5);
		Clear(a6,a7);
}

    
void CGameDlg::ChangeShape(OBJECT object)//转组
{	int z[4][2]={0},i,j,k;
	if(Runing==0){OnDestroy();return;}

	do{
	switch(object.group)
	{
	case 1:  ZZ=0; z[1][0]=1;z[1][1]=0; z[2][0]=0;z[2][1]=1;z[3][0]=1;z[3][1]=1;break;
	case 2:  ZZ++; ZZ%=4;
			switch(ZZ)
			{	case 0:  z[1][0]=-1;z[1][1]=0; z[2][0]=1;z[2][1]=0;z[3][0]=-1;z[3][1]=-1;break;
				case 1:  z[1][0]=0;z[1][1]=1; z[2][0]=0;z[2][1]=-1;z[3][0]=1;z[3][1]=-1;break;
				case 2:  z[1][0]=-1;z[1][1]=0; z[2][0]=1;z[2][1]=0;z[3][0]=1;z[3][1]=1;break;
				case 3:	 z[1][0]=0;z[1][1]=-1; z[2][0]=0;z[2][1]=1;z[3][0]=-1;z[3][1]=1;break;
			}
			break;
	case 3:  ZZ++; ZZ%=4;
			switch(ZZ)
			{	case 0:  z[1][0]=-1;z[1][1]=0; z[2][0]=1;z[2][1]=0;z[3][0]=1;z[3][1]=-1;break;
				case 1:  z[1][0]=0;z[1][1]=1; z[2][0]=0;z[2][1]=-1;z[3][0]=1;z[3][1]=1;break;

⌨️ 快捷键说明

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