Java版坦克大战游戏源码示例
程序员文章站
2024-02-22 15:47:18
整理文档,搜刮出一个java版坦克大战游戏的代码,稍微整理精简一下做下分享。
package tankwar;
import java.awt.colo...
整理文档,搜刮出一个java版坦克大战游戏的代码,稍微整理精简一下做下分享。
package tankwar; import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.io.file; import java.io.fileinputstream; import java.util.arraylist; import java.util.random; import javax.swing.borderfactory; import javax.swing.imageicon; import javax.swing.jframe; import javax.swing.jpanel; import tankwar.tank.direction; import barrier.enemyborn; import barrier.gold; import barrier.home; import barrier.iron; import barrier.selfborn; import barrier.wall; /** * java学习交流qq群:589809992 我们一起学java! */ public class tankwar implements keylistener { static boolean timeout = false; private jframe f; private jpanel gamepanel; private panelshow messgepanel; private mypanel p; private tank mytank; public static final int area_width = 830; public static final int area_height = 800; private arraylist<missle> missles = new arraylist<missle>(); private arraylist<tank> alltanks = new arraylist<tank>(); private arraylist<boom> booms = new arraylist<boom>(); private arraylist<wall> walls = new arraylist<wall>(); private arraylist<iron> irons = new arraylist<iron>(); private arraylist<gold> golds = new arraylist<gold>(); private arraylist<item> items = new arraylist<item>(); private arraylist<enemyborn> enemyborns = new arraylist<enemyborn>(); private selfborn selfborn; private home home; private tank enemytank; private random r; private imageicon background; private final string map; private int tankmax; private boolean over = false; private static int selfmax = 3; private boolean win; private boolean flash = false; private tankwar tw = this; static int score = 0; private final jframe mainf; private int style; public tankwar(string map, int tankmax, jframe mainf, int style) throws exception { this.map = map; this.tankmax = tankmax; this.mainf = mainf; this.style = style; init(); } private void init() { f = new jframe("坦克大战 v3.0"); gamepanel = new jpanel(null); p = new mypanel(); p.setbackground(color.white); r = new random(); messgepanel = new panelshow(); initmap(new file("map/" + map)); try { mytank = new tank(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 = new imageicon( 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); new thread(new runnable() { public void run() { 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 (int i = 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 = new item(alltanks, booms, irons, home); items.add(item); item.start(); } try { thread.sleep(30); } catch (interruptedexception e) { } } over(); } }).start(); } private class mypanel extends jpanel { /** * */ private static final long serialversionuid = 4408440723797225328l; @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawimage(background.getimage(), 0, 0, null); for (int j = 0; j < walls.size(); j++) { walls.get(j).draw(g); } for (int j = 0; j < irons.size(); j++) { irons.get(j).draw(g); } for (int j = 0; j < golds.size(); j++) { golds.get(j).draw(g); } for (int j = 0; j < enemyborns.size(); j++) { enemyborns.get(j).draw(g); } home.draw(g); selfborn.draw(g); for (int j = 0; j < alltanks.size(); j++) { alltanks.get(j).drawtank(g); } for (int j = 0; j < irons.size(); j++) { irons.get(j).draw(g); } for (int i = 0; i < missles.size(); i++) { missles.get(i).drawmissle(g); if (!missles.get(i).islive()) missles.remove(i); } for (int i = 0; i < booms.size(); i++) { if (booms.get(i).islive()) booms.get(i).drawboom(g); else booms.remove(i); } for (int j = 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(); } } @override public void keytyped(keyevent e) { // todo auto-generated method stub } @override public void keypressed(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); } } else if (e.getkeycode() == keyevent.vk_space) { mytank.fire(); } else { mytank.keypress(e); } } @override public void keyreleased(keyevent e) { mytank.keyreleased(e); } public void ai(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; } else if (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; } else if (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; } else if (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); } else if (r.nextint(300) == 1) { tank.setup(true); tank.setleft(false); tank.setdown(false); tank.setright(false); tank.setdir(direction.u); } else if (r.nextint(300) == 2) { tank.setup(false); tank.setleft(true); tank.setdown(false); tank.setright(false); tank.setdir(direction.l); } else if (r.nextint(300) == 3) { tank.setup(false); tank.setleft(false); tank.setdown(true); tank.setright(false); tank.setdir(direction.d); } else if (r.nextint(300) == 4) { tank.setup(false); tank.setleft(false); tank.setdown(false); tank.setright(true); tank.setdir(direction.r); } } public void initmap(file file) { try { fileinputstream read = new fileinputstream(file); for (int i = 0; i < area_height / 50; i++) { for (int j = 0; j < area_width / 50; j++) { switch (read.read()) { case 1: walls.add(new wall(j * 50, i * 50)); break; case 2: irons.add(new iron(j * 50, i * 50)); break; case 3: golds.add(new gold(j * 50, i * 50)); break; case 4: selfborn = new selfborn(j * 50, i * 50); break; case 5: enemyborns.add(new enemyborn(j * 50, i * 50)); break; case 6: home = new home(j * 50, i * 50); break; } } } read.close(); } catch (exception e) { } ; } public void addtank() { if (tankmax <= 0) return; for (int i = alltanks.size(); i < enemyborns.size() + 1; i++) { try { int temp = r.nextint(enemyborns.size()); enemytank = new tank(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; } } public static void addmytanklives() { selfmax++; } private void over() { for (int i = 0; i < area_height / 50; i++) { for (int j = 0; j < area_width / 50; j++) { irons.add(new iron(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); } } private void drawover(graphics g) { p.repaint(); g.setcolor(color.red); g.setfont(new font("arial", 1, 100)); g.drawstring("game over", 100, 200); g.setfont(new font("arial", 2, 50)); if (win) g.drawstring("congratulation! you win!", 100, 400); else g.drawstring("so sorry, you lose!", 100, 400); if (flash) { g.setfont(new font("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); } } }
代码运行效果截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。