private void gameUpdate() { if (!isPaused && !gameOver) { if (jack.willHitBrick()) { // collision checking first jack.stayStill(); // stop jack and scenery bricksMan.stayStill(); ribsMan.stayStill(); } ribsMan.update(); // update background and sprites bricksMan.update(); jack.updateSprite(); fireball.updateSprite(); if (showExplosion) explosionPlayer.updateTick(); // update the animation } } // end of gameUpdate()
private void gameRender() { if (dbImage == null) { dbImage = createImage(PWIDTH, PHEIGHT); if (dbImage == null) { System.out.println("dbImage is null"); return; } else dbg = dbImage.getGraphics(); } // draw a white background dbg.setColor(Color.white); dbg.fillRect(0, 0, PWIDTH, PHEIGHT); // draw the game elements: order is important ribsMan.display(dbg); // the background ribbons bricksMan.display(dbg); // the bricks jack.drawSprite(dbg); // the sprites fireball.drawSprite(dbg); if (showExplosion) // draw the explosion (in front of jack) dbg.drawImage(explosionPlayer.getCurrentImage(), xExpl, yExpl, null); reportStats(dbg); if (gameOver) gameOverMessage(dbg); if (showHelp) // draw the help at the very front (if switched on) dbg.drawImage( helpIm, (PWIDTH - helpIm.getWidth()) / 2, (PHEIGHT - helpIm.getHeight()) / 2, null); } // end of gameRender()
private void processKey(KeyEvent e) // handles termination, help, and game-play keys { int keyCode = e.getKeyCode(); // termination keys // listen for esc, q, end, ctrl-c on the canvas to // allow a convenient exit from the full screen configuration if ((keyCode == KeyEvent.VK_ESCAPE) || (keyCode == KeyEvent.VK_Q) || (keyCode == KeyEvent.VK_END) || ((keyCode == KeyEvent.VK_C) && e.isControlDown())) running = false; // help controls if (keyCode == KeyEvent.VK_H) { if (showHelp) { // help being shown showHelp = false; // switch off isPaused = false; } else { // help not being shown showHelp = true; // show it isPaused = true; // isPaused may already be true } } // game-play keys if (!isPaused && !gameOver) { // move the sprite and ribbons based on the arrow key pressed if (keyCode == KeyEvent.VK_LEFT) { jack.moveLeft(); bricksMan.moveRight(); // bricks and ribbons move the other way ribsMan.moveRight(); } else if (keyCode == KeyEvent.VK_RIGHT) { jack.moveRight(); bricksMan.moveLeft(); ribsMan.moveLeft(); } else if (keyCode == KeyEvent.VK_UP) jack.jump(); // jumping has no effect on the bricks/ribbons else if (keyCode == KeyEvent.VK_DOWN) { jack.stayStill(); bricksMan.stayStill(); ribsMan.stayStill(); } } } // end of processKey()
public JackPanel(JumpingJack jj, long period) { jackTop = jj; this.period = period; setDoubleBuffered(false); setBackground(Color.white); setPreferredSize(new Dimension(PWIDTH, PHEIGHT)); setFocusable(true); requestFocus(); // the JPanel now has focus, so receives key events addKeyListener( new KeyAdapter() { public void keyPressed(KeyEvent e) { processKey(e); } }); // initialise the loaders ImagesLoader imsLoader = new ImagesLoader(IMS_INFO); clipsLoader = new ClipsLoader(SNDS_FILE); // initialise the game entities bricksMan = new BricksManager(PWIDTH, PHEIGHT, BRICKS_INFO, imsLoader); int brickMoveSize = bricksMan.getMoveSize(); ribsMan = new RibbonsManager(PWIDTH, PHEIGHT, brickMoveSize, imsLoader); jack = new JumperSprite( PWIDTH, PHEIGHT, brickMoveSize, bricksMan, imsLoader, (int) (period / 1000000L)); // in ms fireball = new FireBallSprite(PWIDTH, PHEIGHT, imsLoader, this, jack); // prepare the explosion animation explosionPlayer = new ImagesPlayer("explosion", (int) (period / 1000000L), 0.5, false, imsLoader); BufferedImage explosionIm = imsLoader.getImage("explosion"); explWidth = explosionIm.getWidth(); explHeight = explosionIm.getHeight(); explosionPlayer.setWatcher(this); // report animation's end back here // prepare title/help screen helpIm = imsLoader.getImage("title"); showHelp = true; // show at start-up isPaused = true; // set up message font msgsFont = new Font("SansSerif", Font.BOLD, 24); metrics = this.getFontMetrics(msgsFont); } // end of JackPanel()