Tank war source code

Source: Internet
Author: User

When I was a child, didn't you play overlord like me? So you know how to implement it with a program? I have been thinking about this problem since that time! I realized this ideal when I was studying! Tank wars are divided into eight categories. I upload one to my blog every other time, and then I will be able to play after the accumulation is complete! It is my pleasure to share with you, and I hope you can give suggestions! Leave a message for me. On the last day of the upload, I will give everyone a document to play this game! Pictures of the game are in my album!

 

This class controls the direction of tanks!

Zid. Java

Package tank;

Import java. AWT. graphics;
Import java. AWT. image;
Import java. AWT. rectangle;
Import java. AWT. toolkit;
Import java. util. List;
Import java. util. Random;

Import javax. Swing. joptionpane;

Import tank. Tank. direction;

Public class Zid {
Int X;
Int y;
Public int xspeed;
Public int yspeed;
Int GG, X1, Y1, dj1;
Random RD = new random ();
Tank. Direction dir;
Boolean life = true;
Tankclient Tc = NULL;
Boolean good = false;
Toolkit TK = toolkit. getdefatooltoolkit ();
Image IMG = NULL;
Public Zid (){}
Public Zid (int x, int y, direction DIR ){
This. x = x + 25;
This. Y = Y + 25;
This. dir = dir;
}
Public Zid (int x, int y, direction Dir, tankclient TC ){
This. x = x + 25;
This. Y = Y + 25;
This. dir = dir;
This. tc = tc;
}
Public Zid (int x, int y, direction Dir, tankclient TC, Boolean good ){
This. x = x + 25;
This. Y = Y + 25;
This. dir = dir;
This. tc = tc;
This. Good = good;
}
Public void draw (Graphics g, int xspeed, int yspeed)
{
This. xspeed = xspeed;
This. yspeed = yspeed;
X1 = RD. nextint (700 );
Y1 = RD. nextint (500 );
Dj1 = RD. nextint (7 );
G. drawimage (IMG, X, Y, null );
Move ();
If (! Life)
TC. Bullets. Remove (this );
}
Public void move (){
Zhangq (TC. Wils );
Hitzid (TC. Bullets );
If (DIR = direction. l)
{X-= xspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missilel.gif "));
}
Else if (DIR = direction. Lu)
{
X-= xspeed;
Y-= yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missilelu.gif "));
}
Else if (DIR = direction. U)
{
Y-= yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missiled.gif "));
}
Else if (DIR = direction. ur)
{
X + = xspeed;
Y-= yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missileru.gif "));
}
Else if (DIR = direction. R)
{
X + = xspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missiler.gif "));
}
Else if (DIR = direction. RD)
{
X + = xspeed;
Y + = yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missilerd.gif "));
}
Else if (DIR = direction. d)
{
Y + = yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missileu.gif "));
}
Else if (DIR = direction. dl)
{
X-= xspeed;
Y + = yspeed;
IMG = Tk. getimage (tankclient. Class. getclassloader (). getresource ("images/missileld.gif "));
}
If (x> TC. getwidth () | Y> TC. getheight () | x <0 | Y <0)
Life = false;
Hittanks (TC. Enemy );
Hittank (TC. mytank );
}

Public Boolean isgood (){
Return good;
}
Public Boolean islife (){
Return life;
}
Public Boolean hittank (tank T)
{
Boolean flag = false;
If (good! = T. Good)
{
Flag = This. getrectangle (). intersects (T. getrectangle ());
If (FLAG)
{
If (T. Life)
{
TC. explodes. Add (New explode (x, y ));
Life = false;
If (! Good ){
TC. huanlife ();
}
If (good & (T. bdx = 2) | (T. bdx = 3) | (T. bdx = 4 ))){
If (New rectangle (x1, Y1, 50,50). intersects (New rectangle (250,450, 50*5, 50*3 )))
{
TC. DJ. Add (New csqj (98,89, dj1, Tc ));
}
Else
{
TC. DJ. Add (New csqj (x1, Y1, dj1, Tc ));
}
}
}

}
}
Return flag;
}
Public void hitzid (list <Zid> Zid)
{
For (INT I = 0; I <Zid. Size (); I ++)
{
Zid ZD = Zid. Get (I );
Int x = ZD. X;
Int y = ZD. Y;
If (this. Good! = ZD. Good)
{
If (this. getrectangle (). intersects (New rectangle (X, Y, 26,26 )))
{
This. Life = false; ZD. Life = false;
}}
}

}
Public void zhangq (list <wall> Wils)
{
For (INT I = 0; I <Wils. Size (); I ++)
{
If (this. getrectangle (). intersects (Wils. Get (I). getrectangle ()))
{
If (INT) Wils. Get (I). Shu! = 3 & (INT) Wils. Get (I). Shu! = 7)
{
TC. explodes. Add (New explode (x, y ));
This. Life = false;
}
TC. Del (I, this );
If (this. getrectangle (). intersects (New rectangle (350,550, 50, 50 )))
{
TC. Frame ();
This. x = 0;
This. Y = 0;
}
}
}
}
Public rectangle getrectangle (){
Return new rectangle (X, Y, 5, 5 );
}
Public void hittanks (list <tank> tanks)
{
For (INT I = 0; I <tanks. Size (); I ++ ){
If (this. hittank (tanks. Get (I )))
If (TC. clera! = 1 ){
TC. Enemy. Remove (I );
TC. fenshu + = 50;
} Else {
TC. clera = 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.