📄 tank.java
字号:
package com.zhoutenghn;
import java.awt.*;
import java.awt.event.*;
import java.util.List;
import java.util.*;
public class Tank {
public static final int X2 = 5;
public static final int Y2 = 5;
public static final int WIDTH = 40;
public static final int HEIGHT = 40;
public static boolean isStop ;
private int x, y,x0,y0;
BeginPlay tc;
private boolean good = true;
private boolean live = true;
private static Random r = new Random();
private int step = r.nextInt(20) + 3 ;
private boolean bl = false, bu = false , br = false, bd = false;
enum Direction {L,LD,D,DR,R,RU,U,UL,STOP}
private Direction dir = Direction.STOP;
private Direction ptdir = Direction.D;
public Tank(int x, int y,boolean good,BeginPlay tc) {
this.x = x;
this.y = y;
x0 = x;
y0 = y;
this.good = good ;
this.tc = tc ;
}
public void draw(Graphics g) {
if(!live)return;
Color c = g.getColor();
if (good){
g.setColor(Color.YELLOW);
}else g.setColor(new Color(250, 0, 0));
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
switch (ptdir){
case L :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x , y + HEIGHT/2);
break;
case UL :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x , y );
break;
case U :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y );
break;
case RU :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y );
break;
case R :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT/2);
break;
case DR :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH, y + HEIGHT);
break;
case D :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x + WIDTH/2, y + HEIGHT);
break;
case LD :g.drawLine(x + WIDTH/2, y + HEIGHT/2, x , y + HEIGHT);
break;
}
//if(crash(tc.tanks)){
// x = x0;
// y = y0;
// }
move();
}
public void move(){
x0 = x;
y0 = y;
switch (dir){
case L :
x -= X2;
break;
case LD :
x -= X2;
y += Y2;
break;
case DR :
x += X2;
y += Y2;
break;
case R :
x += X2;
break;
case RU :
x += X2;
y -= Y2;
break;
case U :
y -= Y2;
break;
case D :
y += Y2;
break;
case UL :
x -= X2;
y -= Y2;
break;
case STOP :
break;
}
if(this.dir != Direction.STOP) {
this.ptdir = this.dir;
}
if (x < 0) x = 0;
if (y < 26) y = 26;
if ((x + this.WIDTH)>BeginPlay.GAME_WIDE )
x = BeginPlay.GAME_WIDE - this.WIDTH;
if ((y + this.HEIGHT)>BeginPlay.GAME_HEIGHT)
y = BeginPlay.GAME_HEIGHT - this.HEIGHT;
if (!good) {
Direction [] dirs = Direction.values();
if(step == 0 ){
step = r.nextInt(20) + 3;
int rn = r.nextInt(dirs.length);
dir = dirs[rn];
}
step--;
if (r.nextInt(100) >95) this.fire();
}
}
public void press(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_1:
if (!this.isLive()) tc.myTank = new Tank(x,y + 30,true,tc);
this.live = true;
tc.times ++;
break;
case KeyEvent.VK_UP:
bu = true;
break;
case KeyEvent.VK_DOWN:
bd = true;
break;
case KeyEvent.VK_RIGHT:
br = true;
break;
case KeyEvent.VK_LEFT:
bl = true;
break;
}
location();
}
public void released (KeyEvent e){
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_SPACE :
fire();
break;
case KeyEvent.VK_UP:
bu = false;
break;
case KeyEvent.VK_DOWN:
bd = false;
break;
case KeyEvent.VK_RIGHT:
br = false;
break;
case KeyEvent.VK_LEFT:
bl = false;
break;
}
location();
}
public void location(){
if (bl && !bu && !br && !bd)dir = Direction.L;
if (!bl && bu && !br && !bd)dir = Direction.U;
if (!bl && !bu && br && !bd)dir = Direction.R;
if (!bl && !bu && !br && bd)dir = Direction.D;
if (bl && bu && !br && !bd)dir = Direction.UL;
if (bl && !bu && !br && bd)dir = Direction.LD;
if (!bl && !bu && br && bd)dir = Direction.DR;
if (!bl && bu && br && !bd)dir = Direction.RU;
if (!bl && !bu && !br && !bd)dir = Direction.STOP;
}
public Missile fire(){
int X = x + this.WIDTH/2 - 10, Y = y + this.HEIGHT/2 ;
Missile m = new Missile (X,Y,this.good,ptdir,tc);
tc.missiles.add(m);
return m;
}
public Rectangle getRect(){
return new Rectangle(x,y,this.WIDTH,this.HEIGHT);
}
public boolean isGood() {
return good;
}
public boolean isLive() {
return live;
}
public void crash(Tank t){
if(this.getRect().intersects(t.getRect())){
x = x0;
y = y0;
}
}
public void setLive(boolean live) {
this.live = live;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -