欢迎来到三一文库! | 帮助中心 三一文库31doc.com 一个上传文档投稿赚钱的网站
三一文库
全部分类
  • 研究报告>
  • 工作总结>
  • 合同范本>
  • 心得体会>
  • 工作报告>
  • 党团相关>
  • 幼儿/小学教育>
  • 高等教育>
  • 经济/贸易/财会>
  • 建筑/环境>
  • 金融/证券>
  • 医学/心理学>
  • ImageVerifierCode 换一换
    首页 三一文库 > 资源分类 > DOC文档下载  

    坦克大战源代码.doc

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

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

    坦克大战源代码.doc

    import java.awt.Color;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.util.ArrayList;import java.util.List;public class TankClient extends Frame public static final int GAME_WIDTH = 800;public static final int GAME_HEIGHT = 600;Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);List<Missile> missiles = new ArrayList<Missile>();List<Explode> explodes = new ArrayList<Explode>();List<Tank> tanks = new ArrayList<Tank>();Image offScreenImage = null;Overridepublic void paint(Graphics g) g.drawString("missiles count:" + missiles.size(), 10, 50);g.drawString("explodes count:" + explodes.size(), 10, 70);g.drawString("tanks count:" + tanks.size(), 10, 90);for(int i=0; i<missiles.size(); i+) Missile m = missiles.get(i);m.hitTanks(tanks);m.draw(g);for(int i=0; i<explodes.size(); i+) Explode e = explodes.get(i);e.draw(g);for(int i=0; i<tanks.size(); i+) Tank t = tanks.get(i);t.draw(g);myTank.draw(g);Overridepublic void update(Graphics g) if(offScreenImage = null) offScreenImage = this.createImage(800, 600);Graphics gOffScreen = offScreenImage.getGraphics();Color c = gOffScreen.getColor();gOffScreen.setColor(Color.GREEN);gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage, 0, 0, null);public void launchFrame() /生产多少地方坦克for(int i=0; i<5; i+) tanks.add(new Tank(50 + 40*(i+1), 50, false, Tank.Direction.D, this);this.setLocation(400, 300);this.setSize(GAME_WIDTH, GAME_HEIGHT);this.setTitle("TankWar");this.addWindowListener(new WindowAdapter() Overridepublic void windowClosing(WindowEvent e) System.exit(0););this.setResizable(false);this.setBackground(Color.GREEN);this.addKeyListener(new KeyMonitor();this.setVisible(true);new Thread(new PaintThread().start();public static void main(String args) TankClient tc = new TankClient();tc.launchFrame();class PaintThread implements Runnable public void run() while(true) repaint();try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace();class KeyMonitor extends KeyAdapter Overridepublic void keyReleased(KeyEvent e) myTank.keyReleased(e);Overridepublic void keyPressed(KeyEvent e) myTank.keyPressed(e);import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.awt.event.KeyEvent;import java.util.Random;public class Tank public static final int XSPEED = 5;public static final int YSPEED = 5;public static final int WIDTH = 30;public static final int HEIGHT = 30;boolean good;int x, y;private static Random r = new Random();private boolean live = true;private int step = r.nextInt(12) + 3;TankClient tc;boolean bL, bU, bR, bD;enum Direction L, LU, U, RU, R, RD, D, LD, STOP;Direction dir = Direction.STOP;Direction ptDir = Direction.D;public Tank(int x, int y, boolean good) this.x = x;this.y = y;this.good = good;public Tank(int x, int y, boolean good, Direction dir, TankClient tc) this(x, y, good);this.dir = dir;this.tc = tc;public void draw(Graphics g) if(!live) if(!good) tc.tanks.remove(this);return;Color c = g.getColor();if(good) g.setColor(Color.RED);else g.setColor(Color.BLUE);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);switch(ptDir) case L:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT/2);break;case LU:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y);break;case U:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y);break;case RU:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y);break;case R:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT/2);break;case RD:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT);break;case D:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y + HEIGHT);break;case LD:g.drawLine(x + WIDTH/2, y + HEIGHT/2, x, y + HEIGHT);break;move();private void move() switch(dir) case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;if(dir != Direction.STOP) ptDir = dir;if(x < 0) x = 0;if(y < 30) y = 30;if(x + WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - WIDTH;if(y + HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - HEIGHT;if(!good) if(step = 0) step = r.nextInt(12) + 3;Direction dirs = Direction.values();dir = dirsr.nextInt(dirs.length);step -;if(r.nextInt(40) > 38) this.fire();public void keyPressed(KeyEvent e) int key = e.getKeyCode();switch (key) case KeyEvent.VK_LEFT:bL = true;break;case KeyEvent.VK_UP:bU = true;break;case KeyEvent.VK_RIGHT:bR = true;break;case KeyEvent.VK_DOWN:bD = true;break;locateDirection();private void locateDirection() if(bL && !bU && !bR && !bD) dir = Direction.L;else if(bL && bU && !bR && !bD) dir = Direction.LU;else if(!bL && bU && !bR && !bD) dir = Direction.U;else if(!bL && bU && bR && !bD) dir = Direction.RU;else if(!bL && !bU && bR && !bD) dir = Direction.R;else if(!bL && !bU && bR && bD) dir = Direction.RD;else if(!bL && !bU && !bR && bD) dir = Direction.D;else if(bL && !bU && !bR && bD) dir = Direction.LD;else if(!bL && !bU && !bR && !bD) dir = Direction.STOP;public void keyReleased(KeyEvent e) int key = e.getKeyCode();switch (key) case KeyEvent.VK_CONTROL:fire();break;case KeyEvent.VK_LEFT:bL = false;break;case KeyEvent.VK_UP:bU = false;break;case KeyEvent.VK_RIGHT:bR = false;break;case KeyEvent.VK_DOWN:bD = false;break;locateDirection();private Missile fire() int x = this.x + WIDTH/2 - Missile.WIDTH/2;int y = this.y + HEIGHT/2 - Missile.HEIGHT/2;Missile m = new Missile(x, y, this.good, this.ptDir, this.tc);tc.missiles.add(m);return m;public Rectangle getRect() return new Rectangle(x, y, WIDTH, HEIGHT);public boolean isLive() return live;public void setLive(boolean live) this.live = live;import java.awt.Color;import java.awt.Graphics;import java.awt.Rectangle;import java.util.List;public class Missile public static final int XSPEED = 10;public static final int YSPEED = 10;public static final int WIDTH = 10;public static final int HEIGHT = 10;TankClient tc;int x, y;Tank.Direction dir = Tank.Direction.R;boolean live = true;private boolean good;public Missile(int x, int y, boolean good, Tank.Direction dir) this.x = x;this.y = y;this.good = good;this.dir = dir;public Missile(int x, int y, boolean good, Tank.Direction dir, TankClient tc) this(x, y, good, dir);this.tc = tc;public void draw(Graphics g) if(!live) tc.missiles.remove(this);return;Color c = g.getColor();g.setColor(Color.BLACK);g.fillOval(x, y, WIDTH, HEIGHT);g.setColor(c);move();private void move() switch(dir) case L:x -= XSPEED;break;case LU:x -= XSPEED;y -= YSPEED;break;case U:y -= YSPEED;break;case RU:x += XSPEED;y -= YSPEED;break;case R:x += XSPEED;break;case RD:x += XSPEED;y += YSPEED;break;case D:y += YSPEED;break;case LD:x -= XSPEED;y += YSPEED;break;case STOP:break;if(x < 0 | y < 0 | x > TankClient.GAME_WIDTH | y > TankClient.GAME_HEIGHT) live = false;public Rectangle getRect() return new Rectangle(x, y, WIDTH, HEIGHT);public boolean hitTank(Tank t) if(this.live && t.isLive() && this.good != t.good &&this.getRect().intersects(t.getRect() this.live = false;t.setLive(false);tc.explodes.add(new Explode(x, y, tc);return true;return false;public boolean hitTanks(List<Tank> tanks) for(int i=0; i<tanks.size(); i+) if(this.hitTank(tanks.get(i) return true;return false;import java.awt.Color;import java.awt.Graphics;public class Explode int x, y;private int diameters = 4, 7, 12, 18, 26, 32, 49, 30, 14, 6;private boolean live = true;private TankClient tc;int step = 0;public Explode(int x, int y, TankClient tc) this.x = x;this.y = y;this.tc = tc;public void draw(Graphics g) if(!live) tc.explodes.remove(this);return;Color c = g.getColor();g.setColor(Color.ORANGE);g.fillOval(x, y, diametersstep, diametersstep);g.setColor(c);step +;if(step = diameters.length) live = false; 8

    注意事项

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

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




    经营许可证编号:宁ICP备18001539号-1

    三一文库
    收起
    展开