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

📄 realghost.java

📁 用Java做的小游戏:Pacman 和小时候玩的吃豆豆一样
💻 JAVA
字号:
import cs101.lang.*;import java.util.*;import java.awt.*;import gaming.*;public class RealGhost extends Ghost implements ScreenObject{   Pacman pacman;    protected java.awt.Color myColor;    protected boolean tag;    public RealGhost(int x,int y,Pacman p){        super(x, y,p);        pacman = p;        myColor =  java.awt.Color.cyan;        }    protected int ghostDirection = 1;    WalledSquare[][] walls;    public int getX() {	return x;    }        public int getY() {	return y;     }         public Color getColor() {	return myColor;    }   /*    * The four functions below is used to change the direction of the ghosts moving     */     public void ghost_Direction_1()    {     WalledSquare[][] walls = PacmanBoard.getWalls();     if(ghostDirection == 1)      {         if(!walls[x][y].hasLowerWall())         {              ghostDirection = 4;             y++;            }//change the direction to the down     else if(!walls[x][y].hasRightWall())      x++;     else ghostDirection = 2;    }     }      public void ghost_Direction_2()     {        walls=PacmanBoard.getWalls();       if(ghostDirection == 2) {if(!walls[x][y].hasRightWall())      {          ghostDirection = 1;          x++;        }       else if(!walls[x][y].hasUpperWall())          y--;       else ghostDirection = 3;    }      }      public void ghost_Direction_3()      {       walls=PacmanBoard.getWalls();       if(ghostDirection == 3)        {           if(!walls[x][y].hasUpperWall())             {ghostDirection = 2;               y--;            }            else if(!walls[x][y].hasLeftWall())            x--;            else ghostDirection = 4;        }       }       public void ghost_Direction_4()       {             WalledSquare[][] walls = PacmanBoard.getWalls();           if(ghostDirection == 4)           {                if(!walls[x][y].hasLeftWall())                 {                   ghostDirection = 3;                   x--;                }           else if(!walls[x][y].hasLowerWall())              y++;           else ghostDirection = 1;        }       }    public void move(){    walls = PacmanBoard.getWalls();    if(pacman.getX()<(x+4)&&pacman.getX() >(x-4)&&pacman.getY()>(y-4)&&pacman.getY()<(y+4)){           tag = true;          }     else tag = false;   if(tag == true){        Chase(pacman);    }//if the pacman is in the range of 4*4 square of the ghost,the ghost will chase him   else switch(ghostDirection){        case 1:         ghost_Direction_1();        ghost_Direction_2();        ghost_Direction_3();        break;        case 2:         ghost_Direction_2();        ghost_Direction_3();        ghost_Direction_4();        break;        case 3:         ghost_Direction_3();        ghost_Direction_4();        ghost_Direction_1();        break;        case 4:        ghost_Direction_4();        ghost_Direction_1();        ghost_Direction_2();        break;        default:       }        PacmanBoard.repaint();     } }    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -