JAVA入门到精通-第37讲-事件总结-坦克大战5

1167156-20181130180318995-1171860743.png
 新建一个包:新建一个类,MyTankGame02.java

}1167156-20181130180319774-908987278.png
  1167156-20181130180320718-1818825604.png
坦克的移动:
1167156-20181130180321438-1099901206.png
 重新绘制repaint才能动起来;
 最后,需要注册监听;
1167156-20181130180322092-814118344.png
 这样,改起来速度很灵活;

改变坦克的方向:
Outline:大纲:
1167156-20181130180322783-1333862587.png
  类-方法:
  根据通包的访问机制,Members和MyTankGame
  是可以相互访问的。
  根据方向对函数进行扩展:(坦克炮筒方向)
  1167156-20181130180323465-339818670.png
 参照点需要从左上角,移动到中心点;
  1167156-20181130180324137-1811892644.png
 横着放,矩形,宽度、高度需要变化;
1167156-20181130180325053-1506406242.png
  1167156-20181130180325724-1520455838.png
 Vector线程安全;用Vector集合实现;
 敌人的 坦克:
 加一个颜色color:
1167156-20181130180328861-1559969297.png
 
 在面板上定义敌人的坦克:
1167156-20181130180329531-1898741400.png

 在构造函数里面进行初始化:
 每次横坐标间隔50,纵坐标为0:
  1167156-20181130180330202-1599461588.png
 创建一辆敌人坦克对象,加入到集合中;
 
 画出敌人坦克:
 用向量算出敌人坦克数量;
1167156-20181130180330958-1688110800.png
 g,direct,type  画笔,方向,坦克颜色
 初始化的时候换个方向:
  1167156-20181130180331678-1985982737.png
 
