import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
public class Tank {
private int x,y;
private int id;
private int speed = 5;
private boolean live = true;
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isGood() {
return good;
}
public void setGood(boolean good) {
this.good = good;
}
public Direction getDir() {
return dir;
}
public void setDir(Direction dir) {
this.dir = dir;
}
private boolean good;
public Tank(int x, int y,int id, boolean good, Direction dir) {
super();
this.x = x;
this.y = y;
this.id = id;
this.good = good;
this.dir = dir;
}
public static final int WIDTH = 40;
public static final int HEIGHT = 40;
static Map<String,Image> images = new HashMap<String,Image>();
static {
try {
images.put("u", ImageIO.read(new File("images/tankU.gif")));
images.put("d", ImageIO.read(new File("images/tankD.gif")));
images.put("l", ImageIO.read(new File("images/tankL.gif")));
images.put("r", ImageIO.read(new File("images/tankR.gif")));
images.put("ld", ImageIO.read(new File("images/tankLD.gif")));
images.put("lu", ImageIO.read(new File("images/tankLU.gif")));
images.put("rd", ImageIO.read(new File("images/tankRD.gif")));
images.put("ru", ImageIO.read(new File("images/tankRU.gif")));
} catch (IOException e) {
e.printStackTrace();
}
}
Direction dir;
boolean left,right,up,down;
public void draw(Graphics g){
if(!live){
return;
}
if(up&&!down&&!left&&!right){
dir = Direction.U;
y -=speed;
}else if(!up&&down&&!left&&!right){
dir = Direction.D;
y +=speed;
}else if(!up&&!down&&left&&!right){
dir = Direction.L;
x -=speed;
}else if(!up&&!down&&!left&&right){
dir = Direction.R;
x +=speed;
}else if(!up&&down&&left&&!right){
dir = Direction.LD;
x -=speed;
y +=speed;
}else if(up&&!down&&left&&!right){
dir = Direction.LU;
x -=speed;
y -=speed;
}else if(!up&&down&&!left&&right){
dir = Direction.RD;
x +=speed;
y +=speed;
}else if(up&&!down&&!left&&right){
dir = Direction.RU;
x +=speed;
y -=speed;
}
switch (dir) {
case U:
g.drawImage(images.get("u"),x,y,null);
break;
case D:
g.drawImage(images.get("d"),x,y,null);
break;
case L:
g.drawImage(images.get("l"),x,y,null);
break;
case R:
g.drawImage(images.get("r"),x,y,null);
break;
case LD:
g.drawImage(images.get("ld"),x,y,null);
break;
case LU:
g.drawImage(images.get("lu"),x,y,null);
break;
case RD:
g.drawImage(images.get("rd"),x,y,null);
break;
case RU:
g.drawImage(images.get("ru"),x,y,null);
break;
}
}
public void keyPressed(KeyEvent e){
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
case KeyEvent.VK_SPACE:
Bullet b = new Bullet(x+WIDTH/2,y+HEIGHT/2,this.dir);
TankClient.bullets.add(b);
break;
}
}
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
}
}
public Tank(int x, int y, Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public Rectangle getRect(){
return new Rectangle((int)x,(int)y,WIDTH,HEIGHT);
}
}
enum Direction{
U,D,L,R,LD,LU,RD,RU
}
没有合适的资源?快使用搜索试试~ 我知道了~
JAVA网络版坦克大战
共41个文件
gif:25个
class:8个
java:4个
5星 · 超过95%的资源 需积分: 10 9 下载量 40 浏览量
2011-05-04
18:13:45
上传
评论
收藏 49KB RAR 举报
温馨提示
网络版的坦克大战,能够实现基本的游戏规则
资源推荐
资源详情
资源评论
收起资源包目录
Tank.rar (41个子文件)
Tank
.project 380B
bin
TankClient$KeyMonitor.class 842B
TankClient.class 3KB
Bullet.class 3KB
TankClient$PaintThread.class 816B
TankClient$1.class 638B
Direction.class 1KB
Explode.class 2KB
Tank.class 5KB
.settings
org.eclipse.jdt.core.prefs 629B
bom
e15.gif 460B
e3.gif 846B
e16.gif 280B
e11.gif 1KB
e1.gif 312B
e14.gif 814B
e6.gif 1KB
e5.gif 1KB
e8.gif 1KB
e4.gif 1KB
e13.gif 1022B
e7.gif 1KB
e10.gif 1KB
e9.gif 1KB
e2.gif 565B
e12.gif 1KB
src
Bullet.java 2KB
Explode.java 2KB
TankClient.java 2KB
Tank.java 4KB
images
0004.gif 5KB
tankRU.gif 2KB
tankLD.gif 1KB
tankLU.gif 1KB
tankL.gif 1KB
tankRD.gif 1KB
tankR.gif 1KB
tankD.gif 1KB
wall.jpg 5KB
tankU.gif 1KB
.classpath 301B
共 41 条
- 1
资源评论
- WANG1432013-05-14可以看到作者的思路,很好
- Lucky_目2013-02-01很好,值得去琢磨的代码
zy365212593
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功