开发者

javaME game creation help

开发者 https://www.devze.com 2023-02-07 04:05 出处:网络
i am stuck on how to go ahead on creating a simple game for my project, it is simply a puzzle game where i have a image on the background and boxes will be covering the image, the boxes are to be matc

i am stuck on how to go ahead on creating a simple game for my project, it is simply a puzzle game where i have a image on the background and boxes will be covering the image, the boxes are to be matched and when matched they will remove themselves to reveal part of the image, currently what i have done is to draw the grids but how can i make them stacked against each other? do i make each generated grid a imageitem?

javaME game creation help

The canvas of generating the blocks

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


    public class blockcanvas extends Canvas
{
   private Image pokeballImage;
   private int screenW,screenH,imageW,imageH;

   public blockcanvas()
   {
        //get screen size
        screenW = getWidth();
        screenH = getHeight();

        try {
                pokeballImage = Image.createImage("/pokeball.png");
                imageW = pokeballImage.getWidth();
                imageH = pokeballImage.getHeight();
        } catch (IOException e) {
                e.printStackTrace();
        }
   }

    protected void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());
        drawPokeBall(g);

    }

    public void drawPokeBall(Graphics g)
    {
        int maxNumberOfBlockX = screenW / imageW;
        int maxNumberOfBlockY = screenH / imageH;

        Sprite pokeball = new Sprite(pokeballImage);
        pokeball.defineCollisionRectangle(0,0,20,20);
        pokeball.defineReferencePixel(imageW/2,imageH/2);
        for (int i = 0; i <maxNumberOfBlockX; i++)
        {
            pokeball.setRefPixelPosition(i*20,0);
            pokeball.paint(g);
        }
        for (int i = 0; i <maxNumberOfBlockY; i++)
        {
            pokeball.setRefPixelPosition(0,i*20);
            pokeball.paint(g);
        }
        System.out.println(maxNumberOfBlockX);

    }


}

The canvas for the image to be covered by blocks

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


    public class gameCanvas extends Canvas
{
   private Image image;
   private int screenW,screenH;
    blockcanvas block;

   public gameCanvas()
   {
        block = new blockcanvas();
        //get screen size
        screenW = getWidth()/2;
        screenH = getHeight()/2;

        try {
                image = Image.createImage("/gby001.gif");

        } catch (IOException e) {
                e.printStackTrace();
        }
   }

    protected void paint(Graphics g) {
        g.setColor(255,255,255);
        g.fillRect(0,0,getWidth(),getHeight());
        g.drawImage(resizeImage(image), 10, 10, Graphics.TOP | Graphics.LEFT);
        block.drawPokeBall(g);
    }

    private Image resizeImage(Image src) {
      int srcWidth = src.getWidth();
      int srcHeight = src.getHeight();
      Image tmp = Image.createImage(screenW, srcHeight);
      Graphics g = tmp.getGraphics();
      int ratio = (srcWidth << 16) / screenW;
      int pos = ratio/2;

      //Horizontal Resize        

      for (int x = 0; x < screenW; x++) {
          g.setClip(x, 0, 1, srcHeight);
          g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }

      Image resizedImage = Image.createImage(screenW, screenH);
      g = resizedImage.getGraphics();
      ratio = (srcHeight << 16) / screenH;
      pos = ratio/2;        

      //Vertical resize

      for (int y = 0; y < screenH; y++) {
          g.setClip(0, y, screenW, 1);
          g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
          pos += ratio;
      }
      return resizedImage;

  }//resize image    



}

The main app

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.microedition.lcdui.Canvas;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
/**
 * @author Lotus
 */
public class game extends MIDlet  implements CommandListener{
    //Variables Declartion
    private Display theDisp;
    private Command cmdExit,cmdGuess,cmdReturn;
    private gameCanvas canvas1;
    private blockcanvas canvas2;
    private Image gameImage;
    public game()
    {
       theDisp = Display.getDisplay(this);
       cmdExit = new Command("Exit",Command.EXIT,1);
       cmdGuess = new Command("Guess",Command.SCREEN,2);
       cmdReturn = new Command("Back",Command.BACK,2);
       canvas1 = new gameCanvas();
       canvas2 = new blockcanvas();
       canvas1.addCommand(cmdExit);
       canvas1.addCommand(cmdGuess);
       canvas1.setCommandListener(this);


    }
    public void startApp() {
  开发者_如何学JAVA  theDisp.setCurrent(canvas1);
    }

    public void pauseApp() {

    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {

        if(c == cmdExit)
        {
            destroyApp(true);
            notifyDestroyed();
        }
        else if(c== cmdGuess)
        {

        }

    }


}


You may want to change

g.fillRect(i, i, 20, 20);

to something like

g.fillRect(i*20, i*20, 20, 20);
0

精彩评论

暂无评论...
验证码 换一张
取 消