Yang zhongke C # Shooting Game

Source: Internet
Author: User

Learning Yang zhongke version of C # Shooting Game video, you can refer to http://player.youku.com/player.php/sid/XMTUyNjQ3NjQ0/v.swf

Prepare materials, an airplane image, and a WAV airplane encounters a bullet explosion.
1. Create a winform Application
Form, drag a panel control, equivalent to the battlefield. Drag a timer control to manipulate aircraft and bullet run.
Three events are used in the panel control. One is the paint event, which is used to trigger the call to draw planes and bullets. The first is the mouseclick event, which is used to launch bullets when you click the Panel area at will. Another is the timer tick incident, which is used to control the movement of planes and bullets.
2. Create an abstract class gameobject for the bullet class and airplane class inheritance. Bullets and planes, common attributes, coordinates of X and Y on the panel, and methods for drawing bullets/planes and moving.
3. the class for creating a bullet bullut and plane inherits from the abstract class gameobject. It also adds the unique attributes and methods of the bullet itself. For example, the bullet class has its own attributes bool isfired, determine whether a bullet is fired (a bullet needs to be fired only when you click the mouse). The bullet class has its own method to determine whether the bullet hits the airplane bool ishit ();
The Panel class has its own attribute: the number of times a hittime bullet hits. In this game, a bullet must be hit twice before it can be exploded and an explosion occurs.
The bool isvisble attribute is used when the first time a bullet hits the plane, the plane starts to flash. (the effect of flashing is that the plane will be painted on the panel for a while, and then hidden again ), resart is your method in panel. When an airplane flies to a boundary or breaks down, the plane needs to appear again from the left.
4. Create a gamepanel class that inherits from the panel control and solves the problem of aircraft flashing continuously in the game (not the first time the plane was hit ). Dual buffering is applied.
The Code is as follows:

Form1.cs
Using system;
Using system. Windows. forms;
Using system. drawing;
Using Game. properties;
Using system. Media;

Namespace game
{
Public partial class form1: Form
{
Plane plane;
Bullet bullet;
Public form1 ()
{
Initializecomponent ();
Newbullet ();
Newplane ();
}

// Instantiate a bullet

Private void newbullet ()
{
Bullet = new bullet ();
Bullet. isfired = false;
Bullet. rect = panelfiled. clientrectangle;
Bullet. Y = panelfiled. clientrectangle. height;
}
// Instantiate an airplane
Private void newplane ()
{
Plane = new plane ();
Plane. Restart ();
Plane. rect = panelfiled. clientrectangle;
Random rad = new random ();
Plane. Y = rad. Next (0,100) // The plane appears randomly in the left Y axis.

}

Private void panelfiled_paint (Object sender, painteventargs E)
{

Plane. Draw (E. Graphics );
Bullet. Draw (E. Graphics );

}
Private void timereffectick (Object sender, eventargs E)
{
Bullet. Move ();
Plane. Move ();
If (bullet. ishit (plane) = true) // determines when a bullet hits the plane
{
Newbullet ();
Plane. hittime ++;
If (plane. hittime = 2) // when two hits occur, an explosion occurs.
{
Newplane ();
Soundplayer player = new soundplayer (resources. Bomb );
Player. Play ();

}
}
Panelfiled. invalidate ();
}

Private void panelfiled_mouseclick (Object sender, mouseeventargs E)
{
Bullet. isfired = true; // when the Mouse starts to click, the bullet is fired.
Bullet. x = E. X;
Bullet. Y = panelfiled. clientrectangle. height;
Panelfiled. invalidate ();

}


}

}
// Abstract class
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. drawing;

Namespace game
{


Abstract class gameobject
{
Public int X {Get; set ;}
Public int y {Get; set ;}
Public rectangle rect {Get; set ;}
Public abstract void draw (Graphics E );
Public abstract void move ();
}

}

// Bullet
Using system. drawing;
Using system;

Namespace game
{

Class bullet: gameobject
{
Public const int r = 5; // bullet radius
Public bool isfired {Get; set ;}
// Draw a bullet
Public override void draw (Graphics E)
{
If (isfired = false) // used to determine whether to start clicking. A bullet cannot be automatically fired if not clicked.
{
Return;
}
E. fillellipse (New solidbrush (color. Black), X, Y, R * 2, R * 2 );

}
Public override void move ()
{
If (isfired = false)
{
Return;
}
Y = Y-3; // move up
}
// Determine whether a bullet hits the plane and the distance between the two objects. if the distance between the two objects is smaller than the radius of the small object, the collision is determined.
Public bool ishit (plane)
{
Int detalx = plane. X-X;
Int detaly = plane. Y-Y;
Double Dist = math. SQRT (detalx * detalx + detaly * detaly );
If (Dist> bullet. R + plane. R)
{
Return false;

}
Else
Return true;
}
}
}

// Aircraft

Namespace game
{
Class plane: gameobject
{
Public const int r = 25;
Public int hittime {Get; set ;}
Private bool isvisble;
Public int Speed {Get; set ;}
Random rad = new random ();
Public override void draw (Graphics E)
{
If (hittime = 1) // when the first hit is detected, the plane starts to flash to make it visible and disappear.
{
Isvisble =! Isvisble;
If (! Isvisble)
{
Return;
}

}
E. drawimage (resources. Plane, X, Y, R * 2, R * 2 );
}
// When the plane explodes or both hit the right panel, it appears again
Public void restart ()
{
Speed = rad. Next (1, 3 );
Y = rad. Next (0, rect. Height); // returns the position where the Y axis appears at random.
X = 0;
Hittime = 0; // The number of hits displayed after reproduction is 0.
}
Public override void move ()
{
// Speed of movement
X = x + speed; // move to the right
If (x> rect. width)
{
Restart ();
}
}
}
}

// Solve the double buffer class
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. Windows. forms;

Namespace game
{
Class gamepanel: Panel
{
Public gamepanel ()
{
Setstyle (controlstyles. userpaint |
Controlstyles. allpaintinginwmpaint |
Controlstyles. optimizeddoublebuffer |
Controlstyles. resizeredraw |
Controlstyles. supportstransparentbackcolor, true );
}
}
}

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.