赞
踩
马哥的淘宝店:https://shop592330910.taobao.com/
观看马士兵老师讲课的坦克大战 视频的源代码。
学编程要亲自敲写代码,不要照抄代码,要跟着思路总结去写代码,就像你定义的类名,变量名等都可以不一样。
在这几个方法里面遍历一个集合的时候,遍历的同时还要删除某个元素,这里要特别注意写法,要用迭代器。其他的方法会报错的。
drawExplode(g);
drawEnemyTanks(g);
drawMissiles(g);
package tank.war.client.com;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
public class TankClient extends Frame {
private static final String title = "大战";
private Tank tank = new Tank(50, 50, true, this);
public ArrayList<Missile> missileList = new ArrayList<Missile>();
public ArrayList<Explode> explodeList = new ArrayList<Explode>();
public ArrayList<Tank> enemyTankList = new ArrayList<Tank>();
public ArrayList<Wall> wallList = new ArrayList<Wall>();
public Blood blood = new Blood();
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
private static final int x = 0;
private static final int y = 0;
private static final Color GAME_BACKGROUND = Color.lightGray;
private Image offScreenImage = null;
private void launchFrame() {
createEnemyTanks(50);
createWalls();
this.setLocation(x, y);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setBackground(GAME_BACKGROUND);
this.setTitle(title);
this.setResizable(false);
this.addWindowListener(new WindowsMonitor());
this.addKeyListener(new KeyMonitor());
setVisible(true);
new Thread(new PaintThread()).start();
}
private void createWalls() {
wallList.add(new Wall(100, 150, 20, 250, this));
wallList.add(new Wall(300, 400, 300, 20, this));
}
private void createEnemyTanks(int count) {
for (int i = 0; i <= count; i++) {
Tank t = new Tank(50 + 40 * (i + 1), new Random().nextInt(GAME_HEIGHT), false, this);
Iterator<Wall> wallIterator = wallList.iterator();
while (wallIterator.hasNext()) {
Wall wall = wallIterator.next();
if (!tank.getRectangle().intersects(wall.getRectangle())) {
t.setGood(false);
t.setDirection(Tank.Direction.D);
this.enemyTankList.add(t);
}
}
}
}
@Override
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(GAME_BACKGROUND);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
drawMainTank(g);
drawEnemyTanks(g);
drawMissiles(g);
drawExplode(g);
drawString(g);
drawWall(g);
drawBlood(g);
}
private void drawMainTank(Graphics g) {
tank.draw(g);
}
private void drawBlood(Graphics g) {
if (blood.isLive()) {
tank.eatBlood(blood);
blood.draw(g);
}
}
private void drawString(Graphics g) {
g.drawString("Missile size: " + this.missileList.size(), 10, 50);
g.drawString("Explode size: " + this.explodeList.size(), 10, 70);
g.drawString("Tank size: " + this.enemyTankList.size(), 10, 90);
g.drawString("life : " + this.tank.getLife(), 10, 110);
}
private void drawWall(Graphics g) {
Iterator<Wall> wallIterator = wallList.iterator();
while (wallIterator.hasNext()) {
Wall wall = wallIterator.next();
wall.draw(g);
}
}
private void drawExplode(Graphics g) {
Iterator<Explode> explodeIterator = explodeList.iterator();
while (explodeIterator.hasNext()) {
Explode explode = explodeIterator.next();
if (!explode.isLive()) {
explodeIterator.remove();
} else {
explode.draw(g);
}
}
}
private void drawMissiles(Graphics g) {
Iterator<Missile> missileIterator = missileList.iterator();
while (missileIterator.hasNext()) {
Missile missile = missileIterator.next();
if (!missile.isLive()) {
missileIterator.remove();
} else {
missile.hitTanks(enemyTankList);
missile.hitWalls(wallList);
missile.hitTank(tank);
missile.draw(g);
}
}
}
private void drawEnemyTanks(Graphics g) {
if (enemyTankList.size() <= 10) {
createEnemyTanks(20);
}
Iterator<Tank> tankIterator = enemyTankList.iterator();
while (tankIterator.hasNext()) {
Tank enemyTank = tankIterator.next();
if (!enemyTank.isLive()) {
tankIterator.remove();
} else {
enemyTank.hitWalls(wallList);
enemyTank.hitTanks(enemyTankList);
enemyTank.draw(g);
}
}
}
private class WindowsMonitor extends WindowAdapter {
@Override
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。