"WinForm Development 2048 mini-Games"

Source: Internet
Author: User

First look at the interface:

Game Help Class

Class Gamecore {//game map Private int[,] map = new int[4, 4];        Temporary array used when merging private int[] Mergearr = new Int[4];        Empty number private int emptycount = 16;        All vacant positions Private location[] Emptyarr = new LOCATION[16];        Private random random = new random ();        Artboard private Graphics GP = null;        Make artboards with bitmaps private image image = null;        Game Item size Private Const int itemsize = 80;        Game score private int score = 0;        public bool Ischange {get; set;} <summary>//Current score///</summary> public int Score {get {return s Core            }}///<summary>///number of vacancies///</summary> public int Emptycount {            get {return emptycount;         }}///<summary>///game initialization///</summary> public Gamecore () {   Computeemptycount ();            Initializes an array of location loc1;            Generatenum (out Loc1);            Computeemptycount ();            Generatenum (out Loc1); Initialize artboard image = new Bitmap (itemsize * 4 + 5 * 8, ItemSize * 4 + 5            * 8);        GP = graphics.fromimage (image); #region Move and merge the same number adjacent///<summary>///Mobile cell data///</summary>//<par            Am Name= "direction" ></param> public void Move (string direction) {Ischange = false;                switch (direction) {case ' up ': MoveUp (); Case "Down": MoveDown ();                Break Case "left": MoveLeft ();                Break Case "right": MoveRight ();            Break            }}///<summary>////</summary> public void MoveUp () { 1 for (int c = 0; c< 4; C + +)//column {for (int r = 0, index = 0; r < 4; r++)//Line {if (m                     Ap[r, c]! = 0)//Non 0 {mergearr[index++] = map[r, c];//put into merged array}                        The number position changes if (mergearr[r]! = Map[r, c]) {                    Ischange = true;                } map[r, c] = 0;//The matrix takes away the clear 0}//2 merge Mergecell (); 3 Put the merged data into the matrix for (int i = 0, r = 0; i < 4; i++) {if (Mergea                        Rr[i]! = 0) {map[r++, c] = mergearr[i];                    Mergearr[i] = 0; }}}///<summary>//Down//</summary> Publi c void MoveDown () {//1 for (int c = 0; c < 4; c++)//column {for (int r = 3, index = 0; r >= 0; r--)//Line {if (MA                    P[r, c]! = 0)//Non 0 {mergearr[index++] = map[r, c];//put into merged array}                    if (mergearr[3-r]! = Map[r, c]) {Ischange = true; } map[r, c] = 0;//takes the matrix out of the clear 0}//2 Merge Mergecell (                ); 3 Put the merged data into the matrix for (int i = 0, r = 3; i < 4; i++) {if (Mergearr[i]!                        = 0) {map[r--, c] = mergearr[i];                    Mergearr[i] = 0; }}}}///<summary>//Merge cells///</summary> PR ivate void Mergecell () {for (int i = 0; i < 3; i++) {if (mergearr[i] = 0 && mergearr[i] = = mergearr[i + 1]) {Mergearr[i] + + mergearr[i + 1];                    Score + = Mergearr[i];                    Ischange = true;                Mergearr[i + 1] = 0;         }}}///<summary>//Left shift//</summary> public void MoveLeft () {//1 for (int r = 0; r < 4; r++)//Line {for (int c = 0, index = 0; C < 4; C + +)//column {if (MAP[R, c]! = 0)//non 0 {mergearr[i                        ndex++] = map[r, c];//into the merged array} if (Mergearr[c]! = Map[r, c]) {                    Ischange = true;                } map[r, c] = 0;//The matrix takes away the clear 0}//2 merge Mergecell (); 3 Put the merged data into the matrix for (int i = 0, c = 0; i < 4; i++) {if (mergearr[i]! = 0) {map[r, C + +] =                        Mergearr[i];                    Mergearr[i] = 0; }}}///<summary>//Right Shift///</summary> Publi c void MoveRight () {//1 for (int r = 0; r < 4; r++)//Line {for (i NT C = 3, index = 0; C >= 0; c--)//column {if (MAP[R, c]! = 0)//not 0 mergearr[index++] = map[r, c];/                                    /Put the merged array if (MERGEARR[3-C]! = Map[r, c]) Ischange = true;                Map[r, c] = 0;//The matrix takes away the clear 0}//2 merges Mergecell (); 3 Put the merged data into the matrix for (int i = 0, c = 3; i < 4; i++) {if (Mergearr[i]!                    = 0) {    Map[r, c--] = mergearr[i];                    Mergearr[i] = 0; }}} #endregion//<summary>///&LT;/SU            mmary> public void Computeemptycount () {emptycount = 0;            Array.clear (Emptyarr, 0, 16);                    for (int r = 0; r < 4; r++)//Line {for (int c = 0; c < 4; C + +)//column {                    if (map[r, c] = = 0) {emptyarr[emptycount++] = new Location (r, c); }}}}///<summary>///To generate a new number//</summ ary> public int generatenum {int index = random.             Next (0, Emptycount);             location = Emptyarr[index]; Map[location. X, location. Y] = random. Next ()% 10 = = 0?             4:2; Return map[location. X, location.   Y];     }///<summary>//Determine if the game is over//</summary>//&LT;RETURNS&GT;&LT;/RETURNS&G        T            public bool Isgameover () {computeemptycount ();            If there is a vacancy, the game does not end if (Emptycount > 0) return false;                 Check for the same number in the adjacent cell, for (int r = 0; r < 4; r++) {for (int c = 0; c < 3; C + +) {//Horizontal if (map[r, c] = = Map[r, C + 1]) return                    False                Vertical if (map[c, r] = = Map[c + 1, R]) return false;        }} return true; } #region Draw the game screen///<summary>///Draw game screen///</summary>//<returns&gt ;</returns> public Image Drawmap () {//Empty artboard original content GP.            Clear (Color.darkgray); for (int r = 0; r < map. GetLength (0); r++) {for (int c = 0; c < map. GetLength (1);                C + +) {DrawItem (R, C, GetColor (Map[r, c]));        }} return image; }///<summary>///use numbers to find pictures from the Assembly resources///</summary>//<param name= "num" >< /param>//<returns></returns> public Image GetColor (int num) {return Pro Perties.                      Resources.                      ResourceManager. GetObject ("_" + Num.)        ToString ()) as Image; }///<summary>///Draw a number block///</summary>//<param name= "rowindex" > Row index &lt ;/param>//<param name= "columnindex" > Column index </param>//<param name= "img" > Picture to be drawn </par am> private void DrawItem (int rowindex, int columnindex, Image img) {//Where to draw rowindex Columnind ex int x = (columnindex + 1) * 8 + columnindex * itemsize;            int y = (rowindex + 1) * 8 + rowindex * itemsize; Draw content img To draw how big ItemSize GP.        DrawImage (IMG, x, y, itemsize, itemsize); } #endregion}

Define a structure:

struct location    {        private int x;        private int y;        public location (int x, int y)        {            this.x = x;            This.y = y;        }             public int X        {            get {return X;}        }        public int Y        {            get {return Y;}        }    }

Form code:

  Public partial class Frmmain:form {private Gamecore gamecore = null;        Public Frmmain () {InitializeComponent ();        } public void Drawgamemap () {this.picMap.Image = Gamecore.drawmap ();        }//Start game private void Frmmain_load (object sender, EventArgs e) {startgame ();            } private void Startgame () {//to distinguish 0 Lblscore.text = "0";            Gamecore = new Gamecore ();        Drawgamemap ();        } private void btnStart_Click (object sender, EventArgs e) {startgame (); }//Keyboard control game action private void Frmmain_keydown (object sender, KeyEventArgs e) {string Keystri            ng = e.keydata.tostring ();                Switch (keystring) {case ' up ': Case ' down ': Case ' left ':     Case "right": Gamecore.move (keystring);               Update game map Updategamemap ();            Break            }} public void Updategamemap () {//game flow?                Map has changed if (gamecore.ischange) {//Draw game map drawgamemap ();                Refresh Score This.lblScore.Text = GameCore.Score.ToString ();                Recalculate vacancy gamecore.computeemptycount (); If the number of seats is greater than 0 if (Gamecore.emptycount > 0) {//Generate new number Lo                     Cation Loc;                    int num = gamecore.generatenum (out loc);                    Turns on animating the new number asynchronously calls action<int,int,int> handler = Drawnewnumber; Handler. BeginInvoke (Loc. X, loc.                                    Y, num, null, NULL);       }//Determine if the game continues if (Gamecore.isgameover ())         {MessageBox.Show ("GAME over");  }}} public void Drawnewnumber (int r,int c,int num) {int xofmap =            (c + 1) * 8 + c * 80;            int Yofmap = (R + 1) * 8 + R * 80;            Graphics GP = This.picMap.CreateGraphics ();            Image img = gamecore.getcolor (num);            The numbers appear to be small and large, with a minimum width of 10 and a maximum of int w = 10;                while (w <=) {int x =xofmap+ 80/2-W/2;                int y =yofmap+ 80/2-w/2; Gp.                DrawImage (IMG, x, y, W, W);                W + = 5;            System.Threading.Thread.Sleep (10); }        }    }

OK, it's done.

"WinForm Development 2048 mini-Games"

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.