C # game programming: "console game series"-"7. Example of keyboard skilled workers"

Source: Internet
Author: User
Tags drawtext gety
I. Game Analysis I believe that everyone is very familiar with Kingsoft typing, typing genie, and other typing software. I want to use their basic typing skills when I first came into contact with computers. The game demo in this chapter is a typing game. Of course, it is very simple, just ~ Z-letter typing practice games. The design of the game is not complex. At the beginning of the game, the number of dropped letters and the probability of each dropped letter are initialized. As time goes on, the letters gradually fall from the top of the interface to the bottom of the interface, in this process, you need to tap the corresponding key position on the keyboard to match the letters on the interface. If the key is the same as the drop letter, the correct number of typing records will be recorded. Otherwise, no record will be recorded, in any case, when a letter falls under the interface, the letter stops dropping. The letter is regarded as invalid, and the number of hits is also recorded. The correct rate, number of hits, and other information will be displayed on the interface, and there will be a countdown to the game. When the time is reached, it will be deemed as the end of the game.
2. The game implementation first defines an alphabet, and the implementation of the alphabet is: // lettertable class implementation
Using system; namespace typing {internal sealed class lettertable {Private Static string [] letters_table = {"A", "B", "C", "D", "E ", "F", "g", "H", "I", "J", "k", "L", "M", "n", "O ", "P", "Q", "r", "S", "T", "U", "V", "W", "X", "Y ", "Z" };/// <summary> /// get the alphabet length /// </Summary> /// <returns> </returns> Public static int32 getlength () {return letters_table.length ;} /// <summary> /// obtain letters based on the index /// </Summary> /// <Param name = "Index"> </param> /// <returns> </returns> Public static string getletter (int32 index) {return letters_table [Index];} /// <summary> /// obtain the subject code by letter /// </Summary> /// <Param name = "letter"> </param> // <returns> </returns> Public static int32 getlettercode (string letter) {int32 code = 65; For (int32 I = 0; I <letters_table.length; I ++) {If (getletter (I ). equals (letter) {code = 65 + I ;}} return code ;}}}

/// Letter class implementation

Using system; using cengine; using cgraphics; namespace typing {internal class letter {// <summary> // letter // </Summary> private string m_letter; /// <summary> /// Consumer Code /// </Summary> private int32 m_code; /// <summary> /// letter background color /// </Summary> private consolecolor m_backcolor; /// <summary> /// letter foreground color /// </Summary> private consolecolor m_fontcolor; /// <summary> /// letter activity /// </Summary> private Boolean m_balive; /// <summary> /// letter form position /// </Summary> private cpoint m_position; /// <summary> /// random number /// </Summary> Private Static random m_random = new random (); /// <summary> /// constructor /// </Summary> public letter () {} Public String getletter () {return this. m_letter;} public void setletter (string letter) {This. m_letter = letter;} public int32 getcode () {return this. m_code;} public void setcode (int32 code) {This. m_code = Code;} public char getchar () {return (char) This. m_code;} public consolecolor getbackcolor () {return this. m_backcolor;} public consolecolor getfontcolor () {return this. m_fontcolor;} public void setbackcolor (consolecolor color) {This. m_backcolor = color;} public void setfontcolor (consolecolor color) {This. m_fontcolor = color;} public Boolean getalive () {return m_balive;} public void setalive (Boolean balive) {This. m_balive = balive;} public cpoint getposition () {return this. m_position;} public void setposition (cpoint point) {This. m_position = point;} public void setposition (int32 X, int32 y) {This. m_position.setx (x); this. m_position.sety (y);} // <summary> // obtain a random letter. // </Summary> Public void newletter () {setletter (lettertable. getletter (m_random.next (0, 26); setcode (lettertable. getlettercode (getletter (); setbackcolor (consolecolor) m_random.next (0, 16); setfontcolor (consolecolor) (15-(int32) getbackcolor ())); setposition (m_random.next (1, 30),-1); setalive (false );} /// <summary> /// draw letters /// </Summary> /// <Param name = "Draw"> </param> Public void draw (cdraw draw) {If (m_balive) {draw. setbackcolor (this. m_backcolor); draw. drawtext (m_letter, m_position.getx (), m_position.gety (), 1, 1, this. m_fontcolor);} draw. setdrawsymbol (csymbol. default); draw. fillrect (m_position.getx (), m_position.gety ()-1, 1, 1, consolecolor. black );}}}

Defines a letter management class, which is used to manage all fallen letters and the number of dropped letters each time. The letter management class is defined:/// Lettermanager class implementation

Using system; using system. collections. generic; using cgraphics; namespace typing {internal sealed class lettermanager {private list <letter> m_letters; /// <summary> /// Number of dropped letters /// </Summary> private int32 m_fallnum; private random m_random; private int32 m_fallrate; Public lettermanager (int32 num, int32 rate) {This. m_random = new random (); this. m_fallnum = num; this. m_fallrate = rate; this. m_letters = new list <letter> (); For (int32 I = 0; I <m_fallnum; I ++) {letter = new letter (); letter. newletter (); this. m_letters.add (letter) ;}} public list <letter> getletters () {return this. m_letters;} public int32 getfallnum () {return this. m_fallnum;} public int32 getfallrate () {return this. m_fallrate;} public void setfallrate (int32 rate) {If (rate <0) {This. m_fallrate = 1; return;} This. m_fallrate = rate;} public void addfallnum (int32 num) {If (Num <= 0) {return;} This. m_fallnum + = num; For (int32 I = 0; I <num; I ++) {letter = new letter (); letter. newletter (); this. m_letters.add (letter) ;}} public void draw (cdraw draw) {for (int32 I = 0; I <m_letters.count; I ++) {m_letters [I]. draw (draw );}}}}

Finally, the game class implements the game logic. The game class is implemented as follows:/// Typinggame class implementation

Using system; using system. collections. generic; using cengine; using cgraphics; namespace typing {public sealed class typinggame: cgame {private lettermanager m_letbuilder; /// <summary> /// correct number of typing operations /// </Summary> private float m_rightcount; /// <summary> /// number of types /// </Summary> private float m_typingcount; private cdraw m_draw; /// <summary> /// countdown /// </Summary> private int32 m_countdown; private I Nt32 m_lasttime; // <summary> // Fall Speed Control /// </Summary> private int32 m_falltime; /// <summary> /// whether or not you are typing /// </Summary> private Boolean m_istype; private random m_random; protected override void gameinit () {settitle ("console game-keyboard trainer V1.0"); m_letbuilder = new lettermanager (20, 2); setcursorvisible (false); setupdaterate (40); m_rightcount = 0f; m_typingcount = 0f; m_countdown = 200; m_falltime = 10; M_istype = false; m_random = new random (); m_draw = base. getdraw (); m_draw.setdrawsymbol (csymbol. rect_solid); m_draw.drawrect (0, 0, 31, 25, consolecolor. white); m_draw.setdrawsymbol (csymbol. default); m_draw.fillrect (1, 0, 29, 1, consolecolor. black); m_draw.setdrawsymbol (csymbol. fig); m_draw.drawrect (31, 0, 9, 13, consolecolor. darkyellow); m_draw.setdrawsymbol (csymbol. fig); m_draw. Drawrect (31, 12, 9, 13, consolecolor. darkyellow); m_draw.drawtext ("Countdown:", 65, 2, lelecolor. red); m_draw.drawtext ("correct rate:", 65, 4, consolecolor. green); m_draw.drawtext ("number of keys:", 65, 6, consolecolor. green); m_draw.drawtext ("FPS:", 65, 8, consolecolor. green); m_draw.drawtext ("rate:", 65, 10, consolecolor. green); m_draw.drawtext ("Operation: keyboard A--Z key, press the keyboard corresponding to the whereabouts of the letter, the correct rate reflects your keyboard operation familiarity. ", New crect (33, 14, 5, 10), consolecolor. darkgreen);} protected override void gamedraw (cgraphics. cdraw draw) {m_letbuilder.draw (draw); draw. setbackcolor (consolecolor. black); draw. drawtext (m_countdown.tostring ("000"), 73, 2, consolecolor. red); If (m_typingcount! = 0) {draw. drawtext (m_rightcount/m_typingcount) * 100 ). tostring ("000") + "%", 73, 4, consolecolor. red);} else {draw. drawtext ("0%", 73, 4, consolecolor. red);} draw. drawtext (m_typingcount.tostring ("000"), 73, 6, consolecolor. red); draw. drawtext (getfps (). tostring (), 73, 8, consolecolor. blue); draw. drawtext (getupdaterate (). tostring (), 73, 10, consolecolor. blue);} protected override void gamelo OP () {If (m_falltime = 0) {list <letter> letters = m_letbuilder.getletters (); For (int32 I = 0; I <letters. count; I ++) {// The probability of each letter falling if (m_random.next (0, m_letbuilder.getfallnum () * m_letbuilder.getfallrate () = I) {letters [I]. setalive (true);} // If (letters [I]. getalive () {letters [I]. setposition (letters [I]. getposition (). getx (), letters [I]. getposition (). gety () + 1);} // The letter falls to the ground if (lett ERS [I]. getposition (). gety ()> = getclientrect (). getheight ()-1) {letters [I]. newletter (); m_typingcount ++;} m_falltime = 5;} m_falltime --; If (environment. tickcount-m_lasttime> 1000) {m_lasttime = environment. tickcount; m_countdown --; If (m_countdown = 0) {setgameover (true) ;}} protected override void gameexit () {m_letbuilder = NULL; base. getdraw (). clear (consolecolor. black); base. getdr Aw (). drawtext ("game over! ", 25, 10, consolecolor. red); base. getdraw (). drawtext ("Copyright (c) D-zone Studio", 40, 10, consolecolor. white); console. readline ();} protected override void gamekeydown (ckeyboardeventargs e) {If (! M_istype) {list <letter> letters = m_letbuilder.getletters (); For (int32 I = 0; I <letters. count; I ++) {If (letters [I]. getalive () {// whether to match the letter if (letters [I]. getchar (). tostring () = E. getkey (). tostring () {base. update (New crect (letters [I]. getposition (). getx (), letters [I]. getposition (). gety (), 1, 1); letters [I]. newletter (); m_rightcount ++; break ;}}m_typingcount ++; If (E. getkey () = ckeys. escape) {setgameover (true) ;}m_istype = true ;}} protected override void gamekeyup (ckeyboardeventargs e) {m_istype = false ;}}}

  From the previous and current games, we can see that, apart from the logical design differences, many other parts of different games are universal, we do not need to repeat coding every time we write a game. This is also the reason and necessity for writing the game framework.
Finally, let's take a look at our labor achievements: the trial link: http://download.csdn.net/detail/hwenycocodq520/4630387 conclusion: This game demo has been implemented in a rush, and the code is messy, but it is a simple logic, I believe everyone can understand it.

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.