Use Gamecanvas to make a starry effect

xiaoxiao2021-03-06  45

Game development-specific APIs, such as Gamecanvas, etc. in MIDP 2.0. They are located in the javax.microedition.lcdui.game package. This article describes the basic use of Gamecanvas and implements an effect of rolling the stars. You can refer to

Game Canvas Basic get more detailed information.

Gamecanvas is a subclass of Canvas, so he also inherits some of the features of the Canvas class, such as the showNotify () method calls when CANVAS is displayed on the screen, and hideNotify () is called when the Canvas leaves the screen. We can use them as a listener to initialize and destroy resources. For example, //hen the canvas is shown, start a thread to // run the game loop.

Protected void shownotify () {random = new random (); thread = new thread (this); thread.start ();} //en the game canvas is Hidden, Stop The Thread.

Protected void hideNotify () {thread = null;}

The most important thing in game development is to accept the user triggered event and then resemble the screen, usually we use the getKeyStates () method to determine which key is pressed, then draw the screen, call flushgraphics (). In GAMECANVAS, the system has actually implemented double buffer technology, so every time we draw, it is drawn on OFF-screen. After the end, copy it to the screen via flushgraphics. Below is a typical acceptance event, processing logic, code to draw screens. // Get the Graphics object for the off-screen bufferGraphics g = getGraphics (); while (true) {// Check user input and update positions if necessary int keyState = getKeyStates ();! If ((keyState & LEFT_PRESSED) = 0 ) {Sprite.move (-1, 0);} else if (KeyState & right_pressed)! = 0) {sprite.move (1, 0);} // clean the background to whiteg.setcolor (0xfffffff); g .fillRect (0, 0, getWidth (), getHeight ()); // Draw the sprite sprite.paint (g); // flush the off-screen buffer flushgraphics ();}

Here is the effect of our rolling starry sky, and the idea of ​​design is very simple. We launched a thread that uses the copyArea () method to copy the distance of the screen down to the distance. Then painted the first blank line, randomly painted on the straight line, which looks like a starry sky. The logic code is as follows: // The game loop.

Public void Run () {int w = getWidth (); int h = getHeight () - 1; while (thread == thread.crentthread ()) {// increment or Decrement the scrolling interval // based on key presses int State = getKeyStates (); if ((state & DOWN_PRESSED) = 0!) {sleepTime = SLEEP_INCREMENT; if (sleepTime> SLEEP_MAX) sleepTime = SLEEP_MAX;} (! (state & UP_PRESSED) = 0) else if {sleepTime - = SLEEP_INCREMENT ; If (Sleeptime <0) Sleeptime = 0;}

// repaint the screen by first scrolling the // EXISTING STARFIELD DOWN One and Painting In // New stars ...

Graphics.copyarea (0, 0, W, H, 0, 1, Graphics.top | Graphics.Lraphics); Graphics.SetColor (0, 0, 0); graphics.drawline (0, 0, w, 0); Graphics .SetColor (255, 255, 255); for (int i = 0; i

// now Wait ...

Try {thread.currentthread (). Sleep (SleepTime);} catch (interruptedexception e) {}}}

Below the source code / * * license * * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved. * /

Import javax.microedition.lcdui. *; import javax.microedition.lcdui.game. *; import javax.microedition.midlet. *;

Public Class Gamecanvastest Extends Midlet ImmanceListener {Private Display Display;

Public Static Final Command EXITCOMMAND = New Command ("EXIT", Command.exit, 1);

Public gamecanvastest () {}

Public void CommandAction (Command C, Displayable D) {if (c == exitcommand) {exitmidlet ();}}

Protected Void DestroyApp (Boolean Unconditional) THROWS MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();

Public void exitmidlet () {notifydestroyed ();

Public Display getDisplay () {Return Display;

Protected void initmidlet () {Gamecanvas C = New Starfield (); C.Addcommand; C.SetCommandListener (this);

GetDisplay (). setcurrent (c);

protected void pauseapp () {}

protected void startApp () throws MIDletStateChangeException {if (display == null) {display = Display.getDisplay (this); initMIDlet ();}}} / * * License * * Copyright 1994-2004 Sun Microsystems, Inc. All Rights Reserved * /

Import java.util.randem; import javax.microedition.lcdui. *; import javax.microedition.lcdui.game.gamecanvas;

// a Simple Example of a Game Canvas That DisplayS // A Scrolling Star Field. Use the Up or Slow Down The Rate of Scrolling.

Public Class Starfield Extends Gamecanvas Implements Runnable {

Private static final int Sleep_increment = 10;

Private static final int Sleep_initial = 150;

PRIVATE STATIC FINAL INT SLEP_MAX = 300;

PRIVATE Graphics Graphics;

Private random

Private int sleeptime = Sleep_initial;

Private volatile thread thread;

Public starfield () {super (true); graphics = getGraphics (); graphics.setcolor (0, 0, 0); graphics.FillRect (0, 0, getWidth (), getHeight ());}

// The game loop.

Public void Run () {int w = getWidth (); int h = getHeight () - 1; while (thread == thread.crentthread ()) {// increment or Decrement the scrolling interval // based on key presses int State = getKeyStates ();

if (! (state & DOWN_PRESSED) = 0) {sleepTime = SLEEP_INCREMENT; if (sleepTime> SLEEP_MAX) sleepTime = SLEEP_MAX;} else if (! (state & UP_PRESSED) = 0) {sleepTime - = SLEEP_INCREMENT; if (sleepTime < 0) SleepTime = 0;}

// repaint the screen by first scrolling the // EXISTING STARFIELD DOWN One and Painting In // New stars ...

Graphics.copyarea (0, 0, W, H, 0, 1, Graphics.top | Graphics.Lraphics); Graphics.SetColor (0, 0, 0); graphics.drawline (0, 0, w, 0); Graphics .SetColor (255, 255, 255); for (int i = 0; i

// now Wait ...

Try {thread.sleep (sleeptime);} catch (interruptedexception e) {}}}

// when the canvas is shown, start a thread to // run the game loop.

protected void showNotify () {random = new Random (); thread = new Thread (this); thread.start ();} // When the game canvas is hidden, stop the thread.protected void hideNotify () {thread = null }}

转载请注明原文地址:https://www.9cbs.com/read-57692.html

New Post(0)