📄 playtanksprite.java
字号:
package tankgame611;
import java.applet.Applet;
import java.awt.Image;
import java.awt.event.*;
public class PlayTankSprite extends SpriteDrawing{
int AppletWidth, AppletHeight;
int myTankWidth, myTankHeight;
int direction;
int myTankX, myTankY;
Image myTankImg[];
Applet GameApplet;
static int speed = 5;
ShellSprite shell;
boolean keyIsReleased = false; //是否按键不放
public final int TO_LEFT=0;
public final int TO_RIGHT=1;
public final int TO_UP=2;
public final int TO_DOWN=3;
public PlayTankSprite(Image[] myTankImg,
int myTankStartX,
int myTankStartY,
ShellSprite shell, //炮弹的实例
Applet GameApplet) {
//调用父类的创建方法
super(myTankImg, myTankStartX, myTankStartY, GameApplet);
this.X = myTankStartX;
this.Y = myTankStartY;
this.myTankImg = myTankImg;
this.shell = shell;
this.GameApplet = GameApplet;
AppletWidth = GameApplet.getWidth();
AppletHeight = GameApplet.getHeight();
direction = TO_UP;
setVisible(true);
setMove(false);
//为Applet加上键盘事件侦听
GameApplet.addKeyListener(new keyAction());
}
//SpiritDirection : 0=左; 1=右; 2=上; 3=下;
public void updateState(int SpiritDirection) {
myTankWidth=myTankImg[0].getWidth(GameApplet);
myTankHeight=myTankImg[0].getHeight(GameApplet);
//移动坦克
switch (SpiritDirection) {
case TO_LEFT: //左
this.X=this.getX()-speed;
if(this.X<=1){
this.X=1;
}
if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,SpiritDirection)){
this.X=this.getCollisionX();
}
break;
case TO_RIGHT: //右
this.X=this.getX()+speed;
if(this.X>=AppletWidth-DrawGameState.StatusBarWidth-myTankWidth-1){
this.X=AppletWidth-DrawGameState.StatusBarWidth-myTankWidth-1;
}
if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,SpiritDirection)){
this.X=this.getCollisionX();
}
break;
case TO_UP: //上
this.Y=this.getY()-speed;
if(this.Y<=1){
this.Y=1;
}
if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,SpiritDirection)){
this.Y=this.getCollisionY();
}
break;
case TO_DOWN: //下
this.Y=this.getY()+speed;
if(this.Y>=AppletHeight-myTankHeight-1){
this.Y=AppletHeight-myTankHeight-1;
}
if(this.isCollide(TankGame611.TILE_WIDTH,TankGame611.TILE_HEIGHT,TankGame611.MAP_COLS,SpiritDirection)){
this.Y=this.getCollisionY();
}
break;
}
}
public int getTankDirection(){
return direction;
}
//调整炮弹的位置
public void setShellPos(int direction) {
switch (direction) {
case TO_LEFT: //左
shell.setLocation(this.X - shell.getWidth(),
this.Y + (myTankHeight - shell.getHeight()) / 2);
break;
case TO_RIGHT:
shell.setLocation(this.X + myTankWidth + shell.getWidth(),
this.Y + (myTankHeight - shell.getHeight()) / 2);
break;
case TO_UP:
shell.setLocation(this.X + (myTankHeight - shell.getHeight()) / 2,
this.Y - myTankWidth / 2);
break;
case TO_DOWN:
shell.setLocation(this.X + (myTankHeight - shell.getHeight()) / 2,
this.Y + myTankWidth);
break;
}
}
//使用内部类实现按键事件处理
class keyAction extends KeyAdapter {
long startTime, endTime;
public void keyPressed(KeyEvent e) {
startTime = System.currentTimeMillis();
myTankWidth = myTankImg[0].getWidth(GameApplet);
myTankHeight = myTankImg[0].getHeight(GameApplet);
if (e.getKeyCode() == e.VK_SPACE) { //发射炮弹的处理
if (keyIsReleased == true) { //释放了键(防止长按键不放)
shell.setShellDirection(direction);
setShellPos(direction);
shell.setVisible(true);
shell.setMove(true);
keyIsReleased = false; //未释放了键
}
}
if (e.getKeyCode() == e.VK_LEFT) {
direction = TO_LEFT;
setMove(true);
}
if (e.getKeyCode() == e.VK_RIGHT) {
direction = TO_RIGHT;
setMove(true);
}
if (e.getKeyCode() == e.VK_UP) {
direction = TO_UP;
setMove(true);
}
if (e.getKeyCode() == e.VK_DOWN) {
direction = TO_DOWN;
setMove(true);
}
}
public void keyReleased(KeyEvent e) {
endTime = System.currentTimeMillis();
if(endTime-startTime>40){ //防止快速按键切换带来的短暂停顿
setMove(false);
}
keyIsReleased = true; //释放了键
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -