Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 5:13 am

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Show me better !
PostPosted: Wed May 23, 2012 3:39 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Hello guys, I'm kinda new into java, slick, and lwjgl, and I was working on simple game ( kind of ) where you have ball on screen, and you click on it and drag it, similar to icons on desktop. I added a filled rectangle for collision and stuff.

So I'm asking you ( those who are better than I am at this ) to tell me what would you change in my code.

Here is the project ( made in Eclipse IDE ) :

Meat Blaster !

Funny name right :D


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 2:32 pm 
Offline
User avatar

Joined: Fri Nov 18, 2011 3:39 pm
Posts: 48
Location: Germany
Does anyone else here get a virus warning on the attempt to open it? :evil:


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 3:53 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Nepomuk wrote:
Does anyone else here get a virus warning on the attempt to open it? :evil:


What are you talking about ?!


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 3:55 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
I'm using AVG, and didn't get a virus warning. I scanned it before opening it, and then i extracted it, and scanned it again. no issues..

Looked through the files, etc.. Seems theres nothing wrong with it. I think you had a false positive, Nepomuk,


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 7:01 pm 
Offline

Joined: Mon Feb 16, 2009 11:33 am
Posts: 16
What you currently do within the initStatesList() method is useless because the init method of the different states is already called by the init method of the StateBasedGame :

Code:
/**
* @see org.newdawn.slick.BasicGame#init(org.newdawn.slick.GameContainer)
*/
public final void init(GameContainer container) throws SlickException {
   this.container = container;
   initStatesList(container);
   
   Iterator gameStates = states.values().iterator();
   
   while (gameStates.hasNext()) {
      GameState state = (GameState) gameStates.next();
   
      state.init(container, this);
   }
   
   if (currentState != null) {
      currentState.enter(container, this);
   }
}


What I generally do within that method is creating the states and adding them in the desired order (the first state added will be the initial state of the application).


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 7:09 pm 
Offline

Joined: Mon Feb 16, 2009 11:33 am
Posts: 16
Regarding the render method of your Menu class, I would recommend that you do only rendering there and that you use the input listening methods of the class BasicGameState by redefining the required ones to delegate some work.

Basically, all your game logic should be within the update methods, the input handling should be done by the inputlistening methods and the rendering only by the render method.

Oh, almost forgot. For better performances, you should not recreate the balloon image at every game loops. It means that you should do it for example within the init method of the state but not in the update or in the render method (which actually is the worst place to do it :)).


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 7:26 pm 
Offline

Joined: Mon Feb 16, 2009 11:33 am
Posts: 16
This has nothing to do with Slick but you also defined some static attributes for the state ids within your game class but you don't use them from the getId() method of your states.


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Thu May 24, 2012 10:51 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
  • Don't create a new image every frame (as Makenshi pointed out)
  • To get the mouse coordinates, use input.getMouseX()/getMouseY()
  • Using Input.MOUSE_LEFT_BUTTON is more standard than using inputInstance.MOUSE_LEFT_BUTTON (Eclipse probably gives you a warning)
  • It's better practice to do simple checks before complex checks; i.e. the following:
    Code:
    if ( complexCollisionTest(blah) || mouseButton == 1 )
        ...

    Should be changed to this instead:
    Code:
    if ( mouseButton == 1 || complexCollisionTest(blah) )
       ...
  • You can use the new-line character ('\n') when writing strings so you don't need to draw each line individually :)
  • You don't need to manually initialize states (otherwise they'll end up getting initialized twice). Instead, use this:
    Code:
    public void initStatesList(GameContainer gc) throws SlickException {
       addState(new MyState1(blah));
       addState(new MyState2(blah));
       
       //after this method is called, all the states you've added will be initialized for you
       //then the game will enterState() the first state you added (in this case MyState1)
       }


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Fri May 25, 2012 7:11 am 
Offline
User avatar

Joined: Fri Nov 18, 2011 3:39 pm
Posts: 48
Location: Germany
Okay, my bad. :D
I'm not really sure why got a virus warning, now it's gone!


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Fri May 25, 2012 7:55 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
It might have been one of megauploads advert popups or something like that. i know i've had a similar experience before.


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Fri May 25, 2012 6:54 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Thank you guys, I'm still learning, thanks for the performance thing !


Top
 Profile  
 
 Post subject: Re: Show me better !
PostPosted: Fri May 25, 2012 6:59 pm 
Offline

Joined: Tue May 22, 2012 8:24 pm
Posts: 49
Also does someone know is there better way for collision, maybe using alpha or something, and how to create rectangle filled with gradient ?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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

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