number fall

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

number fall

Postby rahul7star » Fri Jul 04, 2008 5:51 am

HI i want to develop number game wer number used to fall from top . number will appear randomly (0-9) . each time screen is having only 1 number .the moment any number levaes the screen ..next will appear .............any idea to start with
thanks
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby rahul7star » Fri Jul 04, 2008 7:08 am

this code lets you to have 1 image fall down ..but i want to do this in continuity like whenevr 1 leaves other will appear.........



Code: Select all
import javax.microedition.lcdui.*;
import java.util.Random;
import javax.microedition.lcdui.game.*;
public class game2 extends GameCanvas implements Runnable {
private boolean isPlay; // Game Loop runs when isPlay is true
private long delay; // To give thread consistency
private Image i1,i2,i3,i4,i5,i6;
private int ran,lop,a;
// Sprites to be used
private Sprite[] sprite;
// Layer Manager
private int index;
private Random random;
private LayerManager layerManager;
// Constructor and initialization
public game2()  {
super(true);
delay = 20;
lop=1;
try {i1=Image.createImage("/score.png");
i2=Image.createImage("/name.png");
i3=Image.createImage("/player.png");
i4=Image.createImage("/mobile.png");
i5=Image.createImage("/score.png");
i6=Image.createImage("/score.png");
        }
        catch (Exception e) {
            e.printStackTrace();
        }

// Load Images to Sprites

sprite=new Sprite[6];
sprite[0]=new Sprite(i2);
sprite[1]=new Sprite(i1);
sprite[2]=new Sprite(i3);
sprite[3]=new Sprite(i4);
sprite[4]=new Sprite(i5);
sprite[5]=new Sprite(i6);
layerManager = new LayerManager();
}
public void start() {
isPlay = true;
Thread t = new Thread(this);
t.start();
}
public void stop() { isPlay = false; }
// Main Game Loop
public void run() {
Graphics g = getGraphics();
while (isPlay == true) {
   
try { Thread.sleep(70); }
catch (InterruptedException ie) {}
drawScreen(g);
player();
pos();
ran();
}

}
// Method to Handle User Inputs


// Method to Display Graphics
private void drawScreen(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0,0,0);
g.fillRect(0,getHeight()-10,getWidth(),20);
layerManager.paint(g,90,0);
flushGraphics();
System.out.println(a);
}
public void player()
{

// if(p1.getY()==getHeight()-p1.getHeight())
{

}
//else
{
//p1.setPosition(0,i++);
}


}
private void input() {
int keyStates = getKeyStates();
{
if ((keyStates & RIGHT_PRESSED) !=0 )
{
//p5.move(2,0);
}
if ((keyStates & LEFT_PRESSED) !=0 )
{
// p5.move(-2,0);
}
if ((keyStates & UP_PRESSED) !=0 )
{
// p5.move(0,-2);
}
if ((keyStates & DOWN_PRESSED) !=0 )
{
// p5.move(0,2);

}
}

}
public int ran()
{
random = new Random();
a = Math.abs(random.nextInt() % 2);
return a;

}

public void pos()
{

if(a==1)
{a++;
if(System.currentTimeMillis() -10 > 300) {
layerManager.append(sprite[1]);
sprite[1].move(0,lop++);
}
else if(a==0)
       
{a++;
if(System.currentTimeMillis() -10 > 300) {
layerManager.append(sprite[4]);
sprite[1].move(0,lop++);
}
}
else if(a==3)
{
if(System.currentTimeMillis() -10 > 300) {
layerManager.append(sprite[3]);
sprite[3].move(0,lop++);
}
else if(a==4)
{
if(System.currentTimeMillis() -10 > 300) {
layerManager.append(sprite[4]);
sprite[4].move(0,lop++);
}
}}
}
}
}
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby DevelopmentTeam » Fri Jul 04, 2008 9:34 am

Check if the position goes more than the screen and then reset the position to 0. This will make the repeating, and when you repeat run the random function.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: number fall

Postby rahul7star » Fri Jul 04, 2008 11:23 am

