问题描述
- 我用c++在做俄罗斯方块,但是出现了一个网上也没搜到的问题
-
为什么运行一段时间后,窗体就会变成白色了呢?这个问题我困扰了很久,不解决的话,下面我就难以实行其他功能了!我是个新手,请大家多多指教!有图有源码!#include "stdafx.h"
#include "俄罗斯方块游戏2.h"
#include
#define MAX_LOADSTRING 100
#define N 20 //小方块大小
HWND hWnd = NULL;
#define W 600 //窗口大小
#define H 610
HGDIOBJ oldbrush;
HBRUSH hb;int X1=10;
int Y1=0;
bool ShowTetris[30][30]={0};class Tetris //“父类”组成大方块中的小方块
{
public:
POINT p[4];
public:
virtual void one(HDC hdc,int x,int y){};
virtual void two(HDC hdc,int x,int y){};
virtual void three(HDC hdc,int x,int y){};
virtual void four(HDC hdc,int x,int y){};
void drawing(HDC hdc ,int x,int y);};
void Tetris::drawing(HDC hdc ,int x,int y)
{
POINT A[4];
for(int i=0;i<4;i++)
{A[0].x = x+p[i].x*N; A[0].y = y+p[i].y*N; A[1].x = x + N+p[i].x*N; A[1].y = y+p[i].y*N; A[2].x = x + N+p[i].x*N; A[2].y = y + N+p[i].y*N; A[3].x = x+p[i].x*N; A[3].y = y + N+p[i].y*N; Polygon(hdc, A, 4); }
}
class T1:public Tetris //"田条"
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(255,2,100));
oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=1,p[1].y=0;
p[2].x=0,p[2].y=1;
p[3].x=1,p[3].y=1;
drawing(hdc,x,y);
}
void two(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(255,2,100));
oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=1,p[1].y=0;
p[2].x=0,p[2].y=1;
p[3].x=1,p[3].y=1;
drawing(hdc,x,y);
}
void three(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(255,2,100));
oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=1,p[1].y=0;
p[2].x=0,p[2].y=1;
p[3].x=1,p[3].y=1;
drawing(hdc,x,y);
}
void four(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(255,2,100));
oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=1,p[1].y=0;
p[2].x=0,p[2].y=1;
p[3].x=1,p[3].y=1;
drawing(hdc,x,y);
}};class T2:public Tetris //“竖条”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(52,52,100));
oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=0,p[1].y=1;
p[2].x=0,p[2].y=2;
p[3].x=0,p[3].y=3;
drawing(hdc,x,y);} void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(52,52,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=2,p[2].y=0; p[3].x=3,p[3].y=0; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(52,52,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=0,p[1].y=1; p[2].x=0,p[2].y=2; p[3].x=0,p[3].y=3; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(52,52,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=2,p[2].y=0; p[3].x=3,p[3].y=0; drawing(hdc,x,y); }
};
class T3:public Tetris //“T条”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(95,102,100));oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=1,p[0].y=0;
p[1].x=0,p[1].y=1;
p[2].x=1,p[2].y=1;
p[3].x=2,p[3].y=1;
drawing(hdc,x,y);} void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(95,102,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=0,p[1].y=1; p[2].x=0,p[2].y=2; p[3].x=1,p[3].y=1; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(95,102,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=0,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=2,p[3].y=0; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(95,102,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=1,p[1].y=1; p[2].x=1,p[2].y=2; p[3].x=0,p[3].y=1; drawing(hdc,x,y); }
};
class T4:public Tetris //“L形条第一种”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(200,82,150));oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0;
p[1].x=0,p[1].y=1;
p[2].x=0,p[2].y=2;
p[3].x=1,p[3].y=0;
drawing(hdc,x,y);} void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(200,82,150)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=2,p[2].y=0; p[3].x=2,p[3].y=1; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(200,82,150)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=1,p[1].y=1; p[2].x=1,p[2].y=2; p[3].x=0,p[3].y=2; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(200,82,150)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=0,p[1].y=1; p[2].x=1,p[2].y=1; p[3].x=2,p[3].y=1; drawing(hdc,x,y); }
};
class T5:public Tetris //“L形条第二种”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(270,152,170));oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=1,p[0].y=0; p[1].x=0,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=1,p[3].y=2; drawing(hdc,x,y); } void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(270,152,170)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=2,p[0].y=0; p[1].x=2,p[1].y=1; p[2].x=1,p[2].y=1; p[3].x=0,p[3].y=1; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(270,152,170)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=0,p[1].y=1; p[2].x=0,p[2].y=2; p[3].x=1,p[3].y=2; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(270,152,170)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=2,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=0,p[2].y=0; p[3].x=0,p[3].y=1; drawing(hdc,x,y); }
};
class T6:public Tetris //“Z形条第一种”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(255,252,100));oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=0,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=2,p[3].y=1; drawing(hdc,x,y); } void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(255,252,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=1,p[1].y=1; p[2].x=0,p[2].y=1; p[3].x=0,p[3].y=2; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(255,252,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=0,p[0].y=0; p[1].x=1,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=2,p[3].y=1; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(255,252,100)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=1,p[1].y=1; p[2].x=0,p[2].y=1; p[3].x=0,p[3].y=2; drawing(hdc,x,y); }
};
class T7:public Tetris //“Z形条第二种”
{
public:
void one(HDC hdc,int x,int y)
{
hb=CreateSolidBrush(RGB(290,255,255));oldbrush=SelectObject(hdc,(HBRUSH)(hb));
p[0].x=1,p[0].y=0; p[1].x=2,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=0,p[3].y=1; drawing(hdc,x,y); } void two(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(290,255,255)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=1; p[1].x=0,p[1].y=1; p[2].x=0,p[2].y=0; p[3].x=1,p[3].y=2; drawing(hdc,x,y); } void three(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(290,255,255)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=0; p[1].x=2,p[1].y=0; p[2].x=1,p[2].y=1; p[3].x=0,p[3].y=1; drawing(hdc,x,y); } void four(HDC hdc,int x,int y) { hb=CreateSolidBrush(RGB(290,255,255)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); p[0].x=1,p[0].y=1; p[1].x=0,p[1].y=1; p[2].x=0,p[2].y=0; p[3].x=1,p[3].y=2; drawing(hdc,x,y); }
};
T1 t1;
T2 t2;
T3 t3;
T4 t4;
T5 t5;
T6 t6;
T7 t7;Tetris *p=new Tetris;
void ChooseTetris1(HDC hdc,int a)
{
switch(a)
{
case 0:p=new T1;break;
case 1:p=new T2;break;
case 2:p=new T3;break;
case 3:p=new T4;break;
case 4:p=new T5;break;
case 5:p=new T6;break;
case 6:p=new T7;break;
default:break;}
}
void display(HDC hdc)
{
t1.one(hdc,500,10);
t2.one(hdc,500,75);
t3.one(hdc,500,180);
t4.one(hdc,500,245);
t5.one(hdc,500,325);
t6.one(hdc,500,410);
t7.one(hdc,500,480);}
// 全局变量:
HINSTANCE hInst; // 当前实例
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);int R;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);// TODO: 在此放置代码。 MSG msg; HACCEL hAccelTable; R=rand()%7; // 初始化全局字符串 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MY2CPP, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); srand((unsigned)time(NULL)); // 执行应用程序初始化: if (!InitInstance (hInstance, nCmdShow)) { return FALSE; } hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY2CPP)); // 主消息循环: while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
// 仅当希望
// 此代码与添加到 Windows 95 中的“RegisterClassEx”
// 函数之前的 Win32 系统兼容时,才需要此函数及其用法。调用此函数十分重要,
// 这样应用程序就可以获得关联的
// “格式正确的”小图标。
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY2CPP)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = MAKEINTRESOURCE(IDC_MY2CPP); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL)); return RegisterClassEx(&wcex);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;hInst = hInstance; // 将实例句柄存储在全局变量中
hWnd = CreateWindow(szWindowClass, L"Tetris",WS_CAPTION|WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT, W,H, NULL, NULL, hInstance, NULL);if (!hWnd)
{
return FALSE;
}ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);return TRUE;
}int a=0;
int X[1000];
int Y[1000];int JudgeTetris()
{
for(int i=0;i
{
if(p->p[i].y+Y1+1>=28)
return 0;} for(int i=0;i<4;i++) { for(int j=0;j<a;j++) { if(p->p[i].y+Y1+1>=Y[j]&&p->p[i].x+X1==X[j]) { return 0; } } } return 1;
}
void StopTetris()
{
int x, y;
for(int i=0;i
{
x=X1+p->p[i].x;
y=Y1+p->p[i].y;// ShowTetris[x][y] = true;
X[a]=x;
Y[a]=y;
ShowTetris[X[a]][Y[a]] = true;
a++;
}}
void Down(HDC hdc,int n)
{
switch(n)
{case 0: p->one(hdc,X1*N,Y1*N);break; case 1: p->two(hdc,X1*N,Y1*N);break; case 2: p->three(hdc,X1*N,Y1*N);break; case 3: p->four(hdc,X1*N,Y1*N);break; default:break; } if(JudgeTetris()) { Y1++; } else { StopTetris(); R=rand()%7; delete p; //到达低端时,随机选择一个数 ChooseTetris1(hdc,R); Y1=0; X1=10; } for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { if (ShowTetris[i][j]) { hb=CreateSolidBrush(RGB(34,139,34)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); POINT a[4] = { i*N, j*N, (i + 1)*N, j*N, (i + 1)*N, (j + 1)*N, i*N, (j+1)*N }; Polygon(hdc, a, 4); } } }
}
void QuickDown(HDC hdc)
{Y1+=1; InvalidateRect(hWnd,NULL,TRUE);
}
void GoLeft(HDC hdc)
{
if(X1<=0)
{
X1-=0;} else X1-=1; InvalidateRect(hWnd,NULL,TRUE);
}
int JR() //判断右边界
{
for(int i=0;i
{
if(p->p[i].x+X1>=20)return 0; } return 1;
}
void GoRight(HDC hdc)
{
if(!JR())
{X1+=0; } else X1+=1; InvalidateRect(hWnd,NULL,TRUE);
}
int L=0;
void Deforming(HDC hdc)
{L+=1; L=L%4; InvalidateRect(hWnd,NULL,TRUE);
}
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
HGDIOBJ oldbrush;
HBRUSH hb;
RECT rect;
srand((unsigned)time(NULL));switch (message) { case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); // 分析菜单选择: switch (wmId) { case IDM_ABOUT: DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About); break; case IDM_EXIT: DestroyWindow(hWnd); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_CREATE: SetTimer(hWnd,1,700,NULL); //函数反回值就是第一个参数值1,表示此定时器的ID号;第二个参数表示要等待700毫秒时间再重新处理一次。第三个参数在这种方法中一般用NULL。 case WM_TIMER: InvalidateRect(hWnd,NULL,TRUE); case WM_KEYDOWN: //键盘控制方块移动 hdc=GetDC(hWnd); switch((int)wParam) { case VK_DOWN: QuickDown(hdc);break; //加速下落 case VK_LEFT: GoLeft(hdc);break; //键盘左移 case VK_RIGHT: GoRight(hdc);break; //键盘右移 case VK_UP: Deforming(hdc);break; //变形 } ReleaseDC ( hWnd,hdc ); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); ChooseTetris1(hdc,R); hb=CreateSolidBrush(RGB(255,228,181));//hb=CreateSolidBrush(RGB(255,192,250)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); Rectangle(hdc,0,0,420,560); //游戏区域画刷 hb=CreateSolidBrush(RGB(176,224,230));//hb=CreateSolidBrush(RGB(255,192,250)); oldbrush=SelectObject(hdc,(HBRUSH)(hb)); Rectangle(hdc,420,0,600,560); //游戏区域画刷 display(hdc); Down(hdc,L); // TODO: 在此添加任意绘图代码... EndPaint(hWnd, &ps); break; case WM_DESTROY: KillTimer (hWnd, 1) ; PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0;
}
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;case WM_COMMAND: if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) { EndDialog(hDlg, LOWORD(wParam)); return (INT_PTR)TRUE; } break; }![图片](http://img.ask.csdn.net/upload/201605/23/1464003773_584645.jpg)![图片](http://img.ask.csdn.net/upload/201605/23/1464003943_873881.jpg)![图片](http://img.ask.csdn.net/upload/201605/23/1464004017_919643.jpg)
解决方案
解决方案三:
代码太长了,估计是你刷新窗口之后没有再调用你原来的刷墙工刷上你原来的颜色所以是白的