Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 3:18 pm

All times are UTC




Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Including tile map
PostPosted: Tue Sep 18, 2012 5:25 pm 
Offline

Joined: Mon Sep 17, 2012 8:42 pm
Posts: 48
Hi!! I wanna use my Map.java from old source into Slick2D.

Here's Code for Map.java
Code:
package TLT;


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.Image;


public class Map {
   /*@author Skatty
    *
    *started: czerwiec 2010
    *
    *modified 8 wrzesien 2012
    *Klasa opisująca mapę. Grafiki dla wskazanych X,Y
    *oraz wartości blocked dla wskazanych X,Y
    *
    *
    */


   private static final int CLEAR = -2;
   public static final int blocked1 = 999;

   //parametry mapy
   public static final int WIDTH2 = 5;
   private static final int WIDTH = 100;
   private static final int HEIGHT = 100;

   //rozmiar tiles'a
   public static final int TILE_SIZE = 64;

   //2 tablice - 1 reprezentuje "blocked" druga grafike tiles
   public int [][] blockedxy = new int[WIDTH] [HEIGHT];
   private int[][] data = new int[WIDTH][HEIGHT];

   //Tablica z obrazkami tiles'ów
Image[] tils = new Image[39]; {
tils[0] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/1.png");
tils[1] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grass.png");
tils[2] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/water.gif");
tils[3] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/sandHD2.png");
tils[4] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grasan.png");
tils[5] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsan.png");
tils[6] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/darkwater.gif");
tils[7] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grasanRot.png");
tils[8] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/stone2.png");
tils[9] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/stone1.png");
tils[10] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanROT.png");
tils[11] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/glone.png");
tils[12] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gloneLD.png");
tils[13] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gloneRD.png");
tils[14] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gloneLU.png");
tils[15] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gloneRU.png");
tils[16] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/lakegrass.png");
tils[17] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/lakesand.png");
tils[18] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/road.png");
tils[19] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/roadRD.png");
tils[20] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanD.png");
tils[21] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanL.png");
tils[22] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanLD.png");
tils[23] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanLU.png");
tils[24] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanRD.png");
tils[25] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanRU.png");
tils[26] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/watsanU.png");
tils[27] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/cornerwatRD.png");
tils[28] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/cornerwatLU.png");
tils[29] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/cornerawatLD.png");
tils[30] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/cornerwatRU.png");
tils[31] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gscLU.png");
tils[32] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gscLD.png");
tils[33] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gscRU.png");
tils[34] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/gscRD.png");
tils[35] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grasanD.png");
tils[36] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grasanR.png");
tils[37] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/grasanU.png");
tils[38] = Toolkit.getDefaultToolkit().getImage("GFX/Tiles/barchest.png");

}
public Map() {
            for (int y=0;y<20;y++) {
            data[1][y] = 2;
            data[2][y] = 2;
            data[3][y] = 2;
            data[4][y] = 2;
            data[5][y] = 6;
            data[6][y] = 6;
            data[7][y] = 6;
            data[8][y] = 6;
            data[9][y] = 6;
            data[10][y] = 3;
            data[11][y] = 3;
            data[12][y] = 3;
            data[13][y] = 3;
            data[14][y] = 3;
            data[15][y] = 3;
            data[16][y] = 3;
            data[17][y] = 3;
            data[18][y] = 3;
            data[19][y] = 3;
            data[20][y] = 3;

      }

   }


   public void paint(Graphics2D g) {
      for (int x=0;x<WIDTH;x++) {
         for (int y=0;y<HEIGHT;y++) {
          if ((data[x][y] >0) && (data[x][y] <39)) {
              g.drawImage(tils[data[x][y]], x*TILE_SIZE, y*TILE_SIZE, null);
               }
   }
   }
   }

   //Metoda zwraca wartość "blocked" dla wszystkich X Y ktore wynosza 999
   public boolean blocked1(float x, float y) {
      return blockedxy[(int) x][(int) y] == blocked1;
   }
}


No errors in Map class but how i can now paint Map?

My render method:
Code:
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      map.paint(g); //draw the map at 0,0 to start
      player.draw(shiftX,shiftY); //draw player at 320, 160 (center of the screen)
      g.drawString("player X: "+playerPositionX+"\nplayer Y: "+playerPositionY, 400, 20); //indicator to see where player is in his world
     
      //when they press escape
      if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         if(quit==false){
            g.clear();
         }
      }
   }

map.paint(g); //draw the map at 0,0 to start have error "g"

please help


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Tue Sep 18, 2012 6:07 pm 
Offline
Regular
User avatar

Joined: Mon Feb 07, 2011 8:48 pm
Posts: 182
Location: United Kingdom
Hi there,

Your map.paint() method is expecting a Graphics2D object and instead is being given a Slick Graphics object. The easiest way to achieve what you are trying is to use a SpriteSheet object:

Code:
SpriteSheet spriteSheet = new SpriteSheet(new Image("Path/To/Your/Image/Here.png"), 32, 32)


This particular spritesheet is being broken up into 32x32 tiles, each tile has a co-ordinate (0,1 - 0,2 - etc). Your tiles can then simply store int co-ordinate references to the individual 'tiles' you wish to draw as part of a bigger sheet. Here is a chopped example of my own code:

Code:
      TileData currentTile;
      TileData[] thisRow;
      TileData[][] tiles = mapData.getTiles();

      tileSheet.startUse();
     
      for (int x = playerX - 14; x < playerX + 15; x++)
      {
         if (x >= 0 && x < mapData.getSizeX())
         {
            // Draw the map
            thisRow = tiles[x];

            for (int y = playerY - 10; y < playerY + 11; y++)
            {
               if (y >= 0 && y < mapData.getSizeY())
               {
                  currentTile = thisRow[y];
               
                  // Ground
                  spriteSheet.renderInUse(x*32, y*32, currentTile.getGroundImageX(), currentTile.getGroundImageY());
               }
            }
         }
      }
     
      tileSheet.endUse();


The loop is badly chopped out of one of my methods so ignore the loop logic, what you are interested in is the renderInUse() call to the spriteSheet. See how my 'Tile' objects simply store x and y values. Also notice how tileSheet.startUse(); is called before the spritesheet rendering beginds, and enduse(); is called once it has finished for that render call.

This should hopefully help get you on your way,
Liam

_________________
Mirage Realms - A free, retro, 2D MMORPG being developed using Slick2D and TWL


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 2:28 am 
Offline

Joined: Mon Sep 17, 2012 1:59 am
Posts: 35
For the images that you're manually putting in the array use "new Image("filepath"); o.o Make sure they are slick images.

and change the graphics2d to a slick graphics arg. call paint from render in the slick gameloop. And it should draw fine. For slick drawImage there will be no need for the null.

Edit:
The way Liam shows is the proper way. The way I'm showing you is just a tweak on yours.


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 7:15 am 
Offline

Joined: Mon Sep 17, 2012 8:42 pm
Posts: 48
Quote:
and change the graphics2d to a slick graphics arg.


How I can change g2d method into Slick one?

After I change my images into Slick Image it will work?


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 12:48 pm 
Offline
Oldbie

Joined: Thu Mar 15, 2012 12:38 am
Posts: 260
Skatty wrote:
Quote:
and change the graphics2d to a slick graphics arg.


How I can change g2d method into Slick one?


In your map class:
Code:
public void paint(Graphics2D g) {
      for (int x=0;x<WIDTH;x++) {
         for (int y=0;y<HEIGHT;y++) {
          if ((data[x][y] >0) && (data[x][y] <39)) {
              g.drawImage(tils[data[x][y]], x*TILE_SIZE, y*TILE_SIZE, null);
               }
   }
   }
   }


Becomes:

Code:
public void paint(Graphics g) {
   .....
   }


(Will need to change your import)


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 3:44 pm 
Offline

Joined: Mon Sep 17, 2012 8:42 pm
Posts: 48
Now i have:

Code:
   public void paint(org.newdawn.slick.Graphics g) {
      for (int x=0;x<WIDTH;x++) {
         for (int y=0;y<HEIGHT;y++) {
          if ((data[x][y] >0) && (data[x][y] <39)) {
              g.drawImage(tils[data[x][y]], x*TILE_SIZE, y*TILE_SIZE, null);
               }
   }
   }
   }


In map

And
Code:
map.paint(g);
in my Play.java and 1 error:

In Map its
Code:
The method paint(Graphics) in the type Map is not applicable for the arguments (Graphics) line 39   Java Problem


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 4:01 pm 
Offline
Regular
User avatar

Joined: Mon Feb 07, 2011 8:48 pm
Posts: 182
Location: United Kingdom
I'm not being rude here, but you need to learn a lot more Java before attempting to work with Slick matey. If you don't understand method parameters and how API calls work on the most basic level, there's nothing we can do to help you.

_________________
Mirage Realms - A free, retro, 2D MMORPG being developed using Slick2D and TWL


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 4:29 pm 
Offline
Oldbie

Joined: Thu Mar 15, 2012 12:38 am
Posts: 260
Skatty wrote:
In Map its
Code:
The method paint(Graphics) in the type Map is not applicable for the arguments (Graphics) line 39   Java Problem



This looks like because you are using Java2D images with Slick. You need to convert the rest of your Map class to use the proper Slick classes (ie: the images).


Top
 Profile  
 
 Post subject: Re: Including tile map
PostPosted: Wed Sep 19, 2012 6:20 pm 
Offline

Joined: Mon Sep 17, 2012 8:42 pm
Posts: 48
WOW! I FIXED IT OUT! MAP IS LOADING PERFECT.

I only need help: My sprite of player and camera dont move (only player change anim when using diff. directions) but X and Y of player is changing.

My Play code:
Code:
public class Play extends BasicGameState{

   Animation player, movingUp, movingDown, movingLeft, movingRight; //4 animations,
   Image worldMap;
   public Map map;
   boolean quit = false;
   int[] duration = {200,200}; //duration or length of the frame
   float playerPositionX = 0; //player will start at coordinates 0,0
   float playerPositionY = 0;
   float shiftX = playerPositionX + 320; //this will shift the screen so player appears in middle
   float shiftY = playerPositionY + 160; //half the length and half the width of the screen
   
   public Play(int state){
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
     map = new Map();
      //worldMap = new Image("res/world.png");
      Image[] walkUp = {new Image("res/GFX/Animes/charB.png"), new Image("res/GFX/Animes/charB.png")}; //these are the images to be used in the "walkUp" animation
      Image[] walkDown = {new Image("res/GFX/Animes/char.png"), new Image("res/GFX/Animes/char.png")};
      Image[] walkLeft = {new Image("res/GFX/Animes/charL.png"), new Image("res/GFX/Animes/charL.png")};
      Image[] walkRight = {new Image("res/GFX/Animes/charR.png"), new Image("res/GFX/Animes/charR.png")};
     
      movingUp = new Animation(walkUp, duration, false); //each animation takes array of images, duration for each image, and autoUpdate (just set to false)
      movingDown = new Animation(walkDown, duration, false);
      movingLeft = new Animation(walkLeft, duration, false);
      movingRight = new Animation(walkRight, duration, false);
      player = movingDown; //by default as soon as game loads, player will be facing down
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
       map.paint(g);
      //worldMap.draw(playerPositionX, playerPositionY); //draw the map at 0,0 to start
      player.draw(shiftX,shiftY); //draw player at 320, 160 (center of the screen)
      g.drawString("player X: "+playerPositionX+"\nplayer Y: "+playerPositionY, 400, 20); //indicator to see where player is in his world
     
      //when they press escape
      if(quit==true){
         g.drawString("Resume (R)", 250, 100);
         g.drawString("Main Menu (M)", 250, 150);
         g.drawString("Quit Game (Q)", 250, 200);
         g.drawString("Game Menu (G)", 250, 250);
         if(quit==false){
            g.clear();
         }
      }
   }
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input input = gc.getInput();
      //during the game if the user hits the up arrow...
      if(input.isKeyDown(Input.KEY_W)){
         player = movingUp; //change bucky to up image
         playerPositionY += delta * .1f; //increase the Y coordinates of bucky (move him up)
         if(playerPositionY>162){
            playerPositionY -= delta * .1f; //dont let him keep going up if he reaches the top
         }
      }
      if(input.isKeyDown(Input.KEY_S)){
         player = movingDown;
         playerPositionY -= delta * .1f;
         if(playerPositionY<-9000){
            playerPositionY += delta * .1f;
         }
      }
      if(input.isKeyDown(Input.KEY_A)){
         player = movingLeft;
         playerPositionX += delta * .1f;
         if(playerPositionX>324){
            playerPositionX -= delta * .1f;
         }
      }
////////////POBIERZ X,Y/////////////
         /* if (input.isKeyDown(Input.KEY_LALT)) {
        int x = Container.getInput().getMouseX();
          y= MouseInfo.getPointerInfo().getLocation().y;

          System.out.println("x: " + String.valueOf(x) + "  y: " + String.valueOf(y));
              }*/
///////////////////////////////
      if(input.isKeyDown(Input.KEY_D)){
         player = movingRight;
         playerPositionX -= delta * .1f;
         if(playerPositionX<-8040){
            playerPositionX += delta * .1f;
         }
      }
     
      //escape
      if(input.isKeyDown(Input.KEY_ESCAPE)){
         quit = true;
      }     
     
      //when they hit escape
      if(quit==true){
         if(input.isKeyDown(Input.KEY_R)){
            quit = false;
         }
         if(input.isKeyDown(Input.KEY_M)){
            sbg.enterState(0);
            try{
               Thread.sleep(250);
            }catch(InterruptedException e){
               e.printStackTrace();
            }
         }
         if(input.isKeyDown(Input.KEY_G)){
             sbg.enterState(3);
             try{
                Thread.sleep(250);
             }catch(InterruptedException e){
                e.printStackTrace();
             }
          }
         if(input.isKeyDown(Input.KEY_Q)){
            System.exit(0);
         }
      }
   }
   
   public int getID(){
      return 1;
   }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group