can you modify mthis code .......................so dat it will be helpfull...
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby DevelopmentTeam » Fri Jul 04, 2008 4:27 pm

The code you have attached seems to be messy and not self explanatory, I have to struggle to find the variable which you use for position.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

word freeze

Postby rahul7star » Mon Jul 21, 2008 10:18 am

hi i got stuck in .......when the word is falling how to make dem freeze into the ground......like tetris concept....somt time words can be 3 letter somtime 4..so it will fall and freeze werevr space is empty in ground...plz show me any way to proceed with this.
thanks
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: word freeze

Postby DevelopmentTeam » Mon Jul 21, 2008 10:39 am

The concept of tetris is completely different from what we discussed before. You have to keep an array of what each cell has in it and check everytime if there is any empty cell and a cell filled with a character above it. If so you have to swap them. This might be better for tetris kind of games.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: number fall

Postby rahul7star » Tue Jul 22, 2008 11:31 am

look i need to refine this code....i want to write 1 function that will hold ever word falling ...right now i am using 'w'1 flag holding each word here is the code........
//////////

Code: Select all
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Canvas;
import java.util.*;
import java.io.*;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;


public class MainGame extends Canvas implements Runnable
{
int w1=330;
    int width,height,startX,startY,endX,endY,storeX,storeY;
    int i=0,j=0,k=0,d=0;
     private int[] endingPoint={147,147,147,147,147,147,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204};
    private int[] startingPoint={20,20,20,20,20,20,20,20,20,20,20,20,20};
        private int[] Point={0,0,0,0,0,0,0,0,0,0,0,0,0};
        private boolean startGame;
        private int Ans1,Ans2,Ans3,Ans4,Ans5,Ans6;
        int time=10;
        int store=0;
       
       
  Random r = new Random();
  private char firstChar,secondChar,thirdChar;

public char [][] words={{'a','b','c','w'},{'d','e','f'},{'k','l','m','n'},{'p','q','r'},{'s','t','u','v'},{'h','e','l','l','o'},{'s','t','e','v','e','n'},{'w','a','t','e','r'},{'b','r','a','v','e'},{'b','o','l','d'}};




