Results 1 to 12 of 12
  1. #1
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Red face tetris type game--- boundary help

    i am working on a tetris type game and i have the game board drawn and i have a peice that falls and will go left and right however i cannot figure out how to do the boundaries. how to tell it not to exceed the width or fall below the bottom of the board. i have tried several ideas to no avail. then methods for boundaries are at the bottom of the BlocktrisGame class.... here is what i have so far...

    Java Code:
    import java.awt.Color;
    import java.awt.event.KeyEvent;
    
    
    public class BlocktrisGame
    {
    	private int x;
    	private int y;
    	private int width;
    	private int height;
    	public static final int BLOCK_SIZE = 25;
    	
    	public BlocktrisGame( int width, int height )
    	{
    		this.width = width;
    		this.height = height;
    	}
    	
    	//TODO write rest of BlocktrisGame methods (e.g. init, getX, setX, etc. etc.)
    	
    	//set x method
    	public void setX(int x){
    		this.x = x;
    	} 
    	
    	//get x method
    	public int getX(){
    		return x;
    	}
    	
    	//set y method
    	public void setY(int y){
    		this.y = y;
    	}
    	
    	//gt y method
    	public int getY(){
    		return y;
    	}
    	
    	//get height method
    	public int getHeight(){
    		return height;
    	}
    	
    	//get width method
    	public int getWidth(){
    		return width;
    	}
    	
    	//init method
    	protected void init(Draw d){
    		d.setColor(Color.green);
    		d.drawRectangle(5, 5, width * BLOCK_SIZE + 2, height * BLOCK_SIZE + 2);
    	}
    	
    	public void drawActivePeice(Draw d){
    	
    		d.fillRectangle(6 + x * BLOCK_SIZE, 6 + y * BLOCK_SIZE, BLOCK_SIZE - 2, BLOCK_SIZE - 2);
    		d.draw();
    	}
    	
    	public boolean isActivePeiceFallen(){
    		boolean isFallen = false;
    		if(this.y <= height){
    			isFallen = true;
    		}
    		return isFallen;
    	}
    	
    	public boolean isActivePieceOutOfBounds(){
    		boolean isOutOfBounds = true;
    		if(this.x < width){
    			isOutOfBounds=false;
    		}
    		return isOutOfBounds;
    	}
    
    	
    	public static void main( String[] args )
    	{
    		Draw d = new Draw( "Blocktris", 400, 410 );
    		d.setColor( Color.white );
    		
    		
    		//start the timer
    		d.startAnimation( 300 );
    		
    		BlocktrisGame game = new BlocktrisGame( 10, 16 );
    		game.init( d );
    		d.draw();
    		
    		for( int event : d.getEventIterable() ) {
    			if( event == KeyEvent.VK_LEFT ) {
    				//move block to the left
    				//TODO erase the block
    				d.setColor(Color.black);
    				game.drawActivePeice(d);
    				game.setX( game.getX() - 1 );
    				if( game.isActivePieceOutOfBounds() ) {
    					game.setX( game.getX() + 1 );
    					
    				}
    				//TODO draw the block
    				d.setColor(Color.white);
    				game.drawActivePeice(d);
    				
    			}
    			else if( event == KeyEvent.VK_RIGHT ) {
    				//TODO move block to the right
    				d.setColor(Color.black);
    				game.drawActivePeice(d);
    				game.setX(game.getX() + 1);
    				if( game.isActivePieceOutOfBounds()){
    					game.setX(game.getX() + 1);
    				}
    			
    			}
    			else if( event == Draw.TIMER_EVENT ) {
    				//make the block fall down
    				//TODO erase the old block
    				d.setColor(Color.black);
    				game.drawActivePeice(d);
    				game.setY( game.getY() + 1 );
    				if( game.isActivePieceOutOfBounds() ) {
    					game.setY( game.getY() - 1 );
    					//TODO draw the current block
    					d.setColor(Color.white);
    					game.drawActivePeice(d);
    					//TODO start a new block at the top,
    					//means we need to set X and Y
    					game.setX(0);
    					game.setY(0);
    					
    				}
    				//TODO draw the block
    				d.setColor(Color.white);
    				game.drawActivePeice(d);
    			}
    			
    		}
    		d.draw();
    	}
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-08-2010 at 10:06 PM. Reason: Moderator Edit: Code tags added

  2. #2
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    do not know y everything aligned left it was all indented before i posted it .... sorry

  3. #3
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    Adding code tags around the code keeps the indenting in tact.

    Erik
    I'm new to Java but I like to help where ever I can. :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got 3 previous threads where you've gotten helpful replies but never bothered to reply back or thank those that helped you. Since we'll never know if you read any of the stuff we post, why should we respond?

  5. #5
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    i am sorry i am new to this... if this even gets to you... i am unaware of how to respond to helping people

  6. #6
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    test... is this how i respond to a person

  7. #7
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    ok guess not.... if someone could tell me how to respond back to a poster that would be great... didnt mean to offend neone

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've added code tags to your code to help make it readable. To learn how to do this yourself, please see the link in my signature.

    To check for boundary issues, you'll need to know the size of your piece, then check to see if when you move the piece it has crossed any boundaries -- usually done with if blocks. The position of most graphic objects in most graphics systems is the upper left corner, so this number can be use to check for left and top boundary violations. The right side will require that you check the x position plus the piece width compared to the right boundary and the bottom check will require that you check the y position plus the height compared to the bottom boundary.

    Your current graphics library doesn't look like one I'm familiar with, so I cannot say with 100% confidence that these recommendations will apply to you. If you need more help, you may need to supply more code.

    One suggestion that I know does apply is that you're often better off separating out the logic issues from the GUI issues by creating a non-GUI model that has all the game logic, and then using this model to guide the drawing of the graphics. If you divide and conquer, you can simplify your problems and make the easier to solve.

    Oh, and to reply you simply reply in the thread that the question was asked as you are doing now.

    Luck!

  9. #9
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    thank you for your input (is this the proper way to do this?).... once i was able to talk it out w/ someone that knew what i was saying i actually figured out all but one problem on my own (i happen to be proud of that fact)... but ur advice would have tipped me into the right direction.. thank you again for your post it was helpful!

  10. #10
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    how do i mark this as solved now?

  11. #11
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Top of the thread, theres a menu called 'Thread Tools'. Should be a mark as solved button in that menu.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  12. #12
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

Similar Threads

  1. how to get mime boundary
    By chotto in forum Advanced Java
    Replies: 1
    Last Post: 10-20-2010, 04:59 PM
  2. Tetris Applet Help
    By Growler in forum Java Applets
    Replies: 5
    Last Post: 08-04-2010, 07:03 PM
  3. Replies: 6
    Last Post: 12-15-2009, 02:33 AM
  4. Creating a boundary line in java
    By Riz in forum New To Java
    Replies: 2
    Last Post: 09-03-2009, 12:54 AM
  5. Replies: 22
    Last Post: 04-21-2009, 05:40 PM

Tags for this Thread

Posting Permissions

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