• 画坦克__敌人坦克


    一、代码如下

    package www.tainiu.gui__V2;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Vector;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class af__TankGame__V2 extends JFrame{
    
    	myPanel_V2 mp= null;
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		af__TankGame__V2 tv= new af__TankGame__V2();
    
    	}
    	
    	public af__TankGame__V2() {
    		// TODO Auto-generated constructor stub
    		mp = new myPanel_V2();
    		this.add(mp);
    
    		this.setSize(400, 300);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setVisible(true);
    		//绑定监听
    		this.addKeyListener(mp);
    	}
    
    }
    
    
    
    
    
    class Tank_V2 {
    	int x = 0;
    	int y = 0;
    	int direct= 1;
    	int speed= 20;
    	
    	public Tank_V2(int x, int y) {
    		// TODO Auto-generated constructor stub
    		this.x = x;
    		this.y = y;
    	}
    }
    
    //敌人坦克
    class DRTank extends Tank_V2{
    
    	public DRTank(int x, int y) {
    		super(x, y);
    		// TODO Auto-generated constructor stub
    	}
    	
    }
    
    
    
    //自己坦克
    class Hero_V2 extends Tank_V2 {
    		public Hero_V2(int x, int y) {
    			// TODO Auto-generated constructor stub
    			super(x, y);
    		}
    }
    
    class myPanel_V2 extends JPanel implements KeyListener {
    	Hero_V2 hero = null;
    	
    	//DRTank drt= null;
    	Vector<DRTank> dts= new Vector<>();
    	int dtsSize= 3;
    	public myPanel_V2() {
    		// TODO Auto-generated constructor stub
    		hero = new Hero_V2(10, 10);
    		for(int i=0; i<dtsSize; i++) {
    			DRTank dt= new DRTank((i+1)*50, 0);
    			dts.add(dt);
    		}
    	}
    
    	@Override
    	public void paint(Graphics g) {
    		// TODO Auto-generated method stub
    		super.paint(g);
    		//g.setColor(Color.yellow);
    		//g.drawRect(hero.x, hero.y , 5, 30);
    		//g.setColor(Color.CYAN);
    		g.fillRect(0, 0, 400, 300);
    		g.setColor(Color.CYAN);
    		
    //		g.fill3DRect(hero.x, hero.y, 5, 30, false);
    //		g.fill3DRect(hero.x+15, hero.y, 5, 30, false);
    //		g.fill3DRect(hero.x+5, hero.y+15, 10, 20, false);
    //		g.fillOval(hero.x+4, hero.y+20, 10, 10);
    //		g.drawLine(hero.x+10, hero.y+20, hero.x+10, 60);
    		//画出自己的坦克
    		this.drawTank(hero.x, hero.y, g, hero.direct, 0);
    		//画出敌人坦克
    		for(int i=0; i<dtsSize; i++) {
    			this.drawTank(dts.get(i).x, dts.get(i).y, g, 1, 1);
    		}
    
    	}
    	
    	private void drawTank(int x, int y, Graphics g, int derict, int type) {
    		// TODO Auto-generated method stub
    		switch (type) {
    		case 0://自己坦克
    			g.setColor(Color.CYAN);
    			break;
            case 1://敌人坦克
            	g.setColor(Color.yellow);			
    			break;
    		default:
    			break;
    		}
    		
    		switch (derict) {
    		case 1://方向想下
    			g.fill3DRect(x, y, 5, 30, false);
    			g.fill3DRect(x+15, y, 5, 30, false);
    			g.fill3DRect(x+5, y+15, 10, 20, false);
    			g.fillOval(x+4, y+20, 10, 10);
    			g.drawLine(x+10, y+20, x+10, y+60);
    			break;
    
    		case 2://方向想左
    			g.fill3DRect(x, y, 30, 5, false);
    			g.fill3DRect(x, y+15, 30, 5, false);
    			g.fill3DRect(x+15, y+5, 20, 10, false);
    			g.fillOval(x+15, y+5, 10, 10);
    			g.drawLine(x+17, y+10, x-19, y+10);
    			break;
    		case 3://方向想上
    			g.fill3DRect(x, y, 5, 30, false);
    			g.fill3DRect(x+15, y, 5, 30, false);
    			g.fill3DRect(x+5, y+15, 10, 20, false);
    			g.fillOval(x+4, y+20, 10, 10);
    			g.drawLine(x+10, y+20, x+10, y-20);
    			break;
    		case 4://方向想右
    			g.fill3DRect(x, y, 30, 5, false);
    			g.fill3DRect(x, y+15, 30, 5, false);
    			g.fill3DRect(x+15, y+5, 20, 10, false);
    			g.fillOval(x+15, y+5, 10, 10);
    			g.drawLine(x+17, y+10, x+45, y+10);
    			break;
    		}
    
    	}
    
    	@Override
    	public void keyTyped(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		// TODO Auto-generated method stub
    		//System.out.println("pp");
    		if(e.getKeyCode() == KeyEvent.VK_A) {
    			this.hero.x -= this.hero.speed;
    			this.hero.direct= 2;
    		}else if(e.getKeyCode() == KeyEvent.VK_D) {
    			this.hero.x += this.hero.speed;
    			this.hero.direct= 4;
    		}else if(e.getKeyCode() == KeyEvent.VK_W) {
    			this.hero.y -= this.hero.speed;
    			this.hero.direct= 3;
    		}else if(e.getKeyCode() == KeyEvent.VK_S) {
    			this.hero.y += this.hero.speed;
    			this.hero.direct= 1;
    		}
    		
    		this.repaint();
    	}
    
    	@Override
    	public void keyReleased(KeyEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    
  • 相关阅读:
    nyoj 463-九九乘法表
    nyoj 458-小光棍数 (471)
    nyoj 457-大小写互换
    nyoj 455-黑色帽子
    nyoj 412-Same binary weight (bitset ,to_ulong())
    nyoj 399-整除个数 (整除)
    nyoj 366-D的小L (next_permutation())
    nyoj 324-猴子吃桃问题 (m[i] = (m[i-1] + 1) * 2)
    nyoj 283-对称排序 (sort)
    HBase 在人工智能场景的使用
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/7633086.html
Copyright © 2020-2023  润新知