tank game V0.1
/**
* 功能:坦克游戏
* 1.画出坦克.
* 2.我的坦克可以上下左右移动
* 3.可以发射子弹,子弹连发
* 4.当我的坦克击中敌人坦克时,敌人就消失(爆炸的效果)
* 5.我被击中后,显示爆炸效果
*/
package cn.demo;import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;class MyPanel extends JPanel implements KeyListener, Runnable {Tank tank = null;Vector<Bomb> bombs = new Vector<Bomb>();Image image1 = null;Image image2 = null;Image image3 = null;Vector<EnemyTank> ets = new Vector<EnemyTank>();public MyPanel(Tank tank) {this.tank = tank;ets.clear();int random = (int) (Math.random() * 10 % 4);System.out.println(random);EnemyTank tank1 = new EnemyTank(0, 0);Thread t1 = new Thread(tank1);t1.start();tank1.setColor(Color.GREEN);tank1.setDirection(random);tank1.setSpeed(2);random = (int) (Math.random() * 10 % 4);System.out.println(random);EnemyTank tank2 = new EnemyTank(70, 0);Thread t2 = new Thread(tank2);t2.start();tank2.setColor(Color.GREEN);tank2.setDirection(random);tank2.setSpeed(2);random = (int) (Math.random() * 10 % 4);System.out.println(random);EnemyTank tank3 = new EnemyTank(140, 0);Thread t3 = new Thread(tank3);t3.start();tank3.setColor(Color.GREEN);tank3.setDirection(random);tank3.setSpeed(2);ets.add(tank1);ets.add(tank2);ets.add(tank3);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}}public void paint(Graphics g) {super.paint(g);// draw herodrawTank(tank, g, tank.getDirection());// draw emery tankfor (int i = 0; i < ets.size(); i++) {Tank tank = ets.get(i);if (tank != null && tank.isLive) {drawTank(tank, g, tank.getDirection());}if (!tank.isLive) {ets.remove(tank);}}// draw bulletVector<Bullet> bulletList = tank.getBulletList();for (int i = 0; i < bulletList.size(); i++) {Bullet bullet = bulletList.get(i);if (bullet != null && bullet.islive) {drawBullet(g, tank);}if (!bullet.islive) {bulletList.remove(bullet);}for (int j = 0; j < ets.size(); j++) {Tank tank = ets.get(j);if (tank != null && tank.isLive) {hitEmery(bullet, tank);}}}// draw bombfor (int i = 0; i < bombs.size(); i++) {Bomb b = bombs.get(i);if (b != null && b.life > 6) {g.drawImage(image1, b.x, b.y, 30, 30, this);} else if (b != null && 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.decreaseLife();if (!b.isLive) {bombs.remove(b);}}}public void drawBullet(Graphics g, Tank tank) {g.setColor(Color.BLACK);Vector<Bullet> bulletList = tank.getBulletList();for (int i = 0; i < bulletList.size(); i++) {Bullet bullet = bulletList.get(i);if (bullet.islive) {g.fill3DRect(bulletList.get(i).getX(), bulletList.get(i).getY(), 2, 2, false);}}}public void drawTank(Tank tank, Graphics g, int direction) {g.setColor(tank.getColor());switch (direction) {case 0:g.fill3DRect(tank.getX(), tank.getY(), 5, 30, false);g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);g.fill3DRect(tank.getX() + 25, tank.getY(), 5, 30, false);g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);g.drawLine(tank.getX() + 15, tank.getY() - 5, tank.getX() + 15, tank.getY() + 15);break;case 1:g.fill3DRect(tank.getX(), tank.getY(), 5, 30, false);g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);g.fill3DRect(tank.getX() + 25, tank.getY(), 5, 30, false);g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 15, tank.getY() + 35);break;case 2:g.fill3DRect(tank.getX(), tank.getY(), 30, 5, false);g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);g.fill3DRect(tank.getX(), tank.getY() + 25, 30, 5, false);g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);g.drawLine(tank.getX() - 5, tank.getY() + 15, tank.getX() + 15, tank.getY() + 15);break;case 3:g.fill3DRect(tank.getX(), tank.getY(), 30, 5, false);g.fill3DRect(tank.getX() + 5, tank.getY() + 5, 20, 20, false);g.fill3DRect(tank.getX(), tank.getY() + 25, 30, 5, false);g.drawOval(tank.getX() + 10, tank.getY() + 10, 10, 10);g.drawLine(tank.getX() + 15, tank.getY() + 15, tank.getX() + 35, tank.getY() + 15);break;}}public void hitEmery(Bullet bullet, Tank t) {if (bullet.getX() >= t.getX() && bullet.getX() <= t.getX() + 30 && bullet.getY() >= t.getY()&& bullet.getY() <= t.getY() + 30) {t.isLive = false;bullet.islive = false;// create bombBomb b = new Bomb(t.getX(), t.getY());bombs.add(b);}}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubif (e.getKeyCode() == KeyEvent.VK_A) {tank.setDirection(Direction.LEFT);tank.MoveLeft();} else if (e.getKeyCode() == KeyEvent.VK_D) {tank.setDirection(Direction.RIGHT);tank.MoveRight();} else if (e.getKeyCode() == KeyEvent.VK_W) {tank.setDirection(Direction.UP);tank.MoveUp();} else if (e.getKeyCode() == KeyEvent.VK_S) {tank.setDirection(Direction.DOWN);tank.MoveDown();}if (e.getKeyCode() == KeyEvent.VK_J) {tank.shotEmery();}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void run() {// TODO Auto-generated method stubwhile (true) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}this.repaint();}}
}class Direction {public final static int UP = 0;public final static int DOWN = 1;public final static int LEFT = 2;public final static int RIGHT = 3;
}class ClientWindow {public final static int WIDTH = 300;public final static int HEIGHT = 400;
}class MyTestJFrame extends JFrame {MyPanel panel = null;MyTestJFrame(Tank tank) {panel = new MyPanel(tank);Thread thread = new Thread(panel);thread.start();this.addKeyListener(panel);this.add(panel);this.setSize(ClientWindow.WIDTH, ClientWindow.HEIGHT);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setVisible(true);this.setLocation(100, 200);}
}class Tank {int x;int y;int speed;int direction;Color color;boolean isLive = true;private Vector<Bullet> bulletList = new Vector<Bullet>();public Vector<Bullet> getBulletList() {return bulletList;}public Color getColor() {return color;}public void setColor(Color color) {this.color = color;}public int getDirection() {return direction;}public void setDirection(int direction) {this.direction = direction;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public Tank(int x, int y) {super();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 setY(int y) {this.y = y;}public void MoveLeft() {this.x -= speed;}public void MoveRight() {this.x += speed;}public void MoveUp() {this.y -= speed;}public void MoveDown() {this.y += speed;}public void shotEmery() {Bullet bullet = null;if (direction == Direction.LEFT) {bullet = new Bullet(x - 5, y + 15, Direction.LEFT);} else if (direction == Direction.RIGHT) {bullet = new Bullet(x + 35, y + 15, Direction.RIGHT);} else if (direction == Direction.UP) {bullet = new Bullet(x + 15, y - 5, Direction.UP);} else if (direction == Direction.DOWN) {bullet = new Bullet(x + 15, y + 35, Direction.DOWN);}bullet.setSpeed(2);bulletList.add(bullet);Thread thread = new Thread(bullet);thread.start();}
}class Bullet implements Runnable {@Overridepublic String toString() {return "Bullet [x=" + x + ", y=" + y + ", speed=" + speed + ", direction=" + direction + ", islive=" + islive+ "]";}private int x;private int y;private int speed;private int direction;Boolean islive = true;public Bullet(int x, int y, int direction) {super();this.x = x;this.y = y;this.direction = direction;}public int getDirection() {return direction;}public void setDirection(int direction) {this.direction = direction;}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public int getSpeed() {return speed;}public void setSpeed(int speed) {this.speed = speed;}public void MoveLeft() {this.x -= speed;}public void MoveRight() {this.x += speed;}public void MoveUp() {this.y -= speed;}public void MoveDown() {this.y += speed;}@Overridepublic void run() {// TODO Auto-generated method stubwhile (true) {try {Thread.sleep(100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}if (direction == Direction.UP) {this.MoveUp();} else if (direction == Direction.LEFT) {this.MoveLeft();} else if (direction == Direction.RIGHT) {this.MoveRight();} else if (direction == Direction.DOWN) {this.MoveDown();}if (this.x <= 0 || this.y <= 0 || this.x >= ClientWindow.WIDTH || this.y >= ClientWindow.HEIGHT) {islive = false;break;}}}
}class Hero extends Tank {public Hero(int x, int y) {super(x, y);}
}class EnemyTank extends Tank implements Runnable {int bulletNumber;public EnemyTank(int x, int y) {super(x, y);}public void run() {// TODO Auto-generated method stubwhile (true) {switch (this.direction) {case 0:// UPfor (int i = 0; i < 30; i++) {if (y > 0) {y -= speed;}try {Thread.sleep(50);} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}break;case 1:// DOWNfor (int i = 0; i < 30; i++) {// minus tank.sizeif (y < ClientWindow.HEIGHT - 30) {y += speed;}try {Thread.sleep(50);} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}break;case 2:// LEFTfor (int i = 0; i < 30; i++) {if (x > 0) {x -= speed;}try {Thread.sleep(50);} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}break;case 3:// RIGHTfor (int i = 0; i < 30; i++) {// minus tank.sizeif (x < ClientWindow.WIDTH - 30) {x += speed;}try {Thread.sleep(50);} catch (Exception e) {e.printStackTrace();// TODO: handle exception}}break;}this.direction = (int) (Math.random() * 4);if (this.isLive == false) {break;}}}
}class Bomb {int x;int y;int life = 9;boolean isLive = true;public Bomb(int x, int y) {this.x = x;this.y = y;}public void decreaseLife() {if (life > 0) {life--;} else {isLive = false;}}}public class TestMain {public static void main(String[] str) {Hero hero = new Hero(150, 300);hero.setSpeed(5);hero.setColor(Color.RED);MyTestJFrame frame = new MyTestJFrame(hero);}}
tank game V0.1相关推荐
- Tank Game V0.2
/** * 功能:坦克游戏 * 1.画出坦克. * 2.我的坦克可以上下左右移动 * 3.可以发射子弹,子弹连发 * 4.当我的坦克击中敌人坦克时,敌人就消失(爆炸的效果) * 5.我被击 ...
- MMSegmentation V0.27.0训练与推理自己的数据集(二)
1.官方模型转换MMSegmentation风格 如果你想自己转换关键字使用官方存储库的预训练模型,我们还提供了一个脚本swin2mmseg.py在tools directory ,将模型的关键字从官 ...
- 隐藏探针显示php版本号,修改版雅黑PHP探针 支持PHP7+(v0.4.7.2)
雅黑 PHP 探针用于 Linux 系统(不推荐使用于 Windows 系统),每秒更新,不用刷网页.可以实时查看服务器硬盘资源.内存占用.网卡流量.系统负载.服务器时间等信息,1 秒钟刷新一次. 可 ...
- Node.js v0.10版本发布
Node.js研发团队发布了node.js v0.10版本,它是个基于Javascript.用于构建高性能异步服务器的平台.该版本主要更新如下:更易于使用的数据流处理模块,通过域更好地处理错误,此外还 ...
- Apache IoTDB v0.13 发布!
本文约1500字,建议阅读5分钟 下载最新版本Apache IoTDB v0.13. Release Announcement version 0.13 Apache IoTDB v0.13 ...
- Apache IoTDB v0.12.5 发布!
本文约800字,建议阅读5分钟 本文与你分享 Apache IoTDB v0.12.5 的Release Announcement. Release Announcement v0.12.5 Ap ...
- 免费的新一代私有云平台Nano v0.3.1发布:云主机快照及媒体管理
官方网站: https://nanos.cloud/zh-cn/ 下载地址: https://nanos.cloud/zh-cn/download.html 详细更新记录:https://nanos. ...
- [分类整理IV]微软等100题系列V0.1版:字符串+数组面试题集锦
微软等100题系列V1.0版整理IV:字符串+数组面试题集锦 July 2010年12月30日 第4章 字符串+数组面试题 在微软等100题系列V0.1版中,此类字符串+数组的问题,占了足足22道 ...
- .NET Core微服务之路:不断更新中的目录 (v0.42)
原文:.NET Core微服务之路:不断更新中的目录 (v0.42) 微服务架构,对于从事JAVA架构的童鞋来说,早已不是什么新鲜的事儿,他们有鼎鼎大名的Spring Cloud这样的全家桶框架支撑, ...
最新文章
- php每分钟刷新一次的验证码,php如何在进入页面的时候自动刷新一次验证码
- qu.la网站上的小说爬取
- java8 stream遍历_Java8新特性:Stream流详解
- android studio 手动安装gradle,Android Studio 如何安装Gradle?
- .NetCore2.1 WebAPI 根据swagger.json自动生成客户端代码
- php smarty 语法,php之Smarty根本语法和三大变量
- mysql 5.6.26 驱动_mysql版本引起的驱动问题
- 矩阵运算_如何理解矩阵对矩阵求导?
- iOS 的keyChain
- Skyline软件二次开发初级——1如何在web页面中添加控件和加载三维地图数据
- 计算机系统的结构分类,图解计算机结构与系统分类!!
- 微软逆转互联网战局,错过了智能手机却君临游戏帝国
- Window笔记本触摸板手势大全
- Android 强制关闭软键盘/修改软键盘状态——弹出或关闭
- 蚂蚁区块链第2课 如何申请获得100万创新大赛参赛资格?
- csr8670--sink工程的大致工作流程分析(以speaker为例)一
- 【win10蓝屏】记录一下,随机蓝屏,开机蓝屏,使用中蓝屏的经历
- JVM(五)JVM调优
- RK3399驱动开发 | 08 - RK3399显示系统详解(基于RK SDK Linux 4.4.194内核)
- OpenShift架构