- 浏览: 117765 次
- 性别:
- 来自: 长沙
文章分类
最新评论
-
尘土飞扬:
cs6641468 写道同学, 建议还是看看官方文档。你肯定是 ...
Java使用poi读取Excel文件例子 -
qq_24665727:
cs6641468 写道同学, 建议还是看看官方文档。你肯定是 ...
Java使用poi读取Excel文件例子 -
cs6641468:
同学, 建议还是看看官方文档。你肯定是百度找了一些抄来抄去过时 ...
Java使用poi读取Excel文件例子 -
wangyunhom:
push方法返回的不是移除去的对象吧? 移除的应该是前面和现在 ...
LRU-最少使用页面置换算法 -
wangyunhom:
...
TCP实现聊天室
终于完成,修改了很多地方,大家如果需要可以看看,java基础知识练手的!
**********************************************
//线程主类
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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.List;
public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HIGHT = 600;
private static TankClient tc;
public static boolean p=true;
Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);
Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);
/**
* 怎么打多发炮弹,使用容器装炮弹,每当按下Ctr这个键的时候往容器中添加炮弹
* 画出每一个炮弹;
*/
List<Missile> missiles = new ArrayList<Missile>(); // 遍历比较快
// //LinkedList<>;//链表删除快,查找慢
// 画出爆炸
List<Explode> explodes = new ArrayList<Explode>();
private Image offScreenImage = null;
// 坏坦克创建
// 关数
int c = 1;
List<Tank> tanks = new ArrayList<Tank>();
// 重写
Blood b = new Blood();
/**
* //消除闪烁,使用双缓冲
//线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;
*/
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HIGHT);
}
// 拿到图片上的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.blue);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);// 画在背后图片上
g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上
}
/**
* // 画出还有多少炮弹,画一个字符串到屏幕
*/
public void paint(Graphics g) {
g.drawString("missiles count:" + missiles.size(), 30, 50);
g.drawString("Explode count:" + explodes.size(), 30, 70);
g.drawString("Tanks count:" + tanks.size(), 30, 90);
g.drawString("tanks life:" + myTank.getLife(), 30, 110);
g.drawString("关卡数: -" + c + "-", 30, 130);
g.drawString("p为暂停 ", 30, 150);
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.draw(g);
m.hitTank(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
}
for (int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
t.collideWithWall(w1);
t.collideWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
myTank.collideWithWall(w1);
myTank.collideWithWall(w2);
}
for (int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
myTank.draw(g);
w1.draw(g);
w2.draw(g);
myTank.eat(b);
if(myTank.getLife()<=40){
b.draw(g);
}
/**
* 敌人死完后再加入,C为关卡数,不同的关卡,坦克数量,以及出现的地方不同
*/
if (tanks.size() == 0) {
c++;
if (c > 10) {
for (int i = 0; i < 15; i++) {
tanks.add(new Tank((i) * 50, 50, false, Tank.Direction.D, tc));
}
} else if(c>5){
for (int j = 0; j < 10; j++) {
tanks.add(new Tank( 300,j*40+50, false, Tank.Direction.D, tc));
}
}else {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank((i) * 50, 50, false, Tank.Direction.D, tc));
}
}
}
}
/**
* 添加坦克,以及设置窗体
*/
public void lauchFrame() {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank((i + 1) * 50 + 100, 50, false, Tank.Direction.D, tc));
}
setTitle("TankWar");
setBounds(230, 100, 800, 600);
// 匿名内部类,短,不涉及将来的扩展
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setBackground(Color.GREEN);
setVisible(true);
addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
tc = new TankClient();
tc.lauchFrame();
}
/**
* // 内部类,只为这个TankWar服务,方便的访问包装类的方法,不方便公开,
* @author yan
*
*/
private class PaintThread implements Runnable {
public void run() {
while (true) {
//游戏暂停有关,线程不能关,关了以后的话,后面的方法都不会运行
if(p==true){
repaint();}
// 内部调用父类的paint方法;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* // 键盘事件,内部类
* @author yan
*
*/
private class KeyMoniter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}
*********************************************************
//血块类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
//血块
public class Blood {
int step=0;
int x,y,w,h;
Random ran=new Random();
private boolean live=true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
TankClient tc;
/**
* 初始化血块
*/
public Blood(){
w=h=20;
x=300;
y=300;
}
/**
* 用来设定下一次血块随机出现的地点,避免线程带来的闪烁,(不归线程来控制)
*/
public void setBlood(){
x=ran.nextInt(500)+50;
y=ran.nextInt(500)+50;
live=true;
}
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);
}
/**
* 血块的碰撞检测
* @return
*/
public Rectangle getRect(){
return new Rectangle(x,y,w,h);
}
}
*******************************************
//爆炸类
import java.awt.Color;
import java.awt.Graphics;
public class Explode {
int x,y;
private boolean live=true;
private TankClient tc;
public Explode(int x,int y,TankClient tc){
this.x=x;
this.y=y;
this.tc=tc;
}
int[] diameter={4,6,7,10,12,15,18,21,25,26,30,32,35,40,45,49,30,14,6};
int step;
public void draw(Graphics g){
if(step==diameter.length){
live=false;
tc.explodes.remove(this);//爆炸完成后移除此对象
step=0;
return;
}
if(!live)return;
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(x, y, diameter[step], diameter[step]);
step++;
g.setColor(c);
}
}
*********************************************************
//炮弹类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import Frame.Tank.Direction;
/**
* // 炮弹的生死状态,默认为生,出街,打倒敌人为死
* @author yan
*
*/
public class Missile {
private static final int xspeed = 8;
private static final int yspeed = 8;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private List<Tank> tanks;
private boolean Live = true;
private boolean good=true;
public boolean isLive() {
return Live;
}
int x, y;
Tank.Direction dir;
private TankClient tc;
public Missile(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x, int y, Direction dir,boolean good,TankClient tc) {
this(x, y, dir);
this.good=good;
this.tc = tc;
}
public void draw(Graphics g) {
if (!Live) {
tc.missiles.remove(this);
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
private 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 LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
}
if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HIGHT) {
Live = false;
tc.missiles.remove(this);
}
}
/**
* // 得到外围小方块,(碰撞检测辅助类Rectangle)
* @return
*/
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
/**
* // 添加爆炸对象到列表,射击类
* @param t
* @return
*/
public boolean hitTank(Tank t) {
if (this.Live&&this.getRect().intersects(t.getRect()) && t.isLive() == true&&this.good!=t.isGood()) {// 检测是否碰撞,碰撞之后,还得判断坦克是生还是死,否则子弹在那个地方还是会消失
if(t.isGood()){
t.setLife(t.getLife()-20);
if(t.getLife()<=0)
t.setLive(false);
}else{
t.setLive(false);
}
Explode e = new Explode(x, y, tc);
tc.explodes.add(e);
this.Live = false;
return true;
}
return false;
}
/**
* // 对一个数组的坦克进行射击类添加
* @param tanks
* @return
*/
public boolean hitTank(List<Tank> tanks) {
for (int i = 0; i < tanks.size(); i++) {
if (hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
/**
* 坦克碰撞到墙
* @param w
* @return
*/
public boolean hitWall(Wall w){
if(this.Live&&this.getRect().intersects(w.getRect())){
this.Live=false;
return true;
}
return false;
}
}
********************************************************
//坦克类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;
public class Tank {
public static final int xspeed = 5;
public static final int yspeed = 5;
public static final int WIDTH=30;
public static final int HEIGHT=30;
private BloodBar bb=new BloodBar();
TankClient tc=null;
private int life=100;
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
int x, y;
int oldx,oldy;
private boolean bL = false, bU = false, bR = false, bD = false;
private boolean good;//好的坏的
public boolean isGood() {
return good;
}
private boolean Live=true;//活的
private static Random random=new Random();
private int step=random.nextInt(12)+3;//计数,如果i=10,则让坏坦克改变方向
public boolean isLive() {
return Live;
}
public void setLive(boolean live) {
Live = live;
}
public enum Direction {
L, LU, U, RU, R, RD, LD, D, STOP
};// 枚举类型
private Direction dir = Direction.STOP;
//定义一个炮筒的方向;
private Direction ptDir=Direction.D;
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;
}
public void draw(Graphics g) {
if(good)bb.draw(g);
if(Live==false){
if(!good){
tc.tanks.remove(this);
}
return;
}
Color c = g.getColor();
if(good==true){
g.setColor(Color.cyan);
}else{
g.setColor(Color.yellow);
}
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
//根据坦克的方向画一条直线当作炮筒;
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+Tank.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 LD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT);
break;
case D:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT);
break;
}
move();
}
// 移动坦克
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 LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
case STOP:
break;
}
//坦克出界问题
if(x<=0)x=0;
if(y<=30)y=30;
if(x+Tank.HEIGHT>=TankClient.GAME_WIDTH)x=TankClient.GAME_WIDTH-Tank.WIDTH;
if(y+Tank.HEIGHT>=TankClient.GAME_HIGHT)y=TankClient.GAME_HIGHT-Tank.HEIGHT;
if(dir!=Direction.STOP){
ptDir=dir;
}
//给坏坦克自己随机运动
//如果i=10,则让坏坦克改变方向
if(!good){
step--;
if(step==0){
Direction[] dirs=Direction.values();
int r=random.nextInt(dirs.length);
dir=dirs[r];
step=random.nextInt(12)+3;
}
int r1=random.nextInt(40);
if(r1>35){
this.fire();
}
}
}
public void keyPressed(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
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;
case KeyEvent.VK_P://游戏暂停
if(tc.p==true){
tc.p=false;
}else{
tc.p=true;
}
break;
case KeyEvent.VK_S:
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.D;}
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.LD;}
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
private void stay(){
x=oldx;
y=oldy;
}
public void keyReleased(KeyEvent e) {
// 获得键的虚拟码
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:
this.superFire();
break;
case KeyEvent.VK_F2:
if(!this.Live){
this.life=100;
this.Live=true;
}
break;
case KeyEvent.VK_P:
//本来想加一个暂停,但是前面竟然用的内部类来使用线程,所以要大改。。。。
break;
}
locateDirection();
}
//开火
public Missile fire(){
if(!Live){return null;}
Missile m=new Missile(x+Tank.WIDTH/2-Missile.WIDTH/2,y+Tank.HEIGHT/2-Missile.HEIGHT/2,ptDir,good,tc);//传递坦克的位置和方向
tc.missiles.add(m);
return m;
}
public Missile fire(Direction dir){
if(!Live){return null;}
Missile m=new Missile(x+Tank.WIDTH/2-Missile.WIDTH/2,y+Tank.HEIGHT/2-Missile.HEIGHT/2,dir,good,tc);//传递坦克的位置和方向
tc.missiles.add(m);
return m;
}
////得到外围小方块,(碰撞检测辅助类Rectangle)
public Rectangle getRect(){
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean collidesWithTanks(List<Tank>tanks){
for(int i=0;i<tanks.size();i++){
Tank t=tanks.get(i);
if(this!=t){
if(this.Live&&t.isLive()&&this.getRect().intersects(t.getRect())){
this.stay();
t.stay();
return true;
}
}
}
return false;
}
public boolean collideWithWall(Wall w){
if(this.Live&&this.getRect().intersects(w.getRect())){
this.stay();
}
return false;
}
private void superFire(){
Direction[] dirs=Direction.values();
for (int i=0;i<8;i++){
fire(dirs[i]);
}
}
private class BloodBar{
public void draw (Graphics g){
Color c=g.getColor();
g.setColor(Color.red);
g.drawRect(x, y-12, WIDTH, 10);
int w=WIDTH*life/100;
g.fillRect(x, y-12, w, 10);
g.setColor(c);
}
}
public boolean eat (Blood b){
if(this.Live&&b.isLive()&&this.getRect().intersects(b.getRect())){
this.life=100;
b.setLive(false);
b.setBlood();
return true;
}
return false;
}
}
**************************************************************
//墙壁类
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* 墙壁类
* @author yan
*
*/
public class Wall {
int x,y,w,h;
TankClient tc;
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){
g.fillRect(x, y, w, h);
}
/**
* (碰撞检测辅助类Rectangle)
* @return
*/
public Rectangle getRect(){
return new Rectangle(x,y,w,h);
}
}
**********************************************
//线程主类
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
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.List;
public class TankClient extends Frame {
public static final int GAME_WIDTH = 800;
public static final int GAME_HIGHT = 600;
private static TankClient tc;
public static boolean p=true;
Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);
Tank myTank = new Tank(50, 50, true, Tank.Direction.STOP, this);
/**
* 怎么打多发炮弹,使用容器装炮弹,每当按下Ctr这个键的时候往容器中添加炮弹
* 画出每一个炮弹;
*/
List<Missile> missiles = new ArrayList<Missile>(); // 遍历比较快
// //LinkedList<>;//链表删除快,查找慢
// 画出爆炸
List<Explode> explodes = new ArrayList<Explode>();
private Image offScreenImage = null;
// 坏坦克创建
// 关数
int c = 1;
List<Tank> tanks = new ArrayList<Tank>();
// 重写
Blood b = new Blood();
/**
* //消除闪烁,使用双缓冲
//线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;
*/
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HIGHT);
}
// 拿到图片上的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.blue);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);// 画在背后图片上
g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上
}
/**
* // 画出还有多少炮弹,画一个字符串到屏幕
*/
public void paint(Graphics g) {
g.drawString("missiles count:" + missiles.size(), 30, 50);
g.drawString("Explode count:" + explodes.size(), 30, 70);
g.drawString("Tanks count:" + tanks.size(), 30, 90);
g.drawString("tanks life:" + myTank.getLife(), 30, 110);
g.drawString("关卡数: -" + c + "-", 30, 130);
g.drawString("p为暂停 ", 30, 150);
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.draw(g);
m.hitTank(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
}
for (int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
t.collideWithWall(w1);
t.collideWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
myTank.collideWithWall(w1);
myTank.collideWithWall(w2);
}
for (int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
myTank.draw(g);
w1.draw(g);
w2.draw(g);
myTank.eat(b);
if(myTank.getLife()<=40){
b.draw(g);
}
/**
* 敌人死完后再加入,C为关卡数,不同的关卡,坦克数量,以及出现的地方不同
*/
if (tanks.size() == 0) {
c++;
if (c > 10) {
for (int i = 0; i < 15; i++) {
tanks.add(new Tank((i) * 50, 50, false, Tank.Direction.D, tc));
}
} else if(c>5){
for (int j = 0; j < 10; j++) {
tanks.add(new Tank( 300,j*40+50, false, Tank.Direction.D, tc));
}
}else {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank((i) * 50, 50, false, Tank.Direction.D, tc));
}
}
}
}
/**
* 添加坦克,以及设置窗体
*/
public void lauchFrame() {
for (int i = 0; i < 10; i++) {
tanks.add(new Tank((i + 1) * 50 + 100, 50, false, Tank.Direction.D, tc));
}
setTitle("TankWar");
setBounds(230, 100, 800, 600);
// 匿名内部类,短,不涉及将来的扩展
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setBackground(Color.GREEN);
setVisible(true);
addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
tc = new TankClient();
tc.lauchFrame();
}
/**
* // 内部类,只为这个TankWar服务,方便的访问包装类的方法,不方便公开,
* @author yan
*
*/
private class PaintThread implements Runnable {
public void run() {
while (true) {
//游戏暂停有关,线程不能关,关了以后的话,后面的方法都不会运行
if(p==true){
repaint();}
// 内部调用父类的paint方法;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* // 键盘事件,内部类
* @author yan
*
*/
private class KeyMoniter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
}
*********************************************************
//血块类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;
//血块
public class Blood {
int step=0;
int x,y,w,h;
Random ran=new Random();
private boolean live=true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
TankClient tc;
/**
* 初始化血块
*/
public Blood(){
w=h=20;
x=300;
y=300;
}
/**
* 用来设定下一次血块随机出现的地点,避免线程带来的闪烁,(不归线程来控制)
*/
public void setBlood(){
x=ran.nextInt(500)+50;
y=ran.nextInt(500)+50;
live=true;
}
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);
}
/**
* 血块的碰撞检测
* @return
*/
public Rectangle getRect(){
return new Rectangle(x,y,w,h);
}
}
*******************************************
//爆炸类
import java.awt.Color;
import java.awt.Graphics;
public class Explode {
int x,y;
private boolean live=true;
private TankClient tc;
public Explode(int x,int y,TankClient tc){
this.x=x;
this.y=y;
this.tc=tc;
}
int[] diameter={4,6,7,10,12,15,18,21,25,26,30,32,35,40,45,49,30,14,6};
int step;
public void draw(Graphics g){
if(step==diameter.length){
live=false;
tc.explodes.remove(this);//爆炸完成后移除此对象
step=0;
return;
}
if(!live)return;
Color c = g.getColor();
g.setColor(Color.green);
g.fillOval(x, y, diameter[step], diameter[step]);
step++;
g.setColor(c);
}
}
*********************************************************
//炮弹类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.List;
import Frame.Tank.Direction;
/**
* // 炮弹的生死状态,默认为生,出街,打倒敌人为死
* @author yan
*
*/
public class Missile {
private static final int xspeed = 8;
private static final int yspeed = 8;
public static final int WIDTH = 10;
public static final int HEIGHT = 10;
private List<Tank> tanks;
private boolean Live = true;
private boolean good=true;
public boolean isLive() {
return Live;
}
int x, y;
Tank.Direction dir;
private TankClient tc;
public Missile(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Missile(int x, int y, Direction dir,boolean good,TankClient tc) {
this(x, y, dir);
this.good=good;
this.tc = tc;
}
public void draw(Graphics g) {
if (!Live) {
tc.missiles.remove(this);
}
Color c = g.getColor();
g.setColor(Color.black);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
move();
}
private 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 LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
}
if (x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HIGHT) {
Live = false;
tc.missiles.remove(this);
}
}
/**
* // 得到外围小方块,(碰撞检测辅助类Rectangle)
* @return
*/
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
/**
* // 添加爆炸对象到列表,射击类
* @param t
* @return
*/
public boolean hitTank(Tank t) {
if (this.Live&&this.getRect().intersects(t.getRect()) && t.isLive() == true&&this.good!=t.isGood()) {// 检测是否碰撞,碰撞之后,还得判断坦克是生还是死,否则子弹在那个地方还是会消失
if(t.isGood()){
t.setLife(t.getLife()-20);
if(t.getLife()<=0)
t.setLive(false);
}else{
t.setLive(false);
}
Explode e = new Explode(x, y, tc);
tc.explodes.add(e);
this.Live = false;
return true;
}
return false;
}
/**
* // 对一个数组的坦克进行射击类添加
* @param tanks
* @return
*/
public boolean hitTank(List<Tank> tanks) {
for (int i = 0; i < tanks.size(); i++) {
if (hitTank(tanks.get(i))) {
return true;
}
}
return false;
}
/**
* 坦克碰撞到墙
* @param w
* @return
*/
public boolean hitWall(Wall w){
if(this.Live&&this.getRect().intersects(w.getRect())){
this.Live=false;
return true;
}
return false;
}
}
********************************************************
//坦克类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.List;
import java.util.Random;
public class Tank {
public static final int xspeed = 5;
public static final int yspeed = 5;
public static final int WIDTH=30;
public static final int HEIGHT=30;
private BloodBar bb=new BloodBar();
TankClient tc=null;
private int life=100;
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
int x, y;
int oldx,oldy;
private boolean bL = false, bU = false, bR = false, bD = false;
private boolean good;//好的坏的
public boolean isGood() {
return good;
}
private boolean Live=true;//活的
private static Random random=new Random();
private int step=random.nextInt(12)+3;//计数,如果i=10,则让坏坦克改变方向
public boolean isLive() {
return Live;
}
public void setLive(boolean live) {
Live = live;
}
public enum Direction {
L, LU, U, RU, R, RD, LD, D, STOP
};// 枚举类型
private Direction dir = Direction.STOP;
//定义一个炮筒的方向;
private Direction ptDir=Direction.D;
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;
}
public void draw(Graphics g) {
if(good)bb.draw(g);
if(Live==false){
if(!good){
tc.tanks.remove(this);
}
return;
}
Color c = g.getColor();
if(good==true){
g.setColor(Color.cyan);
}else{
g.setColor(Color.yellow);
}
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
//根据坦克的方向画一条直线当作炮筒;
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+Tank.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 LD:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x, y+Tank.HEIGHT);
break;
case D:
g.drawLine(x+Tank.WIDTH/2, y+Tank.HEIGHT/2, x+Tank.WIDTH/2, y+Tank.HEIGHT);
break;
}
move();
}
// 移动坦克
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 LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
case STOP:
break;
}
//坦克出界问题
if(x<=0)x=0;
if(y<=30)y=30;
if(x+Tank.HEIGHT>=TankClient.GAME_WIDTH)x=TankClient.GAME_WIDTH-Tank.WIDTH;
if(y+Tank.HEIGHT>=TankClient.GAME_HIGHT)y=TankClient.GAME_HIGHT-Tank.HEIGHT;
if(dir!=Direction.STOP){
ptDir=dir;
}
//给坏坦克自己随机运动
//如果i=10,则让坏坦克改变方向
if(!good){
step--;
if(step==0){
Direction[] dirs=Direction.values();
int r=random.nextInt(dirs.length);
dir=dirs[r];
step=random.nextInt(12)+3;
}
int r1=random.nextInt(40);
if(r1>35){
this.fire();
}
}
}
public void keyPressed(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
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;
case KeyEvent.VK_P://游戏暂停
if(tc.p==true){
tc.p=false;
}else{
tc.p=true;
}
break;
case KeyEvent.VK_S:
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.D;}
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.LD;}
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
private void stay(){
x=oldx;
y=oldy;
}
public void keyReleased(KeyEvent e) {
// 获得键的虚拟码
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:
this.superFire();
break;
case KeyEvent.VK_F2:
if(!this.Live){
this.life=100;
this.Live=true;
}
break;
case KeyEvent.VK_P:
//本来想加一个暂停,但是前面竟然用的内部类来使用线程,所以要大改。。。。
break;
}
locateDirection();
}
//开火
public Missile fire(){
if(!Live){return null;}
Missile m=new Missile(x+Tank.WIDTH/2-Missile.WIDTH/2,y+Tank.HEIGHT/2-Missile.HEIGHT/2,ptDir,good,tc);//传递坦克的位置和方向
tc.missiles.add(m);
return m;
}
public Missile fire(Direction dir){
if(!Live){return null;}
Missile m=new Missile(x+Tank.WIDTH/2-Missile.WIDTH/2,y+Tank.HEIGHT/2-Missile.HEIGHT/2,dir,good,tc);//传递坦克的位置和方向
tc.missiles.add(m);
return m;
}
////得到外围小方块,(碰撞检测辅助类Rectangle)
public Rectangle getRect(){
return new Rectangle(x,y,WIDTH,HEIGHT);
}
public boolean collidesWithTanks(List<Tank>tanks){
for(int i=0;i<tanks.size();i++){
Tank t=tanks.get(i);
if(this!=t){
if(this.Live&&t.isLive()&&this.getRect().intersects(t.getRect())){
this.stay();
t.stay();
return true;
}
}
}
return false;
}
public boolean collideWithWall(Wall w){
if(this.Live&&this.getRect().intersects(w.getRect())){
this.stay();
}
return false;
}
private void superFire(){
Direction[] dirs=Direction.values();
for (int i=0;i<8;i++){
fire(dirs[i]);
}
}
private class BloodBar{
public void draw (Graphics g){
Color c=g.getColor();
g.setColor(Color.red);
g.drawRect(x, y-12, WIDTH, 10);
int w=WIDTH*life/100;
g.fillRect(x, y-12, w, 10);
g.setColor(c);
}
}
public boolean eat (Blood b){
if(this.Live&&b.isLive()&&this.getRect().intersects(b.getRect())){
this.life=100;
b.setLive(false);
b.setBlood();
return true;
}
return false;
}
}
**************************************************************
//墙壁类
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* 墙壁类
* @author yan
*
*/
public class Wall {
int x,y,w,h;
TankClient tc;
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){
g.fillRect(x, y, w, h);
}
/**
* (碰撞检测辅助类Rectangle)
* @return
*/
public Rectangle getRect(){
return new Rectangle(x,y,w,h);
}
}
- TankWar.zip (24.3 KB)
- 下载次数: 25
发表评论
-
TCP实现聊天室
2016-03-17 22:03 581我们在写一个聊天室的小项目时候,需要有客户端和服务器端;并 ... -
BMP格式存储的自制画图板
2016-03-17 09:20 7881.我们先来了解BMP文件 ... -
数据结构之霍夫曼压缩,更易理解文件压缩过程
2016-03-02 17:11 1916中间遇到过很多问题,不断的找办法,其实网上有很多前辈遇到过类 ... -
关键字static和final
2016-02-26 19:56 461static 与final1.变量的声明和内存地址的开劈 c ... -
哈弗曼算法实现压缩和解压
2015-12-29 21:14 1445//这是我一个学长的,我看了好久才明白,给大家分享,这里的代 ... -
ZipFile和ZipOutputStream实现压缩和解压的小软件
2015-12-29 21:05 2870//这个小软件有两个类,实现了500MB文件压缩,太大的话容 ... -
雷电游戏,实现了飞机碰撞,血块。。。
2015-12-26 14:03 2270程序实现了缓冲图片消除闪烁,飞机碰撞,血块和血条的添加,, ... -
画图软件小项目(实现了颜色盘)
2015-11-27 16:52 853这里只显示实现了颜色盘的代码,其他完整代码在文件里,需要请下载 ... -
调色板的调用
2015-11-27 16:22 548import java.awt.Color; import j ... -
计算器小项目
2015-11-27 16:06 719import java.awt.FlowLayout; imp ... -
java项目,坦克大战
2015-11-23 23:38 1287现在坦克已经可以运动,并且可以发炮弹了 主类,启动类 -- ... -
java项目 ---坦克大战,坦克向8个方向运动
2015-11-23 23:34 923窗体类; -------------------------- ...
相关推荐
JAVA项目坦克大战代码内涵详细解释,本代码是根据马士兵老师的课程编写的,个人加了很多详细的注解。懒得看视频的同学可以下载瞧瞧~~
java项目坦克大战源码,解压就能看到源码,适合新手!
java小游戏 (源码+视频+文档+ppt) swing坦克大战游戏java小游戏 (源码+视频+文档+ppt) swing坦克大战游戏java小游戏 (源码+视频+文档+ppt) swing坦克大战游戏java小游戏 (源码+视频+文档+ppt) swing坦克大战游戏java...
基于Java的坦克大战设计与实现设计软件程序源码+word毕业论文文档. 此系统是使用Java语言实现坦克大战游戏程序,玩家通过连接访问进入游戏,通过操纵坦克来守卫基地,玩家还可以获得超级武器来提升坦克的属性,摧毁...
java编写的坦克大战游戏,重温经典游戏。此项目完整的实现了坦克大战的游戏,非常适合开发者参考使用,也可以根据自己的喜好通过修改代码来实现各种关卡场景或者操作效果。
JAVA项目_坦克大战网络版视频教程
一个用Java技术开发的坦克大战项目,喜欢的人可以下载玩一下
①游戏管理:用户的登录和退出及注册。 ②地图设计:多款地图的设计与开发。 ③难度级别:阶梯难度的设计。 ④坦克攻防:游戏模式设计与开发。 ⑤分享与排行:对游戏结果进行记录和排行。
资源名字:基于java+Socket+Eclipse的坦克大战游戏设计与实现(源码+文档)_JAVA_Eclipse_坦克大战游戏.zip 资源类型:项目全套源码+文档+辅导视频 源码说明: 全部项目源码都是经过测试校正后百分百成功运行。 适合...
Java单人单机版坦克源码【可能存在Bug,请谅解】 隐藏道具(可以打掉铁墙)未显示在游戏帮助中 游戏含8关地图,每关共20只敌方坦克,界面中最多出现5只敌方坦克 游戏中/发射子弹/我方坦克击中敌方坦克/敌方坦克被...
解压,里面有关于坦克大战的素材,图片~主要有源代码·直接运行就行。
3《JAVA桌面开发项目实训》实训报告之坦克大战
还记得小时候坦克大战的乐趣吗?自己动手把,Java实战项目源码坦克大战源码
《坦克大战》项目源码,用Java开发的一款小游戏 《坦克大战》项目源码,用Java开发的一款小游戏 《坦克大战》项目源码,用Java开发的一款小游戏 《坦克大战》项目源码,用Java开发的一款小游戏 《坦克大战》项目源码...
Java坦克大战游戏源代码 关于项目 Java中的坦克大战游戏是一款简单的多人游戏对战游戏。这个游戏和之前的游戏很相似。本系统是在Eclipse IDE中制作的。此外,您需要在计算机上安装 JDK 和 JRE 才能运行它。这是一个...
坦克大战游戏源码.素材.文档 ①源码文件是授课过程中的最后版本,是myeclipse6.5编写的工程. ②源码可以通过myeclipse6.5或者更高版本打开,并可以直接运行 ③给出的源码并没有实现坦克大战文档的全部功能,这个在...
java语言写的小项目,一个坦克大战的小游戏,可以联机运行
java语言写的坦克大战+素材+文档,是学习项目开发的好案例!!!!
# 主要设计 1、要有难度关卡:第一关,第二关,第三关,第四关,第五关;第一关地图最简单,第五关地图最难; 2、坦克要有血条,打多次才会死 ...需要技术指导,写项目程序,等更多服务请私信联系博主