?? gameanimation.java
字號:
// 程序:角色碰撞實例
// 范例文件:GameAnimation.java
import java.awt.*;
import java.applet.*;
class Sprite1
{
public int X,Y,width,height,VX,VY;
int AppletWidth,AppletHeight;
boolean visible;
Image UFO; // 小飛碟
public Sprite1(int AppletWidth,int AppletHeight)
{
this.AppletWidth = AppletWidth; //Applet高度
this.AppletHeight = AppletHeight; //Applet寬度
X = AppletWidth / 2; //起始X坐標(biāo)
Y = 0; //起始Y坐標(biāo)
VX = -4; //X軸速度
VY = -3; //Y軸速度
width = 30; //Sprite高度
height = 30; //Sprite寬度
visible = true; //可見
}
public void updateState(Sprite2 s) //轉(zhuǎn)換Sprite狀態(tài)的方法
{
X = X + VX; //移動X軸方向
Y = Y + VY; //移動Y軸方向
//碰撞偵測,若Sprite1和Sprite2相撞的話則改變速度為反方向
if((X + width >= s.X) && (Y + height >= s.Y) &&
(s.X + s.width >= X) && (s.Y + s.height >= Y))
{
VX = -VX;
VY = -VY;
s.VX = -s.VX;
s.VY = -s.VY;
}
//下面的if-else判斷式用來設(shè)定Sprite1的邊界動作
if(X < 0)
{
X = 0;
VX = -VX;
}
else if(X > AppletWidth - width)
{
X = AppletWidth - width;
VX = -VX;
}
if(Y < 0)
{
Y = 0;
VY = -VY;
}
else if(Y > AppletHeight - height)
{
Y = AppletHeight - height;
VY = -VY;
}
}
public void paintSprite(Graphics g, Applet Game) //繪制Sprite本身的方法
{
if(visible)
g.drawImage(UFO,X,Y,width,height,Game);
}
}
class Sprite2
{
public int X,Y,width,height,VX,VY;
int AppletWidth,AppletHeight;
boolean visible;
Image beast; // 大怪獸
public Sprite2(int AppletWidth,int AppletHeight)
{
this.AppletWidth = AppletWidth; //Applet高度
this.AppletHeight = AppletHeight; //Applet寬度
//設(shè)定Sprite2的位置、速度與大小
X = AppletWidth - width;
Y = AppletHeight - height;
VX = 5;
VY = 2;
width = 60;
height = 60;
visible = true;
}
public void updateState(Sprite1 s) //轉(zhuǎn)換Sprite狀態(tài)的方法
{
X = X + VX;
Y = Y + VY;
//下面的if-else判斷式用來設(shè)定Sprite的邊界動作
if(X + width < 0)
{
X = AppletWidth;
}
else if(X > AppletWidth)
{
X = -width;
}
if(Y < 0)
{
Y = 0;
VY = -VY;
}
else if(Y > AppletHeight - height)
{
Y = AppletHeight - height;
VY = -VY;
}
}
public void paintSprite(Graphics g, Applet Game) //繪制Sprite本身的方法
{
if(visible)
g.drawImage(beast,X,Y,width,height,Game);
}
}
public class GameAnimation extends Applet implements Runnable
{
int AppletWidth,AppletHeight;
Image OffScreen, bk;
Thread newThread;
Graphics drawOffScreen;
Sprite1 a;
Sprite2 b;
public void init()
{
setBackground(Color.white);
AppletWidth = getSize().width; //Applet的寬度
AppletHeight = getSize().height; //Applet的高度
a = new Sprite1(AppletWidth,AppletHeight); //建立Sprite1
b = new Sprite2(AppletWidth,AppletHeight); //建立Sprite2
a.UFO = getImage(getDocumentBase(),"Images/6.gif");
b.beast = getImage(getDocumentBase(),"Images/1.gif");
// 星空背景
bk = getImage(getDocumentBase(),"Images/009.jpg");
//建立次畫面
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start() //start()方法
{
newThread = new Thread(this); //建立與啟動新線程
newThread.start();
}
public void stop() //stop()方法
{
newThread = null; //將線程設(shè)為null
}
public void paint(Graphics g)
{
drawOffScreen.drawImage(bk,0,0,AppletWidth,AppletHeight,this);
a.paintSprite(drawOffScreen, this); //將Sprite1繪制在次畫面中
b.paintSprite(drawOffScreen, this); //將Sprite1繪制在次畫面中
//將次畫面貼到主畫面上
g.drawImage(OffScreen,0,0,this);
}
public void update(Graphics g) //update()方法
{
paint(g); //只單純調(diào)用paint()方法
}
public void run()
{
while(newThread != null)
{
repaint(); //重繪圖像
try
{
Thread.sleep(80); //暫停80毫秒
}
catch(InterruptedException E){ }
a.updateState(b); //更換Sprite狀態(tài)
b.updateState(a);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -