📄 playertanksprite.java
字号:
package tankgame2007;
import java.applet.Applet;
import java.awt.Image;
import java.awt.event.*;
import java.awt.*;
public class PlayerTankSprite extends ImageSprite{
int AppletWidth, AppletHeight;
int myTankWidth, myTankHeight;
int direction;
int myTankX, myTankY;
Image myTankImg[];
Applet GameApplet;
static int speed = 5;
ShellSprite shell;
int nowFrame;
boolean keyIsReleased = false; //是否按键不放
public PlayerTankSprite(Image[] myTankImg,
int myTankX,
int myTankY,
ShellSprite shell, //炮弹的实例
Applet GameApplet) {
//调用父类的创建方法
super(myTankImg, myTankX, myTankY, GameApplet);
this.myTankX = myTankX;
this.myTankY = myTankY;
this.myTankImg = myTankImg;
this.shell = shell;
this.GameApplet = GameApplet;
AppletWidth =GameApplet.getWidth() ;
AppletHeight =GameApplet.getHeight();
direction = 2;
setVisible(true);
setMove(false);
//为Applet加上键盘事件侦听
GameApplet.addKeyListener(new keyAction());
}
//SpiritDirection : 0=左; 1=右; 2=上; 3=下;
public void updateState(int moveDirection,
int TileWidth,
int TileHeight,
int mapCols){
AppletWidth = 704;
AppletHeight = GameApplet.getHeight();
int x, y;
switch (moveDirection) {
case 0: //往左
nowFrame = 0;
this.X = this.X - speed;
if (this.X <= 0) {
this.X = 1;
}
isCollide(TileWidth, TileHeight,
mapCols, moveDirection);
if(this.isCollide(TileWidth, TileHeight,
mapCols, moveDirection)) {
this.X = this.getCollisionX();
}
break;
case 1: //往右
nowFrame = 1;
this.X = this.X + speed;
if (this.X >= AppletWidth - this.getWidth()) {
this.X = AppletWidth - this.getWidth()-1;
}
if (this.isCollide(TileWidth, TileHeight,
mapCols, moveDirection)) {
this.X = this.getCollisionX();
}
break;
case 2: //往上
nowFrame = 2;
this.Y = this.Y - speed;
if (this.Y <= 0) {
this.Y = 1;
}
if (this.isCollide(TileWidth, TileHeight,
mapCols, moveDirection)) {
this.Y = this.getCollisionY();
}
break;
case 3: //往下
nowFrame = 3;
this.Y = this.Y + speed;
if (this.Y >= AppletHeight - this.getHeight()) {
this.Y = AppletHeight - this.getHeight()-1;
}
if (this.isCollide(TileWidth, TileHeight,
mapCols, moveDirection)) {
this.Y = this.getCollisionY();
}
break;
}
}
public int getTankDirection(){
return direction;
}
//调整炮弹的位置
public void setShellPos(int direction) {
switch (direction) {
case 0: //左
shell.setLocation(this.X - shell.getWidth(),
this.Y + (this.getHeight() -
shell.getHeight()) / 2);
break;
case 1:
shell.setLocation(this.X + this.getWidth() + shell.getWidth(),
this.Y +
(this.getWidth() -
shell.getHeight()) / 2);
break;
case 2:
shell.setLocation(this.X +
(this.getHeight() - shell.getHeight()) / 2,
this.Y - this.getWidth() / 2);
break;
case 3:
shell.setLocation(this.X +
(this.getHeight() - shell.getHeight()) / 2,
this.Y + this.getWidth());
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) { //发射炮弹的处理
System.out.println(keyIsReleased);
if (keyIsReleased == true) { //释放了键(防止长按键不放)
shell.setShellDirection(direction);
setShellPos(direction);
shell.setVisible(true);
shell.setMove(true);
keyIsReleased = false; //未释放了键
}
}
if (e.getKeyCode() == e.VK_LEFT) {
direction = 0;
setMove(true);
}
if (e.getKeyCode() == e.VK_RIGHT) {
direction = 1;
setMove(true);
}
if (e.getKeyCode() == e.VK_UP) {
direction = 2;
setMove(true);
}
if (e.getKeyCode() == e.VK_DOWN) {
direction = 3;
setMove(true);
}
}
public void keyReleased(KeyEvent e) {
endTime = System.currentTimeMillis();
if(endTime-startTime>60){ //防止快速按键切换带来的短暂停顿
setMove(false);
}
keyIsReleased = true; //释放了键
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -