坦克大戰(zhàn)源代碼_第1頁
坦克大戰(zhàn)源代碼_第2頁
坦克大戰(zhàn)源代碼_第3頁
坦克大戰(zhàn)源代碼_第4頁
坦克大戰(zhàn)源代碼_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

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() /生產(chǎn)多少地方坦克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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論