Results 1 to 8 of 8
  1. #1
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Simple Java Game -- HELP :)

    Hi all, well this is my first post but hopefully one of many.

    The questions is quite a long one.. I have been having trouble with a program I am trying to develop for school.

    The GUI is fine and I have no problems creating that bit, but the game is basically simple in that.. when a button is clicked (Left) the button located left to the start value changes color, and vice versa for right up down... I have used a simple array and loop to create 900 buttons on a 30 x 30 grid but the question is how should I program in this feature

    Java Code:
    			JPCenter = new JPanel();
    				JPCenter.setPreferredSize(new Dimension(900, 900));
    			    JPCenter.setLayout(new GridLayout(30,30)); //set layout
    			    for (int count=1; count<=900;count++ )	
    				    {
    					JBblank = new JButton(""+count);
    	        		//DEBUG MAKE SURE THAT BUTTONS ARE BEING MADE. 
    					System.out.println("button "+count);	
    					
    						//BOTTOM AND TOP -- BORDER
    						if((count <=30)||(count >870))
    						{
    							JBblank.setBackground(Color.green);
    						}	
    						//RIGHT BORDER 
    						if((count%30==0))
    						{
    							JBblank.setBackground(Color.green);
    						}
    						//LEFT BORDER 
    						if((count%30==1))
    						{
    							JBblank.setBackground(Color.green); 
    							
    						}
    							JPCenter.add(JBblank);
    							JBblank.addActionListener(this);
    						if((count==59))
    						{
    							JBblank.setBackground(Color.cyan); 
    						}
    The output is something like you can see here..
    Simple Java Game -- HELP :)-game.png

    Now, the robot is "Red" and the "End" is the cyan color, the robot cannot go in the green, so these are simple rules..

    Also, there are three fields that will update stating the location (cell number) and (direction) what would be the best way of doing this?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,965
    Rep Power
    8

    Default Re: Simple Java Game -- HELP :)

    I'm not sure what your actual question is. So are there other JButtons (left, right, up, down)? Does the user click these 900 buttons? Which part of this is giving you trouble, exactly?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple Java Game -- HELP :)

    Quote Originally Posted by KevinWorkman View Post
    I'm not sure what your actual question is. So are there other JButtons (left, right, up, down)? Does the user click these 900 buttons? Which part of this is giving you trouble, exactly?
    Hmmm, it's hard to explain in words. Basically the red square is the "Actor" when a button is clicked (see screenshot) it will make the red cell move in relation to that. And the previous cell will return to the normal color Grey.

    Simple Java Game -- HELP :)-betterexample.png

    The problem I am having is... what would be the best way to achieve this.... I can PM full code but due to the fact this is a school assignment don't want it public on the internet just in case.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Simple Java Game -- HELP :)

    Can any of the grey buttons be clicked? What is the red button supposed to do when a grey button is clicked?
    Can you give some examples by using the relative row and column positions of the two buttons involved.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple Java Game -- HELP :)

    Okay, to rephrase for a third time. On the right of the program there is a 3 x 3 grid with movement buttons on (Above the "Option 1" "Option 2" "Option 3") When the RIGHT button is clicked.. if we are taking into account the starting location is Button number 842, when the (">") is clicked 843 will become red and 842 will become grey. NONE OF THE BUTTONS ON THE GRID CAN BE CLICKED :) They are just placeholders to demonstrate a simple game logic, the question is quite simple.

    I used a loop to create the buttons and an array, this created 900 buttons.

    I need to understand, how when you click Up or Down or Right to make the relevant square highlighted red and to store this value so that it can be recalled when the next movement command is called.

    Does that make any more sense :)

    (I would rather use the 2D API, but we have to use this method for the game... stupid I know)

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Simple Java Game -- HELP :)

    If you have the location of the red button then when a direction to move is indicated by the pressing of a direction button, then you want to change the color of the button in that direction from the red button and restore the current red button to grey. Moves at the border now allowed towards the border or do they wrap to the opposite side?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Simple Java Game -- HELP :)

    Quote Originally Posted by Norm View Post
    If you have the location of the red button then when a direction to move is indicated by the pressing of a direction button, then you want to change the color of the button in that direction from the red button and restore the current red button to grey. Moves at the border now allowed towards the border or do they wrap to the opposite side?
    Correct, moves cannot be made at the border (the green)

    I was thinking something like this..

    If Forward(); -30
    If Right(); +1
    If Left() -1
    If Backward() -30

    But how to build that in? Do you want me to PM you full code might help a bit..

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: Simple Java Game -- HELP :)

    I have no idea what your + and - values are used for.
    If you know where the red button is: say row 10 and column 10
    To move up would be to row 9 column 10
    To move left would be to row 10 column 9
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Help needed with simple Java Snake game
    By AndySun94 in forum New To Java
    Replies: 1
    Last Post: 04-01-2012, 06:59 AM
  2. want to make a simple game in java
    By Tarasj in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 09:54 PM
  3. Help with simple java game/image drawing
    By Nighthawk0973 in forum Java 2D
    Replies: 5
    Last Post: 10-02-2011, 04:09 AM
  4. need a simple java game
    By Shashwat in forum New To Java
    Replies: 1
    Last Post: 01-03-2011, 02:42 PM
  5. Simple Game example
    By mayuresh34 in forum CLDC and MIDP
    Replies: 1
    Last Post: 10-08-2009, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •