Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 / Way to treat game entities on: 2010-05-28 22:34:33
I'm kinda stuck trying to find an effective way to treat the entities in my game, currently everything is stored in their own array so there is one array called terrain_content which contains the grass and rocks and soon of my map then there is one for trees, one for enemies and so on, so if i want to hit something with my sword I have to look up what is in the enemy array at the the location above the one the player is in, this poses many problems so what I'm asking is what is a good way for me to handle enemies so I can easily acquire them, run methods trough them and display them on my screen and so on?
2  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-22 10:13:27
Another stupid problem, when you hold in a key it has a small delay before starting to repeat the keypress infinitly. So when you hold down the y key, it will look like this(space is time)
y    yyyyyyyyyyyyyyyy

Is there any way to stop this, it's hard to make a game when you can't make your character move without moving stopping then starting to move again.
3  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-20 14:36:16
Thanks for all the help btw, but I've got a new problem; if all the entities in the game are classes, how do I get a specific class without having to pre program it in the game. ex: Player shoots an arrow, the arrow detects it has hit something, for example another character, now i want the arrow's class to find the name of the object it is hitting and run a method called "KillMe()" or something in the hit class to remove it from the game. Is there any way to run a method in a class you don't know the name if like:
1  
2  
3  
4  
if(this.GetCollision == true) {
hit = getClass(EntAtPos(30,23));
hit.KillMe();
}
4  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-13 21:49:57
What I mean is that I have for example a file that contains something like "1000111010111010101010" so the 1's means grass and 0's are rock. Now I have a game that is grid based so it got small squares that each contain ether grass or rock. The game loads the data about what material goes in which square by checking an array that contains the level data.

Example:

the level array contains "01011110101" each of these numbers are in their own int in the array so
 [1] = 0
 [2] = 1
 [3] = 0
 [4] = 1
 [5] = 1
and so on.
So the level is made by squires so it looks something like this:
|------------|
|1|2|3|4|5|
|------------|
|6|7|8|9|10|
|------------|
|11|12|etc.|  |  |
|------------|

So to check what material squire 1 contains i check what is in the level [1] int, so since in the example [1] = 0 it is made of rock

Ops, got carried away.

Anyway, what i'm asking is how to make the text file which contains 00101001110 to take each number and but them into their corresponding int in the array so the first number which is 0 will go into the level[1] and the next which is a 0 will go into level[2] and the next which is a 1 will go into level[3].

I don't know any way i can explain it any better then that.
5  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-12 17:47:03
New problem, I'm now loading my level data from a text file, which means I have to create a way to read each int(which for now consists of 1's and 0's) into my level array, so the first number in the file should go to lvl_1[1] second into lvl_1[2] etc. How do i do this?

Also when trying to load the file I have to surround it with try or a throws FileNotFound but when I add this to that method and now all the classes using this method needs to have try around them, is there any way to avoid this?

Really appreciate all the help I could get.  Tongue
6  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-07 18:23:45
Ok really noob question here; I have two classes one called Player and one called Main, I want to run a method that is defined in Main in the player class, so it would be like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Main {
int x = 0,y = 0;

        //Turns X and Y coords into My own game Coords

   public int CoordsToUnits(int x, int y) {

      int Unit = x/10-1+12*y/10-12;
      return Unit;
   
}


}


1  
2  
3  
4  
5  
6  
7  
8  
public class Player {

          int XPos = 10, YPos = 20;
         
          int GamePos = CoordsToUnits(XPos,YPos)// <- Want this to run in Player class or a method in Player class which gets the var from a method in the Main class.


}


Forget i figured it out.
7  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-06 19:11:05
New problem, when using my current rendering method it causes extreme lag. Is there a better way? Look for the I THINK THE FAULT MIGHT BE HERE in the comments.

The code is based on Gudradain's code as said earlier(Thanks Gudradain, wouldn't get into Java programming if it wasn't for that.)

And yes i know it is a giant abomination of code, ill fix it later.

(I'll post all the code of requested.)

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  
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;
import java.awt.image.ImageObserver;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;
import java.lang.Math;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game implements Runnable{
   
   final int WIDTH = 400;
   final int HEIGHT = 300;
   //Game area in game units
   final int WIDTH_UNITS = WIDTH/10;
   //ditto
   final int HEIGHT_UNITS = HEIGHT/10;
   //Total units in game area
   final int TOTAL_UNITS = WIDTH_UNITS*HEIGHT_UNITS;
   //Stores Keys Pressed
   int PressedKey;
   static int sysin;
   //Used by terrain renderer
   int CurRenderGround;
   //Creates Player object
   Player pl = new Player();      
   //Creates the level array
   int[] lvl_1 = new int[TOTAL_UNITS];

   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;
   boolean first = true;

   public Game() {

     
      frame = new JFrame("Basic Game");



      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);
     
      panel.add(canvas);
     
      canvas.addMouseListener(new MouseControl());
      canvas.addKeyListener(new KeyControl());

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);
     
      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();
     
      canvas.requestFocus();
     

   }

   
   private class MouseControl extends MouseAdapter{
     
   }
   
   //Key Listener
   private class KeyControl extends KeyAdapter{
        public void keyPressed(KeyEvent e) {
             PressedKey = e.getKeyCode();
            System.out.println(e.getKeyCode());
            if(e.getKeyCode() == 68) pl.MoveX(1);
            if(e.getKeyCode() == 65) pl.MoveX(-1);
            if(e.getKeyCode() == 83) pl.MoveY(1);
            if(e.getKeyCode() == 87) pl.MoveY(-1);
          }
   }
   long desiredFPS = 60;
    long desiredDeltaLoop = (1000*1000*1000)/desiredFPS;
   
   boolean running = true;
   
   public void run(){

     
      long beginLoopTime;
      long endLoopTime;
      long currentUpdateTime = System.nanoTime();
      long lastUpdateTime;
      long deltaLoop;


      while(running){
         beginLoopTime = System.nanoTime();
         render();
   
         lastUpdateTime = currentUpdateTime;
         currentUpdateTime = System.nanoTime();
         try {
            update((int) ((currentUpdateTime - lastUpdateTime)/(1000*1000)));
         } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
         }
         
         endLoopTime = System.nanoTime();
         deltaLoop = endLoopTime - beginLoopTime;
           
           if(deltaLoop > desiredDeltaLoop){
               //Do nothing. We are already late.
           }else{
               try{
                   Thread.sleep((desiredDeltaLoop - deltaLoop)/(1000*1000));
               }catch(InterruptedException e){
                   //Do nothing
               }
           }
      }



   }
   
   private void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }


   
   /**
    * Overwrite this method in subclass
    *
    */

   
   public void update(int deltaTime) throws IOException{

     
   }

   //Do all the first stuff cuz doing it in run() caused errors
   
   public void init() {
      if(first) {
         pl.Speed = 10;
         RandomGenLvl();
         System.out.println("Init Ran");
         first = false;
      }

   }

   
   //Gets the image's used but the program I THINK THE FAULT MIGHT BE HERE!
   
   private Image getIMG(String path1)
   {
      Image img = null;
      try {
         img = ImageIO.read(new File(path1));
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
     
      return img;  
   }
   //Generates a random level
   public void RandomGenLvl() {
     
      for(int i = 0; i < TOTAL_UNITS; i++) {
         Random gen = new Random();
         int lool = gen.nextInt(2);
         lvl_1[i] =  lool;
         
         System.out.println(lool);

      }
   }
   //Unused Random method
   
   private int random()
   {
     
      Random generator = new Random();
      int ret = generator.nextInt(1);
     
   return ret;
   }




   protected void render(Graphics2D g){

      init();      
      //Render Terrain------------------------------------------
     
      int CurLine = 1;
      int CurSprite = 1;
      String Sprite;
      for(int i = 1; i < WIDTH_UNITS&&CurLine < HEIGHT_UNITS; i += 1) {
         CurSprite += 1;
          CurRenderGround = lvl_1[CurSprite];
         if(CurRenderGround == 0) {
            Sprite = "res/grass1.png";
           
         }
         else Sprite = "res/rock.png";
         
         g.drawImage(getIMG(Sprite), i*10, CurLine*10, canvas);

         if(i==WIDTH_UNITS-1) {
            CurLine += 1;
            i = 0;
         }
         //-----------------------------------------------
         
         g.drawImage(getIMG(pl.GetCurSprite()), pl.PosX, pl.PosY, canvas);
         System.out.println(pl.PosX);
      }
   }
     
   
   

   


   public static void main(String [] args) throws IOException{
      Game ex = new Game();
      new Thread(ex).start();

     
     

     
   }




     
   }

8  Game Development / Newbie & Debugging Questions / Re: Help with rendering of game objects on: 2010-05-04 15:17:00
It is as far as I know only one "real" tutorial in the tutorials section which is the Basic game tutorial, which is not a full tutorial but more of a code to get you started, it doesn't explain anything about rendering more then one picture at once or stuff like key listening, so I'm gonna reshape my question. Anyone know of any good simple java game tutorial or source code?
9  Game Development / Newbie & Debugging Questions / Help with rendering of game objects on: 2010-05-02 22:10:07
I am working on a small game based on Gudradain basic game code and as i doesn't have much experience with Java(done some c++)  so i know my way around the coding bit so I'm not asking for the code but as this is my first real "game" I'm trying to make a way for it to render images when it is needed, so far I've just managed to make it render trough a predefined array which says how many it should render and the image's stats, but when making a game I can't have a set amount of images at all time(for example when enemies appear at random)  So how would I go about doing that? Should i for example make a rendering class which receives input from the games objects with the image, x and y coordinates and so on, or is it a better way to do this?
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.

PocketCrafter7 (14 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (67 views)
2014-11-26 15:20:36

toopeicgaming1999 (16 views)
2014-11-26 15:20:08

SHC (30 views)
2014-11-25 12:00:59

SHC (28 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!