画板类
package com.hsp.Tank2.tankegame; import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; //坦克大战的绘图区域 public class MyPanel extends JPanel implements KeyListener { Hero hero = null; public MyPanel() { hero = new Hero(400, 400);//初始化自己的坦克 hero.setSpeed(40); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // g.setColor(Color.magenta);//设置画笔颜色 g.fillRect(0, 0, 1000, 750);//填充矩形 默认黑色//左上角的x,y坐标,宽,高 // g.drawRect(30, 30, 300, 300); //画出自己坦克-封装方法 drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 0); } public void drawTank(int x, int y, Graphics g, int direct, int type) { //根据坦克类型设置颜色 switch (type) { case 0: g.setColor(Color.cyan); break; case 1: g.setColor(Color.red); break; } switch (direct) { case 0://向上 g.fill3DRect(x, y, 10, 60, true); g.fill3DRect(x + 30, y, 10, 60, true); g.fill3DRect(x + 10, y + 10, 20, 40, false); g.fillOval(x + 10, y + 20, 20, 20); g.drawLine(x + 20, y + 30, x + 20, y); break; case 1: g.fill3DRect(x, y, 60, 10, true); g.fill3DRect(x, y + 30, 60, 10, true); g.fill3DRect(x + 10, y + 10, 40, 20, false); g.fillOval(x + 20, y + 10, 20, 20); g.drawLine(x + 30, y + 20, x + 60, y + 20); break; case 2: g.fill3DRect(x, y, 10, 60, true); g.fill3DRect(x + 30, y, 10, 60, true); g.fill3DRect(x + 10, y + 10, 20, 40, false); g.fillOval(x + 10, y + 20, 20, 20); g.drawLine(x + 20, y + 30, x+20 , y+60); case 3: g.fill3DRect(x, y, 60, 10, true); g.fill3DRect(x, y + 30, 60, 10, true); g.fill3DRect(x + 10, y + 10, 40, 20, false); g.fillOval(x + 20, y + 10, 20, 20); g.drawLine(x + 30, y + 20, x, y + 20); break; default: System.out.println("不做处理"); } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_W) { hero.setDirect(0); hero.moveUp(); } else if (e.getKeyCode() == KeyEvent.VK_D) { hero.setDirect(1); hero.moveRight(); } else if (e.getKeyCode() == KeyEvent.VK_S) { hero.setDirect(2); hero.down(); } else if (e.getKeyCode() == KeyEvent.VK_A) { hero.setDirect(3); hero.moveLeft(); } this.repaint(); } @Override public void keyReleased(KeyEvent e) { } }
坦克游戏类
package com.hsp.Tank2.tankegame; import javax.swing.*; @SuppressWarnings({"all"}) class LXTankGameo2 extends JFrame { MyPanel mp = null; public static void main(String[] args) { LXTankGameo2 lxTankGameo1 = new LXTankGameo2(); } public LXTankGameo2() { mp = new MyPanel(); this.add(mp); this.addKeyListener(mp); this.setSize(1000, 750);//把面板(就是坦克大战游戏绘图区域)添加进来 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } }
坦克父类
package com.hsp.Tank2.tankegame; public class Tank { private int x; private int y; private int direct =3; private int speed=5; public void setSpeed(int speed) { this.speed = speed; } public int getSpeed() { return speed; } public Tank(int x, int y) { this.x = x; this.y = y; } public void setY(int y) { this.y = y; } public void setX(int x) { this.x = x; } public int getX() { return x; } public int getY() { return y; } public void setDirect(int direct) { this.direct = direct; } public int getDirect() { return direct; }public void moveUp(){ y=y-speed; }public void moveRight (){ x=x+speed; }public void down(){ y=y+speed; }public void moveLeft(){ x=x-speed; } }
坦克子类(Hero)
package com.hsp.Tank2.tankegame; public class Hero extends Tank { public Hero(int x, int y) { super(x, y); } }