package com.diaodiao; import java.awt.Frame; import java.awt.Color; 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; public class TankClient extends Frame { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; private Tank myTank = new Tank(50, 50); private Missile m = new Missile(50, 60, Tank.Direction.R); Image offScreenImage = null; @Override public void paint(Graphics g) { m.draw(g); myTank.drawTank(g); } public void update(Graphics g) { if(offScreenImage == null) { offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT); } 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, 0, 0, null); } /** * */ private static final long serialVersionUID = 1L; public void lauchFrame() { setLocation(300, 100); setSize(GAME_WIDTH, GAME_HEIGHT); setTitle("TankWar"); setResizable(false); setBackground(Color.GREEN); this.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); this.addKeyListener(new KeyMonitor()); setVisible(true); new Thread(new PaintThread()).start(); } /** * @param args */ public static void main(String[] args) { TankClient tc = new TankClient(); tc.lauchFrame(); } private class PaintThread implements Runnable { @Override public void run() { while(true) { repaint(); try { Thread.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class KeyMonitor extends KeyAdapter { @Override public void keyReleased(KeyEvent e) { myTank.keyReleased(e); } @Override public void keyPressed(KeyEvent e) { myTank.keyPressed(e); } } }
package com.diaodiao; import java.awt.Color; import java.awt.Graphics; import java.awt.event.KeyEvent; public class Tank { public static final int XSPEED = 5; public static final int YSPEED = 5; private int x, y; private boolean bL = false, bU = false, bR = false, bD = false; enum Direction {L, LU, U, RU, R, RD, D, LD, STOP}; private Direction dir = Direction.STOP; public Tank() {} public Tank(int x, int y) { this.x = x; this.y = y; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public void drawTank(Graphics g) { Color c = g.getColor(); g.setColor(Color.RED); g.fillOval(x, y, 30, 30); g.setColor(c); move(); } 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 -= XSPEED; y += YSPEED; break; case STOP: break; } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_RIGHT) { bR = true; }else if(key == KeyEvent.VK_DOWN) { bD = true; }else if(key == KeyEvent.VK_LEFT) { bL = true; }else if(key == KeyEvent.VK_UP) { bU = true; } locateDirection(); } void locateDirection() { if(bL && !bR && !bU && !bD) { dir = Direction.L; }else if(bL && !bR && bU && !bD) { dir = Direction.LU; }else if(!bL && !bR && bU && !bD) { dir = Direction.U; }else if(!bL && bR && bU && !bD) { dir = Direction.RU; }else if(!bL && bR && !bU && !bD) { dir = Direction.R; }else if(!bL && bR && !bU && bD) { dir = Direction.RD; }else if(!bL && !bR && !bU && bD) { dir = Direction.D; }else if(bL && !bR && !bU && bD) { dir = Direction.LD; }else { dir = Direction.STOP; } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if(key == KeyEvent.VK_RIGHT) { bR = false; }else if(key == KeyEvent.VK_DOWN) { bD = false; }else if(key == KeyEvent.VK_LEFT) { bL = false; }else if(key == KeyEvent.VK_UP) { bU = false; } locateDirection(); } }
package com.diaodiao; import java.awt.Color; import java.awt.Graphics; public class Missile { //子弹类 public static final int XSPEED = 10; //每一次移动的位移量X public static final int YSPEED = 10; //每一次移动的位移量Y private int x,y; //子弹坐标 Tank.Direction dir; //子弹飞行的方向 public Missile(int x, int y, Tank.Direction dir) { this.x = x; this.y = y; this.dir = dir; } public void draw(Graphics g) { Color c = g.getColor(); g.setColor(Color.BLACK); g.fillOval(x, y, 10, 10); 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; break; case D: y += YSPEED; break; case LD: x -= XSPEED; y += YSPEED; break; } } }