📄 ball.java
字号:
import java.awt.*;
public class Ball {
public static final int BWIDTH=10;
public static final int BHEIGHT=10;
public static final int XSPEED=10;
public static final int YSPEED=10;
int x,y;
Tank.Direction dir;
TankGameFrame tgf;
public Ball(int x,int y,Tank.Direction dir)
{
this.x=x;
this.y=y;
this.dir=dir;
}
public Ball(int x,int y,Tank.Direction dir,TankGameFrame tgf)
{
this(x,y,dir);
this.tgf=tgf;
}
public void draw(Graphics g)
{
Color c=g.getColor();
g.setColor(Color.orange);
g.fillOval(x,y,BWIDTH,BHEIGHT);
g.setColor(c);
move();
}
void move()
{
switch(dir)
{
case L:
x-=XSPEED;
break;
case LU:
x-=XSPEED;
y-=YSPEED;
break;
case U:
y-=YSPEED;
break;
case RU:
x+=XSPEED;
y-=YSPEED;
break;
case R:
x+=XSPEED;
break;
case RD:
x+=XSPEED;
y+=YSPEED;
break;
case D:
y+=YSPEED;
break;
case LD:
x-=XSPEED;
y+=YSPEED;
}
if(x<0||y<0||x>tgf.GAME_W||y>tgf.GAME_H)
{
tgf.balls.remove(this);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -