📄 startscreen.java
字号:
package java_game_a;
import java.awt.*;
import java.util.*;
public 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";
PressEnter="<<<==请按Enter进入游戏==>>>";
width=AppletWidth;
height=AppletHeight;
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;//指定目前图象为第一张图片
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);//绘制线段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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -