c语言控制台的俄罗斯方块

#include "stdafx.h"
#include "plane.h"
#include "myPlane.h"
#include "missile.h"
#define NUM 3
LRESULT CALLBACK WndProc ( HWND,UINT,WPARAM,LPARAM );
HWND hwnd;
HDC hdc;
plane p[NUM];
myPlane *my_plane = new myPlane((COLS-3)/2,ROWS-3);
missile *ml = new missile((COLS-3)/2,ROWS-3);
UINT timer_id = 0;
UINT timer_missile = 0;
int interval_base=100;
HBRUSH h_bSolid= ( HBRUSH ) GetStockObject ( GRAY_BRUSH ),
	h_bEmpty= ( HBRUSH ) GetStockObject ( WHITE_BRUSH );

byte g_panel[ROWS][COLS] = {0};
int main()
{
	HINSTANCE hInstance=GetModuleHandle ( NULL );
	TCHAR szAppName[]=TEXT ( "teris" );
	MSG msg;
	WNDCLASS wc;
	srand ( time (  NULL ) );
	int temp2 = 0,temp1 = 0;
	for(int i = 0; i<NUM; i++)
	{
 		p[i].cur_top=-(rand()%10);
 		p[i].cur_left = rand()%15;		

	}

	wc.style=CS_HREDRAW|CS_VREDRAW;
	wc.lpfnWndProc=WndProc;
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hInstance=hInstance;
	wc.hIcon=LoadIcon ( NULL,IDI_APPLICATION );
	wc.hCursor=LoadCursor ( NULL,IDC_ARROW );
	wc.hbrBackground= ( HBRUSH ) GetStockObject ( WHITE_BRUSH );
	wc.lpszMenuName=NULL;
	wc.lpszClassName=szAppName;
	if ( !RegisterClass ( &wc ) )
	{
		printf ( "RegisterClass occur errors!" );
		return 0;
	}
	hwnd=CreateWindow ( szAppName,TEXT ( "Teris Demo" ),
		WS_OVERLAPPEDWINDOW,
		0,0,0,0,
		NULL,
		NULL,
		hInstance,
		NULL );
	ShowWindow ( hwnd,SW_SHOW );
	UpdateWindow ( hwnd );
	while ( GetMessage ( &msg,NULL,0,0 ) )
	{
		TranslateMessage ( &msg );
		DispatchMessage ( &msg );
	}
	return msg.wParam;
}

void DrawPanel ( HDC hdc )  		//绘制游戏面板
{
	int x,y;
	RECT rect;

	for ( y=0; y<ROWS; y++ )
	{
		for ( x=0; x<COLS; x++ )
		{
			//计算方块的边框范围
			rect.top=y*CELL+1;
			rect.bottom= ( y+1 ) *CELL-1;
			rect.left=x*CELL+1;
			rect.right= ( x+1 ) *CELL-1;
			FrameRect ( hdc,&rect, ( HBRUSH ) GetStockObject ( BLACK_BRUSH ) );
		}
	}
}
void RefreshPanel( HDC hdc )
{
	int x,y;
	RECT rect;

	//先刷屏
	for ( y=0; y<ROWS; y++ )
	{
		for ( x=0; x<COLS; x++ )
		{ 
			//为避免刷掉方块的边框,rect范围必须比边框范围小1
			rect.top=y*CELL+2;
			rect.bottom= ( y+1 ) *CELL-2;
			rect.left=x*CELL+2;
			rect.right= ( x+1 ) *CELL-2;
			if ( 1 == g_panel[y][x] )
				FillRect ( hdc,&rect,h_bSolid);
			else
			{
				FillRect ( hdc,&rect,h_bEmpty);
				//MessageBox(NULL,"123","123",NULL);
			}
		}
	}
	//再定位方块
	for(int i=0; i<NUM; i++)
	{
		if ( NULL==p[i].block ) 
		{
			//printf("%d\n",i);
			continue;
		}
		p[i].drawPlane(hdc,h_bSolid);
		//printf("%d\n",i);
	}
	
	my_plane->drawPlane(hdc,h_bSolid);
	ml->drawMissile(hdc);
	//my_plane1.drawPlane(hdc,h_bSolid);
	
}
void CALLBACK myFunc(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
	
	hdc=GetDC ( hwnd );
	p[idEvent].flight(hwnd,hdc);
	RefreshPanel ( hdc );
	ReleaseDC ( hwnd,hdc );

}
void CALLBACK SendMissle(HWND hwnd,UINT uMsg,UINT idEvent,DWORD dwTime)
{
	hdc = GetDC(hwnd);
	my_plane->ShootMissile(hdc);
	RefreshPanel(hdc);
	ReleaseDC(hwnd,hdc);
}
LRESULT CALLBACK WndProc ( HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam )
{
	PAINTSTRUCT ps;
	switch ( message )
	{
	case WM_CREATE:
		MoveWindow ( hwnd,400,10,CELL*30,CELL*40,FALSE );		//补齐宽度和高度
		for(int i=0; i<NUM;i++)
			timer_id=SetTimer ( hwnd,i,interval_base,(TIMERPROC)myFunc);
		
		//SetTimer(hwnd,100,interval_base,(TIMERPROC)SendMissle);
		return 0;
 	case WM_KEYDOWN:
 		hdc=GetDC ( hwnd );
 		switch ( wParam )
 		{
  		case VK_LEFT:							//左移
  			 my_plane->DoLeftShift ( hdc );
  			break;
  		case VK_RIGHT:							//右移
  			 my_plane->DoRightShift ( hdc );
  			break;
  		case VK_UP:								//转向
  			 my_plane->DoUpShift ( hdc );
  			break;
  		case VK_DOWN:							//加速
  			 my_plane->DoDownShift( hdc );
  			break;
 		case VK_SPACE:	//发射
			
			if(ml->m_block)
				timer_missile = SetTimer ( hwnd,100,interval_base,(TIMERPROC)SendMissle);
			else
			{
				missile *ml = new missile((COLS-3)/2,ROWS-3);
				ml->drawMissile(hdc);
				timer_missile = SetTimer ( hwnd,100,interval_base,(TIMERPROC)SendMissle);
			}
			//timer_missile = SetTimer ( hwnd,100,interval_base,(TIMERPROC)myFunc);
//  			isPause=!isPause;
//  			if ( isPause )
//  			{
//  				if ( timer_id ) KillTimer ( hwnd,ID_TIMER );
//  				timer_id=0;
//  			}
//  			else
//  			{
//  				timer_id=SetTimer ( hwnd,ID_TIMER,interval_base-level*interval_unit,FALSE );
//  			}
			//my_plane->ShootMissile(hdc);
 			break;
  		}
  		ReleaseDC ( hwnd,hdc );
  		return 0;
	case WM_PAINT:
		hdc=BeginPaint ( hwnd,&ps );
		DrawPanel ( hdc );			//绘制面板
		RefreshPanel ( hdc );		//刷新
		EndPaint ( hwnd,&ps );
		return 0;
	case WM_DESTROY:
		//if ( block ) free ( block );
		if ( timer_id ) KillTimer ( hwnd,ID_TIMER );
		PostQuitMessage ( 0 );
		return 0;
	}
	return DefWindowProc ( hwnd,message,wParam,lParam );
}
	

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值