I remember that when I was studying Java in college, I downloaded a lot of Java videos and saw some of them introduce simple game development. Jack Ma told me that he was very interested. I watched the video and wrote a program. Now I have graduated, because C # is used in my work. Recently I really want to use C # to rewrite the tank war I wrote earlier to familiarize myself with the basic syntax of C.
The program is very simple. It has not changed much compared with the Java code.
The implementation method is as follows:
1. Add a panel to form and obtain the graphics object in the panel paint method.
2. Draw tanks, bullets, and other related content on a panel using the graphics object
3. added the timer control to control the re-painting of the Panel to realize the movement of tanks and bullets.
4. determine the direction of the tank based on the direction keys pressed by the computer. When the Panel is re-painted, modify the X and Y axes of the tank based on the direction of the tank to move the tank.
5. Use the intersectswith function of rectangle to perform collision detection.
Specific implementation code
1. Add enumeration types to the Project
/// <Summary>
/// Enumeration type indicating the direction
/// </Summary>
Public Enum direction {L, U, D, R, stop}
2. Add constants and attributes of the bullet class.
/// <Summary>
/// Speed of the X axis of the bullet, in PX
/// </Summary>
Public static int xspeed = 10;
/// <Summary>
/// Speed of the bullet Y axis, in PX
/// </Summary>
Public static int yspeed = 10;
/// <Summary>
/// Bullet width
/// </Summary>
Public static int width = 10;
/// <Summary>
/// Bullet height
/// </Summary>
Public static int Height = 10;
/// <Summary>
/// Bullet coordinates
/// </Summary>
Int X, Y;
/// <Summary>
/// Bullet direction
/// </Summary>
Direction dir;
/// <Summary>
/// The survival status of the bullet
/// </Summary>
Private bool live = true;
/// <Summary>
/// Tankclient form instance
/// </Summary>
Private tankclient;
/// <Summary>
/// Mark of both sides of the enemy and me
/// </Summary>
Private bool good;
3. Add the draw method to draw the bullet
Public void draw (Graphics g)
{
If (! Live)
{
Tankclient. Missiles. Remove (this );
Return;
}
// Display bullets on the interface by drawing an Elliptical Function
G. fillellipse (brushes. Black, X, Y, missile. Width, missile. Height );
Move ();
}
4. How to add a bullet to combat tanks
Public bool hittank (tank T)
{
// Intersectswith is used to detect the collision between two rectangles.
If (getrectangle (). intersectswith (T. getrectangle () & T. Live & this. Live & this. Good! = T. Good)
{
T. Live = false;
This. Live = false;
Return true;
}
Return false;
}
5. Add the attributes and constants of the tank class.
/// <Summary>
/// Tank X axis speed
/// </Summary>
Public static int xspeed = 5;
/// <Summary>
/// Tank Y axis speed
/// </Summary>
Public static int yspeed = 5;
/// <Summary>
/// Tank width
/// </Summary>
Public static int width = 30;
/// <Summary>
/// Tank height
/// </Summary>
Public static int Height = 30;
/// <Summary>
/// Tank coordinates
/// </Summary>
Private int X, Y;
/// <Summary>
/// Mark whether the upper and lower right keys are pressed
/// </Summary>
Private bool L = false, u = false, r = false, D = false;
/// <Summary>
/// Tank direction
/// </Summary>
Private direction dir = direction. Stop;
/// <Summary>
/// Tank barrel direction
/// </Summary>
Private direction ptdir = direction. D;
/// <Summary>
/// Tankclient form instance
/// </Summary>
Tankclient;
/// <Summary>
/// Mark both sides of the enemy and me
/// </Summary>
Private bool good;
/// <Summary>
/// Used to control irregular operations of enemy tanks
/// </Summary>
Private int step = 0;
/// <Summary>
/// Mark the survival status of the tank
/// </Summary>
Private bool live = true;
6. Implement the tank painting method in the tank class
Public void draw (Graphics g)
{
If (! Live)
{
If (! Good)
{
Tankclient. Tanks. Remove (this );
}
Return;
}
If (good)
{
// Draw a tank using fillellipse
G. fillellipse (brushes. Red, X, Y, width, height );
}
Else
{
G. fillellipse (brushes. Blue, X, Y, width, height );
}
// Draw the barrel of the tank based on the barrel Tank
Switch (ptdir)
{
Case direction. D:
G. drawline (pens. Black, x + width/2, Y + height/2, x + width/2, Y + height );
Break;
Case direction. U:
G. drawline (pens. Black, x + width/2, Y + height/2, x + width/2, y );
Break;
Case direction. L:
G. drawline (pens. Black, x + width/2, Y + height/2, X, Y + height/2 );
Break;
Case direction. R:
G. drawline (pens. Black, x + width/2, Y + height/2, x + width, Y + height/2 );
Break;
}
Move ();
}
7. Code related to keyboard buttons
Public void keypressed (keyeventargs E)
{
Keys key = E. keycode;
Switch (key)
{
Case keys. Right:
R = true;
Break;
Case keys. left:
L = true;
Break;
Case keys. Up:
U = true;
Break;
Case keys. down:
D = true;
Break;
}
Locatedirection ();
}
8. Tank method of issuing bullets
Public missile fire ()
{
If (! Live) return NULL;
Int x = This. x + width/2-missile. width/2;
Int y = This. Y + height/2-missile. Height/2;
Missile = new missile (X, Y, good, ptdir, tankclient );
Tankclient. Missiles. Add (missile );
Return missile;
}
9. Add the main form class to the tank
Mytank = new tank (50, 20, true, this); // put it in front of this cannot use // the Y axis is 30 less than Java
For (INT I = 0; I <15; I ++)
{
// Add 10 tanks with X axis spacing of 40px
Tanks. Add (new tank (50 + 40 * (I + 1), 20, false, this); // the Y axis is 30 less than that of Java
}
10. The method of calling the bullet to hit the tank in the main form class
For (INT I = 0; I <missiles. Count; I ++)
{
Missile M = Missiles [I];
M. hittank (mytank );
M. hittanks (tanks );
M. Draw (g );
}
11. Main form processing key code
Private void form1_keydown (Object sender, keyeventargs E)
{
Mytank. keypressed (E );
}
12. Control the re-painting code
Private void timereffectick (Object sender, eventargs E)
{
// Controls the re-painting of the Panel within 50 milliseconds
Panel1.invalidate ();
}
13. This is basically the completion of the main code, but the game will flash
It can be solved through dual buffering. C # is easy to solve, and a function can solve it.
This. setstyle (controlstyles. optimizeddoublebuffer |
Controlstyles. resizeredraw |
Controlstyles. allpaintinginwmpaint, true );
By the way, I changed a mobile phone version, but the mobile phone version did not solve the dual-buffering problem. The screen was flickering and friends could improve it on their own.
Code download http://files.cnblogs.com/hehe108/tanker.rar
If you find anything unreasonable, you need to improve the place, mail contact 328452421@qq.com (qq perennial not online, mail contact ). Exchange with each other. Thank you.
Author: hehe108
Source: http://www.cnblogs.com/hehe108/
About the author: cainiao. If you have any questions or suggestions, please kindly advise me!
The copyright of this article is shared by the author and the blog. You are welcome to repost this article. However, you must keep this statement without the author's consent and provide the original article connection clearly on the article page.
If you have any questions, please contact me via 328452421@qq.com. Thank you very much.