  public char[][] Map=         
      {
               
                  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
...........................
                                  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                                  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
                                  {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '},
..............................
                         
     
     
      };
             


  public void start()
  {
  startGame = true;
      Thread t = new Thread(this);

      t.start();
                 
               
               
               

     
  }


   private void Map(Graphics g)
   {
            g.setColor(0x000000);
      for(i=0;i<29;i++)
      for(j=0;j<25;j++)
      {
                               
      g.drawChar(Map[i][j],i*7,j*7,g.TOP|g.LEFT);
           
           
           
           
      }
   }
        public void run()
  {
        w1= 2;//   Math.abs(r.nextInt()%2);
           
       
       
   while(startGame)
        {
            try {repaint();
                Thread.sleep(80);
               
                               
            {
               wait(50L);
            }
               
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
           
         
         
        }
       
  }
  public void paint(Graphics g)
  {

     
      g.setColor(0xffffff);
      g.setColor(0xffffff);
    g.fillRect(0, 0, getWidth(), getHeight());
   

         Map(g);

   
   
    //////////////first word fall
      if(w1==2)
      {
       
          g.setColor(0x000000);
         
                       
                    g.drawString(""+words[1][0],0,Point[0],g.TOP|g.LEFT);
         Point[0]+=time;
         
                   
                    g.drawString(""+words[1][1],7,Point[1],g.TOP|g.LEFT);
                    Point[1]+=time;
                   g.drawString(""+words[1][2],14,Point[2],g.TOP|g.LEFT);
                   Point[2]+=time;
                   
                   
                   
                   if(Point[0]>=endingPoint[0])
                   {
                       Point[0]=endingPoint[0];
                       Ans1=1;
                   
                       
                       
                   }
                    if(Point[1]>=endingPoint[1])
                   {
                       Point[1]=endingPoint[1];
                       
                       
                     
                       Ans2=1;
                   }
                    if(Point[2]>=endingPoint[2])
                   {
                       Point[2]=endingPoint[2];
                     
                       
                       Ans3=1;
                   }
                   
                   
                   if(Ans1==1& Ans2==1 & Ans3==1)
                   {endingPoint[0]=endingPoint[0]-7;
                    endingPoint[1]=endingPoint[1]-7;
                    endingPoint[2]=endingPoint[2]-7;
                       w1=1;//////// new word start falling
                       Point[0]=0;
                        Point[1]=0;
                         Point[2]=0;
                   
                      Map[0][21]='d';
                      Map[1][21]='e';
                        Map[2][21]='f';                                                                                   
                                                                   
                       
                       Map(g);
                       
                       
                     
                   }
                 
                   
               
      }///////////first ends
   
     
   
}
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby DevelopmentTeam » Wed Jul 23, 2008 5:13 am

The way you are writing the code seems to be different that the general Tetris idea. We usually dont play with position of the character in any object falling games like Tetris. Instead there is a array which is manipulated accordingly. If you can check Sokoban code or tetris code, you will understand what I am saying.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: number fall

Postby rahul7star » Wed Jul 23, 2008 7:33 am

Hi i am very close to it..............now word falling is random but again to fix them in ground is difficult.
midlet code///////
Code: Select all
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
*
* @author blundell
* @version
*/
public class MainMidlet extends MIDlet {

public void startApp() throws MIDletStateChangeException {

try {
MainGame midlet1 = new MainGame();

Display.getDisplay(this).setCurrent(midlet1);

midlet1.start();
} catch (Exception e) {
e.printStackTrace();
}

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}


////////midlet ends
////////class code
import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MainGame extends Canvas implements Runnable {

/* CONSTANTS */
/* Speed rate at which numbers fall */
private static final int FALL_SPEED = 5;
/* Value range of numbers on screen 0-*+1 */
private static final int VALUE_RANGE = 10;
/* Screen refresh rate */
private static final long FRAME_SPEED = 100;
private String  chars[]={"Hello","Welcome","hit","kick","fine","soccer","play","huge","welcome","matrix","gone","dog"};
/* VARIABLES */
int width = getWidth();
int height = getHeight();
boolean startGame = false;
boolean newNumber = true;
int x = 0;
int y = 0;

Graphics g;

Random generatorNumber = new Random();
Random generatorX = new Random();
int randomNumber = 0;
int randomX = 0;

public void start() {
generatorNumber.setSeed(System.currentTimeMillis());
generatorX.setSeed(System.currentTimeMillis());
startGame = true;
final Thread t = new Thread(this);
t.start();
}

public void run() {
while (startGame) {

goGoGo(g);

repaint();
serviceRepaints();
}
}

private void goGoGo(Graphics g) {

if (y > height) {
y = 0;
newNumber = true;
}

if (newNumber) {

randomNumber = generatorNumber.nextInt();
randomNumber = Math.abs(generatorNumber.nextInt());// This creates
// the random
// number...
randomNumber = randomNumber % VALUE_RANGE;// This is a number
// between 0-100

randomX = generatorX.nextInt();
randomX = Math.abs(generatorX.nextInt());// This creates the random*/
// number...
randomX = 0 + randomX % width;// This is a number between 0-100

newNumber = false;
}


y += FALL_SPEED;
}

public void paint(Graphics g) {

g.setColor(0x000000);
g.fillRect(0, 0, width, height);

g.setColor(0xffffff);
g.drawString(""+chars[randomNumber], randomX, y, Graphics.TOP|Graphics.LEFT);

try {
Thread.sleep(FRAME_SPEED);
} catch (InterruptedException e) {

e.printStackTrace();
}

}
}

////////

can u help me
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby DevelopmentTeam » Wed Jul 23, 2008 9:14 am

The code you have has no sign of storing the characters that have reached the ground. If you still want to keep the code in the same manner and try just to add the character fixing code, then you have to store the positions of the characters that have reached the ground in an array. If you want an optimized way of coding the same game, you have to completely manipulate things in an array.

Code: Select all
private int charPos[][] // = initialize the array as you desire
private int nextChar = 0;

...
...

y += FALL_SPEED;
if(y > height) {
       nextChar++;
       y = 0;
       newNumber = true;
       charPos[nextChar][0] = x;
       charPos[nextChar][1] = y;
}

...
...

//Draw all the characters in the screen that are stored in the posChar array.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: number fall

Postby rahul7star » Thu Jul 24, 2008 9:04 am

hi i am confused with this

see chars[randomNumber]....in this "chars" is string array and "randomNumber" is int...ok now -> chars[randomNumber]. showing current word in the screen


now i want to store this value to some array and display it into the screen.....but
i m tryng to do charPos[nextChar]=chars[randomNumber]; so that i can disply charPos[nextChar]....but error is der might be some conversion problm..

can you suggest me the way...in charPos[nextChar]. charPos is String araay and nextChar is int.
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby rahul7star » Thu Jul 24, 2008 9:06 am

something like this

Code: Select all
import java.util.Random;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

public class MainGame extends Canvas implements Runnable {

/* CONSTANTS */
/* Speed rate at which numbers fall */
private static final int FALL_SPEED = 5;
/* Value range of numbers on screen 0-*+1 */
private static final int VALUE_RANGE = 10;
/* Screen refresh rate */
private static final long FRAME_SPEED = 100;
private String  chars[]={"Hello","Welcome","hit","kick","fine","soccer","play","huge","welcome","matrix","gone","dog"};
/* VARIABLES */
int width = getWidth();
int height = getHeight();
boolean startGame = false;
boolean newNumber = true;
int x = 0;
int y = 0;

Graphics g;

Random generatorNumber = new Random();
Random generatorX = new Random();
int randomNumber = 0;
int randomX = 0;
// = initialize the array as you desire
[b]private int nextChar = 0;
private String charPos[];[/b]

public void start() {
/*generatorNumber.setSeed(System.currentTimeMillis());;
generatorX.setSeed(System.currentTimeMillis());*/
startGame = true;
final Thread t = new Thread(this);
t.start();
}

public void run() {
while (startGame) {
   // System.out.println(charPos[0]);

goGoGo(g);

repaint();
serviceRepaints();
}
}

private void goGoGo(Graphics g) {

if (y > height) {
y = 0;

[b]charPos[nextChar]=chars[randomNumber];
nextChar++;[/b]


newNumber = true;
}

if (newNumber) {

randomNumber = generatorNumber.nextInt();
randomNumber = Math.abs(generatorNumber.nextInt());// This creates
// the random
// number...
randomNumber = randomNumber % VALUE_RANGE;// This is a number
// between 0-100

randomX = generatorX.nextInt();
randomX = Math.abs(generatorX.nextInt());// This creates the random*/
// number...
randomX = 0 + randomX % width;// This is a number between 0-100

newNumber = false;
}


y += FALL_SPEED;
}

public void paint(Graphics g) {

g.setColor(0x000000);
g.fillRect(0, 0, width, height);

g.setColor(0xffffff);
g.drawString(""+chars[randomNumber], randomX, y, Graphics.TOP|Graphics.LEFT);
//[b]g.drawString(""+charPos[nextChar], randomX, y, Graphics.TOP|Graphics.LEFT);[/b]

try {
Thread.sleep(FRAME_SPEED);
} catch (InterruptedException e) {

e.printStackTrace();
}

}
}
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Re: number fall

Postby DevelopmentTeam » Fri Jul 25, 2008 5:07 am

Yes I meant something like this. But you have not drawn all the characters stored, you are storing only the last character. Use a for loop to draw all the characters.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India

Re: number fall

Postby rahul7star » Fri Jul 25, 2008 7:42 am

thanks for you help......you guys are gr8..and helpfull

now a bit off the topic....i am having player who can jump and walk....but i want the BG should move......and somwtimes hill are also ther so ....do i need to replicates tiles agaian and again cause i want deifferent sceene each time he is movien forward .....can you provide me the link just like mario used to move on tiles...somethng like that..

Thanks again
rahul7star
Regular User
 
Posts: 84
Joined: Fri Aug 17, 2007 11:22 am

Next

Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 2 guests

cron