韩顺平 java 第37讲 动起来的坦克
来源:互联网 发布:软件咨询服务公司 编辑:程序博客网 时间:2024/05/22 17:07
让坦克移动起来
注意,创建敌人坦克的时候,需要注意用什么存储??
数组VS集合?
首先,敌人的坦克会爆炸,用数组的时候不好控制敌人的坦克数量什么的,所以用集合。但是集合中要用ArrayList还是Vector?
我们知道后者是线程安全的,坦克后期肯定是多线程的,所以用Vector。
Draw.java
package com.chen;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import javax.swing.*;public class Draw extends JFrame{ MyPanel mp; public static void main(String[] args) { Draw d = new Draw(); } public Draw(){ mp = new MyPanel(); this.add(mp); this.addKeyListener(mp); this.setSize(800,600); this.setLocation(200, 100); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); }}class MyPanel extends JPanel implements KeyListener{ Hero hero; Vector<EnemyTank> ets = new Vector<EnemyTank>(); int ensize = 3; public MyPanel(){ hero = new Hero(100, 200); for(int i = 0;i < 3; ++i){ EnemyTank et = new EnemyTank(50 + i * 50, 0); et.setColor(1); et.setDirect(1); ets.add(et); } } public void paint(Graphics g){ super.paint(g); g.fillRect(0, 0, 650, 600); //画坦克 this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 0); for(int i = 0;i < ets.size(); ++i){ this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirect(), 1); } } 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; } //判断方向 0:上 1:下 2:左 3:右 //坦克规格: //两个轮子:30*5 //中间车厢:10*20 //中间圆形:10*10 //炮筒:15 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; case 1: 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 2: 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, y + 10, x + 15, y + 10); 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 + 30, y + 10); break; } } //asdw 控制方向 public void keyPressed(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_A){ hero.moveLeft(); }else if(e.getKeyCode() == KeyEvent.VK_S){ hero.moveDown(); }else if(e.getKeyCode() == KeyEvent.VK_D){ hero.moveRight(); }else if(e.getKeyCode() == KeyEvent.VK_W){ hero.moveUp(); } this.repaint(); } public void keyTyped(KeyEvent e) { } public void keyReleased(KeyEvent e) { }}
Members.java
package com.chen;class Tank{ int x = 0,y = 0;//坦克的坐标 int direct = 0;//0:上 1:下 2:左 3:右 int speed = 5; int color; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } //0:上 1:下 2:左 3:右 public void moveUp(){ this.setY(this.getY() - speed); this.setDirect(0); } public void moveDown(){ this.setY(this.getY() + speed); this.setDirect(1); } public void moveLeft(){ this.setX(this.getX() - speed); this.setDirect(2); } public void moveRight(){ this.setX(this.getX() + speed); this.setDirect(3); } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } 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 Tank(int x,int y){ this.x = x; this.y = y; }}class Hero extends Tank{ public Hero(int x, int y) { super(x, y); }}class EnemyTank extends Tank{ public EnemyTank(int x, int y) { super(x, y); // TODO Auto-generated constructor stub }}
0 0
- 韩顺平 java 第37讲 动起来的坦克
- 韩顺平 java 第25讲 异常
- 韩顺平 java 第38讲 线程
- 韩顺平 java 第39讲 线程
- 韩顺平 java 第41讲
- 韩顺平 java 第25讲、第26讲 泛型
- Unity3d--让我们的小坦克动起来
- 韩顺平 java 第21讲、第22讲、第23讲、第24讲 集合
- 韩顺平java 第14讲第15讲 作业评讲
- 韩顺平 java 第28讲 面试题
- 韩顺平java 第29讲 swing介绍
- 韩顺平java 第30讲 布局管理器
- 韩顺平 java 第31讲 常用组件
- 韩顺平java 第32讲 swing编程实战
- 韩顺平 java 第33讲 绘图技术
- 韩顺平 java 第35讲 事件处理机制
- 韩顺平 java 第36讲 事件监听
- 韩顺平 java 第40讲 线程同步
- iOSappid生成
- 第七周 项目1-建立顺序环形队列算法库
- js数组的操作
- 第七周项目五~~排队看病模拟
- 杭电acm 1060
- 韩顺平 java 第37讲 动起来的坦克
- 第7周项目2 - 建立链队算法库
- 第三周 项目3 求集合并集
- 数据结构实践—— 负数把正数赶出队列
- 报错:You Must Rebuild It With Bitcode Enabled (Xcode Setting ENABLE_BITCODE)(解决方案)
- 第七周---队列数组
- C++静态库与动态库
- android如何使用真机调试应用程序
- Ubuntu安装VIM