这是我学习JAVA一个多月以来,首次做的一个小项目,由于之前学过别的语言,所以做起来并不觉得困难。通过这个小项目,让我更深的了解了面向对象,同时也更深入的了解了一些新的知识点,稍作总结:
对象能够移动的本质是:改变对象的坐标,再对对象重画。由于重画的速度很快,我们的肉眼无法察觉,因此就以为是移动了。根据坦克的起始坐标,根据坦克的方向设一个变量相应的改变坦克的起始坐标。然后在通过重画,就达到了运动的效果。
其次在不断的重画的过程中,由于刷新的频率大于重画的频率,导致会闪烁,可以通过加入一个线程不断的刷新来消除闪烁问题,具体实现方法:
private class PaintThread implements Runnable{ public void run() { while(true){ repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }
到网上一查,发现还有其他方法,这个待研究。
还有通过构造子弹以及坦克,初步了解了List、ArrayList等的用法,当然此个项目也有一些不足之处,比如说我军子弹和敌军子弹、以及我军坦克和敌军坦克没有分开控制,这样子导致无法完成我军子弹碰到敌军子弹可以爆炸等问题。
知道怎么通过按键监听来实现按键控制操作,如使坦克和炮弹按八个方向跑,可以参看Tank.java和Missile.java里的内容。
···
总的来说,这个项目使我对JAVA的兴趣又增添几分,因为每做出一步,都会让我有点小成就感,然后是到最终完成,又想去做它的升级版···
效果如图:
下面贴出代码:
CSDN下载地址:http://download.csdn.net/detail/yanghai0321/3766202
说明:我军战死后按“F2”键可以重生一辆,按“Ctrl”键发射炮弹,按“A”键发射超级炮弹(可以向八个方向同时发射)!
主窗口:TankClient.java
package com.yanghai.tankwar;
import java.awt.*; import java.awt.event.*; import java.util.List; import java.util.ArrayList; /** * 这个类的作用是坦克游戏的主窗口 * @author Yanghai * */ public class TankClient extends Frame{ public static final int GAME_WIDTH=800; public static final int GAME_HEIGHT=600; Tank myTank=new Tank(750,550,true,Tank.Direction.STOP,this); Wall w1=new Wall(250,200,20,150,this); Wall w2=new Wall(500,300,150,20,this); Blood b=new Blood(); int level=1; int init=10; List<Missile> missiles=new ArrayList<Missile>(); List<Explode> explodes=new ArrayList<Explode>(); List<Tank> tanks=new ArrayList<Tank>(); Image offScreenImage =null;//设置一张背后的图片 public void paint(Graphics g) { g.drawString("missiles count:"+missiles.size(), 8, 45); g.drawString("explodes count:"+explodes.size(), 8, 65); g.drawString("tanks count:"+tanks.size(), 8, 85); g.drawString("myTank's life:"+myTank.getLife(), 8, 105); g.drawString("Fighting level:"+level, 700, 65); if(tanks.size()<=0){ init++; for(int i=0;i<init;i++){ tanks.add(new Tank(30*(i+1),3*(i+20)+5,false,Tank.Direction.D,this)); } level++; myTank.setLife(myTank.HP); } for(int i=0;i<missiles.size();i++){ Missile m=missiles.get(i); m.hitTank(myTank); m.hitTanks(tanks); m.hitWall(w1); m.hitWall(w2); m.hitMissiles(missiles); //if(!m.isLive())missiles.remove(m); //else m.draw(g); m.draw(g); } for(int i=0;i<explodes.size();i++){ Explode e=explodes.get(i); e.draw(g); } for(int i=0;i<tanks.size();i++){ Tank t=tanks.get(i); t.collidesWithWall(w1); t.collidesWithWall(w2); t.collidesWithTanks(tanks); t.draw(g); } myTank.collidesWithTanks(tanks); //myTank.collidesWithWall(w1); //myTank.collidesWithWall(w2); myTank.eat(b); myTank.draw(g); w1.draw(g); w2.draw(g); b.draw(g); } public void update(Graphics g) {//屏幕的画笔 if(offScreenImage==null){ offScreenImage=this.createImage(GAME_WIDTH, GAME_HEIGHT);//设置一张图片 } Graphics gOffScreenImage=offScreenImage.getGraphics();//得到paint画笔,背后图片的画笔 Color c=gOffScreenImage.getColor(); gOffScreenImage.setColor(Color.GREEN); gOffScreenImage.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT); gOffScreenImage.setColor(c); paint(gOffScreenImage);//调用paint()方法,用画笔在后面的图片上画一遍 g.drawImage(offScreenImage, 0, 0, null);//把图片画到屏幕上来 } /** * 本方法显示坦克主窗口 */ public void lauchFrame(){ this.setTitle("TankWar"); this.setLocation(200, 120); this.setSize(GAME_WIDTH,GAME_HEIGHT); for(int i=0;i<init;i++){ tanks.add(new Tank(16*(i*5),40*(i+5),false,Tank.Direction.D,this)); } this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); this.setResizable(false); this.setBackground(Color.GREEN); setVisible(true); new Thread(new PaintThread()).start(); this.addKeyListener(new KeyMonitor()); } public static void main(String[] args) { TankClient tc=new TankClient(); tc.lauchFrame(); } private class PaintThread implements Runnable{ public void run() { while(true){ repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } private class KeyMonitor extends KeyAdapter{ public void keyReleased(KeyEvent e) { myTank.keyReleased(e); } public void keyPressed(KeyEvent e) { myTank.keyPressed(e); } } }
坦克类:Tank.java
package com.yanghai.tankwar; import java.awt.*; import java.awt.event.*; import java.util.*; public class Tank { int x, y; int oldX, oldY; int myTanks = 0; private int life = HP; public static final int HP = 100; public int getLife() { return life; } private BloodBar bb = new BloodBar(); public void setLife(int life) { this.life = life; } private static Random r = new Random();// 设置成静态的,可以被多个对象调用 int step = r.nextInt(12) + 3; public static final int XSPEED = 8; public static final int YSPEED = 8; public static final int WIDTH = 30; public static final int HEIGHT = 30; private boolean bL = false, bU = false, bR = false, bD = false; enum Direction { L, LU, U, RU, R, RD, D, LD, STOP }; private Direction dir = Direction.STOP; private Direction ptDir = Direction.D; TankClient tc; private boolean good; private boolean live = true;// 设置Tank的生死量 public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } public Tank(int x, int y, boolean good) { this.x = x; this.y = y; this.good = good; } public Tank(int x, int y, boolean good, Direction dir, TankClient tc) { this(x, y, good); this.dir = dir; this.tc = tc; } private class BloodBar { public void draw(Graphics g) { Color c = g.getColor(); g.drawRect(x, y - 10, WIDTH, 7); g.setColor(Color.RED); int w = WIDTH * life / 100; g.fillRect(x, y - 10, w, 7); g.setColor(c); } } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } void move() { oldX = x; oldY = y; switch (dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: x += XSPEED; y -= YSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: x -= XSPEED; y += YSPEED; break; case STOP: break; } if (this.dir != Direction.STOP) { this.ptDir = this.dir; } if (x < 0) x = 0; if (y < 20) y = 20; if (x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH; if (y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT; if (!good) { Direction[] dirs = Direction.values(); if (step == 0) {// 前几步让坦克随原来的方向走,然后再随机产生方向(循环) step = r.nextInt(15) + 4; int rn = r.nextInt(dirs.length); dir = dirs[rn]; } step--;// 每画一次,随机产生的要走方向的次数减,这样才能让下一个随机数产生,从而有不同的方向 if (r.nextInt(40) > 37) this.fire(); } } public boolean isGood() { return good; } public void draw(Graphics g) { if (!live) { if (!good) { tc.tanks.remove(this); } return; } Color c = g.getColor(); if (good) g.setColor(Color.RED); else g.setColor(Color.BLUE); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); if (isGood()) bb.draw(g); switch (ptDir) { case L: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2); break; case LU: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y); break; case U: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y); break; case RU: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + WIDTH, y); break; case R: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT / 2); break; case RD: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT); break; case D: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT); break; case LD: g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT); break; } move(); } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_F2: if (!this.isLive()) { myTanks++; if (myTanks < 10) { this.setLive(true); this.setLife(HP); } } break; case KeyEvent.VK_LEFT: bL = true; break; case KeyEvent.VK_UP: bU = true; break; case KeyEvent.VK_RIGHT: bR = true; break; case KeyEvent.VK_DOWN: bD = true; break; } locateDirection(); } public Missile fire() { if (!live) return null; int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2; int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2; Missile m = new Missile(x, y, ptDir, good, this.tc); tc.missiles.add(m); return m; } public Missile fire(Direction dir) { if (!live) return null; int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2; int y = this.y + Tank.HEIGHT / 2 - Missile.HEIGHT / 2; Missile m = new Missile(x, y, dir, good, this.tc); tc.missiles.add(m); return m; } public void keyReleased(KeyEvent e) {// 若没有此段处理代码,则在keyPressed方法后,最先按的两次总会是true int key = e.getKeyCode(); switch (key) { case KeyEvent.VK_CONTROL: fire(); break; case KeyEvent.VK_LEFT: bL = false; break; case KeyEvent.VK_UP: bU = false; break; case KeyEvent.VK_RIGHT: bR = false; break; case KeyEvent.VK_DOWN: bD = false; break; case KeyEvent.VK_A: superFire(); break; } locateDirection(); } void locateDirection() { if (bL && !bU && !bR && !bD) dir = Direction.L; else if (bL && bU && !bR && !bD) dir = Direction.LU; else if (!bL && bU && !bR && !bD) dir = Direction.U; else if (!bL && bU && bR && !bD) dir = Direction.RU; else if (!bL && !bU && bR && !bD) dir = Direction.R; else if (!bL && !bU && bR && bD) dir = Direction.RD; else if (!bL && !bU && !bR && bD) dir = Direction.D; else if (bL && !bU && !bR && bD) dir = Direction.LD; else if (!bL && !bU && !bR && !bD) dir = Direction.STOP; } private void stay() { x = oldX; y = oldY; } /** * 撞墙 * * @param w被撞的墙 * @return 撞上了返回true,否则false */ public boolean collidesWithWall(Wall w) { if (this.live && this.getRect().intersects(w.getRect())) { this.stay(); return true; } return false; } public boolean collidesWithTanks(java.util.List<Tank> tanks) { for (int i = 0; i < tanks.size(); i++) { Tank t = tanks.get(i); if (this != t) { if (this.live && this.getRect().intersects(t.getRect())) { this.stay(); t.stay(); return true; } } } return false; } public boolean eat(Blood b) { if (this.live && b.isLive() && this.getRect().intersects(b.getRect())) { this.life = HP; b.setLive(false); return true; } return false; } public void superFire() { for (int i = 0; i < 8; i++) { Direction[] dirs = Direction.values(); dir = dirs[i]; fire(dir); } } }
子弹类:
Missile.java
package com.yanghai.tankwar; import java.awt.*; import java.util.List; public class Missile { int x, y; public static final int XSPEED = 12; public static final int YSPEED = 12; public static final int WIDTH = 10; public static final int HEIGHT = 10; Tank.Direction dir; TankClient tc; private boolean Live = true;// 设置子弹的生死量 private boolean good = true; public boolean isLive() { return Live; } public void draw(Graphics g) { if (!Live) { tc.missiles.remove(this); return; } Color c = g.getColor(); g.setColor(Color.BLACK); g.fillOval(x, y, WIDTH, HEIGHT); g.setColor(c); move(); } void move() { switch (dir) { case L: x -= XSPEED; break; case LU: x -= XSPEED; y -= YSPEED; break; case U: y -= YSPEED; break; case RU: x += XSPEED; y -= YSPEED; break; case R: x += XSPEED; break; case RD: x += XSPEED; y += YSPEED; break; case D: y += YSPEED; break; case LD: x -= XSPEED; y += YSPEED; break; } if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT) { Live = false; } } public Missile(int x, int y, Tank.Direction dir) { this.x = x; this.y = y; this.dir = dir; } public Missile(int x, int y, Tank.Direction dir, boolean good, TankClient tc) { this(x, y, dir); this.good = good; this.tc = tc; } public Rectangle getRect() { return new Rectangle(x, y, WIDTH, HEIGHT); } public boolean hitTank(Tank t) { if (this.Live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood()) { if (t.isGood()) { t.setLife(t.getLife() - 20); if (t.getLife() <= 0) t.setLive(false); } else { t.setLive(false); } this.Live = false; Explode e = new Explode(x, y, tc); tc.explodes.add(e); return true; } return false; } public boolean hitWall(Wall w) { if (this.Live && this.getRect().intersects(w.getRect())) { this.Live = false; return true; } return false; } public boolean hitTanks(List<Tank> tanks) { for (int i = 0; i < tanks.size(); i++) { if (this.hitTank(tanks.get(i))) { return true; } } return false; } public boolean hitMissiles(List<Missile> missiles) { for (int i = 0; i < missiles.size(); i++) { Missile m = missiles.get(i); if (this != m) { // 修改的地方,这里的判断出现了问题, // 问题出现在我方的坦克发出炮弹是从 // 一个地方打出的,子弹会重叠,所以偶 // 数个的子弹会相互抵消,奇数个的子弹 // 会有一发子弹出来,加上判断条件(不是我方子弹 !this.ggod)即可, // 此外,还要判断敌方的子弹是否重合,再加一个判断条件(敌方 this.good == m.good)。 if (this.Live && m.Live && this.getRect().intersects(m.getRect()) && !this.good && (this.good == m.good)) { this.Live = false; m.Live = false; return true; } } } return false; } }
爆炸类:Explode.java
package com.yanghai.tankwar; import java.awt.*; public class Explode { int x,y; private boolean live=true; int[] diameter={5,12,26,34,56,30,17,6}; int step=0; TankClient tc; public Explode(int x,int y,TankClient tc){ this.x=x; this.y=y; this.tc=tc; } public void draw(Graphics g){ if(!live){ tc.explodes.remove(this); return; } if(step==diameter.length) { live=false; step=0; return; } Color c=g.getColor(); g.setColor(Color.ORANGE); g.fillOval(x, y, diameter[step], diameter[step]); g.setColor(c); step++; } }
血块类:Blood.java
package com.yanghai.tankwar; import java.awt.*; public class Blood { int x,y,w,h; TankClient tc; private boolean live=true; public boolean isLive() { return live; } public void setLive(boolean live) { this.live = live; } int step=0; //指明血块运动的方向 int [][] pos={ {350,300},{360,300},{375,275},{400,200},{370,270},{360,290},{340,280},{350,300} }; public Blood(){ x=pos[0][0]; y=pos[0][1]; w=h=15; } public void draw(Graphics g){ if(!live)return; Color c=g.getColor(); g.setColor(Color.MAGENTA); g.fillRect(x, y, w, h); g.setColor(c); mov(); } private void mov() { step++; if(step==pos.length){ step=0; } x=pos[step][0]; y=pos[step][1]; } public Rectangle getRect(){ return new Rectangle(x,y,w,h); } }
墙类:Wall.java
package com.yanghai.tankwar; import java.awt.*; public class Wall { TankClient tc; int x,y,w,h; public Wall(int x, int y, int w, int h,TankClient tc) { this.x = x; this.y = y; this.w = w; this.h = h; this.tc=tc; } public void draw(Graphics g){ Color c=g.getColor(); g.setColor(Color.ORANGE); g.fillRect(x, y, w, h); g.setColor(c); } public Rectangle getRect(){ return new Rectangle(x,y,w,h); } }
嘿嘿,各位写代码也有点累了吧,特别是深夜的时候是不是觉得应该有点吃的就更好了?可以到本人开的小店“小灰灰休闲美食”店买点休闲食品哦,既满足食欲又可以放松哈子,当然平时也要吃是吧,也可以买来送朋友哦!