📄 m3gcanvas.java
字号:
// So we can recognize them later
wall1.setUserID(LEFT_WALL);
wall2.setUserID(RIGHT_WALL);
wall3.setUserID(TOP_WALL);
wall4.setUserID(BOTTOM_WALL);
wall5.setUserID(FRONT_WALL);
// Make sure we can collide with them
wall1.setPickingEnable(true);
wall2.setPickingEnable(true);
wall3.setPickingEnable(true);
wall4.setPickingEnable(true);
wall5.setPickingEnable(true);
// Add walls to field group
playingField.addChild(wall1);
playingField.addChild(wall2);
playingField.addChild(wall3);
playingField.addChild(wall4);
playingField.addChild(wall5);
}
/**
*
*/
private void createPaddle()
{
// Create a plane using our nifty MeshFactory class
// paddle = MeshFactory.createPlane("/res/paddle.png", PolygonMode.CULL_BACK);
// paddle = MeshFactory.createPlane("/res/paddle.png", PolygonMode.CULL_NONE);
// paddle = MeshFactory.createPlane(null, PolygonMode.CULL_NONE);
paddle = MeshFactory.createAlphaPlane("/res/paddle.png", PolygonMode.CULL_NONE, 0x77);
// Set the paddle at its initial position
trPaddle = new Transform();
trPaddle.postTranslate(paddleCoords[0], paddleCoords[1], paddleCoords[2]);
paddle.setTransform(trPaddle);
// Make sure it's collidable
paddle.setPickingEnable(true);
paddle.setUserID(PADDLE_WALL);
// Add to the playing field
playingField = new Group();
playingField.setUserID(PLAYING_FIELD);
playingField.addChild(paddle);
// Create its vector
// float[] v = {0.0f, 1.0f, 0.0f};
// float[] u = {1.0f, 0.0f, 0.0f};
float[] v = {1.0f, 0.0f, 0.0f};
float[] u = {0.0f, 1.0f, 0.0f};
float[] normVec = VectorOps.calcNormal(v, u);
wallVec[PADDLE_WALL][0] = v;
wallVec[PADDLE_WALL][1] = u;
wallVec[PADDLE_WALL][2] = normVec;
}
/**
* Loads our ball
*/
private void loadBall() throws IOException
{
// Simply allocate an instance of the Ball class
ball = new Ball();
ballField = new Group();
ballField.setUserID(BALL_FIELD);
try
{
ballField.addChild( ball.meshBall );
} catch ( Exception e )
{
System.out.println(ball.meshBall.getParent());
e.printStackTrace();
}
}
/** Draws to screen
*/
private void draw(Graphics g)
{
// Envelop all in a try/catch block just in case
try
{
// Get the Graphics3D context
g3d = Graphics3D.getInstance();
// First bind the graphics object. We use our pre-defined rendering hints.
g3d.bindTarget(g, true, RENDERING_HINTS);
// Clear background
g3d.clear(back);
// Bind camera at fixed position in origo
// cam.setTranslation( 0.0f, 0.0f, -20.0f );
// trCam.postRotate( 1.0f, 0.0f, 1.0f, 0.0f );
g3d.setCamera(cam, trCam);
// Render the playing field and ball
// g3d.render(playingField, identity);
// ball.render(g3d, playingField, wallVec, paddleCoords);
ball.meshBall.setTransform( ball.trBall );
g3d.render(allField, identity);
ball.render(g3d, playingField, wallVec, paddleCoords);
// Check controls for paddle movement
if(key[UP])
{
paddleCoords[1] += 0.2f;
if(paddleCoords[1] > 3.0f)
paddleCoords[1] = 3.0f;
}
if(key[DOWN])
{
paddleCoords[1] -= 0.2f;
if(paddleCoords[1] < -3.0f)
paddleCoords[1] = -3.0f;
}
if(key[LEFT])
{
paddleCoords[0] -= 0.2f;
if(paddleCoords[0] < -3.0f)
paddleCoords[0] = -3.0f;
}
if(key[RIGHT])
{
paddleCoords[0] += 0.2f;
if(paddleCoords[0] > 3.0f)
paddleCoords[0] = 3.0f;
}
// Set paddle's coords
trPaddle.setIdentity();
trPaddle.postTranslate(paddleCoords[0], paddleCoords[1], paddleCoords[2]);
paddle.setTransform(trPaddle);
// Quit if user presses fire
if(key[FIRE])
ball.start();
}
catch(Exception e)
{
reportException(e);
}
finally
{
// Always remember to release!
g3d.releaseTarget();
}
// Do some old-fashioned 2D drawing
if(!ball.isMoving())
{
g.setColor(0);
g.drawString("Press fire to start!", 2, 2, Graphics.TOP | Graphics.LEFT);
}
else
{
int red = Math.min(255, ball.getBounces() * 12);
g.setColor(red, 0, 0);
g.drawString("Score: " + ball.getBounces(), 2, 2, Graphics.TOP | Graphics.LEFT);
}
}
/** Starts the canvas by firing up a thread
*/
public void start() {
Thread myThread = new Thread(this);
// Make sure we know we are running
running = true;
done = false;
// Start
myThread.start();
}
/** Run, runs the whole thread. Also keeps track of FPS
*/
public void run() {
while(running) {
try {
// Call the process method (computes keys)
process();
// Draw everything
draw(getGraphics());
flushGraphics();
// Sleep to prevent starvation
try{ Thread.sleep(30); } catch(Exception e) {}
}
catch(Exception e) {
reportException(e);
}
}
// Notify completion
done = true;
}
/**
* @param e
*/
private void reportException(Exception e) {
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}
/** Pauses the game
*/
public void pause() {}
/** Stops the game
*/
public void stop() { running = false; }
/** Processes keys
*/
protected void process()
{
int keys = getKeyStates();
if((keys & GameCanvas.FIRE_PRESSED) != 0)
key[FIRE] = true;
else
key[FIRE] = false;
if((keys & GameCanvas.UP_PRESSED) != 0)
key[UP] = true;
else
key[UP] = false;
if((keys & GameCanvas.DOWN_PRESSED) != 0)
key[DOWN] = true;
else
key[DOWN] = false;
if((keys & GameCanvas.LEFT_PRESSED) != 0)
key[LEFT] = true;
else
key[LEFT] = false;
if((keys & GameCanvas.RIGHT_PRESSED) != 0)
key[RIGHT] = true;
else
key[RIGHT] = false;
}
/** Checks if thread is running
*/
public boolean isRunning() { return running; }
/** checks if thread has finished its execution completely
*/
public boolean isDone() { return done; }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -