📄 gridnode.java
字号:
/**
* PacMan for J2ME Devices
* CS 327 - Design Project, Fall 2002
* University of Illinois, Urbana-Champaign
*
* file: GridNode.java
* contact: Braden Kowitz
* date: 11/24/02
**/
//----------------------------------------------------------------------------//
import javax.microedition.lcdui.*;
/**
* Represents a cell on the gameboard.
* Nodes can draw itself and also contain informaion on the node's contents
**/
public class GridNode
{
//--------------------------//
// ENUM
//--------------------------//
//@{
/**
* Enum value for the pellet_ member.
**/
public static final int NO_PELLET = 0;
/**
* Enum value for the pellet_ member.
* Small pellets are worth points.
**/
public static final int SMALL_PELLET = 1;
/**
* Enum value for the pellet_ member.
* Large pellets make pacman eat ghosts for a set ammount of time.
**/
public static final int BIG_PELLET = 2;
//@}
//--------------------------//
// GRID SIZE
//--------------------------//
/**
* Defines the size of the grid squares.
* Units are pixels.
**/
private static int size_;
/**
* @param i size in pixels for all GridNode objects
**/
public static void setSize(int i)
{
size_ = i;
}
/**
* @return The size in pixels for all GridNode objects
**/
public static int getSize()
{
return size_;
}
//--------------------------//
// MEMBER VARIABLES
//--------------------------//
/**
* Defines the type of pellet that is in the GridNode.
* Valid values are
* - NO_PELLET
* - SMALL_PELLET
* - BIG_PELLET
**/
private int pellet_;
//@{
/**
* Pointer to the GridNode to the left of this one on the game board.
* This value is null when there is no path for actors to move
* to the left.
**/
private GridNode left_;
/**
* Pointer to the GridNode to the right of this one on the game board.
* This value is null when there is no path for actors to move
* to the right.
**/
private GridNode right_;
/**
* Pointer to the GridNode up from this one on the game board.
* This value is null when there is no path for actors to move up.
**/
private GridNode up_;
/**
* Pointer to the GridNode down from this one on the game board.
* This value is null when there is no path for actors to move down.
**/
private GridNode down_;
//@}
/**
* Position of this GridNode in the X axis.
* - These are grid co-ordinates, not pixels.
* - This is specified during object construction.
**/
private int posX_;
/**
* Position of this GridNode in the Y axis.
* - These are grid co-ordinates, not pixels.
* - This is specified during object construction.
**/
private int posY_;
//@{
/**
* X pixel coord of the Upper Left corner of this GridNode
**/
private int pixelULX_;
/**
* Y pixel coord of the Upper Left corner of this GridNode
**/
private int pixelULY_;
/**
* X pixel coord of the Lower Right corner of this GridNode
**/
private int pixelLRX_;
/**
* Y pixel coord of the Lower Right corner of this GridNode
**/
private int pixelLRY_;
/**
* X pixel coord of the Center of this GridNode
**/
private int pixelCX_;
/**
* Y pixel coord of the Center of this GridNode
**/
private int pixelCY_;
//@}
//--------------------------//
// PUBLIC FUNCTIONS
//--------------------------//
/**
* Constructor for a GridNode
* - These are grid co-ordinates, not pixels.
* @param x Position of this GridNode in the X axis.
* @param y Position of this GridNode in the Y axis.
**/
public GridNode(int x, int y)
{
posX_ = x;
posY_ = y;
left_ = null;
right_ = null;
up_ = null;
down_ = null;
pellet_ = SMALL_PELLET;
//set the pixel positions:
// upper left
pixelULX_ = posX_ * size_;
pixelULY_ = posY_ * size_;
// lower right
pixelLRX_ = pixelULX_ + size_ -1;
pixelLRY_ = pixelULY_ + size_ -1;
// center
pixelCX_ = (pixelULX_ + pixelLRX_)/2;
pixelCY_ = (pixelULY_ + pixelLRY_)/2;
}
/**
* @return the X pixel coord of the Center of this GridNode
**/
public int pixelCenterX()
{
return pixelCX_;
}
/**
* @return the X pixel coord of the Center of this GridNode
**/
public int pixelCenterY()
{
return pixelCY_;
}
//@{
/**
* Sets the pointer to the GridNode to the left of this one on the game board.
* This value is null when there is no path for actors to move
* to the left.
*
* @param n GridNode object or null
**/
public void setLeft(GridNode n)
{
left_ = n;
}
/**
* Sets the pointer to the GridNode to the right of this one on the game board.
* This value is null when there is no path for actors to move
* to the right.
*
* @param n GridNode object or null
**/
public void setRight(GridNode n)
{
right_ = n;
}
/**
* Sets the pointer to the GridNode up from this one on the game board.
* This value is null when there is no path for actors to move up.
*
* @param n GridNode object or null
**/
public void setUp(GridNode n)
{
up_ = n;
}
/**
* Sets the pointer to the GridNode down from this one on the game board.
* This value is null when there is no path for actors to move down.
*
* @param n GridNode object or null
**/
public void setDown(GridNode n) { down_ = n; }
/**
* Gets the pointer to the GridNode left from this one on the game board.
* This value is null when there is no path for actors to move left.
*
* @return GridNode object or null
**/
public GridNode getLeft() { return left_; }
/**
* Gets the pointer to the GridNode right from this one on the game board.
* This value is null when there is no path for actors to move right.
*
* @return GridNode object or null
**/
public GridNode getRight() { return right_; }
/**
* Gets the pointer to the GridNode up from this one on the game board.
* This value is null when there is no path for actors to move up.
*
* @return GridNode object or null
**/
public GridNode getUp() { return up_; }
/**
* Gets the pointer to the GridNode down from this one on the game board.
* This value is null when there is no path for actors to move down.
*
* @return GridNode object or null
**/
public GridNode getDown() { return down_; }
//@}
/**
* Sets the type of pellet that is in this GridNode.
* - Valid values are
* - NO_PELLET
* - SMALL_PELLET
* - BIG_PELLET
* @param i *_PELLET enum.
*/
public void setPellet(int i)
{
pellet_ = i;
}
/**
* Returns the type of pellet that is in this GridNode.
* - Valid values are
* - NO_PELLET
* - SMALL_PELLET
* - BIG_PELLET
* @return *_PELLET enum.
*/
public int getPellet()
{
return pellet_;
}
/**
* This function is responsible for painting the GridNode
* @param g Graphics object where drawing should take place.
**/
public void paint(Graphics g)
{
// clear the square
g.setColor(255,255,255);
g.fillRect(pixelULX_,pixelULY_,pixelLRX_,pixelLRY_);
// draw the borders:
g.setColor(0,0,0);
if (left_ == null) g.drawLine(pixelULX_,pixelULY_,pixelULX_,pixelLRY_);
if (right_ == null) g.drawLine(pixelLRX_,pixelULY_,pixelLRX_,pixelLRY_);
if (up_ == null) g.drawLine(pixelULX_,pixelULY_,pixelLRX_,pixelULY_);
if (down_ == null) g.drawLine(pixelULX_,pixelLRY_,pixelLRX_,pixelLRY_);
// draw the dots:
int radius = 0;
if (pellet_ == BIG_PELLET)
radius = size_ / 3;
if (pellet_ == SMALL_PELLET)
radius = size_ / 5;
if (pellet_ != NO_PELLET)
g.fillArc(pixelCX_,pixelCY_,radius,radius,0,360);
}
}
//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -