Java-坦克移动(面向对象+集合选型)
实现过程
- 普通Tank类;
- 1.增加了坦克朝向属性direction;
- 2.增加了坦克移动速度属性speed;
- 3.为了控制坦克移动,将移动操作(即改变坦克的横纵坐标,重新绘制的过程)封装为方法;
public class Tank{
private int x;
private int y;
private int direction;
private int speed = 5;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public void moveUp(){
this.y -= this.speed;
}
public void moveDown(){
this.y += this.speed;
}
public void moveRight(){
this.x += this.speed;
}
public void moveLeft(){
this.x -= this.speed;
}
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 int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
public class Hero extends Tank {
public Hero(int x, int y) {
super(x, y);
}
}
public class EnemyTank extends Tank{
public EnemyTank(int x, int y) {
super(x, y);
}
}
- 坦克游戏窗口类;
- 1.为了控制坦克移动,需要在游戏窗口类添加键盘监听KeyListener;
class GameFrame extends JFrame {
GamePanel gamePanel = null;
public void draw() {
gamePanel = new GamePanel();
this.add(gamePanel);
this.setSize(1000, 800);
this.setVisible(true);
this.addKeyListener(gamePanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
- 坦克游戏绘制区域;
- 1.在paint()方法中实现根据坦克朝向的不同,绘制不同的坦克;
- 2.实现键盘监听接口KeyListener,并重写该接口中的方法来实现控制坦克向上、向下、向左、向右、加速、减速等操作;
- 3.增加敌方坦克:
- 1)集合选型:考虑到后续需要多线程来操控敌方坦克,因此选择Vector来存储;
- 2)初始化敌方坦克:定义变量enemyTankSize作为默认敌方坦克数量,并使用for循环初始化;
- 3)绘制敌方坦克:考虑到后续敌方坦克可能被消灭,因此遍历Vector进行绘制时,可以使用普通for循环或者增强for循环,需要注意的是,使用普通for循环时,应遍历当前Vector中坦克的实际数量,例如使用enemyTanks.size();
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
class GamePanel extends JPanel implements KeyListener {
Hero hero = null;
Vector<EnemyTank> enemyTanks = null;
int enemyTankSize = 5;
public GamePanel() {
hero = new Hero(250, 400);
enemyTanks = new Vector<EnemyTank>();
for (int i = 0; i < enemyTankSize; i++) {
EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
enemyTank.setDirection(1);
enemyTanks.add(enemyTank);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
g.fillRect(0, 0, 1000, 800);
g.setFont(new Font("楷体", Font.BOLD, 32));
g.setColor(Color.RED);
g.drawString("TankGame V0.1 by SEA-365", 60, 40);
drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 0);
for (EnemyTank enemyTank : enemyTanks) {
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), 1);
}
}
public void drawTank(int x, int y, Graphics g, int direction, int type){
switch (type){
case 0://玩家坦克
g.setColor(Color.YELLOW);
break;
case 1://敌方坦克
g.setColor(Color.CYAN);
break;
}
switch (direction){
case 0://向上坦克
g.fill3DRect(x, y, 10, 60, false);
g.fill3DRect(x+34, y, 10, 60, false);
g.fill3DRect(x+10, y+10, 24, 40, false);
g.fillOval(x+10, y+18, 24, 24 );
g.fill3DRect(x+22, y, 2, 22, false);
break;
case 1://向下坦克
g.fill3DRect(x, y, 10, 60, false);
g.fill3DRect(x+34, y, 10, 60, false);
g.fill3DRect(x+10, y+10, 24, 40, false);
g.fillOval(x+10, y+18, 24, 24 );
g.fill3DRect(x+22, y+42, 2, 22, false);
break;
case 2://向左坦克
g.fill3DRect(x, y, 60, 10, false);
g.fill3DRect(x, y+34, 60, 10, false);
g.fill3DRect(x+10, y+10, 40, 24, false);
g.fillOval(x+18, y+10, 24, 24 );
g.fill3DRect(x, y+22, 22, 2, false);
break;
case 3://向右坦克
g.fill3DRect(x, y, 60, 10, false);
g.fill3DRect(x, y+34, 60, 10, false);
g.fill3DRect(x+10, y+10, 40, 24, false);
g.fillOval(x+18, y+10, 24, 24 );
g.fill3DRect(x+42, y+22, 22, 2, false);
break;
default:
break;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_DOWN || (char)e.getKeyCode() == 'S' || (char)e.getKeyCode() == 's'){
this.hero.setDirection(1);
this.hero.moveDown();
}
if(e.getKeyCode() == KeyEvent.VK_UP || (char)e.getKeyCode() == 'W' || (char)e.getKeyCode() == 'w'){
this.hero.setDirection(0);
this.hero.moveUp();
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT || (char)e.getKeyCode() == 'D' || (char)e.getKeyCode() == 'd'){
this.hero.setDirection(3);
this.hero.moveRight();
}
if(e.getKeyCode() == KeyEvent.VK_LEFT || (char)e.getKeyCode() == 'A' || (char)e.getKeyCode() == 'a'){
this.hero.setDirection(2);
this.hero.moveLeft();
}
if(e.getKeyCode() == '='){
this.hero.setSpeed(this.hero.getSpeed() + 2);
}
if(e.getKeyCode() == '-'){
this.hero.setSpeed(this.hero.getSpeed() - 2);
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
实现效果