欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > DOCX文档下载  

    java写的坦克大战简易.docx

    • 资源ID:3159628       资源大小:43.13KB        全文页数:19页
    • 资源格式: DOCX        下载积分:6.99金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要6.99金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    java写的坦克大战简易.docx

    java写的坦克大战简易MyTankGame2.java package test; import javax.swing.*; import java.awt.*; import java.awt.event.*; /import java.io.*; import java.util.*; class MyTankGame2 extends JFrame implements ActionListener MyPanel mp=null; /定义一个开始的面板 MyStartPanel msp=null; /做出我需要的菜单 JMenuBar jmb=null; /开始游戏 JMenu jm1=null; JMenuItem jmi1=null; /退出系统 JMenuItem jmi2=null; /存盘退出 JMenuItem jmi3=null; JMenuItem jmi4=null; public static void main(String args) /System.out.println("Hello World!"); MyTankGame2 mtg=new MyTankGame2; /构造方法 public MyTankGame2 /创建菜单及菜单选项 jmb=new JMenuBar; jm1=new JMenu("游戏"); /设置助记符 jm1.setMnemonic('G'); jmi1=new JMenuItem("开始新游戏"); jmi2=new JMenuItem("退出游戏(E)"); jmi3=new JMenuItem("存盘退出游戏"); jmi4=new JMenuItem("继续上局游戏"); jmi4.addActionListener(this); jmi4.setActionCommand("continueGame"); jmi3.addActionListener(this); jmi3.setActionCommand("saveExit"); jmi2.setMnemonic('E'); /对jmi1进行响应 jmi1.addActionListener(this); jmi1.setActionCommand("newGame"); jmi2.addActionListener(this); jmi2.setActionCommand("exit"); jm1.add(jmi1); jm1.add(jmi2); jm1.add(jmi3); jm1.add(jmi4); jmb.add(jm1); msp=new MyStartPanel; Thread t=new Thread(msp); t.start; this.setJMenuBar(jmb); this.add(msp); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(600,450); this.setVisible(true); Override public void actionPerformed(ActionEvent e) /对用户不同的点击进行不同的处理 if(e.getActionCommand.equals("newGame") mp=new MyPanel("newGame"); Thread t=new Thread(mp); t.start; /先删除旧的面板 this.remove(msp); this.add(mp); /注册监听 this.addKeyListener(mp); /显示 this.setVisible(true); else if(e.getActionCommand.equals("exit") /保存击毁敌人的数量 Recorder.keepRecording; System.exit(0); /对存盘退出做处理 else if(e.getActionCommand.equals("saveExit") /工作 Recorder re=new Recorder; re.setEts(mp.ets); /保存击毁敌人的数量和敌人的坐标 re.keepRecAndEnemyTank; /退出 System.exit(0); else if(e.getActionCommand.equals("continueGame") / mp=new MyPanel("con"); /mp.flag=; Thread t=new Thread(mp); t.start; /先删除旧的面板 this.remove(msp); this.add(mp); /注册监听 this.addKeyListener(mp); /显示 this.setVisible(true); class MyStartPanel extends JPanel implements Runnable int times=0; public void paint(Graphics g) super.paint(g); g.fillRect(0, 0, 400, 300); /提示信息 if(times%2=0) g.setColor(Color.yellow); /开关信息的字体 Font myFont=new Font("华文新魏",Font.BOLD,30); g.setFont(myFont); g.drawString("stage:1", 150, 140); Override public void run / TODO Auto-generated method stub while(true) /休眠 try Thread.sleep(100); catch (Exception e) / TODO: handle exception times+; /重画 this.repaint; /我的面板 class MyPanel extends JPanel implements KeyListener,Runnable /定义我的坦克 Hero hero=null; /判断继续上局还是新游戏 /定义敌人的坦克组 Vector<EnemyTank> ets=new Vector<EnemyTank> Vector<Node> nodes=new Vector<Node> /定义炸弹集合 Vector<Bomb> bombs=new Vector<Bomb> int enSize=5; /定义三张图片 Image image1=null; Image image2=null; Image image3=null; /构造方法 public MyPanel(String flag) /恢复记录 Recorder.getRecording; hero=new Hero(30,231); if(flag.equals("newGame") /初始化敌人的坦克 for(int i=0;i<enSize;i+) /创建一辆敌人的坦克对象 EnemyTank et=new EnemyTank(i+1)*50,0); et.setColor(0); et.setDirect(2); /将MyPanel的敌人坦克向量交给该敌人坦克 et.setEts(ets); /启动敌人的坦克 Thread t=new Thread(et); t.start; /给敌人的坦克添加一颗子弹 Shot s=new Shot(et.x+10, et.y+30, 2,1); et.ss.add(s); new Thread(s).start; ets.add(et);/加入 else nodes=new Recorder.getNodesAndEnNums; /初始化敌人的坦克 for(int i=0;i<nodes.size;i+) Node node=nodes.get(i); /创建一辆敌人的坦克对象 EnemyTank et=new EnemyTank(node.x,node.y); et.setColor(0); et.setDirect(node.direct); /将MyPanel的敌人坦克向量交给该敌人坦克 et.setEts(ets); /启动敌人的坦克 Thread t=new Thread(et); t.start; /给敌人的坦克添加一颗子弹 Shot s=new Shot(et.x+10, et.y+30, 2,1); et.ss.add(s); new Thread(s).start; ets.add(et);/加入 /*try image1=ImageIO.read(new File("/bomb_1.gif"); image2=ImageIO.read(new File("/bomb_2.gif"); image3=ImageIO.read(new File("/bomb_3.gif"); catch (Exception e) e.printStackTrace; / TODO: handle exception */ /播放开战声音 AePlayWave apw=new AePlayWave("F:myjavaday9坦克大战TankGamae13src111.wav"); apw.start; /初始化图片 image1=Toolkit.getDefaultToolkit.getImage(Panel.class.getResource("/bomb_1.gif"); image2=Toolkit.getDefaultToolkit.getImage(Panel.class.getResource("/bomb_2.gif"); image3=Toolkit.getDefaultToolkit.getImage(Panel.class.getResource("/bomb_3.gif"); /画出提示信息 public void showInfo(Graphics g) /画出提示信息坦克 this.drawTank(100, 320, g, 0, 0); g.setColor(Color.black); g.drawString(Recorder.getEnNum+"", 128, 343); this.drawTank(165, 320, g, 0, 1); g.setColor(Color.black); g.drawString(Recorder.getMyLife+"", 195, 343); /画出玩家的总成绩 g.setColor(Color.black); Font f=new Font("宋体",Font.BOLD,20); g.setFont(f); g.drawString("您的总成绩", 410, 25); this.drawTank(420, 60, g, 0, 0); g.setColor(Color.black); g.drawString(Recorder.getAllEnNum+"", 455, 80); /重写paint函数 public void paint(Graphics g) super.paint(g); g.fillRect(0,0,400,300); /画出提示信息 this.showInfo(g); /画出自己的坦克 if(hero.isLive) this.drawTank(hero.getX,hero.getY,g,hero.getDirect,1); /从ss中取出每一颗子弹并绘制 for(int i=0;i<hero.ss.size;i+) Shot myShot =hero.ss.get(i); /画出子弹 if(myShot!=null&&myShot.isLive=true) g.draw3DRect(myShot.x, myShot.y, 1, 1, false); if(myShot.isLive=false) /从ss中删除掉该子弹 hero.ss.remove(myShot); /画出炸弹 for(int i=0;i<bombs.size;i+) Bomb b=bombs.get(i); if(b.life>6) g.drawImage(image1, b.x, b.y, 30, 30,this); else if(b.life>3) g.drawImage(image2, b.x, b.y, 30, 30,this); else g.drawImage(image3, b.x, b.y, 30, 30,this); /让b的生命值减小 b.lifeDown; if(b.life=0) /把炸弹从向量中去掉 bombs.remove(b); /画出敌人的坦克 for(int i=0;i<ets.size;i+) EnemyTank et=ets.get(i); if(et.isLive) this.drawTank(et.getX,et.getY, g, et.getDirect, 0); /顺便画出敌人的子弹 for(int j=0;j<et.ss.size;j+) /取出子弹 Shot enemyShot=et.ss.get(j); if(enemyShot.isLive) g.draw3DRect(enemyShot.x, enemyShot.y, 1, 1, false); else /如果死亡了,就从Vextor中删除掉 et.ss.remove(enemyShot); /判断我的子弹是否会击中敌人的坦克 public void hitEnemyTank /判断是否会击中 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) if(this.hitTank(myShot, et) /减少敌人数量 Recorder.reduceEnNum; /增加我的记录 Recorder.addEnEnum; /我是否被击中 public void hitMe /取出每一个敌人的坦克 for(int i=0;i<this.ets.size;i+) /取出坦克 EnemyTank et=ets.get(i); /取出每颗子弹 for(int j=0;j<et.ss.size;j+) /取出子弹 Shot enemyShot=et.ss.get(j); if(hero.isLive) if(this.hitTank(enemyShot, hero) /待写 /画出一个专门判断子弹是否击中敌人坦克的函数 public boolean hitTank(Shot s ,Tank et) boolean b2=false; /判断该坦克的方向 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; b2=true; /System.out.println("hytfyfrujyg"); /创建一颗炸弹 放入Vector Bomb b=new Bomb(et.x, et.y); bombs.add(b); break; 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; b2=true; Bomb b=new Bomb(et.x, et.y); bombs.add(b); break; return b2; /画出坦克的函数 public void drawTank(int x,int y,Graphics g,int direction,int type) /判断类型 switch(type) case 0: g.setColor(Color.CYAN);break; case 1:g.setColor(Color.yellow);break; /判断方向 switch(direction) /向上走 case 0: /g.setColor(Color.CYAN); /画出我的坦克 /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:/向右 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); /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+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; Override public void keyTyped(KeyEvent e) / TODO Auto-generated method stub Override /w上,s下,a左,右边 public void keyPressed(KeyEvent e) / TODO Auto-generated method stub if(e.getKeyCode=KeyEvent.VK_W) /设置我的坦克的方向上 this.hero.setDirect(0); if(this.hero.y>0) this.hero.moveUp; else if(e.getKeyCode=KeyEvent.VK_D) /设置我的坦克的方向 右边 this.hero.setDirect(1); if(this.hero.x<370) this.hero.moveRight; else if(e.getKeyCode=KeyEvent.VK_S) /设置我的坦克的方向 xia边 this.hero.setDirect(2); if(this.hero.y<=271) this.hero.moveDown; else if(e.getKeyCode=KeyEvent.VK_A) /设置我的坦克的方向 左边 this.hero.setDirect(3); if(this.hero.x>0) this.hero.moveLeft; if(e.getKeyCode=KeyEvent.VK_K) /玩家按了一个k键,开火 if(this.hero.ss.size<=5) this.hero.shotEnemy; /必须用repaintPanel this.repaint; Override public void keyReleased(KeyEvent e) / TODO Auto-generated method stub Override public void run / TODO Auto-generated method stub /每隔100毫秒去重画 while(true) try Thread.sleep(100); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace; this.hitEnemyTank; this.hitMe; /重绘 this.repaint; Members.java package test; import java.io.*; import java.util.Vector; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; class AePlayWave extends Thread private String filename; public AePlayWave(String wavfile) filename = wavfile; public void run File soundFile = new File(filename); AudioInputStream audioInputStream = null; try audioInputStream = AudioSystem.getAudioInputStream(soundFile); catch (Exception e1) e1.printStackTrace; return; AudioFormat format = audioInputStream.getFormat; SourceDataLine auline = null; DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); try auline = (SourceDataLine) AudioSystem.getLine(info); auline.open(format); catch (Exception e) e.printStackTrace; return; auline.start; int nBytesRead = 0; byte abData = new byte512; try while (nBytesRead != -1) nBytesRead = audioInputStream.read(abData, 0, abData.length); if (nBytesRead >= 0) auline.write(abData, 0, nBytesRead); catch (IOException e) e.printStackTrace; return; finally auline.drain; auline.close; class Node/恢复点 int x,y,direct; public Node(int x,int y,int direct) this.x=x; this.y=y; this.direct=direct; /记录类,同时也可以保存一些设置 class Recorder /记录每关有多少敌人 private static int enNum=5; /设置我有多少可用的人 private static int myLife=3; /记录总共消灭了多少敌人 private static int allEnNum=0; /从文件中恢复记录点 static Vector<Node> nodes=new Vector<Node> private static FileWriter fw=null; private static BufferedWriter bw=null; private static FileReader fr=null; private static BufferedReader br=null; private Vector <EnemyTank> ets=new Vector<EnemyTank> public Vector<EnemyTank> getEts return ets; public void setEts(Vector<EnemyTank> ets) this.ets = ets; /完成读取任务 public Vector<Node> getNodesAndEnNums try fr=new FileReader("F:myRecording.txt"); br=new BufferedReader(fr); String n="" /先读取第一行 n=br.readLine; allEnNum=Integer.parseInt(n); while(n=br.readLine)!=null) String xyz=n.split(" "); Node node=new Node(Integer.parseInt(xyz0), Integer.parseInt(xyz1), Integer.parseInt(xyz2); nodes.add(node); catch (Exception e) e.printStackTrace; / TODO: handle exception finally try br.close; fr.close; catch (Exception e2) / TODO: handle exception return nodes; /保存击毁敌人坦克的数量和敌人坦克的坐标,方向 public void keepRecAndEnemyTank try /创建 fw=new FileWriter("F:myRecording.txt"); bw=new BufferedWriter(fw); bw.write(allEnNum+"rn"); /保存当前还活着的坐标和方向 for(int i=0;i<ets.size;i+) /取出第一个坦克 EnemyTank et=ets.get(i); if(et.isLive) String record=et.x+" "+et.y+" "+et.direct; /写入到文件 bw.write(record+"rn"); catch (Exception e) / TODO: handle exception finally /关闭流 try bw.close; fw.close; catch (Exception e2) / TODO: handle exception /从文件中读取记录 public static void getRecording try fr=new FileReader("F:myRecording.txt"); br=new BufferedReader(fr); String n=br.readLine; allEnNum=Integer.parseInt(n); catch (Exception e) e.printStackTrace; / TODO: handle exception finally try br.close; fr.close; catch (Exception e2) / TODO: handle exception /把玩家击毁敌人的坦克数量保存到文件中 public static void keepRecording try /创建 fw=new FileWriter("F:myRecording.txt"); bw=new BufferedWriter(fw); bw.write(allEnNum+"rn"); catch (Exception e) / TODO: handle exception finally /关闭流 try bw.close; fw.close; catch (Exception e2) / TODO: handle exception public static int getAllEnNum return allEnNum; public static void setAllEnNum(int allEnNum) Recor

    注意事项

    本文(java写的坦克大战简易.docx)为本站会员(小飞机)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开