📄 gamecanvas.java
字号:
/**
* PacMan for J2ME Devices
* CS 327 - Design Project, Fall 2002
* University of Illinois, Urbana-Champaign
*
* file: GameCanvas.java
* contact: Braden Kowitz
* date: 11/24/02
**/
//----------------------------------------------------------------------------//
import javax.microedition.lcdui.*;
/**
* This class is derived from a MIDlet canvas.
* It contains the main functionality of the game.
* It does three things basically
* -# Contains A GameBoard, which contains the game primitives and drawing code.
* -# Runs a FrameTrigger, which spawns a thread to periodically
* update the frames.
* -# Listens for key-presses, and notifies apropriate objects in the gameboard.
**/
public class GameCanvas extends javax.microedition.lcdui.Canvas
{
/**
* The default display for this midlet.
* This variable is assigned during object construction.
**/
private Display disp_;
/**
* The game board contains the game primitives, logic, and drawing code.
* This object is created during construction.
**/
private GameBoard gameBoard_;
/**
* frameTrigger object is responsible for periodically calling back to
* this object to trigger a frame update.
**/
private FrameTrigger frameTrigger_;
/**
* Add this in attempts to get back to the intro screen
*/
private pacman parentApp_;
/**
* Constructor.
* @param disp The display where the game should run.
**/
public GameCanvas(pacman app, Display disp)
{
this.parentApp_ = app;
disp_ = disp;
gameBoard_ = GameBoardFactory.test();
//frameTrigger_ = new FrameTrigger(this, 50);
}
/**
* Called by the system to repaint the screen.
* @param g Graphics object where drawing should take place.
**/
protected void paint(Graphics g)
{
// clear the screen:
g.setColor(255,255,255);
g.fillRect(0,0,this.getWidth(),this.getHeight());
// draw the board
gameBoard_.paint(g);
}
/**
* Stops the frame trigger thread
*/
public void destroyFrameTrigger()
{
frameTrigger_.stopTrigger();
}
/**
* Shows the gameboard and starts the game.
**/
public void show()
{
disp_.setCurrent(this);
frameTrigger_ = new FrameTrigger(this, 50);
frameTrigger_.startTrigger();
frameTrigger_.start();
}
/**
* This function is called periodically by FrameTrigger.
* It simply tells the gameboard to advance it's logic
* by one frame, then it requests a repaint from the system.
**/
public void advanceFrame()
{
if (gameBoard_.advanceFrame())
{
parentApp_.gameOver =0;
repaint();
}
else
{
destroyFrameTrigger();
parentApp_.gameOver = 1;
System.out.println("set gameover to 1");
try {
//parentApp_.gameOver = 1;
parentApp_.restartApp();
} catch (javax.microedition.midlet.MIDletStateChangeException midException) {
midException.printStackTrace();
System.exit(1);
}
}
}
/**
* This function is called by the system when the user
* presses a key.
* @param keyCode The unique int value of the key being pressed'
*
* Modified: 3/4/03 : Paul Force
* Changed to use a more general key binding. Should work
* on any phone now
**/
protected void keyPressed(int keyCode)
{
int action = getGameAction(keyCode);
switch (action)
{
case UP:
gameBoard_.getPacman().setDesiredDirection(
PacmanActor.UP);
break;
case DOWN:
gameBoard_.getPacman().setDesiredDirection(
PacmanActor.DOWN);
break;
case LEFT:
gameBoard_.getPacman().setDesiredDirection(
PacmanActor.LEFT);
break;
case RIGHT:
gameBoard_.getPacman().setDesiredDirection(
PacmanActor.RIGHT);
break;
}
}
}
//----------------------------------------------------------------------------//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -