Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: My first rpg with a custom data type

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Location
    San Diego CA
    Posts
    2
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up [Solved]My first rpg with a custom data type

    Well, I've made an RPG and i've made a mapmaker so my friends can make maps. They save a file that stores the map data. I've also made a custom data type that stores the mapdata from the file. There is some kind of glitch where the data isn't transfered to the main class.

    And, I apologize for the messy code

    Here is the code for the main class:


    import java.applet.Applet;
    import java.awt.Image;
    import java.awt.Graphics;
    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.Event;
    import java.awt.image.*;
     
     
     
     
    public class Kyle extends Applet implements Runnable {
     
        Graphics gBG,gMain,mapGraphics;
        Image grphImg,mapImg,terrainAndTextures[]=new Image[1000] ;
        int i,j,k,count100,count1,lp,speedMUL=1,speed=30,loopCount,playerPos;
        int count2,dir,staleFrame=1,gpX,gpY;
        int motion,staleMotion,inFront,sideIX[]={0,-16,16,-1,1,-17,-15,15,17};
        int whichFrame,xAdder,yAdder;
        int indexSaver;
        int mapData,height=1,width=1;
        double countSlow=1,countSuperSlow=1,di,dj;
        double playerX=33,playerY=33,movUD=1, movLR=1,oldMovUD=1, oldMovLR=1;
        boolean u,d,l,r,mD,pause,noDir,skip;
        Image ih=null;
        int mapBG[]=new int[256];
        Thread game;
     
        int centerX, centerY;
        Image pers[],persBig,backBuffer,main,curs[],cursCol,map;
        MediaTracker tracker;
        ImageFilter filter;
        int terrain[]=new int[257];
        Rectangle2D terrainBox[]= new Rectangle2D[256], playerBox;
        ImageProducer person,curser;
        String data[]=new String[500];
        readmapfile rd=new readmapfile();
        Maps maps=new Maps();
        Graphics rg;
     
     
        @Override
        public void init() {
            maps.declareAndReadMaps();
            resize(512,512);
            mapImg=createImage(512,512);
            mapGraphics=mapImg.getGraphics();
     
            tracker=new MediaTracker(this);
            pers=new Image[15];
            curs=new Image[15];
            backBuffer= createImage(512,512);
            main= createImage(512,512);
            gMain=main.getGraphics();
            gBG = backBuffer.getGraphics();
            persBig=getImage(getCodeBase(),"resources/char.png");
            cursCol=getImage(getCodeBase(),"resources/cursor.png");
            person=persBig.getSource();
            curser=cursCol.getSource();
            tracker.addImage(cursCol, 0);
            buildMap();
            tracker.addImage(main, 0);
            tracker.addImage(backBuffer, 0);
            tracker.addImage(persBig, 0);
     
     
            waitToLoad(0);
     
            k=1;j=0;i=0;
            while (k<13)
    		{
    			filter=new CropImageFilter(j*32,i*32,32,32);
    			pers[k]=createImage(new FilteredImageSource(person,filter));
    			tracker.addImage(pers[k], 1);
     
    			k++;
    			j++;
    			if (j==3)
    			{
    				j=0;
    				i++;
    			}
    		}
            k=1;
            j=0;
            i=0;
            while (k<13)
    		{
    			filter=new CropImageFilter(j*32,i*32,32,32);
    			curs[k]=createImage(new FilteredImageSource(curser,filter));
    			tracker.addImage(curs[k], 1);
     
    			k++;
    			j++;
    			if (j==3)
    			{
    				j=0;
    				i++;
    			}
    		}
            setBackground(Color.WHITE);
            getImage();
            waitToLoad(1);
            //mapDataDraw();
     
     
     
     
        }
        private void getImage() {
            int pk,pj,pi;
            Image img,img1;
            ImageProducer collectionProducer,collectionProducer1;
     
            img=getImage(getCodeBase(),"resources/TileB.png");
            img1=getImage(getCodeBase(),"resources/TileA2.png");
            collectionProducer=img.getSource();
            collectionProducer1=img1.getSource();
            pk=0;pi=0;pj=0;
            while (pk<256)
    		{
    			filter=new CropImageFilter(pj*32,pi*32,32,32);
    			terrainAndTextures[pk]=createImage(new FilteredImageSource(
    				collectionProducer,filter));
    			tracker.addImage(terrainAndTextures[pk],1);
     
    			pk++;
    			pj++;
    			if (pj==16)
    			{
    				pj=0;
    				pi++;
    			}
    		}
            pj=0;pi=0;
            while (pk<500)
    		{
    			filter=new CropImageFilter(pj*32,pi*32,32,32);
    			terrainAndTextures[pk]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[pk],1);
     
    			pk++;
    			pj++;
    			if (pj==15)
    			{
    				pj=0;
    				pi++;
    			}
    		}
     
            int xAdd=0,yAdd=0;
            k=0;
            for (i=1;i<33;i++)
            {
                            filter=new CropImageFilter(16+64*xAdd,48+96*yAdd,32,32);
    			terrainAndTextures[500+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[500+k],1);
     
                            filter=new CropImageFilter(16+64*xAdd,32+96*yAdd,32,32);
    			terrainAndTextures[501+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[501+k],1);
     
                            filter=new CropImageFilter(16+64*xAdd,64+96*yAdd,32,32);
    			terrainAndTextures[502+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[502+k],1);
     
                            filter=new CropImageFilter(0+64*xAdd,48+96*yAdd,32,32);
    			terrainAndTextures[503+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[503+k],1);
     
                            filter=new CropImageFilter(32+64*xAdd,48+96*yAdd,32,32);
    			terrainAndTextures[504+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[504+k],1);
     
                            filter=new CropImageFilter(0+64*xAdd,32+96*yAdd,32,32);
    			terrainAndTextures[505+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[505+k],1);
     
                           filter=new CropImageFilter(32+64*xAdd,32+96*yAdd,32,32);
    			terrainAndTextures[506+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[506+k],1);
     
                            filter=new CropImageFilter(0+64*xAdd,64+96*yAdd,32,32);
    			terrainAndTextures[507+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[507+k],1);
     
                           filter=new CropImageFilter(32+64*xAdd,64+96*yAdd,32,32);
    			terrainAndTextures[508+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[508+k],1);
     
                            filter=new CropImageFilter(0+64*xAdd,0+96*yAdd,32,32);
    			terrainAndTextures[509+k]=createImage(new FilteredImageSource(
    				collectionProducer1,filter));
    			tracker.addImage(terrainAndTextures[509+k],1);
     
                            if(xAdd==7)
                            {
                                xAdd=0;
                                yAdd++;
                            }
                            else
                            xAdd++;
                            k+=10;
     
            }
     
     
     
        }
        @Override
        public void run()
        {
     
     
           while (game !=null)
    		{
                        while (pause==false)
                        {
    			try
    			{
    				game.sleep(30);
    			} catch (InterruptedException e) {}
     
                            count2++;
                            if (count2>2)
                            count2=1;
                            countSlow+=.2;
                            if (countSlow>2)
                            countSlow=1;
                            countSuperSlow+=.01;
                            if (countSuperSlow>2)
                            countSuperSlow=1;
                            cursorSet(1);
     
     
                            move();
     
                            math();
                            draw();
     
     
                            mapCheck();
                            resetVar();
     
     
                        } 
                    }
        }
        @Override
        public void start()
    	{
    		if (game==null)
    		{
    			game=new Thread(this);
    			game.start();
    		}
    	}
        @Override
        public void stop()
    	{
    		if ((game!=null)&&(game.isAlive()))
    		{
    			game.stop();
    		}
    		game=null;
    	}
        public void resetVar()
        {
            motion=0;
            movUD=0;
            movLR=0;
            speedMUL=speed;
            loopCount=0;
        }
        @Override
        public boolean mouseDown(java.awt.Event e,int x, int y)
        {
            /*int lp=512;
            boolean ans=false;
            int xDiff=x-(int)playerX;
            int yDiff=y-(int)playerY;
            mD=true;
            while (ans==false)
            {
                System.out.println(lp);
                if (xDiff%lp<=1&&yDiff%lp<=1)
                {
                    ans=true;
                    xAdder=xDiff/lp;
                    yAdder=yDiff/lp;
                    System.out.print("the lowest common denominator is: "+lp+".");
                }
                else
                lp--;
            }*/
            return true;
        }
        public void setMap(Map map)
        {
     
            j=0;k=0;
            for(i=0;i<256;i++)
            {
                mapGraphics.drawImage(terrainAndTextures[map.getBGValueBySquare(i)], j*32, k*32, this);
                mapGraphics.drawImage(terrainAndTextures[map.getFGValueBySquare(i)], j*32, k*32, this);
                j++;k++;
                if (j==16)
                {
                    j=0;k++;
                }
            }
        }
     
        @Override
        public boolean keyDown(java.awt.Event e,int key)
        {
     
     
            switch (key)
            {
                case 119://w
                    u=true;
                    System.out.println("kyle:"+maps.tutorial.getBGValueBySquare(1));
                    setMap(maps.forrestOutOfTown);
                    break;
                case 97://a
                    l=true;
                   setMap(maps.tutorial);
                    break;
                case 115://s
                    d=true;
     
                    break;
                case 100://d
                    r=true;
     
                    break;
                default:
                    break;
     
            }
     
     
            return true;
        }
        @Override
        public boolean keyUp(Event e, int key)
        {
     
            switch (key)
            {
                case 119://w
                    u=false;
     
                    break;
                case 97://a
                    l=false;
     
                    break;
                case 115://s
                    d=false;
     
                    break;
                case 100://d
                    r=false;
     
                    break;
                default:
                    break;
            }
     
     
           return true; 
        }
        public boolean isCharMoving()
        {
            if (u==true||l==true||u==true||r==true||d==true||l==true||d==true||r==true)
                return true;
            else
                return false;
        }
        public void waitToLoad(int id)
        {
            try
    		{
    			tracker.waitForAll(id);
    		}
    			catch(InterruptedException e) {}
        }
        public void cursorSet(int k)
        {
           Point p=new Point(9,9);
     
           setCursor(getToolkit().createCustomCursor(curs[k],p, "kyle"));
        }
        public void move()
        {
     
     
     
            if(u&&l||u&&r||d&&l||d&&r)
            {
     
            }
            else
            {
            if (u==true)
            {
                motion=1;
                dir=10;
                movUD-=speedMUL;
            }
            if (d==true)
            {
                motion=2;
                dir=1;
                movUD+=speedMUL;
            }
            if (l==true)
            {
                motion=3;
                dir=4;
                movLR-=speedMUL;
            }
            if (r==true)
            {
                motion=4;
                dir=7;
                movLR+=speedMUL;
            }
            }
     
     
     
            staleMotion=motion;
     
           movUD/=10;
           movLR/=10;
           defineBoxes();
           mapCheck();
           switch(motion)
    			{
                                    case 0:
                                            break;
    				case 1:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 2:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 3:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 4:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
                                    case 5:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 6:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 7:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				case 8:
    					playerX+=movLR;
                                            playerY+=movUD;
    					break;
    				default:
    					break;
    			}
     
     
        }
        public void buildMap()
        {
            Graphics g;
            map=createImage(512,512);
            g=map.getGraphics();
            g.setColor(Color.BLACK);
            g.setClip(300, 300, 100, 100);
            g.fillRect(300,300,100,100);
     
        }
     
        public void defineBoxes()
        {
     
           double tempY=(playerY+16-((playerY+16)%32))/32;
           playerPos=(int)Math.round((tempY*16+playerX/32));
           playerBox=new Rectangle2D.Double(playerX-1,playerY-1,34,34);
                    loopCount=0;
                    indexSaver=0;
                    if (playerY<=32&&u)
                        indexSaver=2;
                    if(playerPos+sideIX[staleMotion]<0)
                    {
            indexSaver+=20;
                    }
                    else 
                    {
            indexSaver=0;
                    }
                    if(playerPos+sideIX[staleMotion]>256)
                    {
            indexSaver=-20;
                    }
     
     
            for (dj=0;dj<16;dj++)
            {
                for (di=0;di<16;di++)
                {
     
                    terrainBox[loopCount]=new Rectangle2D.Double(di*32,dj*32,32,32);
                    terrain[loopCount]=0;
     
                    loopCount+=1;
     
                }
            }
     
     
     
            inFront = terrain[playerPos+sideIX[staleMotion]+indexSaver];
        }
        public void math()
        {
             gpX=(int)Math.round(playerX);
             gpY=(int)Math.round(playerY);
              whichFrame=(int)Math.round(countSlow)+dir;
               if (isCharMoving())
            staleFrame=whichFrame;
               //HEIGHT & WIDTH CONFIGURATION
               height=this.getHeight();
           width=this.getWidth();
               if (width!=height)
               {
                   if (width>height)
                   {
                       int diff=width-height;
                       centerX=diff/2;
     
                       width=height;
     
                   }
                   else
                   {
                       int diff=height-width;
                       centerY=diff/2;
                       height=width;
                   }
     
               }
               int h,w;
               if ((h=height%32)!=0||(w=width%32)!=0)
               {
                   height=height-height%32;
                   width=width-width%32;
               }
     
     
        }
        public void wait(int howLong)
        {
            try
    			{
    				game.sleep(howLong);
    			} catch (InterruptedException e) {}
        }
        public void draw()
        {
     
            gBG.setColor(Color.red);
     
     
     
     
     
     
            gBG.drawImage(mapImg, 32, 32, this);
     
            gBG.drawString(""+playerPos,100,100);
           gBG.drawImage(pers[staleFrame], gpX, gpY, this);
     
           gBG.drawString(""+maps.tutorial.getBGValueBySquare(0), 300, 300);
     
     
          repaint();
     
        }
       public void mapCheck()
       {    
    if (inFront==1&&playerBox.intersects(terrainBox[playerPos+sideIX[staleMotion]]))
    {
        motion=0;
        gBG.setColor(Color.red);
        gBG.drawString("ya",100,300);
    }
    if (playerY==0&&u||playerY==480&&d)
    {
        motion=0;
    }
    if (playerX==0&&l||playerX==480&&r)
    {
        motion=0;
    }
     
     
    }
        public void paint(Graphics g)
        {
     
            g.drawImage(ih, centerX, centerY, this);
            ih=createImage(width,height);
     
     
          Graphics rg=ih.getGraphics();
          rg.drawImage(backBuffer, 0, 0,width,height, this);
     
     
     
     
     
     
     
        }
     
     
     
    }
    And the Code with the maps to use:

    /**
     *
     * @author Kyle
     */
    public class Maps
     
    {
     
        public Map tutorial=new Map();
        public Map forrestOutOfTown=new Map();
        public Map forrestTown=new Map();
        int BGData[]=new int[256], FGData[]=new int[256];
        readmapfile rd;
     
        String s,s2;
        int i;
        public void declareAndReadMaps()
        {
            rd=new readmapfile();
             int mapBG;
             int mapData;
             String str[]=new String[256];
             str=rd.read("tutorial");
             Kyle kl=new Kyle();
     
             for (i=0;i<256;i++)
             {
          if (str[i].contains(":"))
          {
          s= str[i].substring(0, str[i].indexOf(":"));
          s2=str[i].substring(str[i].indexOf(":")+1);
          }
          else
          {
              if (str[i]=="0")
              {
              s="0";
              s2="0";
              }
              else
              {
                  s=str[i];
                  s2="0";
              }
     
          }
          BGData[i]=Integer.parseInt(s);
          FGData[i]=Integer.parseInt( s2 );
          }
     
             tutorial.setData(BGData, FGData);
             for (i=0;i<256;i++)
             {
             BGData[i]=0;
             FGData[i]=0;
             s="0";
             s2="0";
             str[i]="0";
             }
             str=rd.read("forrestOutOfTown");
             kl.wait(1000);
             for (i=0;i<256;i++)
             {
          if (str[i].contains(":"))
          {
          s= str[i].substring(0, str[i].indexOf(":"));
          s2=str[i].substring(str[i].indexOf(":")+1);
          }
          else
          {
              if (str[i]=="0")
              {
              s="0";
              s2="0";
              }
              else
              {
                  s=str[i];
                  s2="0";
              }
     
          }
          BGData[i]=Integer.parseInt(s);
          FGData[i]=Integer.parseInt( s2 );
          }
     
             forrestOutOfTown.setData(BGData, FGData);
        }
    }
    And the Map data type class:

    /**
     *
     * @author Kyle
     */
    public class Map {
     
     
     
        private int num;
        private int mapBGData[]=new int[256];
        private int mapFGData[]=new int[256];
     
        public Map() {
     
     
        }
        public void setData(int mapBGData[], int mapFGData[])
        {
            this.mapBGData=mapBGData;
            this.mapFGData=mapFGData;
        }
        public int[] getBGValue() {
            return mapBGData;
        }
        public int[] getFGValue() {
            return mapFGData;
        }
        public int getBGValueBySquare(int whichSquare) {
            return mapFGData[whichSquare];
        }
        public int getFGValueBySquare(int whichSquare) {
            return mapFGData[whichSquare];
        }
     
    }
    And the ReadMapFile class:



    import java.io.*;
     
    public class readmapfile 
    {
     
     String strLine[]=new String[500];
        int i;
        public String[] read(String map)
        {
            for(i=0;i<256;i++)
            {
           strLine[i]="0";
            }
            i=0;
      try{
      // Open the file that is the first 
      // command line parameter
      FileInputStream fstream = new FileInputStream("maps/"+map+".kylemapfile");
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
     
      //Read File Line By Line
      while ((strLine[i] = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine[i]);
      i++;
      }
      //Close the input stream
      in.close();
        }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      }
     
      return strLine;
        }
     
    }
    Last edited by Kylelem62; April 13th, 2012 at 08:31 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Location
    Missouri, United States
    Posts
    17
    Thanks
    4
    Thanked 2 Times in 2 Posts

    Default Re: My first rpg with a custom data type

    I think the following lines in your declareAndReadMaps() method in your Maps class may be incorrect:
    public void declareAndReadMaps()
        {
            .....
             Kyle kl=new Kyle();
            .....
            kl.wait(1000);
        }

    If that is supposed to be the Kyle object that calls the declareAndReadMaps() method, then the method should be modified slightly:

    public void declareAndReadMaps(Kyle kl)
        {
            .....
            kl.wait(1000);
        }

    In your Kyle class, just have it provide itself as the parameter when calling that method.

    I hope that fixes the problem. If not, does your readmapfile.class print the correct input?

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Location
    San Diego CA
    Posts
    2
    My Mood
    Bitchy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My first rpg with a custom data type

    Thanks for the input, that was a problem, but had fixed that right after I posted here. The actual problem was that I wasn't using different variables in the Maps class, and whenever I tried to retrieve the data, it was either not there or it was different.
    My Wrong code:
    tutorial.setData(BGData, FGData);
    //code that changes the data in BGData and FGData
    forrestOutOfTown.setData(BGData, FGData);

    Soulution:
    int BGData[][]=new int[20][256];
    int BGData[][]=new int[20][256];
    tutorial.setData(BGData[0], FGData[0]);

    forrestOutOfTown.setData(BGData[1], FGData[1]);

Similar Threads

  1. Replies: 2
    Last Post: February 17th, 2012, 01:13 AM
  2. [SOLVED] what data type should i use in MySql for a java object to be saved
    By chronoz13 in forum JDBC & Databases
    Replies: 4
    Last Post: October 9th, 2011, 07:17 AM
  3. Primitive data type
    By stuart harper in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2011, 12:14 PM
  4. About heap data type
    By PaddyWangTheGG in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 17th, 2010, 03:02 PM
  5. Selection Sorting of Data type (Char)
    By chronoz13 in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 20th, 2009, 07:28 PM

Tags for this Thread