@Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if (e.getKeyCode() == KeyEvent.VK_W) { this.hero.setDirection(0); hero.moveUp(); } else if (e.getKeyCode() == KeyEvent.VK_D) { this.hero.setDirection(1); hero.moveRight(); } else if (e.getKeyCode() == KeyEvent.VK_S) { this.hero.setDirection(2); hero.moveDown(); } else if (e.getKeyCode() == KeyEvent.VK_A) { this.hero.setDirection(3); hero.moveLeft(); } // 判断玩家是否按下J键 则开火 if (e.getKeyCode() == KeyEvent.VK_J) { if (this.hero.bombs.size() <= 40) { hero.fire(); } } this.repaint(); }
public void paint(Graphics g) { super.paint(g); // 画出我的坦克 g.fillRect(0, 0, 400, 300); // 确定区域为400 300 背景为黑色值 // 使用画坦克方法画出坦克 this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0); // 画出敌人的坦克 for (int i = 0; i < ets.size(); i++) { this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 1); } }
// 重写paint方法 public void paint(Graphics g) { super.paint(g); // float lineWidth = 3.0f; // ((Graphics2D)g).setStroke(new BasicStroke(lineWidth)); // 将坦克的活动区域填充为默认黑色 g.fillRect(0, 0, 800, 600); this.drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 1); for (int i = 0; i < hero.bombs.size(); i++) { Bomb myBomb = hero.bombs.get(i); // 画出一颗子弹 if (myBomb != null && myBomb.isLive == true) { // float lineWidth = 2.0f; // ((Graphics2D) g).setStroke(new BasicStroke(lineWidth));//设置线条为粗线 g.draw3DRect(myBomb.x, myBomb.y, 2, 2, true); } if (myBomb.isLive == false) { hero.bombs.remove(myBomb); } } // 画出爆炸 for (int i = 0; i < baozhas.size(); i++) { BaoZha bz = baozhas.get(i); System.out.println("baozhas.size()= " + baozhas.size()); if (bz.life > 5) { g.drawImage(image3, bz.x, bz.y, 30, 30, this); } else if (bz.life > 3) { g.drawImage(image2, bz.x, bz.y, 30, 30, this); } else { g.drawImage(image1, bz.x, bz.y, 30, 30, this); } bz.liftDown(); if (bz.life == 0) { baozhas.remove(bz); } } // 画出敌人的坦克 for (int i = 0; i < ets.size(); i++) { EnemyTank et = ets.get(i); if (et.isLive) { this.drawTank(et.getX(), et.getY(), g, et.getDirection(), 0); } } }