Results 1 to 12 of 12
- 11-08-2010, 07:19 PM #1
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
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...
Moderator Edit: Code tags addedJava 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(); } }Last edited by Fubarable; 11-08-2010 at 10:06 PM. Reason: Moderator Edit: Code tags added
- 11-08-2010, 07:54 PM #2
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
do not know y everything aligned left it was all indented before i posted it .... sorry
- 11-08-2010, 07:57 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Adding code tags around the code keeps the indenting in tact.
ErikI'm new to Java but I like to help where ever I can. :)
-
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?
- 11-08-2010, 08:33 PM #5
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
i am sorry i am new to this... if this even gets to you... i am unaware of how to respond to helping people
- 11-08-2010, 09:20 PM #6
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
test... is this how i respond to a person
- 11-08-2010, 09:21 PM #7
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
ok guess not.... if someone could tell me how to respond back to a poster that would be great... didnt mean to offend neone
-
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!
- 11-09-2010, 12:36 AM #9
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
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!
- 11-09-2010, 12:39 AM #10
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
how do i mark this as solved now?
- 11-09-2010, 04:05 AM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 11-09-2010, 10:00 PM #12
Member
- Join Date
- Sep 2010
- Location
- Oklahoma
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
how to get mime boundary
By chotto in forum Advanced JavaReplies: 1Last Post: 10-20-2010, 04:59 PM -
Tetris Applet Help
By Growler in forum Java AppletsReplies: 5Last Post: 08-04-2010, 07:03 PM -
Trying to make a functioning Tetris game/ why does my board look weird?
By DeusExMachina90 in forum AWT / SwingReplies: 6Last Post: 12-15-2009, 02:33 AM -
Creating a boundary line in java
By Riz in forum New To JavaReplies: 2Last Post: 09-03-2009, 12:54 AM -
How do I upload my Java Zork type game online for others to test
By alpdog14 in forum New To JavaReplies: 22Last Post: 04-21-2009, 05:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks