Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Newbie & Debugging Questions / Re: Tiled getTileProperty method (Slick2D) on: 2012-07-26 12:15:18
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public void MapInit() throws SlickException {
   map = new TiledMap("assets/maps/testmap.tmx");
   System.out.println(map.getHeight() + "" + map.getWidth());
     
   //puts tiles with blocked set true in the collision array
       blocked = new boolean[map.getWidth()][map.getHeight()];
        for (int x=0;x<map.getWidth();x++) {
        for (int y=0;y<map.getHeight();y++) {
            System.out.println(x + " " + y);
            int id = map.getTileId(x, y, 0);
            String value = map.getTileProperty(id, "blocked", "true");
            if (value.equalsIgnoreCase("true")) {
               System.out.println(value);
               blocked[x][y] = true;
            }
          }
        }
       
}


Am I making one of those errors I'm going to laugh at later? Or am I just misusing the method.

My method instantiates the map, sets the blocked array to the size of the map (in tiles as per the Slick2D method).
Then it loops through the map, checks each tile for the given property, in this case "blocked" defined with "true".
And finally, if the value is true, it adds that tile to the array.

Currently, it is passing every single tile to the array as if it is ignoring the properties set in Tiled.

EDIT: In the meantime I've resorted to using the TileID instead of the tile property which works, but feels a bit cheap since I want to do more complicated things later. I'd rather have the code more readable by using "blocked", than by having magic numbers everywhere.

EDIT2!: After some frustrating fixes I have collision working (badly) and have come to realize I just don't know what the hell I'm doing with floats.
My collision check takes in a float, and my array has to check an integer value and return true or false. I thought I was smart taking the player position it was checking and dividing that by the map's width or height (for x and y) which put the collision near where it should be... but it's acting strangely, letting me move halfway through some blocks before I collide, and occurring on some tiles it shouldn't.

Reference Code:

Collision Check
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
private boolean tryToMove(float x, float y) {
         float newx = playerX + x;
         float newy = playerY + y;
         // checks the blocked array for collision
        if (isBlocked((newx/map.getWidth()), (newy/map.getHeight()))) {
             //can't move if blocked is true at the given location
            return false;
         }else{
            //allows the move
             return true;
         }      
   }


Relevant movement stuff in Update()
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  
Input input = container.getInput();
     
      float fdelta=delta*0.1f;

      if (input.isKeyDown(Input.KEY_UP))
      {
         if(tryToMove(playerX, playerY - fdelta)){
            playerY -= fdelta;
            System.out.println(playerX/map.getTileWidth() + " " + playerY/map.getTileHeight());
         }
      }  
     
      else if (input.isKeyDown(Input.KEY_DOWN))
      {
         if(tryToMove(playerX, playerY + fdelta)){
            playerY += fdelta;
            System.out.println(playerX/map.getTileWidth() + " " + playerY/map.getTileHeight());

         }
      }
      else if (input.isKeyDown(Input.KEY_LEFT))
      {
         if(tryToMove(playerX - fdelta, playerY)){
            playerX -= fdelta;
            System.out.println(playerX/map.getTileWidth() + " " + playerY/map.getTileHeight());

         }
      }
      else if (input.isKeyDown(Input.KEY_RIGHT))
      {
         if(tryToMove(playerX + fdelta, playerY)){
            playerX += fdelta;
            System.out.println(playerX/map.getTileWidth() + " " + playerY/map.getTileHeight());

         }
      }
2  Game Development / Newbie & Debugging Questions / Re: Tiled getTileProperty method (Slick2D) on: 2012-07-26 11:36:55
To keep everything cleaned up I was going to keep the first layer strictly background. The next layer would be collision (and maybe movable objects if I can get this project moving), and then an object layer for collectables/items.

My programming is a bit rusty! My loop seems to think every tile has passable as false. Gonna have to double check my loop. My reward for fixing collision tonight is sleep!

And while I'm at it I should clean up this bit of code I have so in Init() I call a Map Loading method instead of all the messy code. It's all coming back to me :]
3  Game Development / Newbie & Debugging Questions / Tiled getTileProperty method (Slick2D) on: 2012-07-26 11:10:15
Slick2D warns that its getTileProperty() method will not perform well.
Is it a viable way to implement collision despite the warning?
For example setting either a tile or object in Tiled with properties "passable" and "false",
then iterating through the map when it is instantiated and adding all appropriate tiles to an array?

Or should I think of something else! :]

Thanks!
4  Game Development / Newbie & Debugging Questions / Re: Drawing/Movement Help on: 2012-03-31 12:19:46
Interesting! For some reason subtracting the Cameras x and y was keeping the player from moving, when all it did before was keep the screen centered on the player and allowed the camera to "follow" it. Now I need to find a way to fix the camera again >.< the endless joys of programming Tongue

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  
public class Camera {
   private int x, y;
   

   Camera(){
     
   }
   
   public void setPosition(){
      x = Instances.player.getX()-Settings.width/2;
      y = Instances.player.getY()-Settings.heigth/2;
   }
   
   public void setX(int x){
      this.x = x;
   }
   
   public void setY(int y){
      this.y = y;
   }
   
   public int getX(){
      return x;
   }
   
   public int getY(){
      return y;
   }
}


and I always thought this was one of the easier things to do since it worked perfectly before >.< setPosition() is also called in the game loop along with the players update().
5  Game Development / Newbie & Debugging Questions / Re: Drawing/Movement Help on: 2012-03-31 11:57:23
I threw some print statements in tileMovement() to confirm that the players position is changing. It does infact change, so the key events are being handled correctly. Movement update is being called in the players update() method, which is called in the game loop.

At the moment, the players x and y get changed, but nothing occurs on the screen. Before I had changed how the map was being loaded, the movement was great, perfect for what I was going for actually. The only thing that changed was the map rendering.

The magic 15 is how far the player moves. It's half the length of one of my tiles.
6  Game Development / Newbie & Debugging Questions / Re: Drawing/Movement Help on: 2012-03-31 11:17:24
xD I knew people wouldn't notice I edited the original post. I solved that problem. Now my whole ordeal is that movement isn't working anymore.

