

下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、有些图片路径会出错 要注意package com.tankgame;import java.util.Vector;/坦克类class Tank int x=0; int y=0; int color=0; int speed=1; int direct=0; boolean isLive=true; public Tank(int x,int y) this.x=x; this.y=y; public int getX() return x;public void setX(int x) this.x = x;public int getY() return y;public void se
2、tY(int y) this.y = y;public int getDirect() return direct;public void setDirect(int direct) this.direct = direct;public int getColor() return color;public void setColor(int color) this.color = color;/我的坦克class Hero extends TankShot shot=null;Vector shotm=new Vector();public Hero(int x,int y)super(x,
3、y);this.color=5;/坦克具有一个打击敌人的方法public void shotenemy(int x,int y,int direct)switch(direct)case 0:shot=new Shot(this.x+10,this.y,0);shotm.add(shot);break;case 1:shot=new Shot(this.x+30,this.y+10,1);shotm.add(shot);break;case 2:shot=new Shot(this.x+10,this.y+30,2);shotm.add(shot);break;case 3:shot=new
4、Shot(this.x,this.y+10,3);shotm.add(shot);break;Thread th=new Thread(shot);th.start();/调整速度public void moveup()y-=speed;public void moveright()x+=speed;public void movedown()y+=speed;public void moveleft()x-=speed;/敌人的坦克class EnemyTank extends Tank implements RunnableVectorensh=new Vector();Vectorets
5、=new Vector();public EnemyTank(int x, int y)super(x, y);this.setColor(2);this.setDirect(2);/获取MPanel上的敌人坦克public void setets(Vector vv)this.ets=vv;/判断敌人的坦克是否碰撞public boolean isTouch()boolean b=false;EnemyTank et=null;switch(direct)case 0:for(int i=0;i=et.x&this.x=et.x+20&this.yet.y)return true;if(th
6、is.x+20=et.x&this.x+20=et.x+20&this.yet.y)return true;if(et.direct=1|et.direct=3)if(this.x=et.x&this.x=et.x+30&this.yet.y)return true;if(this.x+20=et.x&this.x+20=et.x+30&this.yet.y)return true;break;case 1:for(int i=0;i=et.x&this.x+30=et.x+20&this.yet.y)return true;if(this.x+30=et.x&this.x+30=et.x+2
7、0&this.y+20et.y)return true;if(et.direct=1|et.direct=3)if(this.x+30=et.x&this.x+30=et.x+30&this.yet.y)return true;if(this.x+30=et.x&this.x+30=et.x+30&this.y+20et.y)return true;break;case 2:for(int i=0;i=et.x&this.x=et.x+20&this.y+30et.y)return true;if(this.x+20=et.x&this.x+20=et.x+20&this.y+30et.y)r
8、eturn true;if(et.direct=1|et.direct=3)if(this.x=et.x&this.x=et.x+30&this.y+30et.y)return true;if(this.x+20=et.x&this.x+20=et.x+30&this.y+30et.y)return true;break;case 3:for(int i=0;i=et.x&this.x+30=et.x+20&this.yet.y)return true;if(this.x+30=et.x&this.x+30=et.x+20&this.y+20et.y)return true;if(et.dir
9、ect=1|et.direct=3)if(this.x+30=et.x&this.x+30=et.x+30&this.yet.y)return true;if(this.x+30=et.x&this.x+30=et.x+30&this.y+20et.y)return true;break;return b;public void run() while(true)switch(this.direct)case 0:for(int i=0;i0&this.isTouch()=false)y-=this.speed;try Thread.sleep(50); catch (InterruptedE
10、xception e) / TODO Auto-generated catch blocke.printStackTrace();break;case 1:for(int i=0;i30;i+)if(x365&this.isTouch()=false)x+=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;case 2:for(int i=0;i30;i+)if(y270&this.isTouch()
11、=false)y+=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;case 3:for(int i=0;i0&this.isTouch()=false)x-=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();brea
12、k;this.direct=(int)(Math.random()*4);if(this.isLive=false)break;if(ensh.size()0)lift-;elseisLive=false; /子弹类class Shot implements Runnableint shotX;int shotY;int direct;int shotspeed=1;boolean isLive=true;public Shot(int x,int y,int direct)this.shotX=x;this.shotY=y;this.direct=direct;public int getS
13、hotX() return shotX;public void setShotX(int shotX) this.shotX = shotX;public int getShotY() return shotY;public void setShotY(int shotY) this.shotY = shotY;public int getShotspeed() return shotspeed;public void setShotspeed(int shotspeed) this.shotspeed = shotspeed;public void run()while(true)try T
14、hread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();switch(direct)case 0:shotY-=shotspeed;break;case 1:shotX+=shotspeed;break;case 2:shotY+=shotspeed;break;case 3:shotX-=shotspeed;break;if(shotX400|shotY300)isLive=false;break;/* * 功能:坦克大战4.0 */package
15、 com.tankgame4;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import java.io.*;import javax.swing.*;import javax.imageio.*;public class MyTankGame4 extends JFrame implem
16、ents ActionListenerMyPanel mp=null;MyStartPanel msp=null;/菜单定义JMenuBar jmb=null;JMenu jm1=null;JMenuItem jmi1=null;public static void main(String args) / TODO Auto-generated method stubMyTankGame4 mtg=new MyTankGame4();/构造函数public MyTankGame4()/创建菜单jmb=new JMenuBar();jm1=new JMenu(Game(G);jm1.setMne
17、monic(G);jmi1=new JMenuItem(New Game(N);jmi1.setMnemonic(N);jmi1.addActionListener(this);jmi1.setActionCommand(New Game);jm1.add(jmi1);jmb.add(jm1);this.setJMenuBar(jmb);msp=new MyStartPanel();Thread st=new Thread(msp);st.start();this.add(msp);this.setTitle(坦克大战);this.setSize(400, 300);this.setDefau
18、ltCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);public void actionPerformed(ActionEvent arg0) if(arg0.getActionCommand().equals(New Game)this.remove(msp);mp=new MyPanel();Thread mt=new Thread(mp);mt.start();this.add(mp);this.addKeyListener(mp);this.setVisible(true);/游戏开始面板class MyStartP
19、anel extends JPanel implements Runnableint times=0;public void paint(Graphics g)super.paint(g);g.fillRect(0, 0, 400, 300);if(times%2=0)Font myfont=new Font(行楷,Font.BOLD,30);g.setFont(myfont);g.setColor(Color.yellow);g.drawString(第一关, 130, 130);this.repaint();public void run() while(true)try Thread.s
20、leep(300);times+; catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();/我的面板class MyPanel extends JPanel implements KeyListener,Runnable/定义一个我的坦克Hero hero=null;EnemyTank et=null;Image image1=null;Image image2=null;Image image3=null;Image im=null;Vector bombs =new Vecto
21、r();Vector etm =new Vector();int ensize=5;public void paint(Graphics g)super.paint(g);g.fillRect(0, 0, 400, 300);g.drawImage(im, 0, 0, 400, 300,this);/画出自己的坦克if(hero.isLive=true)this.drawTank(this.hero.getX(), this.hero.getY(), g, this.hero.getDirect(),this.hero.getColor() );/画出子弹for(int i=0;ihero.s
22、hotm.size();i+)Shot myshot=hero.shotm.get(i);if(myshot!=null&myshot.isLive=true)g.fill3DRect(myshot.getShotX(),myshot.getShotY(), 2, 2, false);if(myshot.isLive=false)hero.shotm.remove(myshot);/画出炸弹for(int i=0;i6)g.drawImage(image1, b.x, b.y, 30, 30, this);else if(b.lift3)g.drawImage(image2, b.x, b.y
23、, 30, 30, this);elseg.drawImage(image3, b.x, b.y, 30, 30, this);b.liftdown();if(b.lift=0)bombs.remove(b);/画出敌人的坦克for(int i=0;ietm.size();i+)et=etm.get(i);if(et!=null&et.isLive=true)this.drawTank(et.getX(), et.getY(),g, et.getDirect(), et.getColor();/画出敌人的子弹for(int j=0;jt.getX()&s.getShotX()t.getY()&
24、s.getShotY()t.getX()&s.getShotX()t.getY()&s.getShotY()t.getY()+20) s.isLive=false; t.isLive=false; Bomb b=new Bomb(t.getX(),t.getY(); bombs.add(b); /画出坦克的函数public void drawTank(int xx,int yy,Graphics g,int direct,int type)/判断什么颜色类型的坦克switch(type)case 0:g.setColor(Color.CYAN);break;case 1:g.setColor(
25、Color.pink);break;case 2:g.setColor(Color.red);break;case 3:g.setColor(Color.green);break;case 4:g.setColor(Color.blue);break;case 5:g.setColor(Color.yellow);break;/判断什么方向的坦克switch(direct) /向上case 0:g.fill3DRect(xx, yy, 5, 30, false);g.fill3DRect(xx+15, yy, 5, 30, false);g.fill3DRect(xx+5, yy+5, 10,
26、 20, false);g.fillOval(xx+5, yy+10, 10, 10);g.drawLine(xx+10, yy+15, xx+10, yy);break;/向右case 1:g.fill3DRect(xx, yy, 30, 5, false);g.fill3DRect(xx, yy+15, 30, 5, false);g.fill3DRect(xx+5, yy+5, 20, 10, false);g.fillOval(xx+10, yy+5, 10, 10);g.drawLine(xx+15, yy+10, xx+30, yy+10);break;/向下case 2:g.fi
27、ll3DRect(xx, yy, 5, 30, false);g.fill3DRect(xx+15, yy, 5, 30, false);g.fill3DRect(xx+5, yy+5, 10, 20, false);g.fillOval(xx+5, yy+10, 10, 10);g.drawLine(xx+10, yy+15, xx+10, yy+30);break;/向左case 3:g.fill3DRect(xx, yy, 30, 5, false);g.fill3DRect(xx, yy+15, 30, 5, false);g.fill3DRect(xx+5, yy+5, 20, 10
28、, false);g.fillOval(xx+10, yy+5, 10, 10);g.drawLine(xx+15, yy+10, xx, yy+10);break;public MyPanel()hero=new Hero(100,100);im=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource(/28951278.jpg);/创建敌人坦克for(int i=0;iensize;i+) et=new EnemyTank(i+1)*50,0);Shot enshot=new Shot(et.getX()+10,et.get
29、Y()+30,et.getDirect();Thread eth=new Thread(enshot);eth.start();et.ensh.add(enshot);Thread th=new Thread(et);th.start();etm.add(et);et.setets(etm);/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 (IOException e
30、) / TODO Auto-generated catch block/e.printStackTrace();/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);
31、/控制坦克移动public void keyPressed(KeyEvent arg0) / TODO Auto-generated method stubif(arg0.getKeyCode()=KeyEvent.VK_DOWN)this.hero.setDirect(2);this.hero.movedown();else if(arg0.getKeyCode()=KeyEvent.VK_UP)this.hero.setDirect(0);this.hero.moveup();else if(arg0.getKeyCode()=KeyEvent.VK_LEFT)this.hero.setDirect(3);this.hero.moveleft();else if(arg0.getKeyCode()=KeyEvent.VK_RIGHT)this.hero.setDirect(1);this.h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高校学生坠楼应急预案演练(3篇)
- 2025年品牌形象系统整合设计与制作执行合同
- 2025年企业员工心理素质提升与专业辅导合同
- 2025年度家庭房产分割及个人债务独立承担协议书
- 2025年智能车位租赁与增值服务一体化合同
- 2025年度赛车赛事场地使用权及安全防护责任协议
- 2025年度可持续农业生态鱼塘合作租赁协议书
- 2025年度高品质绿色环保油漆原料采购合同
- 餐饮行业2025年度员工隐私保护协议范本
- 数字化艺术作品装裱与版权交易合作协议
- 化学(基础模块)中职PPT完整全套教学课件
- 京东集团员工手册-京东
- 2023年苏州市星海实验中学小升初分班考试数学模拟试卷及答案解析
- GB/T 37915-2019社区商业设施设置与功能要求
- GB/T 31298-2014TC4钛合金厚板
- GB/T 27746-2011低压电器用金属氧化物压敏电阻器(MOV)技术规范
- GB/T 22237-2008表面活性剂表面张力的测定
- GB/T 13667.3-2003手动密集书架技术条件
- 导轨及线槽项目投资方案报告模板
- 复旦大学<比较财政学>课程教学大纲
- 书法的章法布局(完整版)
评论
0/150
提交评论