java基础学习之坦克击中消失
2016-08-21 15:00
375 查看
/** * */ package com.test1; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.*; import javax.swing.*; /** * @author Administrator * */ public class MyTankGame1 extends JFrame{ /** * @param args */ MyPanel mp=null; public static void main(String[] args) { // TODO 自动生成的方法存根 MyTankGame1 mtg =new MyTankGame1(); } //构造函数 public MyTankGame1() { mp=new MyPanel(); //启动线程 Thread t=new Thread(mp); t.start(); this.add(mp); //注册监听 this.addKeyListener(mp); this.setSize(400,300); this.setVisible(true); } //我的面板 class MyPanel extends JPanel implements KeyListener,Runnable { //定义一个坦克 Hero hero=null; //定义敌人坦克 Vector<EnemyTank> ets=new Vector<EnemyTank>(); int enSize=3; //构造函数 public MyPanel() { hero=new Hero(10,10); //初始化敌人的坦克 for(int i=0;i<enSize;i++) { //创建一辆敌人坦克对象 EnemyTank et=new EnemyTank((i+1)*50,0); et.setColor(0); //加入 ets.add(et); ; } } //重写paint public void paint(Graphics g) { super.paint(g); //将区域设为黑色 g.fillRect(0, 0, 400, 300); this.drawTank(hero.getX(),hero.getY(),g,this.hero.direct,1); //从ss中取出每一颗子弹并绘制 for(int i=0;i<this.hero.ss.size();i++) { Shot myshot=hero.ss.get(i); //画出子弹 if(hero.s!=null) { g.draw3DRect(myshot.x, myshot.y, 2, 2,false); } if(myshot.isLive==false) { //删除子弹 hero.ss.remove(myshot); } } //画出敌人的坦克 for(int i=0;i<ets.size();i++) { EnemyTank et=ets.get(i); if(et.isLive) { this.drawTank(ets.get(i).getX(),ets.get(i).getY(),g,this.hero.direct,0); } } } //写一个函数判断子弹是否击中敌人坦克 public void hitTank(Shot s,EnemyTank et) { //判断坦克方向 switch(et.direct) { case 0: case 2: if(s.x>et.x&&s.x<et.x+20&&s.y>et.y&&s.y<et.y+30) { //击中 //子弹死亡 s.isLive=false; //敌人死亡 et.isLive=false; } case 1: case 3: if(s.x>et.x&&s.x<et.x+30&&s.y>et.y&&s.y<et.y+20) { //击中 //子弹死亡 s.isLive=false; //敌人死亡 et.isLive=false; } } } //画出坦克的函数 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+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; } } //键使用aswd控制上下左右 public void keyPressed(KeyEvent arg0) { // TODO 自动生成的方法存根 if(arg0.getKeyCode()==KeyEvent.VK_W) { //设置我的坦克的方向 this.hero.setDirect(0); this.hero.moveUp(); }else if(arg0.getKeyCode()==KeyEvent.VK_A){ this.hero.setDirect(3); this.hero.moveLeft(); }else if(arg0.getKeyCode()==KeyEvent.VK_S){ this.hero.setDirect(2); this.hero.moveDown(); }else if(arg0.getKeyCode()==KeyEvent.VK_D){ this.hero.setDirect(1); this.hero.moveRight(); } //判断玩家时候按下J键 if(arg0.getKeyCode()==KeyEvent.VK_J){ //开火 if(this.hero.ss.size()<5) { this.hero.shotEnemy(); } } //重新绘制Panel this.repaint(); } public void keyReleased(KeyEvent arg0) { // TODO 自动生成的方法存根 } public void keyTyped(KeyEvent arg0) { // TODO 自动生成的方法存根 } @Override public void run() { // TODO 自动生成的方法存根 //每个100ms重画 while(true) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //判断是否击中 for(int i=0;i<hero.ss.size();i++) { //取出子弹 Shot myShot=hero.ss.get(i); //判断子弹是否有效 if(myShot.isLive) { //取出每个敌人坦克 for(int j=0;j<ets.size();j++) { //取出坦克 EnemyTank et=ets.get(j); if(et.isLive) { this.hitTank(myShot,et); } } } } //重绘 this.repaint(); } } } } //子弹类 class Shot implements Runnable{ int x; int y; int direct; int speed=1; //子弹是否还活着 boolean isLive =true; public Shot(int x,int y,int direct) { this.x=x; this.y=y; this.direct=direct; } @Override public void run() { // TODO 自动生成的方法存根 while(true) { try { Thread.sleep(50); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } switch(direct) { case 0: //速度 y-=speed; break; case 1: x+=speed; break; case 2: y+=speed; break; case 3: x-=speed; break; } System.out.println("子弹坐标"+x+"y="+y); //子弹何时销毁,还未处理 //判断子弹是否碰到边缘 if(x<0||x>400||y<0||y>300) { this.isLive=false; break; } } } } //坦克类 class Tank { int x; int y; //坦克方向0上1右2下3左 int direct=0; int color=0; public int getColor() { return color; } public void setColor(int color) { this.color = color; } public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } //速度 int speed=1; 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; } }package com.test1;import java.util.Vector;//敌人的坦克class EnemyTank extends Tank{boolean isLive=true;public EnemyTank(int x,int y){super(x, y);}}//我的坦克class Hero extends Tank{//子弹//Shot s=null; //这为单个子弹,不能连发Vector<Shot> ss=new Vector<Shot>();Shot s=null;public Hero(int x,int y){super(x, y);}//开火public void shotEnemy(){switch(this.direct){case 0://创建一个子弹s=new Shot(x+10, y,0);//把子弹加入到向量ss.add(s);break;case 1:s=new Shot(x+30, y+10,1);ss.add(s);break;case 2:s=new Shot(x+10, y+30,2);ss.add(s);break;case 3:s=new Shot(x, y+10,3);ss.add(s);break;}//启动子弹线程Thread t=new Thread(s);t.start();}//坦克向上移动public void moveUp(){y-=speed;}//向右移动public void moveRight(){x+=speed;}public void moveLeft(){x-=speed;}public void moveDown(){y+=speed;}}$(".MathJax").remove();
相关文章推荐
- java基础学习之坦克游戏
- java基础学习之线程--坦克子弹的运动
- java基础学习之坦克游戏
- java基础学习之坦克游戏
- java基础学习之坦克
- java基础学习之面对对面的图形绘制 坦克
- JAVA学习心得--基础知识
- Java IO学习基础之读写文本文件
- Java学习备忘录(二)基础语法和数组(原创)
- Java IO学习基础之读写文本文件
- JSP学习记--01(JAVA配置,HTML基础)
- Java基础-学习Java的30个基本概念
- java基础学习---命令行编译执行java类
- JAVA学习笔记(基础知识)
- 安全的基础----学习java安全之前的准备
- java基础学习
- Java2核心技术第七版的学习笔记(三) Fundamental Programming Structures in Java(Java语言的基础)(二)
- 安全的基础----学习java安全之前的准备
- java基础学习:常用的ant的操作
- Java IO学习基础之读写文本文件