坦克⼤战源代码java_Java版坦克⼤战游戏源码⽰例整理⽂档,搜刮出⼀个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 missles = new ArrayList();
private ArrayList allTanks = new ArrayList();
private ArrayList booms = new ArrayList();
private ArrayList walls = new ArrayList();
private ArrayList irons = new ArrayList();
private ArrayList golds = new ArrayList();
private ArrayList items = new ArrayList();
private ArrayList enemyBorns = new ArrayList();
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 X(), Y(), true, allTanks,
walls, irons, golds, missles, home, booms, style);
} catch (Exception e1) {
}
myTank.setDir(Direction.U);
allTanks.add(myTank);
addTank();
try {
backGround = new ImageIcon(
Resource("/pic/whiteback.jpg"));
} catch (Exception e) {
}
入门的java游戏小程序p.ateEtchedBorder(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) {
over = true;
win = false;
break;
} else {
myTank.setLevel(1);
myTank.X());
myTank.Y());
myTank.setDir(Direction.U);
myTank.setHp(50);
myTank.setLive(true);
}
}
if (tankMax <= 0 && allTanks.size() == 1) {
over = true;
win = true;
}
if (!home.isLive()) {
over = true;
win = false;
}
for (int i = 1; i < allTanks.size(); i++) {
<(i).move();
<(i).NoFire() + 1);
// (i).getX()%5==0&&(i).getY()%5==0) (i));
}
if (allTanks.size() <= enemyBorns.size() + 1)
addTank();
myTank.NoFire() + 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.Image(), 0, 0, null);
for (int j = 0; j < walls.size(); j++) {
<(j).draw(g);
}
for (int j = 0; j < irons.size(); j++) {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论