JAVA实现坦克大战1.0版本
2016-06-14 19:59
309 查看
package tankGame; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; /** * 功能:我方坦克可以发子弹 * * */ public class MyTankGame1 extends JFrame { public static void main(String[] args) { // TODO Auto-generated method stub MyTankGame1 mtg =new MyTankGame1(); } //构造函数 public MyTankGame1(){ MyPanel mp=new MyPanel(); //线程启动 Thread t=new Thread(mp); t.start(); this.add(mp); this.addKeyListener(mp);//注册监听 this.setSize(400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } //我的面板 class MyPanel extends JPanel implements KeyListener,Runnable{ Hero hero=null;//定义我的坦克 Vector<Enermy> enermy=new Vector<Enermy>() ;//定义敌人的坦克组 int enSize=3; //构造函数 public MyPanel(){ hero=new Hero(100,100); for(int i=0;i<enSize;i++){ //创建一辆敌人的坦克对象 Enermy en=new Enermy((i+1)*50, 0); en.setColor(0); en.setDerect(3); //加入 enermy.add(en); } } public void paint(Graphics g){//重新paint super.paint(g); g.fillRect(0, 0, 400, 300); //画出我的坦克 this.drawTank(hero.getX(), hero.getY(), g,this.hero.derect, 1); //画出子弹 if(hero.s!=null&&hero.s.isLive==true){ g.fillOval(hero.s.x ,hero.s.y, 2, 2); } //画出敌人的坦克 for(int i=0;i<enermy.size();i++){ this.drawTank(enermy.get(i).getX(), enermy.get(i).getY(), g, enermy.get(i).derect, 0); //System.out.println(enermy.get(i).derect);//默认值为0 } } 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-5, x+10, y+15); 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+35, 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+35); 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-5, y+10,x+15, y+10); break; } } @Override public void keyPressed(KeyEvent e) { // TODO Auto-generated method stub if(e.getKeyCode()==KeyEvent.VK_W){ //设置我的坦克方向 this.hero.setDerect(0); this.hero.moveUp(); }else if (e.getKeyCode()==KeyEvent.VK_D){ this.hero.setDerect(1); this.hero.moveRight(); }else if (e.getKeyCode()==KeyEvent.VK_S){ this.hero.setDerect(2); this.hero.moveDown(); }else if (e.getKeyCode()==KeyEvent.VK_A){ this.hero.setDerect(3); this.hero.moveLeft(); } this.repaint(); //判断玩家是否按下j if(e.getKeyCode()==KeyEvent.VK_J){ //开火,调用shotEnermy this.hero.shotEnermy(); } } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } @Override public void run() {//实现每隔100ms去重绘 // TODO Auto-generated method stub while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.repaint(); } } }
成员类
package tankGame; //子弹类 class Shot implements Runnable{ int x,y,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 Auto-generated method stub while(true){ try { Thread.sleep(50); } catch (InterruptedException e) { // TODO Auto-generated catch block 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"+x+"Y"+y); if (x<0||x>400||y<0||y>300){ this.isLive=false; break; } } } } //坦克类 class Tank { int x=0;//坦克的横坐标 int y=0;//坦克的纵坐标 int derect;//0上 1右 2下 3左 int speed=1;//设置坦克的速度 int color;//设置坦克颜色 public int getColor() { return color; } public void setColor(int color) { this.color = color; } public int getDerect() { return derect; } public void setDerect(int derect) { this.derect = derect; } 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{ Shot s=null;//子弹是和坦克关联的 public Hero (int x,int y){ super(x,y); } public void shotEnermy(){//子弹和坦克方向相关 switch(this.derect){ case 0: s=new Shot(x+10,y-9,0); break; case 1: s=new Shot(x+38,y+9,1); break; case 2: s=new Shot(x+10,y+37,2); break; case 3: s=new Shot(x-9,y+9,3); break; } Thread t=new Thread(s); t.start(); } //坦克向上移动 public void moveUp(){ y-=speed; } //坦克向右移动 public void moveRight(){ x+=speed; }//坦克向下移动 public void moveDown(){ y+=speed; } //坦克向左移动 public void moveLeft(){ x-=speed; } } class Enermy extends Tank{ public Enermy (int x,int y){ super(x,y); } }
效果如下:
相关文章推荐
- maven+spring+junit测试程序时时出现NoSuchMethodError错误
- Java 正则表达式的详细介绍以及实例演示
- Java输入流的学习例子
- Java设计模式-Proxy代理模式
- 用16G内存在Java
- 离线安装eclipse的svn插件
- Java快速读取大文件
- jdk无法运行javac程序
- java中异常处理finally和return语句的执行顺序
- java开发相关资料
- eclipse git冲突解决
- java selenium webdriver第四讲 应用小结
- Spring 依赖注入
- java序列化和反序列化
- eclipse打断点调试进入到class文件中--解决方法
- Java编程思想学习笔记2 - 字节流和字符流
- 如何利用Guava实现方法调用超时自动中断
- java反射机制
- java正则表达式语法详解及其使用代码实例
- springmvc的系统学习之配置方式