/** * 功能:坦克游戏的1.01 * 1、画出坦克 * 2、坦克动起来 */ import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; public class MyTank02 extends JFrame{ //定义组件 MyPanel mp=null; public static void main(String[] args) { MyTank02 mt=new MyTank02(); } //构造函数 public MyTank02(){ //构建组件 mp=new MyPanel(); //监听 this.addKeyListener(mp); //加入组件 this.add(mp); //设置JFrame窗体 this.setTitle("坦克大战");//JFrame标题 this.setSize(400, 300);//JFrame窗体大小 this.setLocationRelativeTo(null);//在屏幕中心显示 this.setVisible(true);//显示 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出并关闭JFrame } } //我的面板Panel class MyPanel extends JPanel implements KeyListener{ //定义一个我的坦克 Hero hero=null; int x=10,y=10,z=0;//初始坦克在MyPanel的位置 //构造函数 public MyPanel(){ hero=new Hero(x, y, z);//我的坦克初始位置 } //重写paint函数 public void paint(Graphics g){ super.paint(g);//调用父类paint方法 //设置Panel底色 g.fillRect(0, 0, 400, 300);//fillRect(0,0,X?,Y?)中X?/Y?为活动区域 //调用坦克 this.drawTank(hero.getX(), hero.getY(), g, 1, hero.getZ()); } //画出坦克的函数 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; case 2: g.setColor(Color.red); break; } //判断坦克的方向 switch(direct){ //向上走的坦克 case 0: //画出我的坦克(到时再封装成一个函数) //1、画出左边的矩形 g.fill3DRect(x, y, 5, 30, false); //2、画出右边的矩形 g.fill3DRect(x+15, y, 5, 30, false); //3、画出中间矩形 g.fill3DRect(x+5, y+5, 10, 20, false); //4、画出中间圆形 g.fillOval(x+5, y+10, 10, 10); //5、画出线(炮筒) g.drawLine(x+10, y+15, x+10, y); break; //向下走的坦克 case 1: //1、画出左边的矩形 g.fill3DRect(x, y, 5, 30, false); //2、画出右边的矩形 g.fill3DRect(x+15, y, 5, 30, false); //3、画出中间矩形 g.fill3DRect(x+5, y+5, 10, 20, false); //4、画出中间圆形 g.fillOval(x+5, y+10, 10, 10); //5、画出线(炮筒) g.drawLine(x+10, y+15, x+10, y+29); break; //向左走的坦克 case 2: //1、画出上边的矩形 g.fill3DRect(x, y, 30, 5, false); //2、画出下边的矩形 g.fill3DRect(x, y+15, 30, 5, false); //3、画出中间矩形 g.fill3DRect(x+5, y+5, 20, 10, false); //4、画出中间圆形 g.fillOval(x+10, y+5, 10, 10); //5、画出线(炮筒) g.drawLine(x+15, y+10, x, y+10); break; //向右走的坦克 case 3: //1、画出左边的矩形 g.fill3DRect(x, y, 30, 5, false); //2、画出右边的矩形 g.fill3DRect(x, y+15, 30, 5, false); //3、画出中间矩形 g.fill3DRect(x+5, y+5, 20, 10, false); //4、画出中间圆形 g.fillOval(x+10, y+5, 10, 10); //5、画出线(炮筒) g.drawLine(x+15, y+10, x+29, y+10); break; } } public void keyPressed(KeyEvent e) {//按下键事件 if(e.getKeyCode()==KeyEvent.VK_DOWN){ hero.setX(x); hero.setY(y++); hero.setZ(z+1); }else if(e.getKeyCode()==KeyEvent.VK_UP){ hero.setX(x); hero.setY(y--); hero.setZ(z+0); }else if(e.getKeyCode()==KeyEvent.VK_LEFT){ hero.setX(x--); hero.setY(y); hero.setZ(z+2); }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){ hero.setX(x++); hero.setY(y); hero.setZ(z+3); } //调用repaint()函数,来重绘界面 this.repaint(); } public void keyReleased(KeyEvent e) {//弹起键事件 } public void keyTyped(KeyEvent e) {//按键输出值 } } //定义坦克类 class Tank{ //表示坦克的X横坐标Y纵坐标Z坦克方向 int x=0,y=0,z=0; public Tank(int x,int y,int z){ this.x=x; this.y=y; this.z=z; } 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 getZ() { return z; } public void setZ(int z) { this.z = z; } } //我的坦克 class Hero extends Tank{ public Hero(int x,int y,int z){ super(x,y,z); } }
x
 
1
/**
2
 * 功能:坦克游戏的1.01
3
 * 1、画出坦克
4
 * 2、坦克动起来
5
 */
6
import java.awt.*;
7
import java.awt.event.KeyEvent;
8
import java.awt.event.KeyListener;
9
 
             
10
import javax.swing.*;
11
public class MyTank02 extends JFrame{
12
    //定义组件
13
    MyPanel mp=null;
14
    public static void main(String[] args) {
15
        MyTank02 mt=new MyTank02();
16
    }
17
    //构造函数
18
    public MyTank02(){
19
        //构建组件
20
        mp=new MyPanel();
21
        
22
        //监听
23
        this.addKeyListener(mp);
24
        
25
        //加入组件
26
        this.add(mp);
27
        
28
        //设置JFrame窗体
29
        this.setTitle("坦克大战");//JFrame标题
30
        this.setSize(400, 300);//JFrame窗体大小
31
        this.setLocationRelativeTo(null);//在屏幕中心显示
32
        this.setVisible(true);//显示
33
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//退出并关闭JFrame
34
        
35
    }
36
}
37
 
             
38
//我的面板Panel
39
class MyPanel extends JPanel implements KeyListener{
40
    //定义一个我的坦克
41
    Hero hero=null;
42
    
43
    int x=10,y=10,z=0;//初始坦克在MyPanel的位置
44
    
45
    //构造函数
46
    public MyPanel(){
47
        hero=new Hero(x, y, z);//我的坦克初始位置
48
    }
49
    
50
    //重写paint函数
51
    public void paint(Graphics g){
52
        super.paint(g);//调用父类paint方法
53
        //设置Panel底色
54
        g.fillRect(0, 0, 400, 300);//fillRect(0,0,X?,Y?)中X?/Y?为活动区域
55
        //调用坦克
56
        this.drawTank(hero.getX(), hero.getY(), g, 1, hero.getZ());
57
    }
58
    
59
    //画出坦克的函数
60
    public void drawTank(int x,int y,Graphics g,int type,int direct){
61
        //判断是什么类型的坦克
62
        switch(type){
63
        case 0:
64
            g.setColor(Color.cyan);//我的坦克颜色
65
            break;
66
        case 1:
67
            g.setColor(Color.yellow);//敌人坦克颜色
68
            break;
69
        case 2:
70
            g.setColor(Color.red);
71
            break;
72
        }
73
        
74
        //判断坦克的方向
75
        switch(direct){
76
        //向上走的坦克
77
        case 0:
78
            //画出我的坦克(到时再封装成一个函数)
79
            //1、画出左边的矩形
80
            g.fill3DRect(x, y, 5, 30, false);
81
            //2、画出右边的矩形
82
            g.fill3DRect(x+15, y, 5, 30, false);
83
            //3、画出中间矩形
84
            g.fill3DRect(x+5, y+5, 10, 20, false);
85
            //4、画出中间圆形
86
            g.fillOval(x+5, y+10, 10, 10);
87
            //5、画出线(炮筒)
88
            g.drawLine(x+10, y+15, x+10, y);
89
            break;
90
        //向下走的坦克
91
        case 1:
92
            //1、画出左边的矩形
93
            g.fill3DRect(x, y, 5, 30, false);
94
            //2、画出右边的矩形
95
            g.fill3DRect(x+15, y, 5, 30, false);
96
            //3、画出中间矩形
97
            g.fill3DRect(x+5, y+5, 10, 20, false);
98
            //4、画出中间圆形
99
            g.fillOval(x+5, y+10, 10, 10);
100
            //5、画出线(炮筒)
101
            g.drawLine(x+10, y+15, x+10, y+29);
102
            break;
103
        //向左走的坦克
104
        case 2:
105
            //1、画出上边的矩形
106
            g.fill3DRect(x, y, 30, 5, false);
107
            //2、画出下边的矩形
108
            g.fill3DRect(x, y+15, 30, 5, false);
109
            //3、画出中间矩形
110
            g.fill3DRect(x+5, y+5, 20, 10, false);
111
            //4、画出中间圆形
112
            g.fillOval(x+10, y+5, 10, 10);
113
            //5、画出线(炮筒)
114
            g.drawLine(x+15, y+10, x, y+10);
115
            break;
116
        //向右走的坦克
117
        case 3:
118
            //1、画出左边的矩形
119
            g.fill3DRect(x, y, 30, 5, false);
120
            //2、画出右边的矩形
121
            g.fill3DRect(x, y+15, 30, 5, false);
122
            //3、画出中间矩形
123
            g.fill3DRect(x+5, y+5, 20, 10, false);
124
            //4、画出中间圆形
125
            g.fillOval(x+10, y+5, 10, 10);
126
            //5、画出线(炮筒)
127
            g.drawLine(x+15, y+10, x+29, y+10);
128
            break;
129
        }
130
    }
131
 
             
132
    public void keyPressed(KeyEvent e) {//按下键事件
133
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
134
            hero.setX(x);
135
            hero.setY(y++);
136
            hero.setZ(z+1);
137
        }else if(e.getKeyCode()==KeyEvent.VK_UP){
138
            hero.setX(x);
139
            hero.setY(y--);
140
            hero.setZ(z+0);
141
        }else if(e.getKeyCode()==KeyEvent.VK_LEFT){
142
            hero.setX(x--);
143
            hero.setY(y);
144
            hero.setZ(z+2);
145
        }else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
146
            hero.setX(x++);
147
            hero.setY(y);
148
            hero.setZ(z+3);
149
        }
150
        //调用repaint()函数,来重绘界面
151
        this.repaint();
152
        
153
    }
