当前页面: 开发资料首页 → Java 专题 → java动画——游戏开头画面
java动画——游戏开头画面
摘要: java动画——游戏开头画面
java动画——游戏开头画面
// 程序:游戏开头画面
// 范例文件:HitPigHead.java
import java.awt.*;
import java.util.*; //为了使用其中的Random类
import java.applet.*;
class StartScreen //开始画面类
{
//=====资料成员==================================================
int width,height,StringWidth,StringHeight,Ascent,Descent,X,Y;
int ImageLeftBound,ImageRightBound,ImageX,ImageY,ImageWidth,
ImageHeight,VX;
Font F1,F2,F3;
Image Normal,bkImage,Hit,currentImage;
String ChineseTitle,EnglishTitle,PressEnter;
HitPigHead Game;
Random R;
boolean showPressEnter;
FontMetrics FM;
//=====函数成员==================================================
public StartScreen(int AppletWidth,int AppletHeight,HitPigHead Game,
Image normal,Image hit, Image bk)
{
R = new Random(); //用来取随机数的类
//绘制字符串需要用到的三种字型
F1 = new Font("TimesRoman",Font.BOLD,72);
F2 = new Font("TimesRoman",Font.BOLD + Font.ITALIC,36);
F3 = new Font("TimesRoman",Font.BOLD,20);
ChineseTitle = "棒打猪头"; //使用F1
EnglishTitle = "Hit Pig's Head"; //使用F2
PressEnter = "<<<==请按Enter键开始游戏==>>>"; //使用F3
width = AppletWidth; //Applet的宽度
height = AppletHeight; //Applet的高度
Normal = normal; //小猪图像1
Hit = hit; //小猪图像2
bkImage = bk; //背景图像
ImageWidth = Normal.getWidth(Game); //小猪图像的宽度
ImageHeight = Normal.getHeight(Game); //小猪图像的高度
ImageLeftBound = 25; //小猪图像移动的左边界
ImageRightBound = AppletWidth - (25 + ImageWidth); //右边界
ImageX = ImageRightBound; //小猪图像的起始位置
VX = -3; //图像移动的速度
this.Game = Game;
currentImage = Normal; //指定目前图像为小猪图像1
showPressEnter = true; //显示PressEnter字符串
}
public void UpdateStatus() //更新动画状态的函数
{
ImageX = ImageX + VX; //指定图像的新位置
if(ImageX <= ImageLeftBound) //如果碰到左边界的话
{
currentImage = Hit; //指定目前图像为小猪图像2
ImageX = ImageLeftBound; //设定图像的新位置
VX = -VX; //改变图像移动方向
}
if(ImageX >= ImageRightBound) //如果碰到右边界的话
{
currentImage = Normal; //指定目前图像为小猪图像1
ImageX = ImageRightBound; //设定图像的新位置
VX = -VX; //改变图像移动方向
}
//利用随机数来让字符串闪动
if(showPressEnter == true)
{
if((R.nextInt(5) + 1) % 5 == 0)
showPressEnter = false;
}
else
{
if((R.nextInt(5) + 1) % 5 == 0)
showPressEnter = true;
}
}
public void paintScreen(Graphics g) //绘制动画的函数
{
g.clearRect(0,0,width,height); //清除次画面
g.setFont(F1); //设定字型
FM = g.getFontMetrics();
Ascent = FM.getAscent();
Descent = FM.getDescent();
StringWidth = FM.stringWidth(ChineseTitle);
StringHeight = Ascent + Descent;
X = (width - StringWidth) / 2;
Y = Ascent;
g.drawImage(bkImage, 0, 0, Game);
g.setColor(Color.white); //设定前景色为白色
g.drawString(ChineseTitle,X,Y); //绘制中文标题
Y = StringHeight;
g.drawLine(X,Y,X+StringWidth,Y); //绘制线段1
X = X + 30;
Y = Y + 5;
g.drawLine(X,Y,X+StringWidth-60,Y);//绘制线段2
//===============================================================
g.setFont(F2); //设定字型
FM = g.getFontMetrics();
Ascent = FM.getAscent();
Descent = FM.getDescent();
StringWidth = FM.stringWidth(EnglishTitle);
StringHeight = Ascent + Descent;
X = (width - StringWidth) / 2;
Y = Y + Ascent;
g.drawString(EnglishTitle,X,Y); //绘制英文标题
//===============================================================
ImageY = Y + Descent + 30;
g.drawImage(currentImage,ImageX,ImageY,Game); //绘制小猪图像
//===============================================================
g.setFont(F3); //设定字型
FM = g.getFontMetrics();
Ascent = FM.getAscent();
Descent = FM.getDescent();
StringWidth = FM.stringWidth(PressEnter);
StringHeight = Ascent + Descent;
X = (width - StringWidth) / 2;
Y = ImageY + ImageHeight + Ascent + 30;
if(showPressEnter)
g.drawString(PressEnter,X,Y); //绘制PressEnter字符串
}
}
public class HitPigHead extends Applet implements Runnable
{
//=====资料成员==================================================
int AppletWidth,AppletHeight;
Image OffScreen,bkImage,PigHead_Normal,PigHead_Hit;
Thread newThread;
Graphics drawOffScreen;
StartScreen S_Screen;
MediaTracker MT;
//=====函数成员==================================================
public void init()
{
setBackground(Color.black); //设定背景颜色
//加载小猪图像1和2(使用MediaTracker)与背景
PigHead_Normal = getImage(getDocumentBase(),"Images/pig1.gif");
PigHead_Hit = getImage(getDocumentBase(),"Images/pig2.gif");
bkImage = getImage(getDocumentBase(),"Images/009.jpg");
MT = new MediaTracker(this);
MT.addImage(PigHead_Normal,0);
MT.addImage(PigHead_Hit,0);
try
{
showStatus("图像加载中(Loading Images)...");
MT.waitForAll();
}
catch(InterruptedException E){ } //没有进行例外处理
AppletWidth = getSize().width; //取得Applet的高度
AppletHeight = getSize().height; //取得Applet的宽度
//建立开始画面
S_Screen = new StartScreen(AppletWidth,AppletHeight,this,
PigHead_Normal,PigHead_Hit, bkImage);
//建立次画面
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start() //start()函数
{
newThread = new Thread(this); //建立与启动新线程
newThread.start();
}
public void stop() //stop()函数
{
newThread = null; //将线程设为null
}
public void paint(Graphics g)
{
//如果图像下载发生任何错误的话...
if((MT.statusAll(false) & MediaTracker.ERRORED) != 0)
{
FontMetrics FM = g.getFontMetrics();
int Ascent = FM.getAscent();
int Descent = FM.getDescent();
int StringWidth = FM.stringWidth("加载图像发生错误...");
int X = (AppletWidth - StringWidth) / 2;
int Y = (AppletHeight - (Ascent + Descent)) / 2 + Ascent;
g.setColor(Color.white); //字符串设为白色
g.drawString("加载图像发生错误...",X,Y); //置中显示字符串
return; //结束函数执行
}
//将次画面的绘制工具输入以在次画面中绘制图像
S_Screen.paintScreen(drawOffScreen);
//将次画面贴到主画面中
g.drawImage(OffScreen,0,0,this);
}
public void update(Graphics g) //update()函数
{
paint(g); //只单纯调用paint()函数
}
public void run()
{
while(newThread != null) //动画循环
{
repaint(); //重新绘制图像
try
{
Thread.sleep(80); //暂停程序执行80毫秒
}
catch(InterruptedException E){ }//没有进行例外处理
S_Screen.UpdateStatus(); //更新开始画面的动画状态
}
}
}