package org.birdy.tank.compent;import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;import javax.imageio.ImageIO;import org.birdy.tank.main.GameFrame;
import org.birdy.tank.util.Constants;
import org.birdy.tank.util.ImageLoader;/*
* 坦克实体
*/
public class Tank extends BasicObject implements TankListener{
/*
* 速度属性
*/
private int speed = 5;
/*
* 速度基础系数
*/
private int speedIatio = 100;
/*
* 运动方向属性
*/
private Direction direction = Direction.stop;
/*
* 指向方向
*/
private Direction pointDirection = Direction.up;
/*
* 阵营属性
* user & enemy & friends
*/
private String group = Constants.GROUP_USER;
/*
* 攻击速度
* 例如(双手斧:3.5)即3.5秒攻击一次
*/
private long rate = (long)(1*1000);
/*
* 状态
* 0:默认
* 1:攻击
* 2:防御
*/
private int status = 0;
public Tank(int x,int y,int width,int height,Direction direction,int speed,String group){
super(x, y, width, height);
setDirection(direction);
setSpeed(speed);
setGroup(group);
}
public void draw(Graphics g){
Image image = null;
try {
if(getPointDirection().equals(Direction.up)){
image = ImageIO.read(ImageLoader.load("user/tank_up.PNG"));
}else if(getPointDirection().equals(Direction.down)){
image = ImageIO.read(ImageLoader.load("user/tank_down.PNG"));
}else if(getPointDirection().equals(Direction.left)){
image = ImageIO.read(ImageLoader.load("user/tank_left.PNG"));
}else if(getPointDirection().equals(Direction.right)){
image = ImageIO.read(ImageLoader.load("user/tank_right.PNG"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(image,this.x, this.y, this.width, this.height, null);
move();
if(getStatus()==1){
attack();
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
} public Direction getDirection() {
return direction;
} public void setDirection(Direction direction) {
this.direction = direction;
}
public Direction getPointDirection() {
return pointDirection;
} public void setPointDirection(Direction pointDirection) {
this.pointDirection = pointDirection;
} public String getGroup() {
return group;
} public void setGroup(String group) {
this.group = group;
} public long getRate() {
return rate;
} public void setRate(long rate) {
this.rate = rate;
}
public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} @Override
public void attack() {
// TODO Auto-generated method stub
Missile m = new Missile(x,y,10,10,getPointDirection(),10);
m.setParent(this);
getGameFrame().addNewObject(m);
} @Override
public void defense() {
// TODO Auto-generated method stub
} @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
setDirection(Direction.up);
setPointDirection(Direction.up);
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
setDirection(Direction.down);
setPointDirection(Direction.down);
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
setDirection(Direction.left);
setPointDirection(Direction.left);
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
setDirection(Direction.right);
setPointDirection(Direction.right);
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){
setStatus(1);
}
} @Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
if(getDirection().equals(Direction.up)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
if(getDirection().equals(Direction.down)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(getDirection().equals(Direction.left)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(getDirection().equals(Direction.right)){setDirection(Direction.stop);}
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){
setStatus(0);
}
} @Override
public void move() {
int newX = x;
int newY = y;
/*
* 边界检测
*/
if(getDirection().equals(Direction.up)){
if((y-getRunDistance())>0){
newY = y-getRunDistance();
}else{
newY = 0;
}
}else if(getDirection().equals(Direction.down)){
if((y + getRunDistance())<(getGameFrame().getHeight()-getHeight())){
newY = y+getRunDistance();
}else{
newY = (int) (getGameFrame().getHeight()-getHeight());
}
}else if(getDirection().equals(Direction.left)){
if((x-getRunDistance())>0){
newX = x - getRunDistance();
}else{
newX = 0;
}
}else if(getDirection().equals(Direction.right)){
if((x + getRunDistance())<(getGameFrame().getWidth()-getWidth())){
newX = x + getRunDistance();
}else{
newX = (int) (getGameFrame().getWidth()-getWidth());
}
}
/*
* 碰撞检测
*/
boolean b = true;
for(int k=0;k<getGameFrame().getChilds().size();k++){
if(getGameFrame().getChilds().get(k).intersects(new Rectangle(new Point(newX,newY),this.getSize())) && !getGameFrame().getChilds().get(k).equals(this)){
b = false;
}
}
/*
* 设置坐标
*/
if(b){
setLocation(newX, newY);
}
}
/*
* 单位时间移动距离
* 最小移动速率为1像素
*/
public int getRunDistance(){
int runDistance = (getGameFrame().getFRESH_DELAY()*speed*speedIatio)/1000;
if(runDistance<1){
runDistance = 1;
}
return runDistance;
}}
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;import javax.imageio.ImageIO;import org.birdy.tank.main.GameFrame;
import org.birdy.tank.util.Constants;
import org.birdy.tank.util.ImageLoader;/*
* 坦克实体
*/
public class Tank extends BasicObject implements TankListener{
/*
* 速度属性
*/
private int speed = 5;
/*
* 速度基础系数
*/
private int speedIatio = 100;
/*
* 运动方向属性
*/
private Direction direction = Direction.stop;
/*
* 指向方向
*/
private Direction pointDirection = Direction.up;
/*
* 阵营属性
* user & enemy & friends
*/
private String group = Constants.GROUP_USER;
/*
* 攻击速度
* 例如(双手斧:3.5)即3.5秒攻击一次
*/
private long rate = (long)(1*1000);
/*
* 状态
* 0:默认
* 1:攻击
* 2:防御
*/
private int status = 0;
public Tank(int x,int y,int width,int height,Direction direction,int speed,String group){
super(x, y, width, height);
setDirection(direction);
setSpeed(speed);
setGroup(group);
}
public void draw(Graphics g){
Image image = null;
try {
if(getPointDirection().equals(Direction.up)){
image = ImageIO.read(ImageLoader.load("user/tank_up.PNG"));
}else if(getPointDirection().equals(Direction.down)){
image = ImageIO.read(ImageLoader.load("user/tank_down.PNG"));
}else if(getPointDirection().equals(Direction.left)){
image = ImageIO.read(ImageLoader.load("user/tank_left.PNG"));
}else if(getPointDirection().equals(Direction.right)){
image = ImageIO.read(ImageLoader.load("user/tank_right.PNG"));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
g.drawImage(image,this.x, this.y, this.width, this.height, null);
move();
if(getStatus()==1){
attack();
}
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
} public Direction getDirection() {
return direction;
} public void setDirection(Direction direction) {
this.direction = direction;
}
public Direction getPointDirection() {
return pointDirection;
} public void setPointDirection(Direction pointDirection) {
this.pointDirection = pointDirection;
} public String getGroup() {
return group;
} public void setGroup(String group) {
this.group = group;
} public long getRate() {
return rate;
} public void setRate(long rate) {
this.rate = rate;
}
public int getStatus() {
return status;
} public void setStatus(int status) {
this.status = status;
} @Override
public void attack() {
// TODO Auto-generated method stub
Missile m = new Missile(x,y,10,10,getPointDirection(),10);
m.setParent(this);
getGameFrame().addNewObject(m);
} @Override
public void defense() {
// TODO Auto-generated method stub
} @Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
setDirection(Direction.up);
setPointDirection(Direction.up);
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
setDirection(Direction.down);
setPointDirection(Direction.down);
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
setDirection(Direction.left);
setPointDirection(Direction.left);
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
setDirection(Direction.right);
setPointDirection(Direction.right);
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){
setStatus(1);
}
} @Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()==KeyEvent.VK_UP){
if(getDirection().equals(Direction.up)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
if(getDirection().equals(Direction.down)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(getDirection().equals(Direction.left)){setDirection(Direction.stop);}
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(getDirection().equals(Direction.right)){setDirection(Direction.stop);}
}
if(e.getKeyCode()==KeyEvent.VK_SPACE){
setStatus(0);
}
} @Override
public void move() {
int newX = x;
int newY = y;
/*
* 边界检测
*/
if(getDirection().equals(Direction.up)){
if((y-getRunDistance())>0){
newY = y-getRunDistance();
}else{
newY = 0;
}
}else if(getDirection().equals(Direction.down)){
if((y + getRunDistance())<(getGameFrame().getHeight()-getHeight())){
newY = y+getRunDistance();
}else{
newY = (int) (getGameFrame().getHeight()-getHeight());
}
}else if(getDirection().equals(Direction.left)){
if((x-getRunDistance())>0){
newX = x - getRunDistance();
}else{
newX = 0;
}
}else if(getDirection().equals(Direction.right)){
if((x + getRunDistance())<(getGameFrame().getWidth()-getWidth())){
newX = x + getRunDistance();
}else{
newX = (int) (getGameFrame().getWidth()-getWidth());
}
}
/*
* 碰撞检测
*/
boolean b = true;
for(int k=0;k<getGameFrame().getChilds().size();k++){
if(getGameFrame().getChilds().get(k).intersects(new Rectangle(new Point(newX,newY),this.getSize())) && !getGameFrame().getChilds().get(k).equals(this)){
b = false;
}
}
/*
* 设置坐标
*/
if(b){
setLocation(newX, newY);
}
}
/*
* 单位时间移动距离
* 最小移动速率为1像素
*/
public int getRunDistance(){
int runDistance = (getGameFrame().getFRESH_DELAY()*speed*speedIatio)/1000;
if(runDistance<1){
runDistance = 1;
}
return runDistance;
}}
解决方案 »
- 求解'Apache的FTPClient上传.gz文件解压失败'
- JAVA中如何对比时间
- 昏了,我用struts2做项目 eclipse老给我配 1。4的JDK 怎么改成1。6的啊?我根本没装1。4的只装了个1。6的JDK,疯了。
- 如何在已有的框架中加入spring来进行事务管理?
- 我下的xdoclet为什么没有plugins文件夹啊?
- myeclipse下的struts+hibernate问题.
- actionbean必须搭配一个formbean吗
- 有谁在JBuilder9上成功配置成功CVS的?
- 数据库导出xml文件,对于数据库中的特殊字符该如何处理。急急急!在线等。
- 怎样培训新人JSPStruts相关技术?请junnef(光明圣堂武士)、
- 求问文本框输入保存基本格式的教程
- extjs4.2 rowcolumn列 当删除一条数据后,自动重新排号
貌似都是以前J2SE的吧!
现在都是安卓的了!
http://product.dangdang.com/product.aspx?product_id=21046296
随书课件的下载地址:
http://download.csdn.net/source/3120911