java写的坦克大战简易.docx

上传人:小飞机 文档编号:3159628 上传时间:2023-03-11 格式:DOCX 页数:19 大小:43.13KB
返回 下载 相关 举报
java写的坦克大战简易.docx_第1页
第1页 / 共19页
java写的坦克大战简易.docx_第2页
第2页 / 共19页
java写的坦克大战简易.docx_第3页
第3页 / 共19页
java写的坦克大战简易.docx_第4页
第4页 / 共19页
java写的坦克大战简易.docx_第5页
第5页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《java写的坦克大战简易.docx》由会员分享,可在线阅读,更多相关《java写的坦克大战简易.docx(19页珍藏版)》请在三一办公上搜索。

1、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 j

2、m1=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.setMne

3、monic(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.addActionLis

4、tener(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.setDefaultCloseOperati

5、on(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.addKeyList

6、ener(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(

7、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

8、 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

9、(Exception e) / TODO: handle exception times+; /重画 this.repaint; /我的面板 class MyPanel extends JPanel implements KeyListener,Runnable /定义我的坦克 Hero hero=null; /判断继续上局还是新游戏 /定义敌人的坦克组 Vector ets=new Vector; Vector nodes=new Vector; /定义炸弹集合 Vector bombs=new Vector; int enSize=5; /定义三张图片 Image image1=null;

10、 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;ienSize;i+) /创建一辆敌人的坦克对象 EnemyTank et=new EnemyTank(i+1)*50,0); et.setColor(0); et.setDirect(2); /将MyPanel的敌人坦克向量交给该敌人坦克 et.setEt

11、s(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;inodes.size;i+) Node node=nodes.get(i); /创建一辆敌人的坦克对象 EnemyTank et=new EnemyTank

12、(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); ima

13、ge2=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.

14、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.dr

15、awString(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

16、(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;ihero.ss.size;i+) Shot

17、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;i6) g.drawImage(image1, b.x, b.y, 30, 30,this); else if(b.life3) g.drawImage(image2, b.x, b.y, 30, 30,this);

18、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;iets.size;i+) EnemyTank et=ets.get(i); if(et.isLive) this.drawTank(et.getX,et.getY, g, et.getDirect, 0); /顺便画出敌人的子弹 for(int j=0;jet.ss.size;j+) /取出子弹 Shot enemyShot=et

19、.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;ihero.ss.size;i+) /取出子弹 Shot myShot=hero.ss.get(i); /判断子弹是否有效 if(myShot.isLive) /取出每一个敌人坦克与之匹配 for(i

20、nt j=0;jets.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;ithis.ets.size;i+) /取出坦克 EnemyTank et=ets.get(i); /取出每颗子弹 for(int j=0;jet.x&s.xet.y&s.yet.x&s.xet.

21、y&s.y0) this.hero.moveUp; else if(e.getKeyCode=KeyEvent.VK_D) /设置我的坦克的方向 右边 this.hero.setDirect(1); if(this.hero.x370) this.hero.moveRight; else if(e.getKeyCode=KeyEvent.VK_S) /设置我的坦克的方向 xia边 this.hero.setDirect(2); if(this.hero.y0) this.hero.moveLeft; if(e.getKeyCode=KeyEvent.VK_K) /玩家按了一个k键,开火 if(

22、this.hero.ss.size= 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 /记录每关有多少敌人 privat

23、e static int enNum=5; /设置我有多少可用的人 private static int myLife=3; /记录总共消灭了多少敌人 private static int allEnNum=0; /从文件中恢复记录点 static Vector nodes=new Vector; private static FileWriter fw=null; private static BufferedWriter bw=null; private static FileReader fr=null; private static BufferedReader br=null; pr

24、ivate Vector ets=new Vector; public Vector getEts return ets; public void setEts(Vector ets) this.ets = ets; /完成读取任务 public Vector getNodesAndEnNums try fr=new FileReader(F:myRecording.txt); br=new BufferedReader(fr); String n=; /先读取第一行 n=br.readLine; allEnNum=Integer.parseInt(n); while(n=br.readLin

25、e)!=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; /保存

26、击毁敌人坦克的数量和敌人坦克的坐标,方向 public void keepRecAndEnemyTank try /创建 fw=new FileWriter(F:myRecording.txt); bw=new BufferedWriter(fw); bw.write(allEnNum+rn); /保存当前还活着的坐标和方向 for(int i=0;iets.size;i+) /取出第一个坦克 EnemyTank et=ets.get(i); if(et.isLive) String record=et.x+ +et.y+ +et.direct; /写入到文件 bw.write(record+

27、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); catc

28、h (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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号