Beginner WPF, do a little game practice

Source: Internet
Author: User

Used to do the project when the use of Silverlight, recently learned about WPF, made a simple little game to practiced hand, I hope to communicate with you a lot.

Idea: the strongest brain cattle, the color of the resolution is more stunning, so I thought to do a small game, simple, do grid-like color block, interspersed with a different color but very similar color block, click on the color block is for customs clearance, into the next level, the next level of the number of blocks + 1, until a similar level is reached, no longer +1;

Effect

Say do, new project, began to realize, first prepared a set of color similar color code, Baidu HTML color code, casually selected, the more adjacent color chromatic aberration smaller.

1  Private string[] Colors =New string[][] {2             New string[2]{"#7B7B7B","#8E8E8E"} ,3             New string[2]{"#FF5151","#ff7575"} ,4             New string[2]{"#ffaad5","#FFC1E0"} ,5             New string[2]{"#DCB5FF","#E6CAFF"} ,6             New string[2]{"#79FF79","#93FF93"} ,7             New string[2]{"#97CBFF","#ACD6FF"} ,8             New string[2]{"#FFFF93","#FFFFAA"} ,9             New string[2]{"#B766AD","#C07AB8"} ,Ten             New string[2]{"#B7FF4A","#C2FF68"} One};

Related code

    <summary>////Games Find chromatic aberration instances///</summary> public partial class Mainwindow:window {p   rivate int level = 2;    Current level private Double cavwidth = 0;    Container width, Width height as private double btnwidth = 0; Color block width High const int maxlevel = 8;            Maximum level//color array private string[][] colors = new string[][] {new string[2]{"#7B7B7B", "#8E8E8E"}, New string[2]{"#FF5151", "#ff7575"}, New string[2]{"#ffaad5", "#FFC1E0"}, New string[2]{"#            Dcb5ff "," #E6CAFF "}, New string[2]{" #79FF79 "," #93FF93 "}, New string[2]{" #97CBFF "," #ACD6FF "}, New string[2]{"#FFFF93", "#FFFFAA"}, New string[2]{"#B766AD", "#C07AB8"}, new string[2]{"#B7FF        4 A "," #C2FF68 "}};        The WPF timer uses the DispatcherTimer class object private System.Windows.Threading.DispatcherTimer Dtimer = null;        private int tmnum = 30; Public MainWindow () {InitializeComponent ();        Load ();            } private void Load () {if (Dtimer! = null) {dtimer.stop ();                } else {//timer Dtimer = new DispatcherTimer ();                Note: The Tick here is the event of the Dtimer object (occurs when the timer interval exceeds) Dtimer.tick + = new EventHandler (Dtimer_tick);            Set time: TimeSpan (hours, minutes, seconds) Dtimer.interval = new TimeSpan (0, 0, 1);            }//Start DispatcherTimer object dtime.            Dtimer.start ();            Tmnum = 30; Lbltime.content = string.            Format ("remaining {0} seconds", Tmnum);            Cavwidth = Cavcontent.width;            Btnwidth = Cavwidth/level; int otherindex = Getrandomnum (level * level); Not the same lbllevel.content = string.            Format ("{0} off", LEVEL-1); string[] STRs = colors[getrandomnum (colors.            Length)];            String colorbtnbg = Strs[0];         for (int i = 0; I < level; i++)   {for (int j = 0, J < level; J + +) {//Add a color block to the container (button)  Button btn = New button () {Margin = new Thickness (Btnwidth * I, Btnwidth * J, 0, 0), Background = new SolidColorBrush ((Color) System.Windows.Media.ColorConverter.ConvertFromS Tring (COLORBTNBG)), borderthickness = new Thickness (1), BorderBrush = Brushe                    S.black, Width = btnwidth, Height = btnwidth};                    Randomly generated color block number, color change to a similar color, when clicked on the color block to find the difference, enter the game next close if (Otherindex = = I * level + j) {btn.                        Background = new SolidColorBrush ((Color) System.Windows.Media.ColorConverter.ConvertFromString (strs[1]); Btn.                    Click + = Btn_click;                } cavContent.Children.Add (BTN); }            }}///<summary>//timing//</summary>/<param name= "Sen Der "></param>//<param name=" E "></param> private void Dtimer_tick (object sender, even Targs e) {lbltime.content = string.            Format ("remaining {0} seconds", Tmnum);            if (Tmnum <) {Lbltime.foreground = brushes.red;                } if (tmnum <= 0) {dtimer.stop (); Messageboxresult result = MessageBox.Show ("Time is up, the game is not finished yet, do you want to continue?")                "," Game over ", Messageboxbutton.yesno);                    if (result = = Messageboxresult.yes) {level = 2; Load (); Reset Game} else {this.                Close ();        }} tmnum--; }///<summary>///Click on color blocks of different colors to trigger///</summary>/<param name= "Sender "></param>//<param name=" E "></param> void Btn_click (object sender, RoutedEvent            Args e) {cavContent.Children.Clear ();            The number is not increasing when the maximum level is reached, otherwise one row of color block if (levels <= maxlevel) {level = level + 1;        } Load (); }///<summary>///<param name= "maxnum" >0-maximum interval </  param>//<returns></returns> public int getrandomnum (int maxnum) {Random            Radom = new Random (); Return Radom.        Next (Maxnum); }    }

  

Beginner WPF, do a little game practice

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.