?? 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;
Thread newThread;
static int speed=5;
public PlayTankSprite(Image[] myTankImg,int myTankX,int myTankY,
Applet GameApplet) {
super(myTankImg,myTankX,myTankY,GameApplet);
this.myTankX=myTankX;
this.myTankY=myTankY;
this.myTankImg=myTankImg;
myTankWidth=myTankImg[0].getWidth(GameApplet);
myTankHeight=myTankImg[0].getHeight(GameApplet);
this.GameApplet=GameApplet;
AppletWidth=GameApplet.getWidth();
AppletHeight=GameApplet.getHeight();
setVisible(true);
setActive(false);
//為Applet加上鍵盤監聽偵聽
GameApplet.addKeyListener(new KeyAction());
}
public void updatePos(int SpriteDirection){
switch(SpriteDirection){
case 0:
myTankX=this.getX()-speed;
if(myTankX<0)
myTankX=0;
break;
case 1:
myTankX=this.getX()+speed;
if(myTankX>AppletWidth-myTankWidth)
myTankX=AppletWidth-myTankWidth;
break;
case 2:
myTankY=this.getY()-speed;
if(myTankY<0)
myTankY=0;
break;
case 3:
myTankY=this.getY()+speed;
if(myTankY>AppletHeight-myTankHeight)
myTankY=AppletHeight-myTankHeight;
break;
}
this.setPos(myTankX,myTankY);
}
//啟用本類線程的接口方法
/* public void startRunning(){
newThread=new Thread(this);
newThread.start();
}
public void stopRunning(){
newThread=null;
}
public void run(){
while(newThread!=null){
GameApplet.repaint();//調用主類的paint方法重繪畫面
try{Thread.sleep(10);}
catch(InterruptedException E){}
if(this.active==true){
updatePos(direction);
}
}
}*/
public int getTankDirection(){
return direction;
}
//使用內部類實現按鍵事件處理
class KeyAction extends KeyAdapter{
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==e.VK_SPACE){
}
if(e.getKeyCode()==e.VK_LEFT){
direction=0;
setActive(true);
}
if(e.getKeyCode()==e.VK_RIGHT){
direction=1;
setActive(true);
}
if(e.getKeyCode()==e.VK_UP){
direction=2;
setActive(true);
}
if(e.getKeyCode()==e.VK_DOWN){
direction=3;
setActive(true);
}
}
public void keyRealeased(KeyEvent e){
setActive(false);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -