📄 hitpigheadv3.java
字号:
pigSprite[i].paintSprite(drawOffScreen);
hammerSprite.paintSprite(drawOffScreen);
}
g.drawImage(OffScreen,0,35,this);
}
public void update(Graphics g)
{
paint(g);
}
public void run()
{
while(newThread != null)
{
repaint();
try
{
Thread.sleep(33);
}
catch(InterruptedException E){ }
if(StartGame)
S_Screen.UpdateStatus();
else
{
time = new GregorianCalendar();
if(CurrentSecond != time.get(Calendar.SECOND))
{
CurrentSecond = time.get(Calendar.SECOND);
GameSecond++;
Time.setText("时间: " + GameSecond + "秒");
if(GameSecond > 0 && GameSecond % 5 == 0 && level < 50)
{
for(int i=0;i<9;i++)
{
pigSprite[i].setShow(50 + (level * 5));
pigSprite[i].setHide(9900 - (level * 50));
}
level++;
showStatus("level: " + level);
}
}
for(int i=0;i<9;i++)
pigSprite[i].updateState();
}
}
}
public void endGame(boolean isEndGame)
{
EndGame = isEndGame;
}
public void mouseExited(MouseEvent e)
{
hammerSprite.setVisible(false);
hammerSprite.setLocation(0,0);
}
public void mouseClicked(MouseEvent e){ }
public void mouseEntered(MouseEvent e)
{
hammerSprite.setVisible(true);
hammerSprite.setLocation(e.getX() - (HammerWidth / 2),
e.getY() - (HammerHeight / 2) - 35);
showStatus("X:" + e.getX() + "," + "Y:" + e.getY());
}
public void mousePressed(MouseEvent e)
{
if(StartGame) return;
hammerSprite.updateState();
int X = hammerSprite.getX();
int Y = hammerSprite.getY();
for(int i=0;i<9;i++)
{
if(pigSprite[i].hit(X,Y,FrameWidth,FrameHeight,HammerWidth,
HammerHeight) == true)
{
score = score + 10;
Score.setText("得分: " + score);
}
}
}
public void mouseReleased(MouseEvent e)
{
if(StartGame) return;
hammerSprite.updateState();
}
public void mouseMoved(MouseEvent e)
{
hammerSprite.setLocation(e.getX() - (HammerWidth / 2),
e.getY() - (HammerHeight / 2) - 35);
showStatus("X:" + e.getX() + "," + "Y:" + e.getY());
}
public void mouseDragged(MouseEvent e)
{
hammerSprite.setLocation(e.getX() - (HammerWidth / 2),
e.getY() - (HammerHeight / 2) - 35);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == start)
{
StartGame = false;
start.setEnabled(false);
end.setEnabled(true);
}
if(e.getSource() == end)
{
newThread = null;
CD.show();
if(EndGame)
{
score = 0;
GameSecond = 0;
CurrentSecond = -1;
StartGame = true;
EndGame = false;
start.setEnabled(true);
end.setEnabled(false);
Time.setText("时间: 0");
Score.setText("得分: 0");
}
newThread = new Thread(this);
newThread.start();
}
}
}
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,Hit,currentImage;
String ChineseTitle,EnglishTitle,PressEnter;
Applet Game;
boolean showPressEnter;
FontMetrics FM;
public StartScreen(int AppletWidth,int AppletHeight,Applet Game,
Image normal,Image hit)
{
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";
width = AppletWidth;
height = AppletHeight;
Normal = normal;
Hit = hit;
ImageWidth = Normal.getWidth(Game);
ImageHeight = Normal.getHeight(Game);
ImageLeftBound = 25;
ImageRightBound = AppletWidth - (25 + ImageWidth);
ImageX = ImageRightBound;
VX = -2;
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;
}
}
public void paintScreen(Graphics g)
{
g.setFont(F1);
FM = g.getFontMetrics(); //获取当前字体的字体规格。
Ascent = FM.getAscent();/*确定由此 FontMetrics 对象所描述的 Font 的 font ascent。
font ascent 是字体的 baseline 到大多数字母数字字符顶部的距离。在 Font 中,
有些字符可能扩展超过 font ascent 线。
返回:
Font 的 font ascent。*/
Descent = FM.getDescent();/*确定由此 FontMetrics 对象所描述的 Font 的 font descent。
font descent 是字体的 baseline 到大多数字母数字字符底部的距离。
在 Font 中,有些字符可能扩展到 font descent 线之下。
返回:
Font 的 font descent。
*/
StringWidth = FM.stringWidth(ChineseTitle);
/*返回此 Font 中指定 String 的总 advance width。advance 是字符串
* 的 baseline 上最左边的点到最右边的点之间的距离。*/
StringHeight = Ascent + Descent;
X = (width - StringWidth) / 2;
Y = Ascent;
g.setColor(Color.black);
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);
}
}
class CloseDialog extends Dialog implements ActionListener//关闭时显示的对话框
{
Panel P1,P2;//组件容器
JButton B1,B2;//添加确定和取消按钮
HitPigHeadv3 Game;
public CloseDialog(HitPigHeadv3 Game,Frame owner)//构造函数
{
super(owner,"离开游戏...",true); //设置标题
this.Game = Game;
setLayout(new GridLayout(2,1)); //设置布局管理为GrideLayout
P1 = new Panel();
P2 = new Panel();
P1.add(new Label("真的要离开吗???"));
P2.add(B1 = new JButton("确定"));
P2.add(B2 = new JButton("取消"));
B1.addActionListener(this);
B2.addActionListener(this);
add(P1);
add(P2);
pack(); //以最紧凑的方式显示组件
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == B1)
Game.endGame(true);
else if(e.getSource() == B2)
Game.endGame(false);
//判断事件源
hide();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -