⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hitpighead.java

📁 Java案例开发集锦,里面提供了很好的学习例子
💻 JAVA
字号:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;

abstract class SuperSprite
{
   int     X,Y,width,height;
   boolean visible,active;

   abstract public void paintSprite(Graphics g);
   abstract public void updateState();

   public int getX()
   {
      return X;
   }

   public int getY()
   {
      return Y;
   }

   public void setLocation(int X,int Y)
   {
      this.X = X;
      this.Y = Y;
   }

   public int getWidth()
   {
      return width;
   }

   public int getHeight()
   {
      return height;
   }

   public void setSize(int width,int height)
   {
      this.width  = width;
      this.height = height;
   }

   public boolean canVisible()
   {
      return visible;
   }

   public void setVisible(boolean v)
   {
      visible = v;
   }

   public boolean canMove()
   {
      return active;
   }

   public void setMove(boolean m)
   {
      active = m;
   }
}

class LovelyPig extends SuperSprite
{
   int     seed;
   Image   SpriteImage,Frame;
   Applet  Game;
   Random  R;
   boolean showPig;

   public LovelyPig(Image SpriteImage,Image Frame,Applet Game)
   {
      R = new Random();

      this.SpriteImage = SpriteImage;
      this.Frame       = Frame;
      this.Game        = Game;
      showPig          = false;

      setVisible(true);
      setMove(true);
   }

   public void updateState()
   {
      if(active == true)
      {

         if(R.nextInt(seed) % 100 < 1)
         {
            if(showPig == false)
               showPig = true;
         }
         else if(R.nextInt(seed) % 100 > 95)
         {
            if(showPig == true)
               showPig = false;
         }
      }
   }

   public void paintSprite(Graphics g)
   {
      if(visible == true)
      {
         g.drawImage(Frame,X,Y,Game);

         if(showPig == true)
            g.drawImage(SpriteImage,X + 12,Y + 18,Game);
      }
   }

   public void setSeed(int seed)
   {
      this.seed = seed;
   }


   public boolean hit(int X,int Y,int P_Width,int P_Height,int H_Width,
                      int H_Height)
   {
      if((this.X + P_Width >= X) && (this.Y + (P_Height / 2) >= Y) &&
         (X + (H_Width / 2) >= this.X) && (Y + (H_Height / 2) >= this.Y)
         && showPig)
      {
         showPig = false;
         return true;
      }
      else
         return false;
   }
}

class Hammer extends SuperSprite
{
   Image  hammer1,hammer2,currentImage;
   Applet Game;

   public Hammer(Image hammer1,Image hammer2,Applet Game)
   {
      this.hammer1 = hammer1;
      this.hammer2 = hammer2;
      this.Game    = Game;

      currentImage = hammer1;

      setLocation(0,0);
      setVisible(false);
      setMove(false);
   }

   public void updateState()
   {

      if(currentImage == hammer1)
         currentImage = hammer2;
      else
         currentImage = hammer1;
   }

   public void paintSprite(Graphics g)
   {
      if(visible == true)
         g.drawImage(currentImage,X,Y,Game);
   }
}


public class HitPigHead extends Applet
       implements Runnable,MouseListener,MouseMotionListener,ActionListener
{
   int          AppletWidth,AppletHeight,FrameWidth,FrameHeight,
                countX,countY,HammerWidth,HammerHeight,score,
                CurrentSecond,GameSecond;
   Image        frame,pig,hammer1,hammer2,OffScreen,PigHead1,bkImage,
                PigHead2;
   Thread       newThread;
   Graphics     drawOffScreen;
   MediaTracker MT;

   LovelyPig    pigSprite[];
   Hammer hammerSprite;

   Panel        Status,Control;
   Label        Time,Score;
   Button       start,end;
   boolean      StartGame,EndGame;
   StartScreen  S_Screen;
   CloseDialog  CD;

   GregorianCalendar time;

   public void init()
   {
      addMouseListener(this);
      addMouseMotionListener(this);

      CD      = new CloseDialog(this,new Frame());

      Time    = new Label("时间: 0");
      Score   = new Label("得分: 0");
      end     = new Button("结束游戏");
      start   = new Button("开始游戏");
      end.addActionListener(this);
      start.addActionListener(this);
      end.setEnabled(false);

      Status  = new Panel();
      Control = new Panel();
      Status.setLayout(new GridLayout(1,2));
      Control.setLayout(new GridLayout(1,2));

      Status.add(Time);
      Status.add(Score);
      Control.add(start);
      Control.add(end);

      setLayout(new BorderLayout());
      add(Status,BorderLayout.NORTH);
      add(Control,BorderLayout.SOUTH);

      AppletWidth   = getSize().width;
      AppletHeight  = getSize().height;
      countX        = 3;
      countY        = 3;
      score         = 0;
      GameSecond    = 0;
      CurrentSecond = -1;
      StartGame     = true;
      EndGame       = false;


      MT       = new MediaTracker(this);
      pig      = getImage(getDocumentBase(),"Img/pig.gif");
      frame    = getImage(getDocumentBase(),"Img/frame.gif");
      hammer1  = getImage(getDocumentBase(),"Img/hammer1.gif");
      hammer2  = getImage(getDocumentBase(),"Img/hammer2.gif");
      PigHead1 = getImage(getDocumentBase(),"Img/pighead1.gif");
      PigHead2 = getImage(getDocumentBase(),"Img/pighead2.gif");
      bkImage        = getImage(getDocumentBase(),"Img/back.jpg");
      MT.addImage(pig,0);
      MT.addImage(frame,0);
      MT.addImage(hammer1,0);
      MT.addImage(hammer2,0);
      MT.addImage(PigHead1,0);
      MT.addImage(PigHead2,0);
      MT.addImage(bkImage,0);

      try
      {
         MT.waitForAll();
      }
      catch(InterruptedException E){ }

      FrameWidth  = frame.getWidth(this);
      FrameHeight = frame.getHeight(this);

      pigSprite   = new LovelyPig[9];
      for(int i=0;i<9;i++)
      {

         pigSprite[i] = new LovelyPig(pig,frame,this);


         pigSprite[i].setLocation(i%countX*FrameWidth,
                                  i/countY*FrameHeight);

         pigSprite[i].setSeed(i+100);
      }


      hammerSprite   = new Hammer(hammer1,hammer2,this);
      HammerWidth    = hammer1.getWidth(this);
      HammerHeight   = hammer1.getHeight(this);


      S_Screen = new StartScreen(AppletWidth,AppletHeight,this,
                                 PigHead1,PigHead2,bkImage);


      OffScreen      = createImage(AppletWidth,AppletHeight);
      drawOffScreen  = OffScreen.getGraphics();

      resize(FrameWidth*countX,FrameHeight*countY + 70);
   }

   public void start()
   {
      newThread = new Thread(this);
      newThread.start();
   }

   public void stop()
   {
      newThread = null;
   }

   public void paint(Graphics g)
   {

      drawOffScreen.clearRect(0,0,AppletWidth,AppletHeight);

      if(StartGame)

         S_Screen.paintScreen(drawOffScreen);
      else
      {

         for(int i=0;i<9;i++)
            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 + "秒");
            }

            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,bkImage;
   String  ChineseTitle,EnglishTitle,PressEnter;
   Applet  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";

      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              = -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();
      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);
   }
}

class CloseDialog extends Dialog implements ActionListener
{
   Panel        P1,P2;
   Button       B1,B2;
   HitPigHead Game;

   public CloseDialog(HitPigHead Game,Frame owner)
   {
      super(owner,"离开游戏...",true);

      this.Game = Game;

      setLayout(new GridLayout(2,1));
      P1 = new Panel();
      P2 = new Panel();

      P1.add(new Label("退出游戏?"));
      P2.add(B1 = new Button("确定"));
      P2.add(B2 = new Button("取消"));
      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 + -