实例介绍
【实例截图】
【核心代码】
package tankwar;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.util.*;
public class Tank {
private static final int XDIR=10;//坦克速度
private static final int YDIR=10;//坦克速度
TankWar ta=null;
private static Random r=new Random();//Random随机数产生器
private int step=r.nextInt(10) 2;//
private Bloodbar b=new Bloodbar();
//加载图片
private static Toolkit tk =Toolkit.getDefaultToolkit();//默认工具包
private static Image[] tankImages=null;
private static Map<String,Image> imgs=new HashMap<String,Image>();
static{
tankImages=new Image[]{
tk.getImage(Tank.class.getClassLoader().getResource("images/tankz.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankzs.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tanks.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankys.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tanky.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankyx.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankx.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/tankzx.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankz.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankzs.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytanks.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankys.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytanky.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankyx.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankx.gif")),
tk.getImage(Tank.class.getClassLoader().getResource("images/mytankzx.gif"))
};
imgs.put("z",tankImages[0]);
imgs.put("zs",tankImages[1]);
imgs.put("s",tankImages[2]);
imgs.put("ys",tankImages[3]);
imgs.put("y",tankImages[4]);
imgs.put("yx",tankImages[5]);
imgs.put("x",tankImages[6]);
imgs.put("zx",tankImages[7]);
imgs.put("gz",tankImages[8]);
imgs.put("gzs",tankImages[9]);
imgs.put("gs",tankImages[10]);
imgs.put("gys",tankImages[11]);
imgs.put("gy",tankImages[12]);
imgs.put("gyx",tankImages[13]);
imgs.put("gx",tankImages[14]);
imgs.put("gzx",tankImages[15]);
}
public static final int WIDTH=68;//tankImages[8].getWidth(null);//tank高
public static final int HEIGHT=68;//tankImages[8].getHeight(null);//tank宽
private int life=100;
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
private boolean live=true;//设置一个布尔类型的量
private boolean good;
private int x, y;
private int oldx,oldy;
private boolean bz=false,bs=false,by=false,bx=false;
//定义方向
private Direction d=Direction.STOP; //坦克方向
private Direction ptd=Direction.y;//炮筒的方向
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
public Tank(int x, int y,boolean good) {
this.x = x;
this.y = y;
this.oldx=x;
this.oldy=y;
this.good=good;
}
public Tank(int x,int y,boolean good, Direction d,TankWar ta){
this(x,y,good);
this.d=d;
this.ta=ta;
}
//tank移动
void move(){
this.oldx=x;
this.oldy=y;
switch(d){
case z: x-=XDIR;break;
case zs:x-=XDIR; y-=YDIR;break;
case s: y-=YDIR;break;
case ys:x =XDIR;y-=YDIR;break;
case y: x =XDIR;break;
case yx:x =XDIR;y =YDIR;break;
case x:y =YDIR;break;
case zx:x-=XDIR;y =YDIR;break;
case STOP:break;
}
if(this.d!=Direction.STOP){
this.ptd=this.d;
}
if(x<0)x=0;
if(y<25)y=25;
if(x Tank.WIDTH>TankWar.GAME_WIDTH) x=TankWar.GAME_WIDTH -Tank.WIDTH;
if(y Tank.HEIGHT>TankWar.GAME_HEIGHT)y=TankWar.GAME_HEIGHT-Tank.HEIGHT;
//如果是坏坦克随机移动
if(!good){
Direction[]ds=Direction.values();//定义一个数组
if(step==0){
step=r.nextInt(10) 2;
int rn=r.nextInt(ds.length);//随即产生一个数
d=ds[rn];
}
step--;
if(r.nextInt(40)>38)this.fire();
}
}
public void stay(){
x=oldx;
y=oldy;
}
public void draw(Graphics g){
Color c=g.getColor();
g.setColor(Color.ORANGE);
if(this.life==0){
Font t=new Font("宋体",Font.BOLD,50);
g.setFont(t);
g.drawString("GAME OVER", 200, 300);
}
g.setColor(c);
if(!live){
if(!good){
ta.tank04s.remove(this);
}
return;//如果打中不再返回true;
}
if(good)b.draw(g);
if(!good){
switch(ptd){
case z:g.drawImage(imgs.get("z"), x, y, null);break;
case zs:g.drawImage(imgs.get("zs"), x, y, null);break;
case s: g.drawImage(imgs.get("s"), x, y, null);break;
case ys:g.drawImage(imgs.get("ys"), x, y, null);break;
case y: g.drawImage(imgs.get("y"), x, y, null);break;
case yx:g.drawImage(imgs.get("yx"), x, y, null);break;
case x:g.drawImage(imgs.get("x"), x, y, null);break;
case zx:g.drawImage(imgs.get("zx"), x, y, null);break;
}
}else
{ System.out.println(ptd);switch(ptd){
case z:g.drawImage(imgs.get("gz"), x, y, null);break;
case zs:g.drawImage(imgs.get("gzs"), x, y, null);break;
case s: g.drawImage(imgs.get("gs"), x, y, null);break;
case ys:g.drawImage(imgs.get("gys"), x, y, null);break;
case y: g.drawImage(imgs.get("gy"), x, y, null);break;
case yx:g.drawImage(imgs.get("gyx"), x, y, null);break;
case x:g.drawImage(imgs.get("gx"), x, y, null);break;
case zx:g.drawImage(imgs.get("gzx"), x, y, null);break;
}
}
move();
}
//添加键盘坚挺控制坦克
public void keyPressed(KeyEvent e){
int key=e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT: bz=true;break;
case KeyEvent.VK_UP: bs=true;break;
case KeyEvent.VK_RIGHT:by=true;break;
case KeyEvent.VK_DOWN: bx=true;break;
}
locateDirection();
}
void locateDirection(){
if( bz && !by && !bs && !bx) d=Direction.z;
else if( bz && !by && bs && !bx) d=Direction.zs;
else if(!bz && !by && bs && !bx) d=Direction.s;
else if(!bz && by && bs && !bx) d=Direction.ys;
else if(!bz && by && !bs && !bx) d=Direction.y;
else if(!bz && by && !bs && bx) d=Direction.yx;
else if(!bz && !by && !bs && bx) d=Direction.x;
else if( bz && !by && !bs && bx) d=Direction.zx;
else if(!bz && !by && !bs && !bx) d=Direction.STOP;
}
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
switch(key){
//case KeyEvent.VK_F2:if(!this.live)this.live=true;this.life=100;break;//如果本人over则按f2出现
case KeyEvent.VK_D:fire();break;
case KeyEvent.VK_LEFT: bz=false;break;
case KeyEvent.VK_UP: bs=false;break;
case KeyEvent.VK_RIGHT:by=false;break;
case KeyEvent.VK_DOWN: bx=false;break;
case KeyEvent.VK_A: superFire();break;
}
locateDirection();
}
public Missile fire(){
if(!live){
return null;
}
int x=this.x Tank.WIDTH/2;
int y=this.y Tank.HEIGHT/2;
Missile m=new Missile(x,y,good,ptd,this.ta);
ta.missiles.add(m);
return m;
}
public Missile fire(Direction d){
if(!live){
return null;
}
int x=this.x Tank.WIDTH/2;
int y=this.y Tank.HEIGHT/2;
Missile m=new Missile(x,y,good,d,this.ta);
ta.missiles.add(m);
return m;
}
public Rectangle getRect(){
return new Rectangle(x,y,tankImages[0].getWidth(null),tankImages[0].getHeight(null));
}
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>tank04s){
for(int i=0;i<tank04s.size();i ){
Tank t=tank04s.get(i);
if(this!=t){
if(this.live&&t.isLive()&&this.getRect().intersects(t.getRect())){
this.stay();
t.stay();
return true;
}
}
}
return false;
}
private void superFire(){
Direction [] ds=Direction.values();
for(int i=0;i<8;i ){
fire(ds[i]);
}
}
//建个血条
private class Bloodbar{
public void draw (Graphics g){
Color c=g.getColor();
g.setColor(Color.RED);
g.drawRect(700, 50, 80, 10);//画个框
int w=80*life/100;
if(w<=20)g.setColor(Color.ORANGE);
g.fillRect(700, 50, w, 10);
g.setColor(c);
}
}
public boolean eat(Blood bl){
if(this.live&&bl.isLive()&&this.getRect().intersects(bl.getRect())){
this.life=100;
bl.setLive(false);
return true;
}
return false;
}
}
标签: 娱乐
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论