Help me with this

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

Help me with this

Postby raj » Sun Sep 14, 2008 8:23 am

HI, this is my very first post here.
actually i am developing a game for the first time, i ve setup all the things in the game but i cant move the players yet. is there someone who can help me to make my character work? the development team???
please its urgent, I am to submit the code by today evening.....

here's my code.......

Code: Select all
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

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



/**
* @author Administrator
*/
public class SwapKnight extends MIDlet implements CommandListener {
    private Display display;
    private boolean started;
    private Command exitCommand;
    private Command backCommand;
   // private Command okCommand;
    private List listItem;
    private Ticker ticker;
    //private StringItem stringItem;
    private GameCanvas gameCanvas;
    private InstructionCanvas instructionCanvas;
    private NavigationCanvas navigationCanvas;
    private AboutCanvas aboutCanvas;
   
    public SwapKnight(){
             
              exitCommand=new Command("Exit",Command.EXIT,1);
              backCommand=new Command("Back",Command.BACK,2);
             // okCommand=new Command("OK",Command.OK,3);
              ticker=new Ticker("Select an option");
              listItem=new List(null,Choice.IMPLICIT);
              listItem.append("Play",null);
              listItem.append("Instruction",null);
              listItem.append("Navigation",null);
              listItem.append("About",null);
             
        }
    public void startApp() {
        if(!started){
            started=true;
        display=Display.getDisplay(this);
      //listItem.addCommand(okCommand);
        listItem.addCommand(exitCommand);
        listItem.setTicker(ticker);
        listItem.setCommandListener(this);
        display.setCurrent(listItem);
       
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
    public void commandAction(Command c, Displayable d){
        if(c==backCommand){
            display.setCurrent(listItem);
        }else if(c==exitCommand){
        notifyDestroyed();
        }else if(d==listItem){
                    if(c==List.SELECT_COMMAND){
        int i=listItem.getSelectedIndex();
        switch(i){
            case 0:
                display.setCurrent(getGameCanvas());
                break;
            case 1:
                display.setCurrent(getInstructionCanvas());
                break;
            case 2:
                display.setCurrent(getNavigationCanvas());
                break;
            case 3:
                display.setCurrent(getAboutCanvas());
                break;
        }
        }
        }
    }
GameCanvas getGameCanvas(){
    if(gameCanvas==null){
    gameCanvas=new GameCanvas();
    gameCanvas.addCommand(backCommand);
    gameCanvas.addCommand(exitCommand);
    gameCanvas.setCommandListener(this);
    }
    return gameCanvas;
}
InstructionCanvas getInstructionCanvas(){
if(instructionCanvas==null){
    instructionCanvas=new InstructionCanvas();
    instructionCanvas.addCommand(backCommand);
    instructionCanvas.addCommand(exitCommand);
    instructionCanvas.setCommandListener(this);
    }
    return instructionCanvas;
}
NavigationCanvas getNavigationCanvas(){
if(navigationCanvas==null){
    navigationCanvas=new NavigationCanvas();
    navigationCanvas.addCommand(backCommand);
    navigationCanvas.addCommand(exitCommand);
    navigationCanvas.setCommandListener(this);
    }
    return navigationCanvas;
}
AboutCanvas getAboutCanvas(){
if(aboutCanvas==null){
    aboutCanvas=new AboutCanvas();
    aboutCanvas.addCommand(backCommand);
    aboutCanvas.addCommand(exitCommand);
    aboutCanvas.setCommandListener(this);
    }
    return aboutCanvas;
}
}
class GameCanvas extends Canvas implements CommandListener{
    int sw,sh;
    int cellsize;
    int sx,sy;
    int x,y,a,b;
    int [][] board;
    int  src;
   Image image1,image2;
    boolean isSelected;
    Alert alert;
    public GameCanvas(){
    board=new int[5][];
    try{
        image2=Image.createImage("/white_horse.png");
    }catch(Exception e){}
    int i,j;
    for(i=0;i<5;i++){
    for(j=0;j<6;j++){
    board[i]=new int[j];
    }
    }
    }
    public void commandAction(Command c, Displayable s){
   
    }
    protected void paint(Graphics g){
    int i,j;
    g.setColor(150,150,150);
    g.fillRect(0,0, getWidth(),getHeight());
    g.setColor(175,175,200);
    sw=getWidth();
    sh=getHeight();
    if(sw<sh){
    cellsize=sw/5;
    sx=0;
    sy=(sh-sw)/2;
    }else {      //more width than height
            cellsize=(sh-15)/5;
            sy=15;
            sx=(sw-sh+15)/2;
        }
    g.fillRect(sx,sy,cellsize*5,cellsize*5);
    g.setColor(0,0,0);
    for(i=0;i<5;i++){
    // draw horizontal lines           
            g.fillRect(sx, sy+i*cellsize,5*cellsize, 2);
            // draw vertical lines
            g.fillRect(sx+i*cellsize, sy, 2 , 5*cellsize+2);
    }
    /* Draw the Cursor */
        g.setColor(0,70,200);
        g.drawRect(sx+x*cellsize, sy+y*cellsize, cellsize+1, cellsize+1);
        g.drawRect(sx+x*cellsize+1, sy+y*cellsize+1, cellsize-1, cellsize-1);
        g.drawRect(sx+x*cellsize+2, sy+y*cellsize+2, cellsize-3, cellsize-3);
   /* Draw the Empty Box */
        g.setColor(0,100,0);    // very dark green
        g.fillRect(sx+x*cellsize+1, sy+y*cellsize+1, cellsize, cellsize);
        repaint();
       
    /*Creating the image*/
    try{
        image1=Image.createImage("/black_horse.png");
    }catch(Exception e){}
               
        /*putting black horse*/
        g.drawImage(image1,sx+cellsize*0,sy+cellsize*0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*1,sy+cellsize*0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*2,sy+cellsize*0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*3,sy+cellsize*0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*4,sy+cellsize*0,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*0,sy+cellsize*1,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*1,sy+cellsize*1,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*2,sy+cellsize*1,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*3,sy+cellsize*1,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*4,sy+cellsize*1,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*0,sy+cellsize*2,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image1,sx+cellsize*1,sy+cellsize*2,Graphics.TOP|Graphics.LEFT);
       
               
        /*putting white horse*/       
        g.drawImage(image2,sx+cellsize*3,sy+cellsize*2,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*4,sy+cellsize*2,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*0,sy+cellsize*3,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*1,sy+cellsize*3,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*2,sy+cellsize*3,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*3,sy+cellsize*3,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*4,sy+cellsize*3,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*0,sy+cellsize*4,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*1,sy+cellsize*4,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*2,sy+cellsize*4,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*3,sy+cellsize*4,Graphics.TOP|Graphics.LEFT);
        g.drawImage(image2,sx+cellsize*4,sy+cellsize*4,Graphics.TOP|Graphics.LEFT);
           
    }
    void up() {
        if(y==0) return;
        board[x][y]=board[x][y-1];
        board[x][y-1]=0;
        y--;
    }
   

    void down() {
        if(y==4) return;
        board[x][y]=board[x][y+1];
        board[x][y+1]=0;
        y++;
    }
   

    void left() {
        if(x==0) return;
        board[x][y]=board[x-1][y];
        board[x-1][y]=0;
        x--;
    }
   
    void right() {
        if(x==4) return;
        board[x][y]=board[x+1][y];
        board[x+1][y]=0;
        x++;
    }
    void select(){
        a=this.x;
        b=this.y;
           }
    void move(){
       this.x=a;
       this.y=b;       
    }
   
    public void keyPressed (int keyCode) {
        switch(keyCode) {
            case KEY_NUM1:
                select();
                break;
            case KEY_NUM2:
                    up();
                break;
            case KEY_NUM4:
                    left();
                break;
            case KEY_NUM5:
                move();
                break;
            case KEY_NUM6:
                    right();
                break;
            case KEY_NUM8:
                    down();
                break;
            default:
                switch(getGameAction(keyCode)) {
                    case UP:
                        up();
                        break;
                    case DOWN:
                        down();
                        break;
                    case RIGHT:
                        right();
                        break;
                    case LEFT:               
                        left();
                        break;
                    case FIRE:
                        move();
                        break;
                }
        }
       }
   
}
     
   

class InstructionCanvas extends Canvas{
    Font font;
    public InstructionCanvas(){
    font = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
    }
    protected void paint(Graphics g){
    g.setColor(155,155,200);
    g.fillRect(0, 0, getWidth(),getHeight());
    g.setFont(font);
    g.setColor(0,0,0);
    g.drawString(" In this game the targate is to ",0,0,Graphics.TOP|Graphics.LEFT);
    g.drawString(" stop your opponent to reach to ", 0, 15, Graphics.TOP|Graphics.LEFT);
    g.drawString(" the any of the corner ",0,30,Graphics.TOP|Graphics.LEFT);

}
}
class NavigationCanvas extends Canvas{
    Font font;
    public NavigationCanvas(){
    font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
        }
protected void paint(Graphics g){
    g.setColor(100,180,255);
    g.fillRect(0, 0, getWidth(),getHeight());
    g.setColor(0, 0, 0);
    g.setFont(font);
g.drawString(" How to play:",0,0,Graphics.TOP|Graphics.LEFT);
g.drawString(" Use your mobile's 2,4,5,6 and 8 ",0, 15, Graphics.TOP|Graphics.LEFT);
g.drawString(" keys to navigate and fire", 0, 30,Graphics.TOP|Graphics.LEFT);
}
}
class AboutCanvas extends Canvas{
    Font font;
    public AboutCanvas(){
    font=Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
    }
protected void paint(Graphics g){
    g.setColor(155,155,185);
    g.fillRect(0, 0, getWidth(),getHeight());
    g.setColor(0,0,0);
    g.setFont(font);
g.drawString("Story Board :  Gaurav",0,0, Graphics.TOP|Graphics.LEFT);
g.drawString("Logic       :  Not Created Yet",0,25, Graphics.TOP|Graphics.LEFT);
g.drawString("Programmer  :  Gaurav",0,50,Graphics.TOP|Graphics.LEFT);
g.drawString("Designer    :  Gaurav",0,75,Graphics.TOP|Graphics.LEFT);
}
}
raj
 
Posts: 4
Joined: Sun Sep 14, 2008 8:16 am

Re: Help me with this

Postby DevelopmentTeam » Sun Sep 14, 2008 5:42 pm

Before we can suggest any changes please check if the up(), down(), right() and left() functions are called properly when the keys are pressed. just try displaying something using System.out.print();
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Help me with this

Postby raj » Mon Sep 15, 2008 12:54 pm

Hi team, thanks for a reply
all the functions are working fine except the move() which i want to redraw the image on the selected partition.
raj
 
Posts: 4
Joined: Sun Sep 14, 2008 8:16 am

Re: Help me with this

Postby DevelopmentTeam » Fri Sep 19, 2008 11:28 am

What problem you get and can you explain exactly what is your problem so that it might help us fixing the code?
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: Help me with this

Postby raj » Mon Sep 22, 2008 2:29 pm

Actually my problem is associated with the movement of images. they r automatically moving and redrawn. i think the error is in move() code.
raj
 
Posts: 4
Joined: Sun Sep 14, 2008 8:16 am

Re: Help me with this

Postby raj » Sat Sep 27, 2008 3:02 pm

i have done it myself now
raj
 
Posts: 4
Joined: Sun Sep 14, 2008 8:16 am


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 2 guests

cron