代碼是這樣的 :
#include <windows.h>
#include<stdio.h>
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName[ ] = "CodeBlocksWindowsApp";
int blocks[5][4] = {0}; // 螢屏上只顯示4行4列,blocks[0]這一行畫在螢屏上方
int cxClient , cyClient ;
int block_width ;
int mutex ; //偏移量
void init(){
for(int i = 0 ; i < 4;i++){
blocks[i][rand() % 4] = 1 ;
}
}
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
init();
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_VREDRAW | CS_HREDRAW; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
"別踩白快PC", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
void onPaint(HDC hdc){ // 不處理 hdc銷毀
SelectObject(hdc,GetStockObject(BLACK_PEN));
for(int i = 0 ; i < 4 ; i ++)
{
// 橫線
MoveToEx(hdc,0 , (cyClient/4 *i + mutex) % cyClient ,NULL);
LineTo(hdc,cxClient,(cyClient/4 *i + mutex) % cyClient);
//豎線
MoveToEx(hdc,cxClient/4 * (i+1) , 0 ,NULL);
LineTo(hdc,cxClient/4 * (i+1), cyClient );
}
for(int i =-1 ; i < 4 ; i++)
{
for(int j = 0 ; j < 4 ;j++)
{
if(blocks[i + 1][j] == 1){
SelectObject(hdc,GetStockObject(BLACK_BRUSH));
Rectangle(hdc,cxClient/4* j , (cyClient/4 *i + mutex) % cyClient , cxClient/4 * (j+1),(cyClient/4 *i + mutex) % cyClient + block_width);
}
}
}
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
HDC hdcMen ;
PAINTSTRUCT ps;
HBITMAP hBitmap ;
switch (message) /* handle the messages */
{
case WM_CREATE :
SetTimer(hwnd,1,50,NULL);
return 0;
case WM_TIMER :
mutex += 5 ;
if(mutex >= block_width) // 有一格被完全移動在螢屏下方,這時,將mutex置為0,并將陣列的值向前移動
{
mutex = 0 ;
for(int i=4;i>0;i--)
for(int j = 0;j<4;j++)
blocks[i][j] = blocks[i-1][j];
for(int j=0;j<4;j++)
blocks[0][j] = 0 ;
blocks[0][rand() % 4] = 1 ; // blocks[0][N] 這一行總是畫在距螢屏上一格的地方
}
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_SIZE :
mutex = 0 ;
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
block_width = cyClient / 4 ;
InvalidateRect(hwnd,NULL,TRUE);
return 0 ;
case WM_PAINT :
hdc = BeginPaint(hwnd,&ps);
onPaint(hdc);;
DeleteDC(hdcMen);
EndPaint(hwnd,&ps);
return 0 ;
case WM_DESTROY:
KillTimer(hwnd,1);
PostQuitMessage (0);/* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
謝謝各位大大
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
別踩白塊是什么游戲啊?好玩不?介紹下子撒.uj5u.com熱心網友回復:
閃爍的話,就是在記憶體建個圖片,先畫到圖片上,再把圖片貼到canvas上就不閃爍了。uj5u.com熱心網友回復:
額。賣萌可恥uj5u.com熱心網友回復:
額。賣萌可恥
uj5u.com熱心網友回復:
額。賣萌可恥。uj5u.com熱心網友回復:
解決寶典在這里:void __fastcall TForm1::FormCreate(TObject *Sender)
{
DoubleBuffered =true; // 雙緩沖避免繪圖時閃爍
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/120995.html
