📄 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 + -