java新手小项目---坦克大战

1.坦克类(Tank)

//坦克类
class Tank {
    int x = 0;
    int y = 0;

    //坦克颜色
    int color ;

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    //坦克的速度
    int speed = 1;
    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    //0表示上  1表示右  2表示下  3表示左
    int direct = 0;

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    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;
    }
}

 

2.画出我的坦克(Hero)

//我的坦克
class Hero extends Tank {
    public Hero(int x,int y)
    {
        super(x,y);
    }

    //向上移动
    public void moveUp(){
        y -= speed;
    }

    //向下移动
    public void moveDown(){
        y += speed;
    }

    //向左移动
    public void moveLeft(){
        x -= speed;
    }

    //向右移动
    public void moveRight(){
        x += speed;
    }
}

 3.画出敌人的坦克(EnemyTank)

class EnemyTank extends Tank{
    public EnemyTank (int x,int y){
        super(x,y);
    }
}

4.画出我的面板(MyPanel)

//我的面板
class MyPanel extends JPanel implements KeyListener {
    //定义一个我的坦克
    Hero hero = null;

    //定义敌人坦克集合
    Vector<EnemyTank> ets = new Vector<EnemyTank>();

    int enSize = 3;

    //构造函数
    public MyPanel() {
        hero = new Hero(100,100);

        //初始化敌人坦克
        for(int i=0;i<enSize;i++){
            EnemyTank et = new EnemyTank((i+1)*50,0);
            et.setColor(1);
            et.setDirect(2);
            ets.add(et);
        }
    }

    //重新paint
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,600,400);
        //画出自己的坦克
        this.drawTank(hero.getX(),hero.getY(),g,0,this.hero.direct);
        //画出敌人的坦克
        for(int i=0;i<ets.size();i++){
            this.drawTank(ets.get(i).getX(),ets.get(i).getY(),g,1,ets.get(i).getDirect());
        }
    }

    public void drawTank(int x,int y,Graphics g,int type,int direct) {
        //判断坦克类型
        switch (type) {
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
            default:
                break;
        }

        //判断方向
        switch (direct) {
            //画我的坦克
            //向上
            case 0:
                g.fill3DRect(x,y,5,30,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fillOval(x+5,y+10,10,10);
                g.drawLine(x+10,y+15,x+10,y);
                break;
            //向右
            case 1:
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x+30,y+10);
                break;
            //向下
            case 2:
                g.fill3DRect(x,y,5,30,false);
                g.fill3DRect(x+15,y,5,30,false);
                g.fill3DRect(x+5,y+5,10,20,false);
                g.fillOval(x+5,y+10,10,10);
                g.drawLine(x+10,y+15,x+10,y+30);
                break;
            //向左
            case 3:
                g.fill3DRect(x,y,30,5,false);
                g.fill3DRect(x,y+15,30,5,false);
                g.fill3DRect(x+5,y+5,20,10,false);
                g.fillOval(x+10,y+5,10,10);
                g.drawLine(x+15,y+10,x,y+10);
                break;
            default:
                break;
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    //wasd控制方向上下左右
    @Override
    public void keyPressed(KeyEvent e) {
        //设置我的坦克方向
        if(e.getKeyCode() == KeyEvent.VK_W) {
            this.hero.setDirect(0);
            this.hero.moveUp();
        }else if(e.getKeyCode() == KeyEvent.VK_A){
            this.hero.setDirect(3);
            this.hero.moveLeft();
        }else if(e.getKeyCode() == KeyEvent.VK_S){
            this.hero.setDirect(2);
            this.hero.moveDown();
        }else if(e.getKeyCode() == KeyEvent.VK_D){
            this.hero.setDirect(1);
            this.hero.moveRight();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

}

5.主类(MyTankGame2)

/**
 * 功能:坦克大战 2.0
 * 1.画出坦克
 * 2.我的坦克可以移动
 */
package com.test3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

public class MyTankGame2 extends JFrame {
    MyPanel mp = null;
    public static void main(String[] args)
    {
        MyTankGame2 mtg = new MyTankGame2();
    }

    public MyTankGame2() {
        mp = new MyPanel();

        this.add(mp);
        this.addKeyListener(mp);
        this.setSize(600,400);
        this.setLocation(500,220);
        this.setVisible(true);
    }
}

此项目尚未完成,先上传目前做完的部分(可以控制坦克移动),后面会陆续上传并完善流程。

目前效果为:

java新手小项目---坦克大战