📄 hitpighead.java
字号:
// 程序:游戏开头画面
// 范例文件:HitPigHead.java
import java.awt.*;
import java.util.*;
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 = "棒打猪头";
EnglishTitle = "Hit Pig's Head";
PressEnter = "<<<==请按Enter键开始游戏==>>>";
width = AppletWidth;
height = AppletHeight;
Normal = normal;
Hit = hit;
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;
showPressEnter = true;
}
public void UpdateStatus()
{
ImageX = ImageX + VX;
if(ImageX <= ImageLeftBound)
{
currentImage = Hit;
ImageX = ImageLeftBound;
VX = -VX;
}
if(ImageX >= ImageRightBound)
{
currentImage = Normal;
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);
X = X + 30;
Y = Y + 5;
g.drawLine(X,Y,X+StringWidth-60,Y);
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);
}
}
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);
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;
AppletHeight = getSize().height;
S_Screen = new StartScreen(AppletWidth,AppletHeight,this,
PigHead_Normal,PigHead_Hit, bkImage);
OffScreen = createImage(AppletWidth,AppletHeight);
drawOffScreen = OffScreen.getGraphics();
}
public void start()
{
newThread = new Thread(this);
newThread.start();
}
public void stop()
{
newThread = 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)
{
paint(g);
}
public void run()
{
while(newThread != null)
{
repaint();
try
{
Thread.sleep(80);
}
catch(InterruptedException E){ }
S_Screen.UpdateStatus();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -