坦克大战程序源代码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
m.hitWall(w1);
m.hitWall(w2);
}
for(int i=0;i<tanks.size();i++)
{
Tank t=tanks.get(i);
t.collidesWall(w1);
t.collidesWall(w2);
t.collidesTanks(tanks);
t.collidesTank(myTank);
List<Missle> missles=new ArrayList<Missle>();
private static Random r=new Random();
Wall w1=new Wall(300,100,300,40,this),w2=new Wall(100,150,40,300,this);
}
}
}
import java.awt.*;
public class Wall {
int x,y,w,h;
TankClient tc;
public Wall(int x, int y, int w, int h, TankClient tc) {
this.x = x;
this.y = y;
this.w = w;
tk.getImage(Tank.class.getClassLoader().getResource("images/tankRD.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankD.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankLD.gif")),
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGH);
{
Addb.setLive(true);
}
}
Addb.draw(g);
myTank.eat(Addb);
w1.draw(g);
w2.draw(g);
}
public void update(Graphics g)
{
if(offScreenImage==null)
{
offScreenImage=this.createImage(GAME_WIDTH,GAME_HIGH);
t.draw(g);
}
for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
myTank.collidesTanks(tanks);
myTank.collidesWall(w1);
myTank.collidesWall(w2);
{
if(!live)
{
tc.missles.remove(this);
return;
}
//Color c=tank.getColor();
if(good)
{
tank.setColor(Color.RED);
drawBlood(tank);
}
else
tank.setColor(Color.BLUE);
}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.Random;
import java.awt.Image;
private int step=0,rn=0;
TankClient tc;
int life=100;
int dir=3;
public Tank(int x, int y,TankClient tc,boolean good)
{
this.x = x;
this.y = y;
this.oldx=x;
this.oldy=y;
tk.getImage(Tank.class.getClassLoader().getResource("images/tankU.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankRU.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankR.gif")),
this.h = h;
this.tc = tc;
}
public void draw(Graphics g)
{
g.setColor(Color.BLACK);
g.fillRect(x, y, w, h);
}
public Rectangle getRect()
{
return new Rectangle(x,y,w,h);
}
}
}
}
private class KeyMonitor extends KeyAdapter
{
public void keyReleased(KeyEvent e)
{
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e)
{
myTank.KeyPressed(e);
new Thread(new ThreadPaint()).start();
this.addKeyListener(new KeyMonitor());
}
public static void main(String[] args)
{
TankClient tc=new TankClient();
tc.showFrame();
AddBlood Addb=new AddBlood();
List<Explode> explodes = new ArrayList<Explode>();
Image offScreenImage=null;
public void paint(Graphics g)
{
g.drawString("missles count:"+missles.size(), 10,50);
this.tc=tc;
this.good=good;
}
private static Toolkit tk=Toolkit.getDefaultToolkit();
private static Image [] images={
tk.getImage(Tank.class.getClassLoader().getResource("images/tank1U.gif")),
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
public void showFrame()
{
for(int i=0;i<10;i++)
{
tanks.add(new Tank(600,50+(i+1)*50,this,false));
public static final int GAME_WIDTH=800;
public static final int GAME_HIGH=600;
Tank myTank=new Tank(50,50,this,true);
List<Tank> tanks=new ArrayList<Tank>();
g.drawString("tanks count:" + tanks.size(), 10, 70);
g.drawString("exlpode count:" +explodes.size(), 10, 90);
g.drawString("mytank life:" + myTank.getLife(), 10, 110);
myTank.draw(g);
while(myTank.life<=80&&!Addb.isLive())
{
Addb.x=r.nextInt(780);
Addb.y=r.nextInt(580);
if(Addb.x>10&&!Addb.collidesWall(w2)&&!Addb.collidesWall(w1)&&!Addb.collidesTanks(tanks)&&!Addb.collidesTank(myTank))
}
private class ThreadPaint implements Runnable
{
public void run() {
while(true)
{
repaint();
try {
Thread.sleep(50);
}
catch (InterruptedException e)
{
e.printStackTrace();
tank.drawImage(images[dir], x, y,null);
tank.setColor(Color.BLACK);
if(!good)
{
dir=direction(dir);
if(r.nextInt(30)>28)
tc.missles.add(fire(dir));
move(dir);
tk.getImage(Tank.class.getClassLoader().getResource("images/tankL.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankLU.gif"))
};
public void draw(Graphics tank)
{
public void windowClosing(WindowEvent e) {
System.exit(0);//表示正常退出
}
}
);
this.setBackground(Color.GREEN);
this.setResizable(false);//禁止窗口改变大小
this.setVisible(true);
}
this.setLocation(40, 40);
this.setSize(GAME_WIDTH, GAME_HIGH);
this.setTitle("TankWar");
//setBounds(40,40,800,600);
this.addWindowListener(new WindowAdapter()
for(int i=0;i<missles.size();i++)
{
Missle m=(Missle) missles.get(i);
if(!m.isMissle_live())
missles.remove(m);
else
m.draw(g);
m.hitTanks(tanks);
m.hitTank(myTank);
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
public class TankClient extends Frame
{
private static final long serialVersionUID = 1L;
private boolean A=false,W=false,S=false,D=false;
private boolean good;
private boolean live=true;
private int x,y,oldx,oldy;
private static Random r=new Random();
import java.awt.Toolkit;
public class Tank
{
public static final int LARGE_T=40;
public static final int LARGE_M=10;
public int XSPEED=4;
public int YSPEED=4;
}百度文库
else
{
if(direction(dir)==0)
stay();
else dir=direction(dir);
move(dir);
}
}
void move(int dir)
{
oldx=x;
oldy=y;
相关文档
最新文档