您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > 数据库 > 韩顺平java坦克大战1.0版本_源代码
/**画坦克1.0*/importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;importjava.awt.event.MouseMotionListener;importjava.awt.event.WindowEvent;importjava.awt.event.WindowListener;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassMyTankGameextendsJFrame{MyPanelmp=null;publicstaticvoidmain(String[]args){MyTankGamemtk=newMyTankGame();}publicMyTankGame(){mp=newMyPanel();this.add(mp);//把面板加入窗体//注册监听this.addMouseListener(mp);this.addKeyListener(mp);this.addMouseMotionListener(mp);this.addWindowListener(mp);this.setTitle(坦克大战);//窗体标题this.setSize(600,400);//大小,宽,高(像素)this.setLocation(300,300);//显示位置。左边距,上边距//禁止用户改变窗口大小this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);//显示}}//我的面板classMyPanelextendsJPanelimplementsWindowListener,MouseListener,MouseMotionListener,KeyListener{//定义一个我的坦克Herohero=null;Direndiren=null;publicMyPanel(){hero=newHero(300,200);diren=newDiren(100,0);//diren.start();}publicvoidpaint(Graphicsg){super.paint(g);//画坦克(到时候封装到一个函数)//画左边矩形//g.setColor(Color.black);g.fillRect(0,0,600,400);this.drawTank(hero.getX(),hero.getY(),g,hero.dstct,1);this.drawTank(diren.getX(),diren.getY(),g,1,0);this.drawTank(diren.getX()+50,diren.getY(),g,1,0);this.drawTank(diren.getX()+100,diren.getY(),g,1,0);}//画坦克的函数publicvoiddrawTank(intx,inty,Graphicsg,intdirect,inttype){switch(type){//0我的坦克case0:g.setColor(Color.blue);break;//1敌人坦克case1:g.setColor(Color.GREEN);break;}//判断方向switch(direct){//0向上case0:g.fill3DRect(x,y,5,30,false);//左g.fill3DRect(x+15,y,5,30,false);//右g.fill3DRect(x+5,y+5,10,20,true);//中g.setColor(Color.RED);g.drawLine(x+10,y+15,x+10,y);//线g.setColor(Color.YELLOW);g.fillOval(x+5,y+10,10,10);//圆break;case1://下g.fill3DRect(x,y,5,30,false);g.fill3DRect(x+15,y,5,30,false);g.fill3DRect(x+5,y+5,10,20,true);g.setColor(Color.RED);g.drawLine(x+10,y+15,x+10,y+30);g.setColor(Color.YELLOW);g.fillOval(x+5,y+10,10,10);break;case2://左g.fill3DRect(x,y,30,5,false);g.fill3DRect(x,y+15,30,5,false);g.fill3DRect(x+5,y+5,20,10,true);g.setColor(Color.RED);g.drawLine(x+15,y+10,x,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10,y+5,10,10);break;case3://右g.fill3DRect(x,y,30,5,false);g.fill3DRect(x,y+15,30,5,false);g.fill3DRect(x+5,y+5,20,10,true);g.setColor(Color.RED);g.drawLine(x+15,y+10,x+30,y+10);g.setColor(Color.YELLOW);g.fillOval(x+10,y+5,10,10);break;}}publicvoidkeyPressed(KeyEvente){//键按下监听System.out.println(按下:+e.getKeyChar());if(e.getKeyCode()==KeyEvent.VK_DOWN){hero.setY(hero.getY()+hero.getSpeed());hero.dstct=1;}elseif(e.getKeyCode()==KeyEvent.VK_UP){hero.setY(hero.getY()-hero.getSpeed());hero.dstct=0;}elseif(e.getKeyCode()==KeyEvent.VK_LEFT){hero.setX(hero.getX()-hero.getSpeed());hero.dstct=2;}elseif(e.getKeyCode()==KeyEvent.VK_RIGHT){hero.setX(hero.getX()+hero.getSpeed());hero.dstct=3;}this.repaint();}publicvoidkeyReleased(KeyEvente){//键松开监听}publicvoidkeyTyped(KeyEvente){//键的一个值被输出}publicvoidmouseClicked(MouseEvente){//点击System.out.println(鼠标被点击了x=+e.getX()+y=+e.getY());}publicvoidmouseEntered(MouseEvente){//TODOAuto-generatedmethodstub//进入System.out.println(鼠标进入x=+e.getX()+y=+e.getY());}publicvoidmouseExited(MouseEvente){//TODOAuto-generatedmethodstub//离开System.out.println(鼠标离开x=+e.getX()+y=+e.getY());}publicvoidmousePressed(MouseEvente){//TODOAuto-generatedmethodstub//压下System.out.println(鼠标被按下x=+e.getX()+y=+e.getY());}publicvoidmouseReleased(MouseEvente){//TODOAuto-generatedmethodstubSystem.out.println(鼠标被松开x=+e.getX()+y=+e.getY());//松开}publicvoidmouseDragged(MouseEvente){//拖拽System.out.println(鼠标被拖拽x=+e.getX()+y=+e.getY());}publicvoidmouseMoved(MouseEvente){//移动System.out.println(鼠标被移动x=+e.getX()+y=+e.getY());}publicvoidwindowActivated(WindowEvente){//激活TODOAuto-generatedmethodstub}publicvoidwindowClosed(WindowEvente){//窗口关闭}publicvoidwindowClosing(WindowEvente){//正在关闭TODOAuto-generatedmethodstub}publicvoidwindowDeactivated(WindowEvente){//TODOAuto-generatedmethodstub}publicvoidwindowDeiconified(WindowEvente){//TODOAuto-generatedmethodstub}publicvoidwindowIconified(WindowEvente){//TODOAuto-generatedmethodstub}publicvoidwindowOpened(WindowEvente){//打开TODOAuto-generatedmethodstub}}//坦克父类classTank{//坦克横坐标intx=0;//坦克纵坐标inty=0;intspeed=5;//速度staticintdstct=0;publicTank(intx,inty){this.x=x;this.y=y;}publicintgetX(){returnx;}publicvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}publicstaticintgetDstct(){returndstct;}publicstaticvoidsetDstct(intdstct){Tank.dstct=dstct;}publicintgetSpeed(){returnspeed;}publicvoidsetSpeed(intspeed){this.speed=speed;}}//我的坦克classHeroextendsTank{publicHero(intx,inty){super(x,y);}//publicinttankup//{//y=y-getSpeed();//}//publicinttankdown//{//y=y+speed;//}//publicinttankleft//{//x=x-speed;//}//publicinttankright//{//x=x+speed;//}//}//敌人坦克classDirenextendsTankimplementsRunnable{publicDiren(intx,inty){super(x,y);}publicvoidrun(){while(true){//休眠一秒//1000秒=1秒.进入堵塞。BLOCKED.try{Thread.sleep(1000);
本文标题:韩顺平java坦克大战1.0版本_源代码
链接地址:https://www.777doc.com/doc-4877233 .html