坦克大战源代码_第1页
坦克大战源代码_第2页
坦克大战源代码_第3页
坦克大战源代码_第4页
坦克大战源代码_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.ArrayList;import java.util.List;public class TankClient ex

2、tends Frame public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);List<Missile> missiles = new ArrayList<Missile>();List<Explode> explodes = new ArrayList<Explode>();List<Tank>

3、tanks = new ArrayList<Tank>();Image offScreenImage = null;Overridepublic void paint(Graphics g) g.drawString("missiles count:" + missiles.size(), 10, 50);g.drawString("explodes count:" + explodes.size(), 10, 70);g.drawString("tanks count:" + tanks.size(), 10, 90);

4、for(int i=0; i<missiles.size(); i+) Missile m = missiles.get(i);m.hitTanks(tanks);m.draw(g);for(int i=0; i<explodes.size(); i+) Explode e = explodes.get(i);e.draw(g);for(int i=0; i<tanks.size(); i+) Tank t = tanks.get(i);t.draw(g);myTank.draw(g);Overridepublic void update(Graphics g) if(off

5、ScreenImage = null) offScreenImage = this.createImage(800, 600);Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.GREEN);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage,

6、0, 0, null);public void launchFrame() /生产多少地方坦克for(int i=0; i<5; i+) tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this);this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle("TankWar");this.addWindowListener(new WindowAdapter() Overridepublic v

7、oid windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);this.addKeyListener(new KeyMonitor();this.setVisible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.launchFrame();class Pain

8、tThread implements Runnable public void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace();class KeyMonitor extends KeyAdapter Overridepublic void keyReleased(KeyEvent e) myTank.keyReleased(e);Overridepublic void keyPressed(KeyEvent e) myTank.keyPres

9、sed(e);import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.util.Random;public class Tank public static final int XSPEED = 5;public static final int YSPEED = 5;public static final int WIDTH = 30;public static final int HEIGHT = 30;boolea

10、n good;int x, y;private static Random r = new Random();private boolean live = true;private int step = r.nextInt(12) + 3;TankClient tc;boolean bL, bU, bR, bD;enum Direction L, LU, U, RU, R, RD, D, LD, STOP;Direction dir = Direction.STOP;Direction ptDir = Direction.D;public Tank(int x, int y, boolean

11、good) this.x = x;this.y = y;this.good = good;public Tank(int x, int y, boolean good, Direction dir, TankClient tc) this(x, y, good);this.dir = dir;this.tc = tc;public void draw(Graphics g) if(!live) if(!good) tc.tanks.remove(this);return;Color c = g.getColor();if(good) g.setColor(Color.RED);else g.s

12、etColor(Color.BLUE);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);switch(ptDir) case L:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT/2);break;case LU:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y);break;case U:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y);break;case RU:g.drawLine(x + WIDT

13、H/2, y + HEIGHT/2, x + WIDTH, y);break;case R:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT/2);break;case RD:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT);break;case D:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y + HEIGHT);break;case LD:g.drawLine(x + WIDTH/2, y

14、+ HEIGHT/2, x, y + HEIGHT);break;move();private void move() switch(dir) case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= X

15、SPEED;y += YSPEED;break;case STOP:break;if(dir != Direction.STOP) ptDir = dir;if(x < 0) x = 0;if(y < 30) y = 30;if(x + WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - WIDTH;if(y + HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - HEIGHT;if(!good) if(step = 0) step

16、= r.nextInt(12) + 3;Direction dirs = Direction.values();dir = dirsr.nextInt(dirs.length);step -;if(r.nextInt(40) > 38) this.fire();public void keyPressed(KeyEvent e) int key = e.getKeyCode();switch (key) case KeyEvent.VK_LEFT:bL = true;break;case KeyEvent.VK_UP:bU = true;break;case KeyEvent.VK_RI

17、GHT:bR = true;break;case KeyEvent.VK_DOWN:bD = true;break;locateDirection();private void locateDirection() if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR &&a

18、mp; !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR &&

19、amp; bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;public void keyReleased(KeyEvent e) int key = e.getKeyCode();switch (key) case KeyEvent.VK_CONTROL:fire();break;case Ke

20、yEvent.VK_LEFT:bL = false;break;case KeyEvent.VK_UP:bU = false;break;case KeyEvent.VK_RIGHT:bR = false;break;case KeyEvent.VK_DOWN:bD = false;break;locateDirection();private Missile fire() int x = this.x + WIDTH/2 - Missile.WIDTH/2;int y = this.y + HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile

21、(x, y, this.good, this.ptDir, this.tc);tc.missiles.add(m);return m;public Rectangle getRect() return new Rectangle(x, y, WIDTH, HEIGHT);public boolean isLive() return live;public void setLive(boolean live) this.live = live;import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;impo

22、rt java.util.List;public class Missile public static final int XSPEED = 10;public static final int YSPEED = 10;public static final int WIDTH = 10;public static final int HEIGHT = 10;TankClient tc;int x, y;Tank.Direction dir = Tank.Direction.R;boolean live = true;private boolean good;public Missile(i

23、nt x, int y, boolean good, Tank.Direction dir) this.x = x;this.y = y;this.good = good;this.dir = dir;public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) this(x, y, good, dir);this.tc = tc;public void draw(Graphics g) if(!live) tc.missiles.remove(this);return;Color c = g.get

24、Color();g.setColor(Color.BLACK);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);move();private void move() switch(dir) case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;

25、break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;if(x < 0 | y < 0 | x > TankClient.GAME_WIDTH | y > TankClient.GAME_HEIGHT) live = false;public Rectangle getRect() return new Rectangle(x, y, WIDTH, HEIGHT);public boolean hitTank(Tank t) if(this.live && t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论