码迷,mamicode.com
首页 > 其他好文 > 详细

坦克大战编程

时间:2019-04-22 22:36:46      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:static   ret   opened   nbsp   图片   graphics   lin   end   pac   

Demo1:

技术图片
package demo1;

//横着x 竖着y
import java.awt.*;
import javax.swing.*;


public class Demo2 extends JFrame {
    Myframe mp;

    public static void main(String[] args) {
        Demo2 demo = new Demo2();
    }

    public Demo2() {
        mp = new Myframe();
        this.add(mp);
        this.setSize(400, 300);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
}

// Myframe定义的面板用于绘图
class Myframe extends JPanel {
    Hero hero;

    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0, 0, 400, 300);
        this.drawTank(hero.getX(), hero.getY(), g, 0, 1);//可以实现坦克的类型0 1
    }

    public Myframe() {
        hero = new Hero(100, 50);//可以实现改变位置
    }

    // 画出坦克的函数
    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.YELLOW);
            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, x + 10, y + 15);
            break;
        }
    }
}

class Tank {
    int x;
    int y;

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

}

class Hero extends Tank {

    public Hero(int x, int y) {
        super(x, y);

    }

}
View Code

 

坦克大战编程

标签:static   ret   opened   nbsp   图片   graphics   lin   end   pac   

原文地址:https://www.cnblogs.com/helloworld2019/p/10753476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!