#define WIN32_LEAN_AND_MEAN
/*
创建Windows程序有两种方式——(1)使用MFC,(2)使用SDK。
WIN32_LEAN_AND_MEAN指示编译器(实际上确定了头文件的包含逻辑)不要包含MFC内容。
*/
#include <windows.h>//包含所有的windows头文件
#include <windowsx.h>//包含许多重要的宏和常量的头文件
#include <stdlib.h> void GameStart();
void GameRun(HDC hdc);
void GameEnd(); #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)? 0:1) HINSTANCE g_hInstance;
HWND g_hWnd; const int WINDOW_WIDTH = GetSystemMetrics(SM_CXSCREEN);
const int WINDOW_HEIGHT = GetSystemMetrics(SM_CYSCREEN);
void DrawStar(HDC hdc, int x, int y, COLORREF color)
{
SetPixel(hdc, x, y, color);
SetPixel(hdc, x, y+1, color);
for(int i=0; i<3; i++)
SetPixel(hdc, x-1+i, y+2, color);
for(int i=0; i<7; i++)
SetPixel(hdc, x-3+i, y+3, color);
for(int i=0; i<3; i++)
SetPixel(hdc, x-1+i, y+4, color);
SetPixel(hdc, x, y+5, color);
SetPixel(hdc, x, y+6, color);
} const int STAR_COUNT = 20;
POINT starPoints[STAR_COUNT];
COLORREF color;
int delay = 255;
int span = 5; void GameStart()
{
srand(GetTickCount());
for(int i=0; i<STAR_COUNT; i++) //计算星星端位置
{
starPoints[i].x= rand()%WINDOW_WIDTH;
starPoints[i].y= rand()%WINDOW_HEIGHT;
}
color = RGB(255,255,255);
} void GameRun(HDC hdc)
{
delay-=span;
if(delay<=0 || delay>=255)
span = -span;
color = RGB(delay,delay,delay);
for(int i=0; i<STAR_COUNT; i++)
{
DrawStar(hdc,starPoints[i].x, starPoints[i].y, color);
}
} void GameEnd()
{
} LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
} return DefWindowProc(hwnd, uMsg, wParam, lParam);
} int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
g_hInstance = hInstance;
WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "GameWNDCLASS";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&winclass))
return 0; HWND hwnd;
if(!(hwnd = CreateWindowEx(NULL,
"GameWNDCLASS",
"Game Window",
WS_POPUP | WS_VISIBLE,
0,0,WINDOW_WIDTH,WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL)))
return 0; g_hWnd = hwnd;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd); GameStart(); HDC hdc = GetDC(hwnd); MSG msg;
while(1) {
if(KEYDOWN(VK_ESCAPE))
SendMessage(hwnd,WM_CLOSE,0,0);
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);//虚拟加速键翻译器
DispatchMessage(&msg);
}
GameRun(hdc);
Sleep(33);
} GameEnd(); return msg.wParam;
}
大家帮帮忙。
如何让星星动起来。
帮忙改一下。
/*
创建Windows程序有两种方式——(1)使用MFC,(2)使用SDK。
WIN32_LEAN_AND_MEAN指示编译器(实际上确定了头文件的包含逻辑)不要包含MFC内容。
*/
#include <windows.h>//包含所有的windows头文件
#include <windowsx.h>//包含许多重要的宏和常量的头文件
#include <stdlib.h> void GameStart();
void GameRun(HDC hdc);
void GameEnd(); #define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)?1:0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code)&0x8000)? 0:1) HINSTANCE g_hInstance;
HWND g_hWnd; const int WINDOW_WIDTH = GetSystemMetrics(SM_CXSCREEN);
const int WINDOW_HEIGHT = GetSystemMetrics(SM_CYSCREEN);
void DrawStar(HDC hdc, int x, int y, COLORREF color)
{
SetPixel(hdc, x, y, color);
SetPixel(hdc, x, y+1, color);
for(int i=0; i<3; i++)
SetPixel(hdc, x-1+i, y+2, color);
for(int i=0; i<7; i++)
SetPixel(hdc, x-3+i, y+3, color);
for(int i=0; i<3; i++)
SetPixel(hdc, x-1+i, y+4, color);
SetPixel(hdc, x, y+5, color);
SetPixel(hdc, x, y+6, color);
} const int STAR_COUNT = 20;
POINT starPoints[STAR_COUNT];
COLORREF color;
int delay = 255;
int span = 5; void GameStart()
{
srand(GetTickCount());
for(int i=0; i<STAR_COUNT; i++) //计算星星端位置
{
starPoints[i].x= rand()%WINDOW_WIDTH;
starPoints[i].y= rand()%WINDOW_HEIGHT;
}
color = RGB(255,255,255);
} void GameRun(HDC hdc)
{
delay-=span;
if(delay<=0 || delay>=255)
span = -span;
color = RGB(delay,delay,delay);
for(int i=0; i<STAR_COUNT; i++)
{
DrawStar(hdc,starPoints[i].x, starPoints[i].y, color);
}
} void GameEnd()
{
} LRESULT CALLBACK WindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
break;
} return DefWindowProc(hwnd, uMsg, wParam, lParam);
} int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
g_hInstance = hInstance;
WNDCLASSEX winclass;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC|CS_DBLCLKS;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = "GameWNDCLASS";
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if(!RegisterClassEx(&winclass))
return 0; HWND hwnd;
if(!(hwnd = CreateWindowEx(NULL,
"GameWNDCLASS",
"Game Window",
WS_POPUP | WS_VISIBLE,
0,0,WINDOW_WIDTH,WINDOW_HEIGHT,
NULL,
NULL,
hInstance,
NULL)))
return 0; g_hWnd = hwnd;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd); GameStart(); HDC hdc = GetDC(hwnd); MSG msg;
while(1) {
if(KEYDOWN(VK_ESCAPE))
SendMessage(hwnd,WM_CLOSE,0,0);
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message == WM_QUIT)
break;
TranslateMessage(&msg);//虚拟加速键翻译器
DispatchMessage(&msg);
}
GameRun(hdc);
Sleep(33);
} GameEnd(); return msg.wParam;
}
大家帮帮忙。
如何让星星动起来。
帮忙改一下。