154
 
             
155
    public void keyReleased(KeyEvent e) {//弹起键事件
156
        
157
        
158
    }
159
    
160
    public void keyTyped(KeyEvent e) {//按键输出值
161
        
162
        
163
    }
164
    
165
}
166
 
             
167
//定义坦克类
168
class Tank{
169
    //表示坦克的X横坐标Y纵坐标Z坦克方向
170
    int x=0,y=0,z=0;
171
 
             
172
    public Tank(int x,int y,int z){
173
        this.x=x;
174
        this.y=y;
175
        this.z=z;
176
    }
177
 
             
178
    public int getX() {
179
        return x;
180
    }
181
 
             
182
    public void setX(int x) {
183
        this.x = x;
184
    }
185
 
             
186
    public int getY() {
187
        return y;
188
    }
189
 
             
190
    public void setY(int y) {
191
        this.y = y;
192
    }
193
 
             
194
    public int getZ() {
195
        return z;
196
    }
197
 
             
198
    public void setZ(int z) {
199
        this.z = z;
200
    }
201
}
202
 
             
203
//我的坦克
204
class Hero extends Tank{
205
    public Hero(int x,int y,int z){
206
        super(x,y,z);
207
    }
208
}
1167156-20181130180332746-818186438.png
 





















转载于:https://www.cnblogs.com/xuxaut-558/p/10045734.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值