ButtonHandler
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  
public void keyPressed(KeyEvent key) {
       
              switch (key.getKeyCode()) {
                        case KeyEvent.VK_UP:
                           Instances.player.setUp(true);
                             break;
                        case KeyEvent.VK_DOWN:
                           Instances.player.setDown(true);
                            break;
                        case KeyEvent.VK_LEFT:
                           Instances.player.setLeft(true);
                            break;
                        case KeyEvent.VK_RIGHT:
                           Instances.player.setRight(true);
                            break;
                        case KeyEvent.VK_ENTER:
                           if(Instances.GAMESTATE == GameState.INGAME){
                              Instances.GAMESTATE = GameState.BATTLE;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.MENU){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }
                           break;
                        case KeyEvent.VK_SPACE:
                           System.out.println(Instances.GAMESTATE);
                           if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.battle.startFight();


Player movement methods

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  
private void tileMovement(){
      if(moveLeft){
         if(tempX>0){
            x--;
            //Instances.camera.setX(Instances.camera.getX()-1);
           tempX--;
            ready = false;
            System.out.println(getX() + getY());
         }else if(tempX<=0){
            moveLeft = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveRight){
         if(tempX>0){
            x++;
            //Instances.camera.setX(Instances.camera.getX()+1);
           tempX--;
            ready = false;
         }else if(tempX<=0){
            moveRight = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveUp){
         if(tempY>0){
            y--;
            //Instances.camera.setY(Instances.camera.getY()-1);
           tempY--;
            ready = false;
         }else if(tempY<=0){
            moveUp = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveDown){
         if(tempY>0){
            y++;
            //Instances.camera.setY(Instances.camera.getY()+1);
           tempY--;
            ready = false;
         }else if(tempY<=0){
            moveDown = false;
            ready = true;
            movementTimer = 1;
         }
      }        
   }

private void movementUpdate(){

      if(ready){
         if(left){
            moveLeft = true;
            tempX = 15;
         }else if(right){
            moveRight = true;
            tempX = 15;
         }else if(up){
            moveUp = true;
            tempY = 15;
         }else if(down){
            moveDown = true;
            tempY = 15;
         }
      }
      movementTimer++;
      if(movementTimer>1){
         tileMovement();
      }

   }
7  Game Development / Newbie & Debugging Questions / Re: Drawing Help (and collision help after the drawing is fixed!) on: 2012-03-30 19:23:58
Sorry about that >.< I do tend to get off track pretty easily.
I'm looking specifically at the render method, renderMenu and renderBattle are working fine atm. I kinda fixed the first problem, the
loadScene method was being called over and over, and then I realized it was inside my game loop. After I moved it, it loaded the scene properly, but when it goes to draw and calls render I get a NullPointerException and the game closes.


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  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;

/**
 * this class was originally meant to simply hold a reference to a map
 * and a collection of sprites, which it delegated all the work to in the
 * logic and render methods of these objects...<p>
 *
 * so it is reasonably fitting that this class also manage the loading of the
 * maps and sprites it uses from files or URLs.<p>
 *
 * Each scene consists of a Map, and a collection of sprites, as intended,
 * but may also be loaded or saved.
 */

public class Scene
{
   int offsetX = 0;
   int offsetY = 0;
   
   float effect_rScale = 1;
   float effect_gScale = 1;
   float effect_bScale = 1;
   float effect_hue = 0;
   float effect_sat = 1;
   
   Map map;
   ArrayList sprites;
   
   GraphicsBank tileset;
   
   /**
    * creates a scene using the given map and sprites.
    */

   public Scene(Map m, ArrayList s, GraphicsBank gfx)
   {
      this.map = m;
      sprites = s;
      this.tileset = gfx;
     
   }
   
   /* Create a new empty scene */
  public Scene() {
    map    = new Map(10, 10, 32, 32);
    tileset = new GraphicsBank();
  }
   
   
   public GraphicsBank getTileset()
   {
      return tileset;
     
   }
   
   public void setTileset(GraphicsBank gfx) {
      tileset = gfx;
      map.setTileset(gfx);
   }
   
   /**
    * loads a scene from the given URL. takes tiles from the given GraphicsBank.
    */

   static Scene loadScene(File f) throws IOException
   {
      boolean hasColourEffect = false;
      float r = 1;
      float g = 1;
      float b = 1;
      float h = 0;
      float s = 1;
   
      BufferedReader reader = new BufferedReader(new FileReader(f));
     
      String line = reader.readLine();
     
      StringTokenizer tokens = new StringTokenizer(line);
      int width = Integer.parseInt(tokens.nextToken());
      int height = Integer.parseInt(tokens.nextToken());
     
      String tileset = tokens.nextToken();
     
      GraphicsBank gfx = new GraphicsBank();
     
      System.out.println("Attempt to load tileset "+tileset);
     
      System.out.println("Working path is "+f.getParentFile());
     
      File ts = new File(f.getParentFile(), tileset);
      System.out.println("Attempt to load tileset "+ts.getAbsoluteFile());
     
     
      gfx.loadTileset(ts);
     
      Map map = new Map(width, height);
     
      line = reader.readLine();
      tokens = new StringTokenizer(line);
     
      if(tokens.nextToken().equalsIgnoreCase("colorization")) {
         hasColourEffect = true;
         r = Float.parseFloat(tokens.nextToken());
         g = Float.parseFloat(tokens.nextToken());
         b = Float.parseFloat(tokens.nextToken());
         h = Float.parseFloat(tokens.nextToken());
         s = Float.parseFloat(tokens.nextToken());
      }
     
      while(! line.equals("."))
      {
         line = reader.readLine();
      }
     
     
      for(int z=0; z<3; z++)
      {
         line = reader.readLine();
         tokens = new StringTokenizer(line);
         
         for(int y=0; y<height; y++)
         {
            for(int x=0; x<width; x++)
            {
               String code = tokens.nextToken();
               map.setTile(x, y, z, gfx.getTile(Integer.parseInt(code)));
            }
         }
      }
      reader.close();
     
      Scene scene = new Scene(map, new ArrayList(), gfx);
      scene.tileset = gfx;
      if(hasColourEffect) {
         System.out.println("Calling setEffect on scene recently loaded.");
         scene.setEffect(r, g, b, h, s, 1f);
      }
      return scene;
     
   }
   
   
    static Scene loadScene(String filename) throws IOException {
      Scene scene = loadScene(new File(filename));
      return scene;
    }
   /**
    * writes the map only (at the moment) to a file.
    */

  public void saveScene(File file)
  {
    if(tileset.isUnsaved()) {
      throw new RuntimeException("Tileset is unsaved. Cannot save the scene");
    }
     
      try
      {
         PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
         
         String line = "";
         
         int width = map.getWidth();
         int height = map.getHeight();
         
         
     
      File wd = new File(file.getParentFile().getCanonicalFile().toString());
         File ts = new File(tileset.getFile().getCanonicalFile().toString());
         
         String relativePath = RelativePath.getRelativePath(wd, ts);
         

         
      line = width + " " + height + " " + relativePath;
         writer.println(line);
         
         line = "colorization " + effect_rScale +
                            " " + effect_gScale +
                            " " + effect_bScale +
                            " " + effect_hue    +
                            " " + effect_sat;
         writer.println(line);

         System.out.println("Colorization red in save is "+effect_rScale);        
         writer.println(".");
         
         for(int z=0; z<3; z++)
         {
            for(int i=0; i<height; i++)
            {
               for(int j=0; j<width; j++)
               {
                  Tile t = map.getTile(j, i, z);
                  if(t != null)
                     writer.print(t.getNumber()+ " ");
                  else writer.print("0 ");
                 
                 
               }
               
            }
            writer.println();
         }
         
         writer.flush();
         writer.close();
         
         
      }
      catch(IOException e)
      {
         throw new RuntimeException("Could not save the level");
      }
     
      System.err.println("Saved");
   }
   
   /**
    * calls each sprites logic method.
    */

   void logic()
   {
      for(int i=0; i<sprites.size(); i++)
      {
         Sprite s = (Sprite)sprites.get(i);
         s.logic();
      }
   }
   
   /**
    * renders the scene to the graphics context.
    * at the moment, sprites appear above everything else.
    *
    * TODO: Fix this up.
    */

   void render(Graphics g)
   {
      //System.out.println("Render Scene");
     map.render(g, offsetX, offsetY);
     
      for(int i=0; i<sprites.size(); i++)
      {
         Sprite s = (Sprite)sprites.get(i);
         //s.render(g, offsetX, offsetY);
     }
     
   }
   public void render(Graphics g, int offX, int offY)
   {
      map.render(g, offX, offY);
   }
   public void render(Graphics g, Camera c)
   {
      map.render(g, c);
   }
   
   
   public void render(Graphics g, Point origin, Dimension size)
   {
      map.render(g, origin, size);
   }
   
   public void render(Graphics g, Point origin, Dimension size, int layer)
   {
      map.render(g, origin, size, layer);
   }
   
   /**
    * sets the visible area of the scene.
    * This will probably not be used once the game is finished.
    */

   void setViewSize(int width, int height)
   {
      map.setViewSize(width, height);
     
   }
   
   /**
    * sets the screen offset. Screen offset can be anywhere, not just
    * following a character around. This is good for cut scenes.
    */

   void setOffset(int x, int y)
   {
      offsetX = x;
      offsetY = y;
   }
   
   /**
    * Apply RGB scalars and hue/sat adjustments to the tiles and characters
    * in the scene. The hue and sat are always applied first
    **/

   public void setEffect(float r, float g, float b, float h, float s, float z) {
      System.out.println("Scene setEffect called. will call for the gfx bank...r" +r+" g"+g+" b"+b+" z"+z);
      effect_rScale = r;
      effect_gScale = g;
      effect_bScale = b;
      effect_hue = h;
      effect_sat = s;
      tileset.setEffect(r, g, b, h, s, z);
      map.setZoom(z);
   }
   
   /**
    * returns the map. not really any reason to want to do this except for the
    * level editor.
    */

   public Map getMap()
   {
      return map;
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      //render(g);
     scene.render(g);
      g.dispose();
      bufferStrategy.show();
   }
8  Game Development / Newbie & Debugging Questions / Drawing/Movement Help on: 2012-03-30 11:52:33
EDIT: Cut down on the giant first post. Also the second problem has been fixed. When I moved the loadScene, there was no scene to render in the Draw class, hence the NullPointer. But now that the map has been drawn, I am still unable to move around despite having been able to before I changed how the map was drawn.

Draw.java

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  
123  
124  
125  
126  
127  
128  
129  
130  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Draw extends JFrame{
   
   private static final long serialVersionUID = 1L;
   
   JFrame frame;
      Canvas canvas;
      BufferStrategy bufferStrategy;
      Image background;
      Image button;
      JPanel battlePane = (JPanel) getContentPane();
      Scene scene;
      Camera cam;
     
     
     
      final int WIDTH = Settings.width;
      final int HEIGHT = Settings.heigth;
      Draw(){
         frame = new JFrame("PlaceHolder");
           
            JPanel panel = (JPanel) frame.getContentPane();
            panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
            panel.setLayout(null);
           
           canvas = new Canvas();
            canvas.setBounds(0, 0, WIDTH, HEIGHT);
            canvas.setIgnoreRepaint(true);
            frame.setIgnoreRepaint(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setResizable(true);
            frame.setVisible(true);
            panel.add(canvas);
            canvas.createBufferStrategy(2);
            bufferStrategy = canvas.getBufferStrategy();
            canvas.requestFocus();
            canvas.setBackground(Color.black);
            canvas.addKeyListener(new ButtonHandler());      
           
      }

   void render() throws IOException {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      scene = Scene.loadScene("scenes/datTestMap.dat");
      scene.render(g);
      g.dispose();
      bufferStrategy.show();
   }
   void renderMenu(){
      //int flicker = 0;
     Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      g.drawString(" PRESS ENTER TO START ", WIDTH/2 - 70, HEIGHT/2 - 5);
      g.drawRect(WIDTH/4, HEIGHT/4, WIDTH/2, HEIGHT/2);
      g.dispose();
      bufferStrategy.show();
   }
   
   void renderBattle(){
     
      try{
              File pic = new File("bg.jpg");
              File b = new File("Button.jpg");
              background = ImageIO.read(pic);
              button = ImageIO.read(b);
              }catch (IOException e){
                 System.out.println("!");
              }
   
      //Image image = Toolkit.getDefaultToolkit().getImage("Button.jpg");      
     //image = new ImageIcon("Button.jpg").getImage();
     JButton testButton = new JButton("Fight!");
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      //Draws background
     //g.drawImage(background, 0, 0, null);
     frame.getContentPane().add(testButton);
      testButton.setLocation(300, 300);
      g.drawString(" FIGHT BITCHES! ", HEIGHT-20, 20);
      //Draws Fight button
     g.drawRect(400, 400, Instances.button.getWidth(), Instances.button.getHeight());
      g.drawImage(button, 400, 400, null);
      g.drawString("FIGHT", 425, 415);
      //Draws Flee button
     g.drawRect(400, 425, Instances.button.getWidth(), Instances.button.getHeight());
      g.drawImage(button, 400, 425, null);
      g.drawString("FLEE", 425, 440);
     
      bufferStrategy.show();
      g.dispose();
     

   }
     
   protected void render(Graphics2D g){
      //g.drawImage(Instances.mapMan.getMap(), 0-Instances.camera.getX(), 0-Instances.camera.getY(), null);
 
      try {
         scene = Scene.loadScene("scenes/datTestMap");
      } catch (IOException e) {
         // TODO Auto-generated catch block
        e.printStackTrace();
      }

      //scene.render(g);
     
      g.setColor(Instances.player.getColor());
      g.fillRect(Instances.player.getX()-Instances.camera.getX(), Instances.player.getY()-Instances.camera.getY(), 15, 15);
   }

}


ButtonHandler.java

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  
import java.io.IOException;

import javax.sound.sampled.Clip;
imporimport java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class ButtonHandler extends KeyAdapter {

    public ButtonHandler() {
       System.out.println(" Button handler initialized! ");
               
    }

    public void keyPressed(KeyEvent key) {
       
              switch (key.getKeyCode()) {
                        case KeyEvent.VK_UP:
                           Instances.player.setUp(true);
                             break;
                        case KeyEvent.VK_DOWN:
                           Instances.player.setDown(true);
                            break;
                        case KeyEvent.VK_LEFT:
                           Instances.player.setLeft(true);
                            break;
                        case KeyEvent.VK_RIGHT:
                           Instances.player.setRight(true);
                            break;
                        case KeyEvent.VK_ENTER:
                           if(Instances.GAMESTATE == GameState.INGAME){
                              Instances.GAMESTATE = GameState.BATTLE;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.MENU){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }
                           break;
                        case KeyEvent.VK_SPACE:
                           System.out.println(Instances.GAMESTATE);
                           if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.battle.startFight();


                             
                           }  
                           break;
                        case KeyEvent.VK_1:
                           Instances.mapMan.setMap("map1");
                           break;
                        case KeyEvent.VK_2:
                           Instances.mapMan.setMap("map2");
                           break;
                        case KeyEvent.VK_3:
                           Instances.mapMan.setMap("map3");
                           break;
              }
    }

    public void keyReleased(KeyEvent key) {
        switch (key.getKeyCode()) {
           case KeyEvent.VK_UP:
              Instances.player.setUp(false);
              System.out.println(" Released UP!");
                break;
           case KeyEvent.VK_DOWN:
              System.out.println(" Released DOWN!");
              Instances.player.setDown(false);
               break;
           case KeyEvent.VK_LEFT:
              System.out.println(" Released LEFT!");
              Instances.player.setLeft(false);
               break;
           case KeyEvent.VK_RIGHT:
              System.out.println(" Released RIGHT!");
              Instances.player.setRight(false);
               break;
       
        }
    }

   @Override
   public void keyTyped(KeyEvent arg0) {
      // TODO Auto-generated method stub
     
   }
}



Throwing this link out there as a test, not sure if anybody can access it, but if so then woot!
https://bitbucket.org/DatGameCompany/westernrpg/src/6e7593f88105/src
9  Game Development / Game Play & Game Design / Re: Your first game! (adventure/rpg like) on: 2012-02-21 23:04:45
Keep up the good work! This is fantastic and very helpful Tongue
10  Game Development / Newbie & Debugging Questions / Re: New to Java Games! on: 2012-02-16 04:33:16
Wow so many replies! I see a lot of good things that will help me rewrite my mess of a program Tongue
Medals for everyone! I'm still open for more input; it's likely that I'll try everyone's examples that they posted and see which one will fit best
with the way my team wants the game to feel.

I'm still in shock that everyone has been so helpful and noob friendly. I think I'll be spending quite a bit of time here as I develop my java skills  Wink

And I guess to cut back on spam, I'll just continue to use this thread in-case I need help with anything! :]
11  Game Development / Newbie & Debugging Questions / Re: New to Java Games! on: 2012-02-15 11:51:49
Who is ReBorn?

Your evil twin obviously.
12  Game Development / Newbie & Debugging Questions / Re: New to Java Games! on: 2012-02-15 11:47:46
What exactly do you mean by:"Do you have any input on items A-F at all, or anything else to add?"?

Next, I think you'd be better off rewriting everything a bit. Wat you are doing will be fine for small games. But it will be really chaotic if you make a bit bigger game. I suppose you have 1 class in which you both handle the movement, and the drawing?
try and make it more like this:
Draw class, in this you will draw everything.
Player class, will handle movement and collision detection.
In your player class youll need 4 funtions, 1 to handle the keyrelease, one to handle the keypress, an update function (So your player actually moves) and a  draw function)
I would love to explain it in detail, but my next lesson is starting soon. Ill explain it in detail asap (that is today, and when nobody has done it yet)

Forgot to quote ReBirth on that post haha, whoops!

No, I perfectly understand what you mean. The code I posted was from the Main, so it looks like I'll be doing a lot of work today on the other classes, and calling them from Main to see if it all works Tongue But if you want an idea of how chaotic it would have been...all the drawing, movement, tiles, etc were all in the Main. The only thing I called from the Player class was the coordinates LOL.
13  Game Development / Newbie & Debugging Questions / Re: New to Java Games! on: 2012-02-15 11:33:47
Welkcome to JGO!
First of all with an applet you can easily show it from a webbrowse, where as a application is more for standalone purpose (this is what i know). For releasing it to the public, depends on what end. You mean selling? Or just freely distribute?

secondly, sure thats fine to do. Just think that it will save the images in the ram as you load them. So unless you are going to load huuuuuge maps at once, it should be fine. Look at one of my previous posts, i explained how you can easily set up a tile based map (Like what you are doing).

thirdly, i have had this problem as well, had to change a bit of my code to change it. Do you mind posting your movement code? So that i can help better with that.

for your savequestion, i have no clue. It should have acces i believe (i can easily store text documents and such) what are you using to save it?

And the library, i have not much information about this, but i believe that the original java libraries are more then fine, if you are not planning to use image scaling and transparancy (because this is supposed to go 'slow').
Also, if you are looking for another member, id love to join Smiley. Programming in a group is so much more fun then doing it alone. Im currently making a rpg as well. Which is going quite alright (Finished inventory, scrolling map, movement engine, movement) so i think im able to help you guys out! ^^


1) Awesome! We plan to freely distribute it depending on how it turns out.

2) I will definitely give your post a look. That should solve most of our problems with thinking the map will be too big. I'm assuming we would need some sort of event upon entering a door or such that tells it to load the map of the building, etc?

3)
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  
 int key = evt.getKeyCode();  // keyboard code for the key that was pressed
   
     
     if (key == KeyEvent.VK_A) {
        if (leftHeld == true)
         playerX -= 5;
        testMessage = KeyEvent.getKeyText(key) + " is pressed";
         repaint();
         leftHeld = true;
         evt.consume();
      }
      else if (key == KeyEvent.VK_D) {
        if (rightHeld == true)
         playerX += 5;
        testMessage = KeyEvent.getKeyText(key) + " is pressed";
         repaint();
         rightHeld = true;
         evt.consume();
      }
      else if (key == KeyEvent.VK_W) {
        if (upHeld == true)
         playerY -= 5;
        testMessage = KeyEvent.getKeyText(key) + " is pressed";
         repaint();
         upHeld = true;
         evt.consume();
      }
      else if (key == KeyEvent.VK_S) {
         if (downHeld == true)
         playerY += 5;
         testMessage = KeyEvent.getKeyText(key) + " is pressed";
          repaint();
          downHeld = true;
          evt.consume();
      }
 

  }  


Ignore the test messages, I added those to see if the applet properly kept track of which keys were down/released.

4) We were trying it using print writer. My friend took care of that portion because I haven't personally done any file IO in java, only python in an intro programming course.

