写了一个简单的打字游戏
程序代码:#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#define X_LEN 500
#define Y_LEN 150
#define TIM1 1
typedef struct Pos
{
int x;
int y;
} POS;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static int ran;
static POS pos;
static TCHAR ch = 'a';
static int speed;
static int count;
static TCHAR score[20];
switch (message)
{
case WM_CREATE:
{
hdc = GetDC (hwnd);
GetClientRect (hwnd, &rect);
ReleaseDC (hwnd, hdc);
srand ((unsigned) time (NULL));
ran = rand () % rect.right -5;
count = 0;
speed = 40;
sprintf (score, "SCORE:%d", count);
SetTimer (hwnd, TIM1, speed, NULL);
pos.x = ran + 5;
pos.y = rect.left + 10;
}
break;
case WM_PAINT:
{
GetClientRect (hwnd, &rect);
hdc = BeginPaint (hwnd, &ps);
SetBkColor (hdc, TRANSPARENT); //TRANSPARENT文本字体背景为透明
SetTextColor (hdc, RGB (0, 255, 0));
TextOut (hdc, pos.x, pos.y, &ch, 1);
SetTextColor (hdc, RGB (255, 0, 0));
TextOut (hdc, 0, 0, score, strlen (score));
EndPaint (hwnd, &ps);
}
break;
case WM_CHAR:
{
TCHAR cha;
TCHAR shu;
sprintf (&shu, "%c", wParam);
sprintf (&cha, "%c", ch);
hdc = GetDC (hwnd);
GetClientRect (hwnd, &rect);
ReleaseDC (hwnd, hdc);
ran = rand () % rect.right -5;
if (shu == cha)
{
speed = 2 + rand () % 10;
count++;
sprintf (score, "SCORE:%d", count);
ran = rand () % rect.right -5;
pos.x = ran;
pos.y = 0;
ran = rand () % rect.right -5;
ch = 'a' + rand () % 26;
if (speed >= 6)
{
speed -= 5;
KillTimer (hwnd, TIM1);
SetTimer (hwnd, TIM1, speed, NULL);
}
InvalidateRect (hwnd, NULL, TRUE);
}
}
break;
case WM_TIMER:
{
hdc = GetDC (hwnd);
GetClientRect (hwnd, &rect);
ReleaseDC (hwnd, hdc);
pos.y += 2;
if (pos.y > rect.bottom)
{
count--;
if (count < -5)
{
KillTimer (hwnd, TIM1);
MessageBox (hwnd, TEXT ("你输了"), TEXT ("哈哈"), MB_OK | MB_ICONINFORMATION);
DestroyWindow (hwnd);
}
sprintf (score, "SCORE:%d", count);
pos.y = rect.top + 5;
pos.x = ran;
if (pos.x > rect.right)
{
pos.x = rect.left + 5;
}
ch += 1;
if (ch > 'a' + 25)
{
ch = 'a';
}
}
InvalidateRect (hwnd, NULL, TRUE);
}
break;
case WM_CLOSE:
{
DestroyWindow (hwnd);
}
break;
case WM_DESTROY:
{
PostQuitMessage (0);
}
break;
default:
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
WNDCLASS wc;
HWND hwnd;
MSG msg;
TCHAR Title[125] = "打字练习by瓦药墙";
TCHAR ClsName[125] = "ClsName";
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = ClsName;
wc.lpszMenuName = NULL;
wc.style = CS_HREDRAW | CS_VREDRAW;
if ( !RegisterClass (&wc) )
{
MessageBox (NULL, "RegisterClass Error", "Error", MB_OK | MB_ICONERROR);
return -1;
}
hwnd = CreateWindow (ClsName, Title, WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, X_LEN, Y_LEN, NULL, NULL, hInstance, NULL);
ShowWindow (hwnd, SW_SHOW);
UpdateWindow (hwnd);
ShowCursor (FALSE);
while ( GetMessage (&msg, NULL, 0, 0) > 0 )
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}
//-----------------------------------------------------
//算法与数据结构q群:87860221 欢迎志同道和的朋友一起讨论进步









,看到最后一句发现楼主貌似醉翁之意不在酒哦~
哎,米办法
