首页 / 互联网 / 正文
Java版坦克大战游戏源码
shanji321 发表于:2019-9-3 15:13:48 复制链接 看图 发表新帖
阅读数:9202

下载APP可以快速和圈友联系

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
packagetankwar; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.event.KeyEvent; importjava.awt.event.KeyListener; importjava.io.File; importjava.io.FileInputStream; importjava.util.ArrayList; importjava.util.Random; importjavax.swing.BorderFactory; importjavax.swing.ImageIcon; importjavax.swing.JFrame; importjavax.swing.JPanel; importtankwar.Tank.Direction; importbarrier.EnemyBorn; importbarrier.Gold; importbarrier.Home; importbarrier.Iron; importbarrier.SelfBorn; importbarrier.Wall; /** * Java进修交换QQ群:589809992 我们一路学Java! */publicclassTankWarimplementsKeyListener{staticbooleanTIMEOUT = false; privateJFrame f; privateJPanel gamePanel; privatePanelShow messgePanel; privatemyPanel p; privateTank myTank; publicstaticfinalintAREA_WIDTH = 830; publicstaticfinalintAREA_HEIGHT = 800; privateArrayList<Missle> missles = newArrayList<Missle>(); privateArrayList<Tank> allTanks = newArrayList<Tank>(); privateArrayList<Boom> booms = newArrayList<Boom>(); privateArrayList<Wall> walls = newArrayList<Wall>(); privateArrayList<Iron> irons = newArrayList<Iron>(); privateArrayList<Gold> golds = newArrayList<Gold>(); privateArrayList<Item> items = newArrayList<Item>(); privateArrayList<EnemyBorn> enemyBorns = newArrayList<EnemyBorn>(); privateSelfBorn selfBorn; privateHome home; privateTank enemyTank; privateRandom r; privateImageIcon backGround; privatefinalString map; privateinttankMax; privatebooleanover = false; privatestaticintselfMax = 3; privatebooleanwin; privatebooleanflash = false; privateTankWar tw = this; staticintSCORE = 0; privatefinalJFrame mainF; privateintstyle; publicTankWar(String map, inttankMax, JFrame mainF, intstyle) throwsException { this.map = map; this.tankMax = tankMax; this.mainF = mainF; this.style = style; init(); } privatevoidinit() { f = newJFrame( "坦克大战 V3.0"); gamePanel = newJPanel( null); p = newmyPanel(); p.setBackground(Color.WHITE); r = newRandom(); messgePanel = newPanelShow(); initMap( newFile( "map/"+ map)); try{ myTank = newTank(selfBorn.getX(), selfBorn.getY(), true, allTanks, walls, irons, golds, missles, home, booms, style); } catch(Exception e1) { } myTank.setDir(Direction.U); allTanks.add(myTank); addTank(); try{ backGround = newImageIcon( TankWar.class.getResource( "/pic/whiteback.jpg")); } catch(Exception e) { } p.setBorder(BorderFactory.createEtchedBorder(Color.BLACK, Color.WHITE)); p.setSize(AREA_WIDTH, AREA_HEIGHT); messgePanel.setBounds(AREA_WIDTH, 0, 200, AREA_HEIGHT); gamePanel.add(messgePanel); gamePanel.add(p); f.add(gamePanel); f.setBounds( 0, 0, AREA_WIDTH + 200, AREA_HEIGHT); f.setDefaultCloseOperation( 3); f.setResizable( true); f.setFocusable( true); f.addKeyListener( this); f.setVisible( true); newThread( newRunnable() { publicvoidrun() { while(!over) { if(!myTank.isLive()) { selfMax--; if(selfMax < 0) { f.removeKeyListener(tw); over = true; win = false; break; } else{ myTank.setLevel( 1); myTank.setX(selfBorn.getX()); myTank.setY(selfBorn.getY()); myTank.setDir(Direction.U); myTank.setHp( 50); myTank.setLive( true); } } if(tankMax <= 0&& allTanks.size() == 1) { f.removeKeyListener(tw); over = true; win = true; } if(!home.isLive()) { f.removeKeyListener(tw); over = true; win = false; } p.repaint(); myTank.move(); for( inti = 1; i < allTanks.size(); i++) { allTanks.get(i).move(); allTanks.get(i).setNoFire(myTank.getNoFire() + 1); // if(allTanks.get(i).getX()%5==0&&allTanks.get(i).getY()%5==0)aI(allTanks.get(i)); } if(allTanks.size() <= enemyBorns.size() + 1) addTank(); myTank.setNoFire(myTank.getNoFire() + 1); messgePanel.setEnemyCount(tankMax); messgePanel.setSelfCount(selfMax); messgePanel.setScore(SCORE); if(SCORE % 500== 0) { SCORE += 100; Item item = newItem(allTanks, booms, irons, home); items.add(item); item.start(); } try{ Thread.sleep( 30); } catch(InterruptedException e) { } } over(); } }).start(); } privateclassmyPanelextendsJPanel{/** * */privatestaticfinallongserialVersionUID = 4408440723797225328L; @OverridepublicvoidpaintComponent(Graphics g) { super.paintComponent(g); g.drawImage(backGround.getImage(), 0, 0, null); for( intj = 0; j < walls.size(); j++) { walls.get(j).draw(g); } for( intj = 0; j < irons.size(); j++) { irons.get(j).draw(g); } for( intj = 0; j < golds.size(); j++) { golds.get(j).draw(g); } for( intj = 0; j < enemyBorns.size(); j++) { enemyBorns.get(j).draw(g); } home.draw(g); selfBorn.draw(g); for( intj = 0; j < allTanks.size(); j++) { allTanks.get(j).drawTank(g); } for( intj = 0; j < irons.size(); j++) { irons.get(j).draw(g); } for( inti = 0; i < missles.size(); i++) { missles.get(i).drawMissle(g); if(!missles.get(i).isLive()) missles.remove(i); } for( inti = 0; i < booms.size(); i++) { if(booms.get(i).isLive()) booms.get(i).drawBoom(g); elsebooms.remove(i); } for( intj = 0; j < items.size(); j++) { if(!items.get(j).isLive()) { items.remove(j); continue; } items.get(j).draw(g); } if(over) drawOver(g); messgePanel.repaint(); } } @OverridepublicvoidkeyTyped(KeyEvent e) { // TODO Auto-generated method stub} @OverridepublicvoidkeyPressed(KeyEvent e) { if(over) { if(e.getKeyCode() == KeyEvent.VK_F1) { over = false; missles.clear(); allTanks.clear(); booms.clear(); walls.clear(); irons.clear(); golds.clear(); enemyBorns.clear(); try{ init(); } catch(Exception e1) { } } else{ f.setVisible( false); mainF.setSize( 800, 800); mainF.setVisible( true); } } elseif(e.getKeyCode() == KeyEvent.VK_SPACE) { myTank.fire(); } else{ myTank.keyPress(e); } } @OverridepublicvoidkeyReleased(KeyEvent e) { myTank.keyReleased(e); } publicvoidaI(Tank tank) { if(TIMEOUT) { tank.setUp( false); tank.setLeft( false); tank.setDown( false); tank.setRight( false); return; } if(r.nextInt( 40) == 0) tank.fire(); if(r.nextInt( 10) == 0) { if(tank.getX() >= myTank.getX() && tank.getX() <= myTank.getX() + Tank.SIZE && tank.getY() > myTank.getY()) { tank.setUp( true); tank.setLeft( false); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.U); return; } elseif(tank.getX() >= myTank.getX() && tank.getX() <= myTank.getX() + Tank.SIZE && tank.getY() < myTank.getY()) { tank.setUp( false); tank.setLeft( false); tank.setDown( true); tank.setRight( false); tank.setDir(Direction.D); return; } elseif(tank.getX() > myTank.getX() && tank.getY() >= myTank.getY() && tank.getY() <= myTank.getY() + Tank.SIZE) { tank.setUp( false); tank.setLeft( true); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.L); return; } elseif(tank.getX() < myTank.getX() && tank.getY() >= myTank.getY() && tank.getY() <= myTank.getY() + Tank.SIZE) { tank.setUp( false); tank.setLeft( false); tank.setDown( false); tank.setRight( true); tank.setDir(Direction.R); return; } } if(tank.getX() <= 0) { tank.setUp( false); tank.setLeft( false); tank.setDown( false); tank.setRight( true); tank.setDir(Direction.R); } if(tank.getY() <= 0) { tank.setUp( false); tank.setLeft( false); tank.setDown( true); tank.setRight( false); tank.setDir(Direction.D); } if(tank.getX() >= AREA_WIDTH - Tank.SIZE) { tank.setUp( false); tank.setLeft( true); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.L); } if(tank.getY() >= AREA_HEIGHT - Tank.SIZE) { tank.setUp( true); tank.setLeft( false); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.U); } elseif(r.nextInt( 300) == 1) { tank.setUp( true); tank.setLeft( false); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.U); } elseif(r.nextInt( 300) == 2) { tank.setUp( false); tank.setLeft( true); tank.setDown( false); tank.setRight( false); tank.setDir(Direction.L); } elseif(r.nextInt( 300) == 3) { tank.setUp( false); tank.setLeft( false); tank.setDown( true); tank.setRight( false); tank.setDir(Direction.D); } elseif(r.nextInt( 300) == 4) { tank.setUp( false); tank.setLeft( false); tank.setDown( false); tank.setRight( true); tank.setDir(Direction.R); } } publicvoidinitMap(File file) { try{ FileInputStream read = newFileInputStream(file); for( inti = 0; i < AREA_HEIGHT / 50; i++) { for( intj = 0; j < AREA_WIDTH / 50; j++) { switch(read.read()) { case1: walls.add( newWall(j * 50, i * 50)); break; case2: irons.add( newIron(j * 50, i * 50)); break; case3: golds.add( newGold(j * 50, i * 50)); break; case4: selfBorn = newSelfBorn(j * 50, i * 50); break; case5: enemyBorns.add( newEnemyBorn(j * 50, i * 50)); break; case6: home = newHome(j * 50, i * 50); break; } } } read.close(); } catch(Exception e) { } ; } publicvoidaddTank() { if(tankMax <= 0) return; for( inti = allTanks.size(); i < enemyBorns.size() + 1; i++) { try{ inttemp = r.nextInt(enemyBorns.size()); enemyTank = newTank(enemyBorns.get(temp).getX(), enemyBorns .get(temp).getY(), false, allTanks, walls, irons, golds, missles, home, booms, r.nextInt( 3) + 1); } catch(Exception e) { e.printStackTrace(); } enemyTank.setDir(Direction.D); enemyTank.setDown( true); allTanks.add(enemyTank); tankMax--; if(tankMax <= 0) return; } } publicstaticvoidaddMyTankLives() { selfMax++; } privatevoidover() { for( inti = 0; i < AREA_HEIGHT / 50; i++) { for( intj = 0; j < AREA_WIDTH / 50; j++) { irons.add( newIron(j * 50, i * 50)); p.repaint(); try{ Thread.sleep( 5); } catch(InterruptedException e) { } } } while( true) { flash = !flash; p.repaint(); try{ Thread.sleep( 1000); } catch(InterruptedException e) { } f.addKeyListener( this); } } privatevoiddrawOver(Graphics g) { p.repaint(); g.setColor(Color.red); g.setFont( newFont( "Arial", 1, 100)); g.drawString( "GAME OVER", 100, 200); g.setFont( newFont( "Arial", 2, 50)); if(win) g.drawString( "Congratulation! You Win!", 100, 400); elseg.drawString( "So Sorry, You Lose!", 100, 400); if(flash) { g.setFont( newFont( "Arial", 2, 30)); g.setColor(Color.BLACK); g.drawString( "Press F1 to try again...,", 150, 500); g.drawString( "Press the other Key to Return the Title...,", 150, 600); } } }代码运转结果截图

Java版坦克大战游戏源码-1.jpeg


上一篇:Java 12 都公布了,你能否是还在用JDK 8?
下一篇:Java 线程Executor 框架详解与利用
温馨提示:
下载好向圈客户端可以随时随地交流学习经验,也可以和圈友发起聊天成为好友
好向圈www.kuaixunai.com是一个专业经验分享交流平台,请提供优质的经验内容分享,拒绝任何广告内容出现,低质量广告内容硬广包含手机号码,微信,QQ或者二维码,网址等形式存在可能会审核不通过 要想被各大搜索引擎尽快收录请做好内容原创工作,才会有更好的推广效果。
返回列表
使用道具 举报
#color, #true, #direction, #event, #white
条评论
您需要登录后才可以回帖 登录 | 立即注册
相关推荐
广告合作以及侵权投诉客服QQ:1623331347 江苏好向圈信息科技有限公司 网站地图1 网站地图2