5) I think that would be great! :] We're trying to get more people on our team, ideally all in person so we can meet up and actually work for a few hours and not slack off on our own! If you have any extra time on your hands though, I think that would work out. I need to run it by them first though, or else we'll end up having another 10 people with access to our DropBox and that could get messy Tongue
14  Game Development / Newbie & Debugging Questions / Re: New to Java Games! on: 2012-02-15 11:22:51
Hello! Smiley
Finish your game and show us that "RPG being first project is bad" was myth!

We plan to >Tongue
The harder the project, the more awesome we'll feel when its done!
Do you have any input on items A-F at all, or anything else to add? I don't want to have my first post go off-topic Tongue
15  Game Development / Newbie & Debugging Questions / New to Java Games! on: 2012-02-15 11:08:48
Hi there JGO community!
I just started working with a team of friends on a 2D RPG.
We only have about a years worth of experience in Java in our College/University.
So far we have written everything from scratch and haven't used any external libraries (everything is in javax.swing)
Before we get any further than we are now, I'd like to stop and ask questions to see if we are on the right track,
or if we are failing horribly and need to do things differently.
The only things currently working are the map loading to the screen, and character movement. No collision yet.


A) The game is currently running as an applet. If the game doesn't turn out as horribly as we think it might, we will consider
making the game available to the public. Is this a viable way of putting the game out there? Or are there better solutions?

