[Win32] typing games

Source: Internet
Author: User
Tags textout

This is a typing exercise program developed by myself after I learned the Win32 SDK in my third-grade college.

It's easy to practice. Random English letters appear and drop them down.

 

/* <Br/> a very basic typing program, which was previously designed for programming. <Br/> English letters may drop down and the same letters will be removed. <Br/> use the SDK to compile the usage of the timer. <Br/> you can set scores and increase the dropping speed. <Br/> to compile this program, use VC ++ to add a case. <Br/> select "win32application" and insert the program "program. <Br/> */<br/> # include <windows. h> <br/> # include <stdlib. h> <br/> # include <time. h> <br/> # define id_timer 1 <br/> # define timelong 300 // set the slowest time of timer <br/> # define maxnum 15 // set the drop letter data limit <br/> # define maxgrade 210 // set the maximum score for each successful elimination of one word <br/> static tchar temp [] = text ("abcdefghijklmnopqrstuvwxyz "); <br/> lresult callback wndproc (hwnd, uint, wparam, lparam); <br/> int winapi winmain (hinstance, hinstance hprevi Nstance, <br/> lpstr lpcmdline, int nshowcmd) <br/>{< br/> static tchar appname [] = text ("typegame "); <br/> hwnd; <br/> MSG; <br/> wndclass wndcls = {cs_savebits, wndproc, 0, hinstance, // steal the cursor so that the cursor window type is not like this <br/> loadicon (null, idi_application), loadcursor (null, idc_arrow), <br/> (hbrush) getstockobject (black_brush), null, appname}; <br/> If (! Registerclass (& wndcls) <br/>{< br/> MessageBox (null, text ("missing blank window"), appname, mb_iconerror ); <br/> return 0; <br/>}< br/> hwnd = createmediawex (ws_ex_topmost, appname, "typed into logs ", // use createmediawex to keep the program top <br/> ws_caption | ws_border | ws_minimizebox | blank, <br/> cw_usedefault, cw_usedefault, <br/> cw_usedefault, cw_usedefault, null, null, hinstance, <br/> null); <br/> showwindow (hwnd, nshowcmd); <br/> updatewindow (hwnd); <Br/> while (getmessage (& MSG, null, 0, 0) <br/>{< br/> translatemessage (& MSG ); <br/> dispatchmessage (& MSG); <br/>}< br/> return MSG. wparam; <br/>}< br/> lresult callback wndproc (hwnd, uint MSG, wparam, lparam) <br/>{< br/> HDC; <br/> paintstruct pS; <br/> textmetric TM; <br/> int I, randchoice; <br/> tchar szbuffer [10]; // used to display data storage areas <br/> static int cxchar, cychar, cxclient, cyclient, // definition fonts and keywords Value of x and y in the display area <br/> life, grade, timenow, level; // life value, score, now separated by timer, wait for the response to determine when the response will speed up <br/> static tchar typer [maxnum], output [1], playerchoice; // because the textout should be put in the specified tag, so use so many static point PT [maxnum]; // The base of the object <br/> srand (Time (null); <br/> switch (MSG) <br/>{< br/> case wm_create: <br/> HDC = getdc (hwnd); <br/> gettextmetrics (HDC, & TM); <br/> cxchar = TM. tmavecharwidth; // obtain the Phantoms <br/> cychar = TM. tmheight + TM. tmexternalleading; // Obtain the body height <br/> releasedc (hwnd, HDC); <br/> life = maxnum; // The lifecycle is equal to the maximum number of letters <br/> grade = 0, level = 0; // The initial data score is 0 <br/> for (I = 0; I <maxnum; I ++) // initialize the metadata: start letter <br/>{< br/> typer [I] = temp [rand () % 26]; <br/>}< br/> timenow = timelong; // set the timer interval <br/> settimer (hwnd, id_timer, timenow, null); // set the timer <br/> return 0; <br/> case wm_size: <br/> cxclient = loword (lparam); <br/> cyclient = hiword (lparam); <br/> for (I = 0; I <maxnum; I ++) // sets the start position. <Br/> {// because wm_create and wm_size are sent when the upper window is created <br/> PT [I]. X = cxclient/3 + (cxclient/30 * I); <br/> PT [I]. y = 10; <br/>}< br/> return 0; <br/> case wm_timer: <br/> srand (Time (null )); <br/> randchoice = rand () % maxnum; // The host selects a word to drop. <br/> PT [randchoice]. Y + = cychar; <br/> If (PT [randchoice]. y> = (cyClient-155) & typer [randchoice]! = '') <Br/>{< br/> typer [randchoice] =''; // The Life Of 1 is deducted when it falls under the upper limit <br/> life --; <br/> If (Life <1) <br/> {<br/> killtimer (hwnd, id_timer); <br/> MessageBox (hwnd, "batch terminate", "gameover", mb_iconerror); <br/> postmessage (hwnd, wm_close, wparam, lparam ); <br/>}< br/> invalidaterect (hwnd, null, true); // re-define the region <br/> return 0; <br/> case wm_paint: <br/> HDC = beginpaint (hwnd, & PS); <br/> textout (HDC, 15,10, text ("grade :"), 6); // score <br/> textout (HDC, 15 + 9 * cxchar, 10, szbuffer, wsprintf (szbuffer, text ("% 8d "), grade); <br/> textout (HDC, 15, 10 + cychar, text ("life:"), 5); // indicates the life <br/> textout (HDC, 15 + 5 * cxchar, 10 + cychar, szbuffer, wsprintf (szbuffer, text ("% 2D"), Life); <br/> for (I = 0; I <maxnum; I ++) <br/>{< br/> output [0] = typer [I]; <br/> textout (HDC, PT [I]. x, PT [I]. y, output, 1); <br/>}< br/> movetoex (HDC, 0, cyClient-150, null ); // set the descent threshold <br/> lineto (HDC, cxclient, cyClient-150); <br/> endpaint (hwnd, & PS); <br/> return 0; <br/> case wm_char: <br/> playerchoice = (tchar) wparam; // capture the delimiter character <br/> If (playerchoice> = 'A' & playerchoice <= 'Z') <br/> playerchoice ^ = 32; <br/> for (I = 0; I <maxnum; I ++) <br/> {<br/> If (typer [I] = playerchoice) <br/>{< br/> typer [I] = temp [rand () % 26]; <br/> grade + = maxgrade-Pt [I]. y; <br/> level = Grade; <br/> If (level> 1000) // judge the increase speed <br/>{< br/> level = 0; <br/> timenow-= 30; <br/> If (timenow> 0) <br/> settimer (hwnd, id_timer, timenow, null ); <br/>}< br/> PT [I]. y = 10; <br/> invalidaterect (hwnd, null, true); // re-define the region domain <br/> break; <br/>}< br/> return 0; <br/> case wm_destroy: <br/> killtimer (hwnd, id_timer ); <br/> postquitmessage (0); <br/> return 0; <br/>}< br/> return defwindowproc (hwnd, MSG, wparam, lparam ); <br/>}< br/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.