?? ai1.java
字號:
// 程序:角色追蹤移動
// 范例文件:AI1.java
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
class Sprite
{
int spriteX,spriteY,VX,VY;
AI1 Game;
Image UFO, Beast;
public Sprite(Image UFO, Image Beast,AI1 Game)
{
this.Game = Game;
this.UFO = UFO;
this.Beast = Beast;
spriteX = 0;
spriteY = 0;
VX = 0;
VY = 0;
}
public void updateState()
{
if(spriteX + 25 > Game.getX())
VX = -1;
else if(spriteX + 25 < Game.getX())
VX = 1;
else
VX = 0;
if(spriteY + 25 > Game.getY())
VY = -1;
else if(spriteY + 25 < Game.getY())
VY = 1;
else
VY = 0;
spriteX = spriteX + VX;
spriteY = spriteY + VY;
}
public void paintSprite(Graphics g)
{
g.drawImage(Beast,spriteX,spriteY,80,80,Game);
g.drawImage(UFO,Game.getX(),Game.getY(),50, 50,Game);
}
}
public class AI1 extends Applet
implements Runnable,MouseListener,MouseMotionListener
{
int mouseX,mouseY,AppletWidth,AppletHeight;
Image OffScreen, UFO, Beast;
Thread newThread;
Graphics drawOffScreen;
boolean update;
Sprite S;
public void init()
{
mouseX = 0;
mouseY = 0;
AppletWidth = getSize().width;
AppletHeight = getSize().height;
update = false;
UFO = getImage(getDocumentBase(),"Images/6.gif");
Beast = getImage(getDocumentBase(),"Images/1.gif");
S = new Sprite(UFO,Beast,this); //建立追蹤Sprite
addMouseListener(this); //注冊事件處理函數(shù)
addMouseMotionListener(this);
//建立次畫面
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start() //start()函數(shù)
{
newThread = new Thread(this); //建立與啟動新線程
newThread.start();
}
public void stop() //stop()函數(shù)
{
newThread = null; //將線程設(shè)為null
}
public void paint(Graphics g)
{
//只清除此部分區(qū)域的圖像
drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);
//繪制Sprite到次畫面中
S.paintSprite(drawOffScreen);
//將次畫面貼到主畫面上
g.drawImage(OffScreen,0,0,this);
}
public void update(Graphics g) //update()函數(shù)
{
paint(g); //只單純呼叫paint()函數(shù)
}
public void run()
{
while(newThread != null)
{
repaint(); //重繪圖像
try
{
Thread.sleep(33); //暫停33毫秒
}
catch(InterruptedException E){ }
if(update)
S.updateState(); //更改Sprite
}
}
//==================================================================
public int getX() //讓Sprite取得鼠標X坐標
{
return mouseX;
}
public int getY() //讓Sprite取得鼠標Y坐標
{
return mouseY;
}
//=====實現(xiàn)MouseListener界面=======================================
public void mouseExited(MouseEvent e) //鼠標離開Component
{
update = false; //不更改Sprite
}
public void mouseClicked(MouseEvent e){} //鼠標按鍵被按下后放開
public void mouseEntered(MouseEvent e) //鼠標進入Component
{
update = true; //更改Sprite
}
public void mousePressed(MouseEvent e){} //鼠標按鍵被按下
public void mouseReleased(MouseEvent e){} //鼠標按鍵放開
//=====實現(xiàn)MouseMotionListener界面=================================
public void mouseMoved(MouseEvent e) //鼠標移動時
{
mouseX = e.getX(); //取得鼠標X坐標
mouseY = e.getY(); //取得鼠標Y坐標
showStatus("(" + mouseX + "," + mouseY + ")");
}
public void mouseDragged(MouseEvent e){} //鼠標拖曳時
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -