Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Better solution for moving my rectangle? (simple game)

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Better solution for moving my rectangle? (simple game)

    Hi,

    I am creating a simple game in Java which currently looks like this:
    mv6dg9.jpg
    The blue rectangle on the right is moving towards the blue square (the blue square is my hero which I can move around with the arrow keys). If I get hit by the blue rectangle my health gets reduced. What I would like to ask is if there's any better function to move my rectangle than the one I use below:
    if(level == 0) {try {
    	        while (true) {
    	        	skott.x -= skottspeed;
                            Thread.sleep(1 * 100);
    		        level = 1;
    		}
    } catch (InterruptedException e) {
                            e.printStackTrace();
    }
    }

    skott.x decides the x value of the rectangle which moves (also known as skott) towards the hero and here it takes minus speed which is a declared variable further up in my code and level is set to 1. I'm not really sure yet how thread.sleep is used but I think there's a better way moving my rectangle than the way I have done it. I would really appreciate some input since I'm quite new to programming a game.

    Thanks,
    John Z
    Last edited by JohnZ; May 24th, 2012 at 04:53 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,986
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Better solution for moving my rectangle? (simple game)

    I'm not really sure what the question is. Does this work? Where is your SSCCE? What do you mean by "better"?

    I would use a Swing Timer instead of threading.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Better solution for moving my rectangle? (simple game)

    Hey,

    I'm sorry I wasn't clear enough. It works yeah. However, I don't feel comfortable using something I'm not completely aware of what it actually does. Therefor I'm looking for an alternative I can use instead of Thread.sleep. I will try get to the bottom with the problem I've got.

    Currently I only understand how to make the rectangle come towards me once. Once it has past my hero it continues the x directions forever. So basically at this point what the game does is moving a rectangle to the left all the time and the hero only has one chance to try avoid it. Not really a game with meaning yet.

    What I think of doing is to make it once the big rectangle has past the hero/exited the window it gets removed completely and in 10 seconds it comes back again to the same position and starts attacking the hero again. I will make an image trying displaying the idea better.

    spel2.jpg (Please click to enlarge the image)

    Thanks in advance for your help.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,986
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Better solution for moving my rectangle? (simple game)

    Sounds good to me. And it sounds like you should be using a Swing Timer. Every time the Timer fires (30 seconds a second is a reasonable framerate), just check the location of the rectangle and either move it forward or respawn it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Better solution for moving my rectangle? (simple game)

    Thanks for your help. Gonna give it a try right away. Respawning it I use repaint();?

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,986
    My Mood
    Hungover
    Thanks
    133
    Thanked 557 Times in 478 Posts

    Default Re: Better solution for moving my rectangle? (simple game)

    Quote Originally Posted by JohnZ View Post
    Thanks for your help. Gonna give it a try right away. Respawning it I use repaint();?
    I'm not sure what you mean by that. Calling repaint() simply tells the component to paint itself using paintComponent(). So you override paintComponent to display a frame of your game.

    Then every time the Swing Timer fires, you do something like this:
    1- Update every game Object. The OO way to do this is to iterate over each Object and call an update function of each.
    2- call repaint

    Then in paintComponent(), you simply iterate over each Object and paint it, perhaps by calling each Object's paint function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Help with simple text game...
    By iansmiler in forum Java Theory & Questions
    Replies: 5
    Last Post: May 24th, 2012, 01:37 PM
  2. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 07:03 AM
  3. Simple game that requires me to load game settings from a file
    By 14fenix in forum Java Theory & Questions
    Replies: 5
    Last Post: December 1st, 2011, 08:21 PM
  4. Simple game problem
    By frozen java in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 30th, 2011, 08:14 PM
  5. Replies: 3
    Last Post: November 10th, 2011, 06:11 AM