B) The map is stored as an array of ints. Each int corresponds to a tile in a switch statement. We currently only have about 10 tiles... but with any more I KNOW this will turn out bad. My idea was to have a tile class and work from there. Does that work?

C) When holding down a movement key, java starts to read the input about as fast as you can type in a text file by holding down a key. Is there any way to limit the input so the movement becomes "fluid"? It currently turns out as one step in any direction, followed by a slight pause, then the character will take off sprinting in that direction.

D) We were probably thinking too far ahead, but we tried making a save method. It gets called when a button was clicked on the applet, just as a test. It then exports the character position into a text file using PrintWriter, which could be loaded later on. Doing so would completely freeze the applet. My professor mentioned that the applet doesn't have permission to write on the C drive, and that may be why the applet freezes. Is this just a hindrance on our applet choice again? If not, are there ways around it?

E) As I mentioned we are only using java strictly and currently do not use any external libraries. Is there any merit at all to writing the game this way?
Or should we start from scratch using something like slick2D? We are doing this as a learning experience and hope to be professional game developers someday. Is it worth it trying to "reinvent the wheel" so to speak and design the engine the way we are now? Or would we be better off implementing the things others have already figured out?

F) Probably the most important thing in my opinion to learn well at this level: ORGANIZATION. I feel as if we are doing too much in the Game class, and not enough in classes like Player, etc. How much should be done in the actual Main program? How much should be handled elsewhere? I know most organization is a matter of taste, but what has worked for you as far as doing things, and where you do them. The most typical error that I keep encountering is making a static reference to a non-static method/value which usually pushes me to write more in the main and less in outside classes.

Sorry for the extended noobness, but even the questions I thought would be simple on these forums turned out to be quite ahead of where I think we are as developers. Any help or ideas would be greatly appreciated (and also gets JGO in the credits when we finally finish a project Tongue)

EDIT(added (F) and this-->) FYI if anyone wants to see the abomination that is our current source code, feel free to ask. Your eyes may burn from looking at it.
Pages: [1]
 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (12 views)
2014-08-28 18:26:30

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (37 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (29 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (28 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56
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!