您的位置:首页 > 编程语言 > C语言/C++

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

2013-12-01 23:33 218 查看
#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 );
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: