?? life.java
字號:
package com.caizi;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
/**
* Life類,用于坦克的生命的增加
* @author user
*
*/
public class Life {
private int x,y;
private boolean live = true;
private static final int WIDTH = 10;
private static final int HIGHTH = 10;
private static float X_SPEED;
private static float Y_SPEED;
private TankClient tc;
public Life(TankClient tc){
this.tc = tc;
this.x = (int)(Math.random()*tc.getWINDOW_WIDTH());
this.y = (int)(Math.random()*tc.getWINDOW_HEIGHT());
this.makeDir();
}
/**
* makeDir()用于確定生命的移動方向和速度
* X_SPEED表示運動的x速度
*/
public void makeDir(){
this.X_SPEED = (float) (Math.random()*3-1.5);
if(Math.random()>0.5){
if(this.X_SPEED>0)
this.Y_SPEED = 2 - this.X_SPEED;
else
this.Y_SPEED = 2 + this.X_SPEED;
} else{
if(this.X_SPEED>0)
this.Y_SPEED = this.X_SPEED - 2;
else
this.Y_SPEED = -( 2 + this.X_SPEED);
}
}
public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.MAGENTA);
g.fillRect(x, y, WIDTH, HIGHTH);
g.setColor(c);
this.move();
}
public void move(){
checkBorder();
this.x += this.X_SPEED;
this.y += this.Y_SPEED;
}
public void checkBorder(){
if(this.x <=0 || this.y <=0 ||this.x >= tc.getWidth() || this.y >= tc.getHeight())
makeDir();
}
public Rectangle getRect(){
return new Rectangle(x,y,WIDTH,HIGHTH);
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -