Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tile-based game help!  (Read 4032 times)
0 Members and 1 Guest are viewing this topic.
Offline Delinitor

Senior Newbie





« Posted 2012-10-13 21:01:43 »

I've been trying to make a tile engine for AAAGES, and I've got further than ever before! Well, I've been able to get the window up without any errors. But still no tiles. At one point there was one red one in the top left corner, but after a bit of tweaking that unfortunately disappeared. I've been using this as my example: https://github.com/captainkraft/Orc-Canyon

But, I've had an idea in my head for a while now for a small and simple-ish top-down puzzle game using the Slick2D library. In the game, you're trying to break out of a jail cell, but walls block your exit. These walls are attached to pressure plates, and you have to stand on them to make the walls move. I've included a basic idea below (Paint ftw):



At the minute I've got 2 sorts of tiles, one black and one green. The window just shows empty blackness. I'm not sure if this is the correct way of going about this, so any suggestions are very welcome!

Code: http://pastebin.java-gaming.org/3038e0e8a2e

Thanks!
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #1 - Posted 2012-10-13 21:11:39 »

You have such nice names for your files..

         tileImg = new Image("res/cunt.png");
         
         tileImg = new Image("res/twat.png");

I'm a java noob, but I'm learning. My little blog. My Games.
Offline Delinitor

Senior Newbie





« Reply #2 - Posted 2012-10-13 21:15:30 »

Oh man, meant to change those before posting  persecutioncomplex
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 802
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-10-13 22:11:15 »

Also, add a break statement to each switch case, or it will fall through.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Delinitor

Senior Newbie





« Reply #4 - Posted 2012-10-13 22:46:39 »

Thanks! I still haven't managed to make it work, I'm stuck with my black screen of doom  Clueless
Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #5 - Posted 2012-10-13 23:18:57 »

Seems tileImg in Tiles is static, so it is replaced for "all" your tiles, every time a new tiles is created.
if I read your map creation code correctly, that means they all use "res/twat.png".

For every new problem, a new source of solutions has come to exist.
Offline Delinitor

Senior Newbie





« Reply #6 - Posted 2012-10-13 23:23:42 »

Really going to have to change those names.

Thanks, how can I get around that? I'm using Slick's drawImage() to draw the tiles, and the image needs to be declared static to be used as an argument.
Offline Vladiedoo
« Reply #7 - Posted 2012-10-13 23:26:02 »

Why not just use Slick2D's built in tile system?

You make your map using a program called Tiled. Tiled saves your map as a .TMX file. You create a new TiledMap object in your game. Then you're able to simply call tiledMapObjectName.render(x, y);
Offline Magn919

Junior Member


Medals: 6
Exp: 4 years



« Reply #8 - Posted 2012-10-13 23:34:06 »

There is nothing there that requires it to be static, but you made the field static, so the it has to be called statically when you draw it.

On the line g.drawImage(Tiles.tileImg, Level.thex, Level.they);
remove "Tiles."
and remove the static modifier on the field "tileImg".

For every new problem, a new source of solutions has come to exist.
Offline Delinitor

Senior Newbie





« Reply #9 - Posted 2012-10-14 00:05:37 »

A-ha! Finally! I have tiles!

Thanks Vladiedoo. Do you have any ideas on how could make my idea work using this system?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Vladiedoo
« Reply #10 - Posted 2012-10-14 00:55:51 »

1  
2  
3  
4  
5  
TiledMap tmap = new TiledMap(ref);
int tileWidth = tmap.getWidth();
int tileHeight = tmap.getHeight();
int mapWidth = tmap.getWidth() * tmap.getTileWidth();
int mapHeight = tmap.getHeight() * tmap.getTileHeight();


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    // Inside the init. function
     for (int x = 0; x < mapWidth; x++) {
         for (int y = 0; y < mapHeight; y++) {
            if (tmap.getTileId(x, y, layerIndex) == PRESSURE_PLATE_ID) {
               Polygon pressurePlate = new Polygon(new float[] { 0, 0, tileWidth , 0, tileWidth, -tileHeight, 0, -tileHeight });
               pressurePlate.setX(x * tileWidth );
               pressurePlate.setY(y * tileHeight);
            }
         }
      }


      // Inside the update function
     if (pressurePlate.intersects(playerPolygon) || pressurePlate.contains(playerPolygon)) {
         // Do your action here.
     }
Offline Delinitor

Senior Newbie





« Reply #11 - Posted 2012-10-14 09:58:10 »

Struggling to get that working  Clueless

No errors, and everything else is rendered just fine, but I can't get the pressure plate to work.
Offline Vladiedoo
« Reply #12 - Posted 2012-10-14 17:10:50 »

The first 8 steps and my code are covered in http://slick.cokeandcode.com/wiki/doku.php?id=01_-_tiled_and_maps
1. Google Tiled
2. First link should be "Tiled Map Editor", click that
3. On the right hand side click download (download your OS version)
4. Open Tiled, go to File->New Map-> Set your map size and type (your game view is Orthogonal)
5. Go to Edit->Preferences->Store tile later data as: "Base64(gzip compressed)"
6. Go to Map->New Tileset->Find the tile image you want to use
7. Draw in your map with your tiles located at the bottom right
8. Go to File->Save As->Save your map into your game's res folder

NOTE: Make sure to save your tile images where you save your map. So have your res folder contain the map AND the tiles.

Here is some example code for running your game, NOTE: I'm no expert in Slick2D and this code is incomplete and not optimized (in the future you will learn about using an entity system) but this should help you as a starting point.
http://pastebin.java-gaming.org/038ee1a8e2e

If all of he following still doesn't help you: remember to add in a System.out.println(); at certain points to check what is and isn't being called. Using System.out.println(); you can also display variable values and see if something is changing or being set properly. If even that doesn't work try to pinpoint what isn't working for you and I can help. Good luck!
Offline Delinitor

Senior Newbie





« Reply #13 - Posted 2012-10-14 20:21:22 »

Thanks, that was really detailed. A big help Smiley

I still don't get this bit though:
1  
2  
3  
4  
5  
6  
7  
8  
9  
if (pressurePlate.intersects(player) || pressurePlate.contains(player)){
         // Set the blocking tile one higher and it's previous posion to a default walking tile.
        map.setTileId(x, y, layerIndex, tileid);
         map.setTileId(x, y, layerIndex, tileid);
      } else {
         // Set the blocking tile to be in the way again.
        map.setTileId(x, y, layerIndex, tileid);
         map.setTileId(x, y, layerIndex, tileid);
      }


How is this supposed to work? Thanks again.
Offline Vladiedoo
« Reply #14 - Posted 2012-10-14 20:42:30 »

NOTE: Currently (and in this post) I've been replying to your post as if the player has to constantly stand on your pressure plate in order for the block to be moved up. Once the player steps off the pressure plate the blocking wall is moved back into position.

This code checks if the player Polygon is either intersecting or contained by the the pressure plate Polygon. It basically reads "If the player is touching or inside the pressure plate".
1  
if (pressurePlate.intersects(player) || pressurePlate.contains(player)){





What this code does is change the map. It looks like to me your map is made up of these tiles, outer wall, pressure block, regular tiles, and blocking wall. In Tiled you'll draw the map with the wall initially blocking the exit. Then with this code, when the player is standing on the pressure plate, you switch the tiles at and above the blocking wall so that the exit is now un-blocked.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
// Each tile in your map has an ID, when you change its ID you change the tile.

// As an example we will say that the ID for a regular tile is 1 and that the ID for a regular tile is 2.
// We will also say that the x location of the blocking tile is 30 and its y location is 16.
if (pressurePlate.intersects(player) || pressurePlate.contains(player)){
         // Set the blocking tile to 1 position higher.
        map.setTileId(30, 15, 0, 2);
         // Set the old blocking position to a regular tile.
        map.setTileId(30, 16, 0, 1);
      } else {
         // Since the map is now blocked, do the reverse of the above
        // Set the blocking tile to default position.
        map.setTileId(30, 16, 0, 2);
         // Set the position above the blocking wall as a regular tile.
        map.setTileId(30, 15, 0, 1);
      }
}
Offline Delinitor

Senior Newbie





« Reply #15 - Posted 2012-10-14 21:07:01 »

That's giving me a NullPointerException  Sad
Offline Vladiedoo
« Reply #16 - Posted 2012-10-14 21:12:24 »

You have to put in values for those lines of code Grin.

For example
1  
2  
3  
4  
5  
int x = 5;
int y = 5;
int layerIndex = 0;
int tileid = 2;
map.setTileId(x, y, layerIndex, tileid);

The above code will set the tile at coordinates (5, 5) to have the ID of 2. Ignore layerIndex, that's used for more advanced collision.



Keep in mind that there are other solutions to use, you don't have to use a tile, you can draw a polygon and move the polygon instead.

Please post your entire error message, it will help me help you Cheesy.
Offline Delinitor

Senior Newbie





« Reply #17 - Posted 2012-10-19 23:48:41 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
java.lang.NullPointerException
   at george.game.cells.Level.update(Level.java:83)
   at org.newdawn.slick.state.StateBasedGame.update(StateBasedGame.java:268)
   at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:657)
   at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
   at george.game.cells.GameMain.main(GameMain.java:28)
Sat Oct 20 00:48:03 BST 2012 ERROR:Game.update() failure - check the game code.
org.newdawn.slick.SlickException: Game.update() failure - check the game code.
   at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663)
   at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
   at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
   at george.game.cells.GameMain.main(GameMain.java:28)


And this is the bit that's wrong Tongue
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
if (pressurePlate.contains(player)){
            // Set the blocking tile to 1 position higher.
           map.setTileId(540, 180, 0, 4);
            // Set the old blocking position to a regular tile.
           map.setTileId(540, 240, 0, 1);
         } else {
            // Since the map is now blocked, do the reverse of the above
           // Set the blocking tile to default position.
           map.setTileId(540, 180, 0, 1);
            // Set the position above the blocking wall as a regular tile.
           map.setTileId(540, 240, 0, 4);
         }
      if (pressurePlate.intersects(player) || pressurePlate.contains(player)) {
      System.out.println("C'est true");
       }


Very sorry for the delay! Thanks for the help.
Offline Vladiedoo
« Reply #18 - Posted 2012-10-20 01:09:43 »

I should have clarified this, sorry.

You have
1  
map.setTileId(540, 180, 0, 4);


But you actually need
1  
map.setTileId(xTileLocation, yTileLocation, 0, 4);


The xTileLocation is the actual map tile location, not the pixel location on screen. For your code to work you would have to have at least 541 tiles in the x direction and 181 tiles in the y direction.
Offline Delinitor

Senior Newbie





« Reply #19 - Posted 2012-10-20 14:52:56 »

Ah, okay Tongue Still getting the same error. The problem line is this one:
1  
if (pressurePlate.contains(player)){


And the code for the Polygon is here:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
for (x = 0; x < mapWidth; x++) {
         for (y = 0; y < mapHeight; y++) {
            //The third variable in "map.getTiledId(x, y, 0)" is the layer, if you don't know what this is just put 0.
           if (level1.getTileId(x, y, 0) == PRESSURE_PLATE_ID) {
               Polygon pressurePlate = new Polygon(new float[] { 0, 0, tileWidth, 0, tileWidth, -tileHeight, 0, -tileHeight });
               pressurePlate.setX(x * tileWidth);
               pressurePlate.setY(y * tileHeight);
            }
         }
      }
      //Makes the player a 50x50 square
     player = new Polygon(new float[] { 0, 0, tileWidth, 0, tileWidth, -tileHeight, 0, -tileHeight });
      // Makes the player start at (50, 50)
     player.setX(playerX);
      player.setY(playerY);


Have I made an error there? Thanks again.
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #20 - Posted 2012-10-20 14:56:13 »

Ah, okay Tongue Still getting the same error. The problem line is this one:
1  
if (pressurePlate.contains(player)){


And the code for the Polygon is here:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
for (x = 0; x < mapWidth; x++) {
         for (y = 0; y < mapHeight; y++) {
            //The third variable in "map.getTiledId(x, y, 0)" is the layer, if you don't know what this is just put 0.
           if (level1.getTileId(x, y, 0) == PRESSURE_PLATE_ID) {
               Polygon pressurePlate = new Polygon(new float[] { 0, 0, tileWidth, 0, tileWidth, -tileHeight, 0, -tileHeight });
               pressurePlate.setX(x * tileWidth);
               pressurePlate.setY(y * tileHeight);
            }
         }
      }
      //Makes the player a 50x50 square
     player = new Polygon(new float[] { 0, 0, tileWidth, 0, tileWidth, -tileHeight, 0, -tileHeight });
      // Makes the player start at (50, 50)
     player.setX(playerX);
      player.setY(playerY);


Have I made an error there? Thanks again.

try
1  
if (pressurePlate.intersects(player){


Does that work?

I'm a java noob, but I'm learning. My little blog. My Games.
Offline DazKins
« Reply #21 - Posted 2012-10-20 15:33:00 »

I understand your just starting out right now, but later on in development i would recommend loading the tiles in from a file structure rather than directly through an array declared within the program, it means later on you can create an engine to design levels in and you'll be able to have a lot less excess code.

Keep going at it, i remember when i was doing what you're doing now and sometimes it gets annoying when things don't work but if you keep trying you'll get used to it all

-Hope it helps Smiley

DazKins

Check out my Dev Blog: http://dazkins.tumblr.com
Offline Delinitor

Senior Newbie





« Reply #22 - Posted 2012-10-20 15:58:26 »

Unfortunately that doesn't work :/ I'm not really sure how to get this to work at all Clueless I may just stick to playing other peoples' games haha Tongue
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #23 - Posted 2012-10-20 16:20:03 »

Unfortunately that doesn't work :/ I'm not really sure how to get this to work at all Clueless I may just stick to playing other peoples' games haha Tongue
Cmon, man. Don't give up. Persistence is the best thing a programmer can have.
Offline Huw

Senior Member


Medals: 1
Projects: 2



« Reply #24 - Posted 2012-10-20 19:07:14 »

Unfortunately that doesn't work :/ I'm not really sure how to get this to work at all Clueless I may just stick to playing other peoples' games haha Tongue

Don't give up, if you're stuck what I sometimes do is create the game or just the methods/classes which arent working from scratch again. This usually makes what's going wrong or what you need to do clearer.

I'm a java noob, but I'm learning. My little blog. My Games.
Offline Vladiedoo
« Reply #25 - Posted 2012-10-20 21:40:45 »

What is your understanding of Java? If Slick2D seems too complicated you might want to do some reading on Slick2D tutorials and/or Java. Also do not feel over overwhelmed trying to make a game; it is a lot of work but it is easy to accomplish when you make the game in steps.

Here is a very basic version of your game, you can simply copy paste this and it will be up and running. If all the code here makes sense to you we can move on to the next step. Please ask any questions you have otherwise.

NOTE: This code does not use the program Tiled and Slick2Ds TiledMap system.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Polygon;

// A basic version of your game.
public class SimplePuzzleGame extends BasicGame {

   // The player and the pressure plate.
  private Polygon player;
   private Polygon pressurePlate;

   // The location of the player.
  private float xLocation;
   private float yLocation;

   // The location of the pressure plate.
  private int yourXPixelOfChoice;
   private int yourYPixelOfChoice;

   // Blocked is equal to false when the player is standing on the pressure plate.
  private boolean blocked = true;

   public SimplePuzzleGame() {
      super("Slick2DPath2Glory - SimplePuzzleGame");
   }

   // Initialize all variables here.
  public void init(GameContainer gc) throws SlickException {

      // Creates the player and pressure plate as a polygon.
     player = new Polygon(new float[] { 0, 0, 32, 0, 32, 32, 0, 32 });
      pressurePlate = new Polygon(new float[] { 0, 0, 32, 0, 32, 32, 0, 32 });

      // Sets the location of the pressure plate.
     yourXPixelOfChoice = 200;
      yourYPixelOfChoice = 200;
      pressurePlate.setX(yourXPixelOfChoice);
      pressurePlate.setY(yourYPixelOfChoice);

      // Sets the initial location of the player.
     xLocation = 50;
      yLocation = 50;
      player.setX(xLocation);
      player.setY(yLocation);
   }

   // Game is drawn here.
  public void render(GameContainer gc, Graphics g) throws SlickException {
      // Draw the pressure plate with the color red.
     g.setColor(Color.red);
      g.fill(pressurePlate);

      // Draw the plate with the color yellow.
     g.setColor(Color.yellow);
      g.fill(player);

      // Draw the blocking wall with the color blue.
     g.setColor(Color.blue);
      if (blocked) {
         // The exit is now blocked so have the blocking wall be in the way.
        g.fillRect(700, 300, 32, 32);
      } else {
         // The exit is now not blocked so move the blocking wall up.
        g.fillRect(700, 200, 32, 32);
      }
     
      g.setColor(Color.white);
      // Text to help understand gameplay.
     g.drawString("This is the pressure plate.", yourXPixelOfChoice - 90, yourYPixelOfChoice - 30);
      g.drawString("This is the player.", xLocation - 60, yLocation - 30);
      g.drawString("Blocked ->", 500, 300);
      g.drawString("Not blocked ->", 500, 200);
   }

   // Game logic is updated here.
  public void update(GameContainer gc, int delta) throws SlickException {
      // This is needed to get input from the user.
     Input input = gc.getInput();

      // WASD movement of the player.
     // In the future you will need to use delta when calculating movement.
     if (input.isKeyDown(Input.KEY_W)) {
         // Move the player up if the user is holding down W.
        yLocation--;
      } else if (input.isKeyDown(Input.KEY_S)) {
         // Move the player down if the user is holding down S.
        yLocation++;
      } else if (input.isKeyDown(Input.KEY_A)) {
         // Move the player left if the user is holding down A.
        xLocation--;
      } else if (input.isKeyDown(Input.KEY_D)) {
         // Move the player right if the user is holding down D.
        xLocation++;
      }

      // Sets the player coordinates
     player.setX(xLocation);
      player.setY(yLocation);

      // Checks to see if the player is standing on the pressure plate.
     if (pressurePlate.intersects(player) || pressurePlate.contains(player)) {
         // If the player is standing on the pressure plate move the blocking wall up.
        blocked = false;
      } else {
         // The player is not standing on the pressure plate so block the exit.
        blocked = true;
      }
   }

   // Program starts here.
  public static void main(String[] args) throws SlickException {
      AppGameContainer app = new AppGameContainer(new SimplePuzzleGame());

      app.setDisplayMode(800, 600, false);
      app.start();
   }
}
Offline Delinitor

Senior Newbie





« Reply #26 - Posted 2012-10-21 20:39:14 »

I have an embarrassingly small understanding of Java, but I'm doing my best to learn Tongue I can understand all of that, that's a great example by the way! Thanks mate.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!