📄 pacmanactor.java
字号:
/**
* PacMan for J2ME Devices
* CS 327 - Design Project, Fall 2002
* CS 329 - Design Project, Spring 2003
* University of Illinois, Urbana-Champaign
*
* file: PacmanActor.java
* contact: Trevor Donarski
* date: 02/01/03
**/
//----------------------------------------------------------------------------//
import javax.microedition.lcdui.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* Represents pacman on the game board.
**/
public class PacmanActor
{
//--------------------------//
// ENUM
//--------------------------//
/**
* Enum for a desired dirrection.
**/
public static final int NONE = 0;
/**
* Enum for a desired dirrection.
**/
public static final int UP = 1;
/**
* Enum for a desired dirrection.
**/
public static final int DOWN = 2;
/**
* Enum for a desired dirrection.
**/
public static final int LEFT = 3;
/**
* Enum for a desired dirrection.
**/
public static final int RIGHT = 4;
//--------------------------//
// MEMBER VARIABLES
//--------------------------//
/**
* The node where pacman currently resides
**/
private GridNode myNode_;
/**
* The number of small pellets that pacman has eaten this game.
* This helps us calculate the score.
**/
private int pelletsEaten_;
/**
* Keeps track of pacman's current score.
*/
public int score_;
/**
* This is the invincibility variable... If pacman is invincible he'll eat the ghost :o)
**/
public boolean invincible_;
/**
* current X Pixel coord of this actor
**/
private int pixelX_;
/**
* current Y Pixel coord of this actor
**/
private int pixelY_;
/**
* destination X Pixel coord for this actor
**/
private int newPixelX_;
/**
* destination Y Pixel coord for this actor
**/
private int newPixelY_;
/**
* Desired dirrecton where pacman should move.
* This is specified by user actions (pressing movment keys)
**/
private int direction_;
/**
* NEW Apr. 3, 2003 - PDF
* - addresses the problem with pacman moonwalking
*
* Direction PacMan is currently moving
**/
private int animDirection_ = RIGHT;
/**
* The Node where pacman starts
**/
private GridNode startNode_;
/**
* NEW Feb. 25, 2003
* Added By Benson Fung
*
* This helps speed up calculations for the mouth angle
*/
//Lags the controls
//private int currentDirection_;
/**
* The current angle of the opening of pacman's mouth
**/
private int mouthAngle_;
/**
* The amount and dirrection of change that the mouth will
* Take in the next frame.
**/
private int mouthAngleChange_;
/**
* Specifies startAngle in fillARc depending on
* direction pacman is facing
*/
private int mouthAngleDirection_;
/**
* New April 15th, 2003
* Implements Lives
*/
private int myLivesLeft_;
/**
* New April 28th, 2003
* Implements Invinciblity
*/
private PacmanTimer invincibleTimer;
//--------------------------//
// FUNCTIONS
//--------------------------//
/**
* Constructor for this PacmanActor.
* @param startingPosition the GridNode where this ghosts begins the game
**/
public PacmanActor(GridNode startingPosition)
{
myNode_ = startingPosition;
startNode_ = startingPosition;
invincible_ = false;
pixelX_ = myNode_.pixelCenterX();
pixelY_ = myNode_.pixelCenterY();
newPixelX_ = pixelX_;
newPixelY_ = pixelY_;
direction_ = NONE;
//currentDirection_ = direction_;
/*
myNode_ = startingPosition;
startNode_ = startingPosition;
pixelX_ = myNode_.pixelCenterX();
pixelY_ = myNode_.pixelCenterY();
newPixelX_ = pixelX_;
newPixelY_ = pixelY_;
direction_ = NONE;
//currentDirection_ = direction_;
*/
pelletsEaten_ = 0;
// mouthAngle starts Pacman facing right
mouthAngleDirection_ = 20;
// 40 degree arc mouth opening
mouthAngle_ = 320;
mouthAngleChange_ = +10;
//Number of lives left
myLivesLeft_ = 2;
}
/**
* @return the current node where pacman resides
**/
public GridNode getNode() {
return myNode_;
}
/**
* @return the number of small pellets that pacman has eaten in this game.
**/
public int getPelletsEaten()
{
return pelletsEaten_;
}
/**
* @return Pacman's current x coordinate
*/
public int getX()
{
return pixelX_;
}
/**
* @return Pacman's current y coordinate
*/
public int getY()
{
return pixelY_;
}
/**
* @return Number of lives pacman has left. If < 0, then game is over
*/
public int getLivesLeft()
{
return myLivesLeft_;
}
/**
* Sets the dirrection the user wishes pacman to move.
* @param d enum: NONE, RIGHT, LEFT, UP, DOWN
**/
public void setDesiredDirection(int d)
{
direction_ = d;
}
/**
* Sets the number of lives for this pacman
* @param i the number of lives left of pacman you want
*/
public void setLivesLeft(int i)
{
myLivesLeft_ = i;
}
/**
* @param n Goes to this node if it is not null
**/
private void goToNode(GridNode n)
{
if (n == null) return;
myNode_ = n;
newPixelX_ = myNode_.pixelCenterX();
newPixelY_ = myNode_.pixelCenterY();
}
public boolean isInvincible()
{
if(invincible_ == true)
{
return true;
}
else
{
return false;
}
}
/**
* Resets Pacman to his original position but keeps the number of pellets eaten
*/
public void restart()
{
myNode_ = startNode_;
pixelX_ = startNode_.pixelCenterX();
pixelY_ = startNode_.pixelCenterY();
newPixelX_ = startNode_.pixelCenterX();
newPixelY_ = startNode_.pixelCenterY();
invincible_ = false;
direction_ = NONE;
animDirection_ = RIGHT;
//currentDirection_ = direction_;
pelletsEaten_ = pelletsEaten_;
// mouthAngle starts Pacman facing right
mouthAngleDirection_ = 20;
// 40 degree arc mouth opening
mouthAngle_ = 320;
mouthAngleChange_ = +10;
// We only restart when pacman dies, so...
myLivesLeft_--;
}
/**
* Called to advance the logic of the game one frame.
**/
public void advanceFrame()
{
// if we are in an animation:
if ((pixelX_ != newPixelX_) || (pixelY_ != newPixelY_))
{
// move closer to the destination:
if (pixelX_ < newPixelX_) pixelX_++;
if (pixelX_ > newPixelX_ ) pixelX_--;
if (pixelY_ < newPixelY_) pixelY_++;
if (pixelY_ > newPixelY_ ) pixelY_--;
}
else
{
// The else and if else statement directly after this comment
// were modified over the course of 2 weeks by Trevor Donarski
//
// Basically what I did was implement the scoring strategy and
// the ability for pacman to become invincible once he ate a
// power pellet (known as BIG_PELLET). I call PacmanTimer which
// is a class that implements the timer function found in
// java.util.Timer
if (myNode_.getPellet() == GridNode.SMALL_PELLET)
{
myNode_.setPellet(GridNode.NO_PELLET);
pelletsEaten_++;
score_++;
}
// eat any pellets:
else if (myNode_.getPellet() == GridNode.BIG_PELLET)
{
myNode_.setPellet(GridNode.NO_PELLET);
pelletsEaten_+=2;
score_+=2;
invincibleTimer = new PacmanTimer();
}
// can we move in the direction requested?
// Modified 4/3/03 - PDF
// - Added support for animDirection_
if(direction_ == LEFT)
{
goToNode(myNode_.getLeft());
animDirection_ = LEFT;
}
if(direction_ == RIGHT)
{
goToNode(myNode_.getRight());
animDirection_ = RIGHT;
}
if(direction_ == DOWN)
{
goToNode(myNode_.getDown());
animDirection_ = DOWN;
}
if(direction_ == UP)
{
goToNode(myNode_.getUp());
animDirection_ = UP;
}
}
}
/**
* Paints the PacmanActor
* @param g Graphics object where drawing should take place.
**/
public void paint(Graphics g)
{
if (mouthAngle_ <= 300) mouthAngleChange_ = +10;
if (mouthAngle_ >= 360) mouthAngleChange_ = -10;
mouthAngle_ += mouthAngleChange_;
//Added by Benson Fung - make mouth move correctly
//Added a variable speed optimization
//if (direction_ != currentDirection_)
//Modified 4/3/03 - PDF
// - Now switches on animDirection_ instead of direction_
switch (animDirection_)
{
case RIGHT: mouthAngleDirection_ = 30; break; // 0 degrees + 20
case LEFT: mouthAngleDirection_ = 210; break; // 180 degrees + 20
case UP: mouthAngleDirection_ = 120; break; // 90 degrees + 20
case DOWN: mouthAngleDirection_ = 300; break; // 270 degrees + 20
default: break;
}
// both sides of his mouth move, so this must account for change also
mouthAngleDirection_ -= mouthAngleChange_;
g.setColor(0,0,0);
//fillArc(x,y,sizeX, sizeY, startAngle, arcAngle
//startAngle should determine which side the mouth faces
//arcAngle should be between 360 - 60 to 360 - 20
g.fillArc(pixelX_-4,
pixelY_-4,
8,8,
mouthAngleDirection_,
mouthAngle_);
}
/**
* Simple timer that uses java.util.Timer to schedule a task
* to execute for 6 seconds
**/
public class PacmanTimer
{
Timer timer;
/**
* Constructor for PacmanTimer.
* Creates a new Timer and schedules a task
* to run in 6000 milliseconds = 6 sec
**/
public PacmanTimer()
{
invincible_ = true;
timer = new Timer ();
timer.schedule(new PacmanTask(), 6000);
}
/**
* This is PacmanTask which simply outputs to the screen
* when the time is up and turns inviciblity off
**/
class PacmanTask extends TimerTask
{
public void run()
{
System.out.println("Time's up!");
//Terminate the timer thread
timer.cancel();
invincible_ = false;
}
}
}
}
//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -