版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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<Shot> shotm=new Vector<Shot>();public H
3、ero(int x,int y)super(x,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
4、);break;case 3:shot=new 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 RunnableVector<
5、;Shot>ensh=new Vector<Shot>();Vector<EnemyTank>ets=new Vector<EnemyTank>();public EnemyTank(int x, int y)super(x, y);this.setColor(2);this.setDirect(2);/获取MPanel上的敌人坦克public void setets(Vector<EnemyTank> vv)this.ets=vv;/判断敌人的坦克是否碰撞public boolean isTouch()boolean b=false;En
6、emyTank et=null;switch(direct)case 0:for(int i=0;i<ets.size();i+)et=ets.get(i);if(et!=this)if(et.direct=0|et.direct=2)if(this.x>=et.x&&this.x<=et.x+20&&this.y<=et.y+30&&this.y>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y
7、<=et.y+30&&this.y>et.y)return true;if(et.direct=1|et.direct=3)if(this.x>=et.x&&this.x<=et.x+30&&this.y<=et.y+20&&this.y>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y<=et.y+20&&this.y>et.y)return t
8、rue;break;case 1:for(int i=0;i<ets.size();i+)et=ets.get(i);if(et!=this)if(et.direct=0|et.direct=2)if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<=et.y+3
9、0&&this.y+20>et.y)return true;if(et.direct=1|et.direct=3)if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>et.y)retu
10、rn true;break;case 2:for(int i=0;i<ets.size();i+)et=ets.get(i);if(et!=this)if(et.direct=0|et.direct=2)if(this.x>=et.x&&this.x<=et.x+20&&this.y+30<=et.y+30&&this.y+30>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+20&&this.y+30<=et
11、.y+30&&this.y+30>et.y)return true;if(et.direct=1|et.direct=3)if(this.x>=et.x&&this.x<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)return true;if(this.x+20>=et.x&&this.x+20<=et.x+30&&this.y+30<=et.y+20&&this.y+30>et.y)
12、return true;break;case 3:for(int i=0;i<ets.size();i+)et=ets.get(i);if(et!=this)if(et.direct=0|et.direct=2)if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y<=et.y+30&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+20&&this.y+20<
13、;=et.y+30&&this.y+20>et.y)return true;if(et.direct=1|et.direct=3)if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y<=et.y+20&&this.y>et.y)return true;if(this.x+30>=et.x&&this.x+30<=et.x+30&&this.y+20<=et.y+20&&this.y+20>e
14、t.y)return true;break;return b;public void run() while(true)switch(this.direct)case 0:for(int i=0;i<30;i+)if(y>0&&this.isTouch()=false)y-=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;case 1:for(int i=0;i<3
15、0;i+)if(x<365&&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;i<30;i+)if(y<270&&this.isTouch()=false)y+=this.speed;try Thread.sleep(50); catch (InterruptedE
16、xception e) / TODO Auto-generated catch blocke.printStackTrace();break;case 3:for(int i=0;i<30;i+)if(x>0&&this.isTouch()=false)x-=this.speed;try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();break;this.direct=(int)(Math.random()*4
17、);if(this.isLive=false)break;if(ensh.size()<5)Shot es=null;switch(this.direct)case 0:es=new Shot(this.getX()+10,this.getY(),0);ensh.add(es);break;case 1:es=new Shot(this.getX()+30,this.getY()+10,1);ensh.add(es);break;case 2:es=new Shot(this.getX()+10,this.getY()+30,2);ensh.add(es);break;case 3:es
18、=new Shot(this.getX(),this.getY()+10,3);ensh.add(es);break;Thread th=new Thread(es);th.start();/炸弹类 class Bomb int x;int y;int lift=9;boolean isLive=true;public Bomb(int x,int y)this.x=x;this.y=y;/炸弹的生命值public void liftdown()if(lift>0)lift-;elseisLive=false; /子弹类class Shot implements Runnableint
19、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 getShotX() return shotX;public void setShotX(int shotX) this.shotX = shotX;public int getShotY() return shotY;public void setShotY(int shotY) this
20、.shotY = shotY;public int getShotspeed() return shotspeed;public void setShotspeed(int shotspeed) this.shotspeed = shotspeed;public void run()while(true)try Thread.sleep(50); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();switch(direct)case 0:shotY-=shotspeed;bre
21、ak;case 1:shotX+=shotspeed;break;case 2:shotY+=shotspeed;break;case 3:shotX-=shotspeed;break;if(shotX<0|shotX>400|shotY<0|shotY>300)isLive=false;break;/* * 功能:坦克大战4.0 */package com.tankgame4;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import j
22、ava.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 implements ActionListenerMyPanel mp=null;MyStartPanel msp=null;/菜单定义JMenuBar jmb=null;JMenu jm1=null;JMenuItem jmi1=nu
23、ll;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.setMnemonic('G');jmi1=new JMenuItem("New Game(N)");jmi1.setMnemonic('N');jmi1.a
24、ddActionListener(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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);thi
25、s.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 MyStartPanel extends JPanel implements
26、 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.sleep(3
27、00);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<Bomb> bombs =new
28、 Vector<Bomb>();Vector<EnemyTank> etm =new Vector<EnemyTank>();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.getDire
29、ct(),this.hero.getColor() );/画出子弹for(int i=0;i<hero.shotm.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;i<bombs.size();i+)Bomb
30、b=bombs.get(i);if(b.lift>6)g.drawImage(image1, b.x, b.y, 30, 30, this);else if(b.lift>3)g.drawImage(image2, b.x, b.y, 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;i<etm.size();i+)et=etm.get(i);if(et!=null&&
31、;et.isLive=true)this.drawTank(et.getX(), et.getY(),g, et.getDirect(), et.getColor();/画出敌人的子弹for(int j=0;j<et.ensh.size();j+)Shot enshot=et.ensh.get(j);if(enshot.isLive=true)g.fill3DRect(enshot.getShotX(),enshot.getShotY(), 2, 2, false);/System.out.println("第"+i+"辆坦克的第"+j+"
32、;颗子弹的Y="+enshot.getShotY();elseet.ensh.remove(enshot);/判断子弹是否击中坦克的函数 public void hitTank(Shot s,Tank t) switch(t.getDirect() case 0: case 2: if(s.getShotX()>t.getX()&&s.getShotX()<t.getX()+20&& s.getShotY()>t.getY()&&s.getShotY()<t.getY()+30) s.isLive=false;
33、t.isLive=false; Bomb b=new Bomb(t.getX(),t.getY(); bombs.add(b); break; case 1: case 3: if(s.getShotX()>t.getX()&&s.getShotX()<t.getX()+30&& s.getShotY()>t.getY()&&s.getShotY()<t.getY()+20) s.isLive=false; t.isLive=false; Bomb b=new Bomb(t.getX(),t.getY(); bombs.a
34、dd(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(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.setCol
35、or(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, 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.fill3DRe
36、ct(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.fill3DRect(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);
37、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, 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.getDefau
38、ltToolkit().getImage(Panel.class.getResource("/28951278.jpg");/创建敌人坦克for(int i=0;i<ensize;i+) et=new EnemyTank(i+1)*50,0);Shot enshot=new Shot(et.getX()+10,et.getY()+30,et.getDirect();Thread eth=new Thread(enshot);eth.start();et.ensh.add(enshot);Thread th=new Thread(et);th.start();etm.a
39、dd(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) / TODO Auto-generated catch block/e.printStackTrace();/image1=Toolkit.getDefaultToolkit
40、().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 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
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 采购办公用品规格及数量确认函7篇范本
- 预防校园暴力营造和谐氛围小学主题班会课件
- 道德修养引领人生方向-小学主题班会课件
- 互联网产品经理产品开发与运营效率绩效评定表
- 可持续建筑节能效率评估表
- 物流仓储业自动化设备与智能仓储布局优化方案
- 2026年智慧农业发展趋势与市场创新分析报告
- 餐饮连锁店标准化管理及运营解决方案
- 抵制欺凌行为倡导友善和谐四年级主题班会课件
- 智能仓储系统物流调度标准化方案
- 2026-2027年大湾区氢能生产项目可行性研究报告
- 杨浦区2025-2026学年第二学期期末考试六年级数学学试卷及答案(上海新教材沪教版)
- 2026重庆巴南区招聘辅警100人笔试参考题库及答案解析(完整版)
- 2026年新疆第二 师铁门关市高校毕业生“三支一扶”计划招募(251人)考试备考试题及答案详解
- 不同年龄段患者雾化吸入护理技巧
- GB/T 13589-2026再生锌及锌合金原料
- 石油化工台账记录管理手册
- 线粒体应激反应-洞察与解读
- 铝合金牺牲阳极的国家标准与行业规范
- 信息技术(WPS Office+人工智能)(第3版)课件全套 徐维祥 第1-11章 文档处理 -人工智能伦理与治理
- 2025年安康杯知识竞赛题库测试卷附答案
评论
0/150
提交评论