看下画的第一个矩形就是不变,而第二个是动态的,问题是那个teris里的xy变量的值怎么不变,要怎么改才能让他显示动态的,求助啊
本想做个俄罗斯方块的
但是画个动的矩形都有问题这是头文件
#include<Windows.h> class MyBlock
{
private :
public: int x ,y;
int stae[16];
MyBlock(int xx,int yy,int state[16])
{ int i;
x = xx;
y = yy;
for(i =0;i<16;i++)
{
this->stae[i] = state[i];
}
};
};这是原文件
#include<Windows.h>
#include"block.h"
#include <stdio.h>
#include<string.h>
#include<tchar.h>
#define width 25
#define shape 5
#define sort 4
#define stat 16
int map[16][24];
int a= 10,b = 20; int block[shape][sort][stat] =
{
{{0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},// I SHAPE
{{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}, {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}},// T SHAPE
{{1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0}, {0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}}, // S SHAPE
{{0,1,0,0,0,1,0,0,1,0,1,1,0,0,0,0},{0,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0}}, // L SHAPE
{{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},
{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0}}
};
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE preInstance, PSTR szCmdLine, int iCmdShow)
{ HWND hWnd;
MSG msg;
WNDCLASSEX ws;
ws.cbClsExtra = 0;
ws.cbSize = sizeof(WNDCLASSEX);
ws.cbWndExtra = 0;
ws.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
ws.hCursor = LoadCursor(NULL,IDC_ARROW);
ws.hIcon = LoadIcon(NULL,IDI_APPLICATION);
ws.hIconSm = NULL;
ws.hInstance = hInstance;
ws.lpfnWndProc = WinProc;
ws.lpszClassName = "teris";
ws.lpszMenuName = NULL ;
ws.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&ws))
{
return 0;
} hWnd = CreateWindow("Teris","Teris",WS_OVERLAPPEDWINDOW,0,0,500,600,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
SetTimer(hWnd,1,10,NULL); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WinProc(HWND hWnd ,UINT msg, WPARAM wParam ,LPARAM lParam)
{ int shap,kind,sta ;
LPCWSTR buf[20];
shap = (int)rand()%4 ;
kind = (int)rand()%7 ;
sta = (int) rand()%16 ;
int i,j;
for(i=0;i<16;i++)
{
for(j=0;j<24;j++)
map[i][j]=0;
};
MyBlock teris(200,0,block[shap][kind]); //= new MyBlock(kind,sta,map);
HDC hdc;
hdc = GetDC(hWnd);
HBRUSH hb;
//hb =(HBRUSH) GetStockObject(GRAY_BRUSH);
hb = (HBRUSH)CreateSolidBrush(RGB(200,222,20));
SelectObject(hdc,hb);
switch(msg)
{
case WM_KEYDOWN:
switch (wParam)
{ case VK_DOWN:
teris.y+=25;
i=i+10;j=j+10;
break;
case VK_NEXT:
teris.x+=25;
break;
}
break;
case WM_CREATE:
break;
case WM_TIMER:
Rectangle(hdc,0,0,25+teris.x,25+ teris.y);
teris.x++;
teris.y++;
Rectangle(hdc,100,200,25+a++,25+b++); for(i = 1;i<16;i++)
{
if(teris.stae[i] == 1)
{
map[teris.x/25+i/4*25][teris.y/25+i%4*25] = 1;
}
}
itoa(teris.x , ch_x, 10);
itoa(teris.y, ch_y, 10);
TextOut(hdc,430,220,_T(ch_x),21);
TextOut(hdc,111,122, _T(ch_y),21);
DrawBlock( hWnd, map);
teris.x =teris.x+ 10 ; teris.y =teris.y +10;
//UpdateWindow(hWnd);
break;
case WM_PAINT:
//Rectangle(hdc,0,0,25+i++,25+j++);
MoveToEx(hdc,370,0,NULL);
LineTo(hdc,a++,b++);
LineTo(hdc,teris.x++,teris.y++);
// InvalidateRect (hWnd, NULL, FALSE) ;
// UpdateWindow(hWnd);
break;
case WM_DESTROY:
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
KillTimer(hWnd,1);
return 0; }
return DefWindowProc(hWnd,msg,wParam,lParam);
}
本想做个俄罗斯方块的
但是画个动的矩形都有问题这是头文件
#include<Windows.h> class MyBlock
{
private :
public: int x ,y;
int stae[16];
MyBlock(int xx,int yy,int state[16])
{ int i;
x = xx;
y = yy;
for(i =0;i<16;i++)
{
this->stae[i] = state[i];
}
};
};这是原文件
#include<Windows.h>
#include"block.h"
#include <stdio.h>
#include<string.h>
#include<tchar.h>
#define width 25
#define shape 5
#define sort 4
#define stat 16
int map[16][24];
int a= 10,b = 20; int block[shape][sort][stat] =
{
{{0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0}, {0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}, {0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},// I SHAPE
{{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0}, {0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0}},// T SHAPE
{{1,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0}, {0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, {0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0},
{0,0,1,0,0,0,1,0,0,1,0,0,0,1,0,0}}, // S SHAPE
{{0,1,0,0,0,1,0,0,1,0,1,1,0,0,0,0},{0,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0}}, // L SHAPE
{{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0},
{0,1,1,0,0,1,1,0,0,1,1,0,0,0,0,0}}
};
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE preInstance, PSTR szCmdLine, int iCmdShow)
{ HWND hWnd;
MSG msg;
WNDCLASSEX ws;
ws.cbClsExtra = 0;
ws.cbSize = sizeof(WNDCLASSEX);
ws.cbWndExtra = 0;
ws.hbrBackground = (HBRUSH)GetStockObject(DKGRAY_BRUSH);
ws.hCursor = LoadCursor(NULL,IDC_ARROW);
ws.hIcon = LoadIcon(NULL,IDI_APPLICATION);
ws.hIconSm = NULL;
ws.hInstance = hInstance;
ws.lpfnWndProc = WinProc;
ws.lpszClassName = "teris";
ws.lpszMenuName = NULL ;
ws.style = CS_HREDRAW|CS_VREDRAW;
if(!RegisterClassEx(&ws))
{
return 0;
} hWnd = CreateWindow("Teris","Teris",WS_OVERLAPPEDWINDOW,0,0,500,600,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,iCmdShow);
UpdateWindow(hWnd);
SetTimer(hWnd,1,10,NULL); while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}LRESULT CALLBACK WinProc(HWND hWnd ,UINT msg, WPARAM wParam ,LPARAM lParam)
{ int shap,kind,sta ;
LPCWSTR buf[20];
shap = (int)rand()%4 ;
kind = (int)rand()%7 ;
sta = (int) rand()%16 ;
int i,j;
for(i=0;i<16;i++)
{
for(j=0;j<24;j++)
map[i][j]=0;
};
MyBlock teris(200,0,block[shap][kind]); //= new MyBlock(kind,sta,map);
HDC hdc;
hdc = GetDC(hWnd);
HBRUSH hb;
//hb =(HBRUSH) GetStockObject(GRAY_BRUSH);
hb = (HBRUSH)CreateSolidBrush(RGB(200,222,20));
SelectObject(hdc,hb);
switch(msg)
{
case WM_KEYDOWN:
switch (wParam)
{ case VK_DOWN:
teris.y+=25;
i=i+10;j=j+10;
break;
case VK_NEXT:
teris.x+=25;
break;
}
break;
case WM_CREATE:
break;
case WM_TIMER:
Rectangle(hdc,0,0,25+teris.x,25+ teris.y);
teris.x++;
teris.y++;
Rectangle(hdc,100,200,25+a++,25+b++); for(i = 1;i<16;i++)
{
if(teris.stae[i] == 1)
{
map[teris.x/25+i/4*25][teris.y/25+i%4*25] = 1;
}
}
itoa(teris.x , ch_x, 10);
itoa(teris.y, ch_y, 10);
TextOut(hdc,430,220,_T(ch_x),21);
TextOut(hdc,111,122, _T(ch_y),21);
DrawBlock( hWnd, map);
teris.x =teris.x+ 10 ; teris.y =teris.y +10;
//UpdateWindow(hWnd);
break;
case WM_PAINT:
//Rectangle(hdc,0,0,25+i++,25+j++);
MoveToEx(hdc,370,0,NULL);
LineTo(hdc,a++,b++);
LineTo(hdc,teris.x++,teris.y++);
// InvalidateRect (hWnd, NULL, FALSE) ;
// UpdateWindow(hWnd);
break;
case WM_DESTROY:
ReleaseDC(hWnd,hdc);
PostQuitMessage(0);
KillTimer(hWnd,1);
return 0; }
return DefWindowProc(hWnd,msg,wParam,lParam);
}
所以,楼主应该这样做:
1) 用数据结构描述当前屏幕的状态,包括各个方块的类型和位置;
2) 在 WM_PAINT 中根据数据结构把所有方块画出来;
3) 在 WM_TIMER 中只是修改数据结构,比如变动某方块的位置,然后调用 Invalidate 让系统重画屏幕.
你在 WM_TIMER 消息里直接画了,这不合适.应该在 WM_TIMER 仅改 x y 的值,不画,而把画的工作全放在 WM_PAINT 里做.