I named the game sabboy --> damage the King. The name is mighty like the demolition team of tianchao! First look at the game class diagram:
Baseentity: abstract class
Isabboy: Interface Class
Ball: small ball games that can be used to implement the "Sports" interface.
Board: baffle, which can be moved to implement the "motion" interface.
BRICK: block type, static brick, no "movement" behavior.
After the basic framework is built, the following is the implementation problem. Step by step, start with the base class:
Baseentity. CS
Public abstract class baseentity <br/>{< br/> // coordinate <br/> Public int xpos {Get; Set ;}< br/> Public int ypos {Get; set ;}< br/> // speed and direction control <br/> Public int speedx {Get; Set ;}< br/> Public int speedy {Get; set ;} <br/> // object carrier <br/> Public rectangle rect {Get; set ;} <br/> // draw object <br/> public abstract void draw (Graphics g); <br/>}
Interface Class: isabboy. CS
Public interface isabboy <br/>{< br/> void run (); <br/>}
Ball Games: ball. CS,
Public class ball: baseentity, isabboy <br/> {<br/> /// <summary> <br/> // initialize the position and offset of the ball. <br/> /// </Summary> <br/> Public ball (int x, int y, int speedx, int speedy) <br/>{< br/> This. xpos = x; <br/> This. ypos = y; <br/> This. speedx = speedx; <br/> This. speedy = speedy; <br/>}</P> <p> Public override void draw (Graphics g) <br/>{< br/> using (solidbrush sbrush = new solidbrush (color. snow) <br/>{< br/> rect = new rectangle (xpos, ypos, 20, 20); <br/> G. drawellipse (new pen (color. gray), rect); <br/> G. fillellipse (sbrush, rect); <br/>}< br/> G. dispose (); <br/>}</P> <p> # region isabboy member <br/> // critical collision detection, using random numbers, improve playability <br/> Public void run () <br/>{< br/> xpos = xpos + speedx; <br/> ypos = ypos-Speedy; <br/> If (xpos <= 0) <br/> speedx = (new random (). next (3, 5); <br/> If (xpos> 378) <br/> speedx =-(new random (). next (3, 5); <br/> If (ypos <= 100) <br/> speedy =-(new random (). next (3, 8); <br/>}< br/> # endregion <br/>}
Baffle: board. CS
// Baffle direction <br/> Public Enum boarddirection <br/>{< br/> left, right <br/>}</P> <p> public class board: baseentity, isabboy <br/>{< br/> Public boarddirection ction {Get; Set ;}</P> <p> Public Board (int x, int y, int speed) <br/>{< br/> This. xpos = x; <br/> This. ypos = y; <br/> This. speedx = speed; <br/>}</P> <p> Public override void draw (Graphics g) <br/>{< br/> using (solidbrush sbrush = new solidbrush (color. lightblue) <br/>{< br/> pen P = new pen (color. saddlebrown); <br/> rect = new rectangle (xpos, ypos, 70, 15); <br/> G. drawrectangle (p, rect); <br/> G. fillrectangle (sbrush, rect); <br/>}< br/> G. dispose (); <br/>}</P> <p> # region isabboy member <br/> Public void run () <br/>{< br/> switch (Direction) <br/>{< br/> case boarddirection. left: <br/>{< br/> xpos-= speedx; <br/>}< br/> break; <br/> case boarddirection. right: <br/>{< br/> xpos + = speedx; <br/>}< br/> break; <br/> default: <br/> break; <br/>}< br/> # endregion <br/>}
The following is the brick class: Brick. CS
The bricks are instantiated using the rectangle class, but the game cannot have only one block of bricks, so we need to construct a large wall --> List <rectangle>, as for what the walls are, they are constructed according to your own interests. Here I create an inverted pyramid wall: (figure 1)
(Figure 1)
Public class BRICK: baseentity <br/>{< br/> private int _ width = 400; // brick Set width <br/> private int _ Height = 300; // Brick set height <br/> // Brick set <br/> public list <rectangle> rects {Get; set ;} <br/> // create a brick wall <br/> public list <rectangle> brickwall () <br/> {<br/> int temp = 0; <br/> rects = new list <rectangle> (); <br/> for (INT I = 100; I <_ height; I ++ = 20) <br/> {<br/> temp + = 20; <br/> for (Int J = temp-20; j <_ width-temp; j + = 30) <br/>{< br/> rect = new rectangle (J, I, 28, 18); <br/> rects. add (rect); <br/>}< br/> return rects; <br/>}< br/> // draw a wall <br/> Public override void draw (Graphics g) <br/>{< br/> using (solidbrush sbrush = new solidbrush (color. orange) <br/>{< br/> G. clear (color. black); <br/> pen P = new pen (color. saddlebrown, 3); <br/> foreach (rectangle R in rects) <br/>{< br/> G. drawrectangle (P, R); <br/> G. fillrectangle (sbrush, R); <br/>}< br/> G. dispose (); <br/>}< br/>}
After the basic construction of the objects in the game is complete, you only need to process the logical relationships between them and display them on the interface! Can't wait? Next time, break it down!