📄 gameboard.java
字号:
/**
* PacMan for J2ME Devices
* CS 327 - Design Project, Fall 2002
* University of Illinois, Urbana-Champaign
*
* file: GameBoard.java
* contact: Braden Kowitz
* date: 11/24/02
**/
//----------------------------------------------------------------------------//
import javax.microedition.lcdui.*;
import java.util.Vector;
/**
* A gameborad consists of a vector of GridNodes, and actors.
**/
public class GameBoard
{
//--------------------------//
// ENUM
//--------------------------//
/**
* Enum for a desired state
**/
public static final int ALIVE = 0;
public static final int DEAD = 1;
//--------------------------//
// MEMBER VARIABLES
//--------------------------//
/**
* A vector containing all of the GridNodes in this board.
**/
private Vector nodes_;
/**
* A vector containing all of the active GhostActors on this board.
**/
private Vector ghosts_;
/**
* The pacman for this board
**/
private PacmanActor pacman_;
/**
* The state of the GameBoard
* - state can be : ALIVE, DEAD
**/
private int state_;
//--------------------------//
// FUNCTIONS
//--------------------------//
/**
* Simple constructor, takes no parameters.
**/
public GameBoard()
{
nodes_ = new Vector();
ghosts_ = new Vector();
state_ = ALIVE;
}
/**
* Adds a GridNode to this board.
**/
public void addGridNode(GridNode n)
{
nodes_.addElement(n);
}
/**
* Paints the board by calling paint on all GridNodes,
* pacman, and each ghost.
* @param g Graphics object where drawing should take place.
*/
public void paint(Graphics g)
{
for (int i=0; i<nodes_.size(); i++)
{
GridNode n = (GridNode) nodes_.elementAt(i);
n.paint(g);
}
pacman_.paint(g);
for (int i=0; i<ghosts_.size(); i++)
{
GhostActor ghost = (GhostActor) ghosts_.elementAt(i);
ghost.paint(g);
g.drawString(Integer.toString(pacman_.score_), 68, 65, 16|4);
g.drawString(Integer.toString(pacman_.getLivesLeft()), 58, 65, 16|4);
}
}
/**
* advance all actors one frame:
* @return true if pacman has enough lives otherwise false
**/
public boolean advanceFrame()
{
// and ghosts:
for (int i=0; i<ghosts_.size(); i++)
{
GhostActor ghost = (GhostActor) ghosts_.elementAt(i);
//March 18, 2003
//Moved .advanceframe functions to try to get killing correct
// (when pacman and ghost are in the same node, visually) codewise this is correct.
//if we use .getNode(), pacman gets killed before we see the ghost touch him
//if (ghost.getNode() == pacman_.getNode())
if ((Math.abs(ghost.getX() - pacman_.getX()) < 3) && (Math.abs(ghost.getY() - pacman_.getY()) < 3))
{
if(pacman_.isInvincible() == false)
{
state_ = DEAD;
}
else
{
pacman_.score_+=5;
ghost.restart();
}
}
ghost.advanceFrame();
}
if (state_ == DEAD)
{
pacman_.restart();
if (pacman_.getLivesLeft() < 0)
return false;
state_ = ALIVE;
for (int i=0; i<ghosts_.size(); i++)
{
GhostActor ghost = (GhostActor) ghosts_.elementAt(i);
ghost.restart();
}
}
else //If he's not dead, then he can still move
pacman_.advanceFrame();
return true;
}
/**
* @param p the PacmanActor for this board
**/
public void setPacman(PacmanActor p)
{
pacman_ = p;
}
/**
* @return the PacmanActor for this board
**/
public PacmanActor getPacman()
{
return pacman_;
}
/**
* Adds a GhostActor to this board.
* @param ghost the ghost to add.
**/
public void addGhost(GhostActor ghost)
{
ghosts_.addElement(ghost);
}
}
//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -