找传奇、传世资源到传世资源站!
Java 正文

Java 坦克大战小游戏源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

坦克大战小游戏源码下载
from clipboard
from clipboard
import java.util.ArrayList;import java.awt.*;import java.awt.event.*;import java.util.List;import javax.swing.JOptionPane;public class TankClient extends Frame implements ActionListener {/** * */private static final long serialVersionUID = 1L;public static final int Fram_width = 800; // 静态全局窗口大小public static final int Fram_length = 600;public static boolean printable = true;MenuBar jmb = null;Menu jm1 = null, jm2 = null, jm3 = null, jm4 = null;MenuItem jmi1 = null, jmi2 = null, jmi3 = null, jmi4 = null, jmi5 = null,jmi6 = null, jmi7 = null, jmi8 = null, jmi9 = null;Image screenImage = null;Tank homeTank = new Tank(300, 560, true, Direction.STOP, this);// 实例化坦克GetBlood blood = new GetBlood(); // 实例化生命Home home = new Home(373, 545, this);// 实例化homeList<River> theRiver = new ArrayList<River>();List<Tank> tanks = new ArrayList<Tank>();List<BombTank> bombTanks = new ArrayList<BombTank>();List<Bullets> bullets = new ArrayList<Bullets>();List<Tree> trees = new ArrayList<Tree>();List<CommonWall> homeWall = new ArrayList<CommonWall>(); // 实例化对象容器List<CommonWall> otherWall = new ArrayList<CommonWall>();List<MetalWall> metalWall = new ArrayList<MetalWall>();public void update(Graphics g) {screenImage = this.createImage(Fram_width, Fram_length);Graphics gps = screenImage.getGraphics();Color c = gps.getColor();gps.setColor(Color.GRAY);gps.fillRect(0, 0, Fram_width, Fram_length);gps.setColor(c);framPaint(gps);g.drawImage(screenImage, 0, 0, null);}public void framPaint(Graphics g) {Color c = g.getColor();g.setColor(Color.green); // 设置字体显示属性Font f1 = g.getFont();g.setFont(new Font("TimesRoman", Font.BOLD, 20));g.drawString("区域内还有敌方坦克: ", 200, 70);g.setFont(new Font("TimesRoman", Font.ITALIC, 30));g.drawString("" tanks.size(), 400, 70);g.setFont(new Font("TimesRoman", Font.BOLD, 20));g.drawString("剩余生命值: ", 500, 70);g.setFont(new Font("TimesRoman", Font.ITALIC, 30));g.drawString("" homeTank.getLife(), 650, 70);g.setFont(f1);if (tanks.size() == 0 && home.isLive() && homeTank.isLive()) {Font f = g.getFont();g.setFont(new Font("TimesRoman", Font.BOLD, 60)); // 判断是否赢得比赛this.otherWall.clear();g.drawString("你赢了! ", 310, 300);g.setFont(f);}if (homeTank.isLive() == false) {Font f = g.getFont();g.setFont(new Font("TimesRoman", Font.BOLD, 40));tanks.clear();bullets.clear();g.setFont(f);}g.setColor(c);for (int i = 0; i < theRiver.size(); i ) { // 画出河流River r = theRiver.get(i);r.draw(g);}for (int i = 0; i < theRiver.size(); i ) {River r = theRiver.get(i);homeTank.collideRiver(r);r.draw(g);}home.draw(g); // 画出homehomeTank.draw(g);// 画出自己家的坦克homeTank.eat(blood);// 充满血--生命值for (int i = 0; i < bullets.size(); i ) { // 对每一个子弹Bullets m = bullets.get(i);m.hitTanks(tanks); // 每一个子弹打到坦克上m.hitTank(homeTank); // 每一个子弹打到自己家的坦克上时m.hitHome(); // 每一个子弹打到家里是for (int j = 0; j < metalWall.size(); j ) { // 每一个子弹打到金属墙上MetalWall mw = metalWall.get(j);m.hitWall(mw);}for (int j = 0; j < otherWall.size(); j ) {// 每一个子弹打到其他墙上CommonWall w = otherWall.get(j);m.hitWall(w);}for (int j = 0; j < homeWall.size(); j ) {// 每一个子弹打到家的墙上CommonWall cw = homeWall.get(j);m.hitWall(cw);}m.draw(g); // 画出效果图}for (int i = 0; i < tanks.size(); i ) {Tank t = tanks.get(i); // 获得键值对的键for (int j = 0; j < homeWall.size(); j ) {CommonWall cw = homeWall.get(j);t.collideWithWall(cw); // 每一个坦克撞到家里的墙时cw.draw(g);}for (int j = 0; j < otherWall.size(); j ) { // 每一个坦克撞到家以外的墙CommonWall cw = otherWall.get(j);t.collideWithWall(cw);cw.draw(g);}for (int j = 0; j < metalWall.size(); j ) { // 每一个坦克撞到金属墙MetalWall mw = metalWall.get(j);t.collideWithWall(mw);mw.draw(g);}for (int j = 0; j < theRiver.size(); j ) {River r = theRiver.get(j); // 每一个坦克撞到河流时t.collideRiver(r);r.draw(g);// t.draw(g);}t.collideWithTanks(tanks); // 撞到自己的人t.collideHome(home);t.draw(g);}blood.draw(g);for (int i = 0; i < trees.size(); i ) { // 画出treesTree tr = trees.get(i);tr.draw(g);}for (int i = 0; i < bombTanks.size(); i ) { // 画出爆炸效果BombTank bt = bombTanks.get(i);bt.draw(g);}for (int i = 0; i < otherWall.size(); i ) { // 画出otherWallCommonWall cw = otherWall.get(i);cw.draw(g);}for (int i = 0; i < metalWall.size(); i ) { // 画出metalWallMetalWall mw = metalWall.get(i);mw.draw(g);}homeTank.collideWithTanks(tanks);homeTank.collideHome(home);for (int i = 0; i < metalWall.size(); i ) {// 撞到金属墙MetalWall w = metalWall.get(i);homeTank.collideWithWall(w);w.draw(g);}for (int i = 0; i < otherWall.size(); i ) {CommonWall cw = otherWall.get(i);homeTank.collideWithWall(cw);cw.draw(g);}for (int i = 0; i < homeWall.size(); i ) { // 家里的坦克撞到自己家CommonWall w = homeWall.get(i);homeTank.collideWithWall(w);w.draw(g);}}public TankClient() {// printable = false;// 创建菜单及菜单选项jmb = new MenuBar();jm1 = new Menu("游戏");jm2 = new Menu("暂停/继续");jm3 = new Menu("帮助");jm4 = new Menu("游戏级别");jm1.setFont(new Font("TimesRoman", Font.BOLD, 15));// 设置菜单显示的字体jm2.setFont(new Font("TimesRoman", Font.BOLD, 15));// 设置菜单显示的字体jm3.setFont(new Font("TimesRoman", Font.BOLD, 15));// 设置菜单显示的字体jm4.setFont(new Font("TimesRoman", Font.BOLD, 15));// 设置菜单显示的字体jmi1 = new MenuItem("开始新游戏");jmi2 = new MenuItem("退出");jmi3 = new MenuItem("暂停");jmi4 = new MenuItem("继续");jmi5 = new MenuItem("游戏说明");jmi6 = new MenuItem("级别1");jmi7 = new MenuItem("级别2");jmi8 = new MenuItem("级别3");jmi9 = new MenuItem("级别4");jmi1.setFont(new Font("TimesRoman", Font.BOLD, 15));jmi2.setFont(new Font("TimesRoman", Font.BOLD, 15));jmi3.setFont(new Font("TimesRoman", Font.BOLD, 15));jmi4.setFont(new Font("TimesRoman", Font.BOLD, 15));jmi5.setFont(new Font("TimesRoman", Font.BOLD, 15));jm1.add(jmi1);jm1.add(jmi2);jm2.add(jmi3);jm2.add(jmi4);jm3.add(jmi5);jm4.add(jmi6);jm4.add(jmi7);jm4.add(jmi8);jm4.add(jmi9);jmb.add(jm1);jmb.add(jm2);jmb.add(jm4);jmb.add(jm3);jmi1.addActionListener(this);jmi1.setActionCommand("NewGame");jmi2.addActionListener(this);jmi2.setActionCommand("Exit");jmi3.addActionListener(this);jmi3.setActionCommand("Stop");jmi4.addActionListener(this);jmi4.setActionCommand("Continue");jmi5.addActionListener(this);jmi5.setActionCommand("help");jmi6.addActionListener(this);jmi6.setActionCommand("level1");jmi7.addActionListener(this);jmi7.setActionCommand("level2");jmi8.addActionListener(this);jmi8.setActionCommand("level3");jmi9.addActionListener(this);jmi9.setActionCommand("level4");this.setMenuBar(jmb);// 菜单Bar放到JFrame上this.setVisible(true);for (int i = 0; i < 10; i ) { // 家的格局if (i < 4)homeWall.add(new CommonWall(350, 580 - 21 * i, this));else if (i < 7)homeWall.add(new CommonWall(372 22 * (i - 4), 517, this));elsehomeWall.add(new CommonWall(416, 538 (i - 7) * 21, this));}for (int i = 0; i < 32; i ) {if (i < 16) {otherWall.add(new CommonWall(220 20 * i, 300, this)); // 普通墙布局otherWall.add(new CommonWall(500 20 * i, 180, this));otherWall.add(new CommonWall(200, 400 20 * i, this));otherWall.add(new CommonWall(500, 400 20 * i, this));} else if (i < 32) {otherWall.add(new CommonWall(220 20 * (i - 16), 320, this));otherWall.add(new CommonWall(500 20 * (i - 16), 220, this));otherWall.add(new CommonWall(220, 400 20 * (i - 16), this));otherWall.add(new CommonWall(520, 400 20 * (i - 16), this));}}for (int i = 0; i < 20; i ) { // 金属墙布局if (i < 10) {metalWall.add(new MetalWall(140 30 * i, 150, this));metalWall.add(new MetalWall(600, 400 20 * (i), this));} else if (i < 20)metalWall.add(new MetalWall(140 30 * (i - 10), 180, this));elsemetalWall.add(new MetalWall(500 30 * (i - 10), 160, this));}for (int i = 0; i < 4; i ) { // 树的布局if (i < 4) {trees.add(new Tree(0 30 * i, 360, this));trees.add(new Tree(220 30 * i, 360, this));trees.add(new Tree(440 30 * i, 360, this));trees.add(new Tree(660 30 * i, 360, this));}}theRiver.add(new River(85, 100, this));for (int i = 0; i < 20; i ) { // 初始化20辆坦克if (i < 9) // 设置坦克出现的位置tanks.add(new Tank(150 70 * i, 40, false, Direction.D, this));else if (i < 15)tanks.add(new Tank(700, 140 50 * (i - 6), false, Direction.D,this));elsetanks.add(new Tank(10, 50 * (i - 12), false, Direction.D,this));}this.setSize(Fram_width, Fram_length); // 设置界面大小this.setLocation(280, 50); // 设置界面出现的位置this.setTitle("坦克大战――(重新开始:R键 开火:F键) 姓名:高胜立 班级:09级网络2班 学号:3109006516");this.addWindowListener(new WindowAdapter() { // 窗口监听关闭public void windowClosing(WindowEvent e) {System.exit(0);}});this.setResizable(false);this.setBackground(Color.GREEN);this.setVisible(true);this.addKeyListener(new KeyMonitor());// 键盘监听new Thread(new PaintThread()).start(); // 线程启动}public static void main(String[] args) {new TankClient(); // 实例化}private class PaintThread implements Runnable {public void run() {// TODO Auto-generated method stubwhile (printable) {repaint();try {Thread.sleep(50);} catch (InterruptedException e) {e.printStackTrace();}}}}private class KeyMonitor extends KeyAdapter {public void keyReleased(KeyEvent e) { // 监听键盘释放homeTank.keyReleased(e);}public void keyPressed(KeyEvent e) { // 监听键盘按下homeTank.keyPressed(e);}}public void actionPerformed(ActionEvent e) {if (e.getActionCommand().equals("NewGame")) {printable = false;Object[] options = { "确定", "取消" };int response = JOptionPane.showOptionDialog(this, "您确认要开始新游戏!", "",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);if (response == 0) {printable = true;this.dispose();new TankClient();} else {printable = true;new Thread(new PaintThread()).start(); // 线程启动}} else if (e.getActionCommand().endsWith("Stop")) {printable = false;// try {// Thread.sleep(10000);//// } catch (InterruptedException e1) {// // TODO Auto-generated catch block// e1.printStackTrace();// }} else if (e.getActionCommand().equals("Continue")) {if (!printable) {printable = true;new Thread(new PaintThread()).start(); // 线程启动}// System.out.println("继续");} else if (e.getActionCommand().equals("Exit")) {printable = false;Object[] options = { "确定", "取消" };int response = JOptionPane.showOptionDialog(this, "您确认要退出吗", "",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);if (response == 0) {System.out.println("退出");System.exit(0);} else {printable = true;new Thread(new PaintThread()).start(); // 线程启动}} else if (e.getActionCommand().equals("help")) {printable = false;JOptionPane.showMessageDialog(null, "用→ ← ↑ ↓控制方向,F键盘发射,R重新开始!","提示!", JOptionPane.INFORMATION_MESSAGE);this.setVisible(true);printable = true;new Thread(new PaintThread()).start(); // 线程启动} else if (e.getActionCommand().equals("level1")) {Tank.count = 12;Tank.speedX = 6;Tank.speedY = 6;Bullets.speedX = 10;Bullets.speedY = 10;this.dispose();new TankClient();} else if (e.getActionCommand().equals("level2")) {Tank.count = 12;Tank.speedX = 10;Tank.speedY = 10;Bullets.speedX = 12;Bullets.speedY = 12;this.dispose();new TankClient();} else if (e.getActionCommand().equals("level3")) {Tank.count = 20;Tank.speedX = 14;Tank.speedY = 14;Bullets.speedX = 16;Bullets.speedY = 16;this.dispose();new TankClient();} else if (e.getActionCommand().equals("level4")) {Tank.count = 20;Tank.speedX = 16;Tank.speedY = 16;Bullets.speedX = 18;Bullets.speedY = 18;this.dispose();new TankClient();}}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复