Simple alphabet-based games

Source: Internet
Author: User

1. first look at the figure

 

2. The idea is simple:

Place a timer control on the form, add a label control to the form at a specified time interval, and use the label control to display randomly generated letters;

When you press the key, traverse all the label controls in the form. If the text value is the same as that of the keyboard, remove the label control;

Note: once found, the loop will be terminated; otherwise, all text values and the labels with the same letters will be clear;

Here, only the upper-case letter A-Z is applied, its ASCII code is 65-90, easy to generate random letters

3,Code

 

Using System;
Using System. drawing;
Using System. Windows. forms;

namespace printletter
{< br> Public partial class letter: form
{< br> Public letter ()
{< br> initializecomponent ();
}

private void letter_load ( Object sender, eventargs e)
{< br> /// A-Z 65-90
/// A-Z 97
timer1.start ();
}

Private   Void Timereffectick ( Object Sender, eventargs E)
{
// Add a label control in the form.
// Assign random letters to the text attribute of the label.
Random rdletter =   New Random ();
Label LBL =   New Label ();
LBL. Text = Convert. tostring (( Char ) Rdletter. Next ( 65 , 90 ));
LBL. Location =   New Point (rdletter. Next ( 10 , This . Clientsize. Width - 10 ), 0 );
LBL. Size =   New Size ( 20 , 20 );
LBL. Font =   New Font ( " " , 15f );;
This . Controls. Add (LBL );

Foreach (Control C In   This . Controls)
{
Label lblcontrol = (Label) C;
Lblcontrol. Location =   New Point (lblcontrol. Location. X,
Lblcontrol. Location. Y +   5 );
}
}

Private   Void Letter_keyup ( Object Sender, keyeventargs E)
{
// Traverses all label controls in the current form,
// If the text value of the label control is equal to the value of the currently pressed key
// Then the control is removed and the total score is + 5.
Foreach (Control C In   This . Controls)
{
Label lblcontrol = (Label) C;
If (Lblcontrol. Text = E. keycode. tostring ())
{
This . Controls. Remove (C );
This . Text = Convert. tostring ( Int . Parse ( This . Text) +   5 );
Break ;
}
}
}
}
}

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.