?? playertanksprite.java~24~
字號:
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 = 704;
AppletHeight = 576;
direction = 2;
setVisible(true);
setMove(false);
//為Applet加上鍵盤事件偵聽
GameApplet.addKeyListener(new keyAction());
}
public void paint(Graphics g){
g.drawImage(myTankImg[nowFrame],myTankX,myTankY,GameApplet);
}
//SpiritDirection : 0=左; 1=右; 2=上; 3=下;
public void updateState(int moveDirection,
int TileWidth,
int TileHeight,
int mapCols){
AppletWidth = Game.getWidth();
AppletHeight = Game.getHeight();
int x, y;
switch (moveDirection) {
case 0: //往左
nowFrame = 0;
this.X = this.X - speed;
if (this.X <= 0) {
this.X = 0;
}
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 + -