New to Java Games!

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 :P)

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.

Hello! :slight_smile:
Finish your game and show us that “RPG being first project is bad” was myth!

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 :). 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! ^^

ALso @ReBirth, i do not believe in this myth, its one of the best ways of learning to develop games in my opinion. Since youll have to look a lot up. And rpg covers many of the basic game principles.

We plan to >:P
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 :stuck_out_tongue:

  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?

 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.

  1. 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.

  2. 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 :stuck_out_tongue:

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 :stuck_out_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.

Who is ReBorn?

Your evil twin obviously.

So scary :slight_smile:

Yes i used to have it all in 1 class as well in my first game (which was written in c++). Then the same in java.
But its rather hard and chaotic to work with yes, plus the fact that all has to be done in 1 class, and without OOP (Object Oriented Programming) for which java is made for (to work with objects, same with c++).
Plus the fact that you wont be able to multithread very easily (I can see it being done, but with a lot of effort and redundant code).
I can help with FileIO :). pm me your skype name, msn adress, email or whatever you prefer to comminucate.

Hi there! :slight_smile:

A. That’s up to you bro. I’m still figuring that out myself. Getting your name out is difficult to do.

B. The basic idea of what you’re doing is right, it just comes down to how you do it. If you stick with an int[][] then you might consider an additional int[][] for each map for collisions to mark each matching tile as solid (So if you have a 8 x 8 tile number array, have another 8 x 8 array full of 1s and 0s to say what tiles are solid). It’s a pain to update changes later on, but it’s fine for your 1st game.

C. I know what is wrong here. It basically has to do with how the AWTEvent threads are managed and called. It’s easy to correct though. Instead of increasing x or y in the keyPressed() method, set an xVelocity or YVelocity variable to the speed you want to move at (negative number to go left or up), then, wherever your player’s updating code is done, simply say x = x + xVelocity (or x += xVelocity, same thing) and y = y + yVelocity. Just don’t forget to reset your velocities to 0 if they release the key!

D. Your professor is mostly right. You can’t have your applet write and read files on a person’s computer (potential viruses and stuff) without signing the applet, which is a pain. The only other option that I know of is to use the JDBC library for reading and writing to databases. Since you’re still learning, I’d recommend just doing an application for this one. Later on though, you’ll probably want to write binary files but for now, .txt files are OK.

E. Most certainly stick to vanilla Java. Until you’re at the point where you’ve written it a hundred times and it is just becoming leg work, pleeeeease write it yourself. You’ll become a much better programmer that way.

Look up object ‘serilization’ for save games, and signing applets to store files on the clients computer if its going to run as an applet. Ive heard signing is annoying to do at first but it gets easier once you’ve done it once.

For the key input I use several boolean variables and if a key is pressed it changes that variable to true, and released it changes it to false. Then somewhere in the gameloop I have a method that just goes through those boolean variables and if one is true then do stuff. For example,


public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
switch(k){
		case KeyEvent.VK_A:
			game.keyPress[0] = true;
			break; 
... // check other keys
// then elsewhere in a method called from the gameloop
	private void checkForInput(){
		if(keyPress[0]){
			player.setDX(-player.getRunSpeed());
		}else{
			player.setDX(0);
		}
// so on, so on

This seems to get me nice smooth movement :slight_smile:

I wouldnt say that is the best way to do it. After you update your game often and expand it a lot, what the hack is game.keyPress[0] then? and what is 1?
I would just do it with 4 booleans, up down left right
Then pressing a, will set left. releasing a will clear left.
Then make an update function, which goes trough all the possibilitys so in pseudo code:


if(left && !right)
dx = -1;
if(!left && right}
dx = 1;
if(up&&!down)
dy=-1;
if(!up&&down)
dy=1;

private void update(){
x+=dx;
y+=dy;
}

This is globally how i do it (But then with collision check, bit more advanced and some more functions in between for animations and such) and works very well.

Yea i agree with you, that array is just me being lazy. Bad idea to use that if your working in a group. :stuck_out_tongue: principle is the same though

Guys, I’m telling ya; velocities. I’ll show you how easy it is.


// Controls.java
public class Controls implements KeyListener {
           private Game game;

           public Controls(Game game) {
                      this.game = game;
           }

           public void keyPressed(KeyEvent key) {
                     switch (key.getKeyCode()) {
                               case KeyEvent.VK_UP:
                                       game.getPlayer().setYVelocity(-5);
                                       break;
                               case KeyEvent.VK_DOWN:
                                       game.getPlayer().setYVelocity(5);
                                       break;
                               case KeyEvent.VK_LEFT:
                                       game.getPlayer().setXVelocity(-5);
                                       break;
                               case KeyEvent.VK_RIGHT:
                                       game.getPlayer().setYVelocity(5);
                                       break;
                     }
           }

           public void keyReleased(KeyEvent key) {
                     if (key.getKeyCode() == KeyEvent.VK_UP || key.getKeyCode() == KeyEvent.VK_DOWN) {
                                game.getPlayer().setYVelocity(0);
                     }
                     if (key.getKeyCode() == KeyEvent.VK_LEFT || key.getKeyCode() == KeyEvent.VK_RIGHT) {
                                game.getPlayer().setXVelocity(0);
                     }
           }
}


// Game.java
public class Game extends JComponent {
           ....

           public Game() {
                      this.addKeyListener(new Controls(this));
                      this.setFocusable(true);
           }

           ...
}


// Player.java

...

public void update(long elapsed) {
           ...
           x += xVelocity;
           y += yVelocity;
           ...
}

public void setXVelocity(int x) { xVelocity = x; }
public void setYVelocity(int y) { yVelocity = y; }

Boom, done!

Takes 5 minutes, is easy to read, and easy to update.

I rather use the method me and embedded use. What is the advantage of yours over ours?
Ours is easier to read, easier to implement and doesnt require another class. Doesnt need .getPlayer.
All i need is this in my drawing class:


    private class TAdapter extends KeyAdapter {
        public void keyPressed(KeyEvent e) {
        	player.keyPressed(e);
        }
        public void keyReleased(KeyEvent e){
        	player.keyReleased(e);
        }
    }

and this in my player class:


	void update(){
	        if(left	  && !right  && !checkCollision(+2,0)){
	            x -= speed;
	        }if(right && !left	 && !checkCollision(-2,0)){
	        	x += speed;
			}if(up	  && !down   &&  !checkCollision(0,+2)){
	            y -= speed;
			}if(down  && !up     &&	 !checkCollision(0,-2)){
	            y += speed;
			}
		}
	}
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT){
        	left = true;
        }else if (key == KeyEvent.VK_RIGHT){
            right = true;
        }else if (key == KeyEvent.VK_UP){
            up = true;
        }else if (key == KeyEvent.VK_DOWN){
            down = true;
        }

	}
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        if (key == KeyEvent.VK_LEFT) {
        	left = false;
        }if (key == KeyEvent.VK_RIGHT){
        	right = false;
        }if (key == KeyEvent.VK_UP){
        	up = false;
        }if (key == KeyEvent.VK_DOWN){ 
        	down = false;
        }
    }
	
	public void run(){
		while(true){
			try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}
			update();
		}
	}

Easier to implement, and on a more logical place in my opinion.

My code doesnt show it very well but i kind of use a hybrid between the two,


player.setDX(-player.getRunSpeed());

I call my velocity variable dx and dy because im special like that, so in this method-


   private void checkForInput(){    // this is only part of the method
		if(keyPress[2]){
			if(!(player.isJumping())){
				player.dy = -player.getJumpSpeed();
				player.setJumping(true);
			}
		}
		
		if(keyPress[3]){
			if(player.isJumping()){
				if(!(player.isDoubleJumping())){
					player.dy = -(player.getJumpSpeed());
					player.setDoubleJumping(true);
				}
			}
		}

The player isnt actually moved it just changes his velocity. I do it this way becuase then its easier to implement stuff like gravity exct…
He moves when the player.moveX() and player.moveY() methods are called, (there seperate for collision detection purposes)

@elamre
That doesn’t seem better to me…If anything, it’s a little tangled.

The Controls should have its own class for modularization anyway and oftentimes there are commands that don’t directly affect the player (like the pause button or the open inventory button). All keyboard Input goes in Controls and someone (including me) can take one look at Controls and can see how it works. Not to mention that if you throw GameStates in the mix (like a main menu gamestate where the arrow keys now select a menu item and don’t move the player anymore), you’ll regret not having it separated.

Keyboard Input doesn’t belong in the Drawing class–drawing code should be in there.

My way only requires two descriptive variables that essentially handle themselves, where 4 booleans are involved in yours which is harder to read.

The getPlayer() should already be here because there will likely be other classes asking for the player. Seeing as how each part of the game stems from Game, it makes sense to have one place where it can be accessed.

@embedded
That’s a bit better.

If I did it I would probably do something like this:


// Controls.java
...
public void keyPressed(KeyEvent key) {
            ...
            case KeyEvent.VK_SPACE:
                       game.getPlayer().jump;
                       break;
}

Controls doesn’t care how the player jumps, it just does it’s job and instructs the player to do it. Only the player knows how to perform a jump.