Tetris-Graphic programming

Source: Internet
Author: User

The third topic is Tetris game. The game algorithm is relatively simple, the main kungfu in the interface experience, such as block graphics, user control, elimination and accumulation.

We use GDI + graphic display, a total of 8 kinds of graphics, of course, can be customized according to their preferences. How do I represent graphics? We can draw on the mosaic effect. All graphics are represented by 3*3 matrices, 0 for background color, 1 for shaded display. Like what

{0,0,0},

{0,1,0}, the T-type, do you see it? Each digit position is actually a square block.

{1,1,1}

User Control? Simple KeyPress event implementation is good. Of course, there is no user control is automatic whereabouts, you can use timer implementation.

Elimination algorithm? ^_^, of course, is very simple, if there are 0 cells on a line, then the expression is not filled, there is no need to eliminate, otherwise it will be eliminated. The cumulative is the array append, elimination is the element deletion.

Of course, the elimination and accumulation of data needs to be reflected in the interface, that is, drawing. Graphics objects We all use, in this Windows Forms game, we can implement OnPaint events. Whenever you need to redraw, such as user control, elimination, and so on, call form refresh, then naturally triggers the paint event. The following is a code example:

public int[][,] figures = new int[8][,]//8种图形
  {
    new int[3,3] {
  {0,0,0},
  {0,1,0},
  {1,1,1}
  },
    new int[3,3]
  {
  {0,0,0},
  {0,2,2},
  {2,2,0}
  },
    new int[3,3]
  {
  {0,0,0},
  {3,3,0},
  {0,3,3}
  },
    new int[2,2]
  {
  {4,4},
  {4,4}
  },
    new int[3,3]
  {
  {5,0,0},
  {5,0,0},
  {5,5,0}
  },
    new int[3,3]
  {
  {0,6,0},
  {0,6,0},
  {6,6,0}
  },
    new int[4,4]
  {
  {7,0,0,0},
  {7,0,0,0},
  {7,0,0,0},
  {7,0,0,0}
  },
    new int[3,3]
  {
  {0,8,0},
  {8,8,8},
  {0,0,0}
  }
  };

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.