Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help adding a few features to Tetris
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Snario




PostPosted: Fri Jun 08, 2012 8:52 pm   Post subject: Help adding a few features to Tetris

I'm using the code that this website provides to build a Tetris game: http://zetcode.com/tutorials/javagamestutorial/tetris/

I'd like to add:
- A ghost piece that rests where the piece would land if the player pressed space or nothing at all.
- A way to hold a piece for later use (e.g., you get a line piece but want to use it when there is a good space, so you hold it and another piece comes)

The holding I feel like I have a good sense of how to get it to work, but the ghost piece isn't working as I expected it to.
Sponsor
Sponsor
Sponsor
sponsor
Amarylis




PostPosted: Fri Jun 08, 2012 9:13 pm   Post subject: RE:Help adding a few features to Tetris

What have you tried so far for the ghost piece?
Snario




PostPosted: Fri Jun 08, 2012 9:31 pm   Post subject: Re: Help adding a few features to Tetris

This is what I've tried so far:

code:
public void ghost(Graphics g, int x, int y, Tetrominoes shape) {
        super.paint(g);
        Dimension size = getSize();
        int boardTop = (int) size.getHeight() - BoardHeight * squareHeight();

        if (shapeAt(x, y - 1) == Tetrominoes.NoShape){
            ghost(g,x, y - 1, shape);
        }

        g.setColor(Color.GRAY);
        g.fillRect(x * squareWidth(), boardTop + (BoardHeight - y - 1) * squareHeight() + 1, squareWidth(), squareHeight());

    }


Obviously the code isn't quite right. Basically what I want to do is find the lowest possible spot that the block would fit if kept on the same path. So that means I need to find the maximum value for y in the board of the lowest values that each part of a shape can rest on and then orient the shape based on that value.
Snario




PostPosted: Sat Jun 09, 2012 10:57 am   Post subject: RE:Help adding a few features to Tetris

Alright so I updated that code a bit and it sort of works now, the shape is drawn in the correct x positions but the y value are a bit off. Ghost shapes appear inside already placed shapes and if the board is empty sometimes some of the blocks fall beneath the lowest line.

code:

public int ghost(Tetrominoes shape) { // This method gets the coordinates for a ghost block
        // To get the coordinates, the smallest y value of a shape is matched with the largest possible y once dropped
        int x;
        int y;
        int minY = 22;
        int maxY = 0;

        if (curPiece.getShape() != Tetrominoes.NoShape) {

            for (int i = 0; i < 4; i++) {                // This for loop gets the x,y coordinates
                x = curX + curPiece.x(i);                // of each block in a shape
                y = curY - curPiece.y(i);

                if (y < minY) {
                    minY = y;    // One the loop is finished, minY will be the lowest y value of a shape
                }

                for (int k = minY; k >= 0; k--) {
                    if (shapeAt(x, k) != Tetrominoes.NoShape) {
                        maxY = k; // This value for maxY will be the highest y value that all blocks in a shape
                        break;    // are all able to exist in relative to their current x
                    }
                }
            }
        }
        return maxY;
    }
Snario




PostPosted: Sat Jun 09, 2012 7:17 pm   Post subject: RE:Help adding a few features to Tetris

I'm checking this thread every 5 minutes or so by the way, so if you want any additional info I'll be able to reply very quickly. :p

I've put the Ghost project aside for now, working on a timer and high scores system as well as a reset button at the moment.
Amarylis




PostPosted: Sat Jun 09, 2012 9:43 pm   Post subject: RE:Help adding a few features to Tetris

My suggestion is to use the system you have for checking when the original piece has reached the bottom-most point it can be placed at on your ghost piece to figure out where it's bottom-most point is
Snario




PostPosted: Sun Jun 10, 2012 2:57 pm   Post subject: Re: Help adding a few features to Tetris

I got it to work! If anyone is interested in the solution, here it is:

Created a ghost method that gathers the co-ordinates of a ghost piece using the contents of the dropDown method but modified a bit (you will see later in the tryMove method)
code:

 private void ghost(Graphics g) {
        Dimension size = getSize();
        // boardTop is the number of pixels leftover at the top of the board
        int boardTop = (int) size.getHeight() - BoardHeight * squareHeight(); // Determines the unused space at the top of the board

        updateTime();
        int newY = curY;
        while (newY > 0) {
            if (!tryMove(curPiece, curX, newY - 1, true)) {
                break;
            }
            newY--;
        }
        drawSquare(g, ghostX * squareWidth(),
                boardTop + (BoardHeight - ghostY - 1) * squareHeight(),
                curPiece.getShape());
    }


Then inside the if statement of the paint method

code:
/* Draws each indidivual block of a ghost of the current shape */
            for (int i = 0; i < 4; ++i) {
                ghost(g);
                x = ghostX + ghostPiece.x(i);
                y = ghostY - ghostPiece.y(i);
                drawSquare(g, x * squareWidth(),
                        boardTop + (BoardHeight - y - 1) * squareHeight(),
                        ghostPiece.getShape());
            }


Had to add this if statement to the tryMove method, and the ghost boolean

code:
private boolean tryMove(Shape newPiece, int newX, int newY, boolean ghost) {
        if (!ghost) {
            curPiece = newPiece;
            curX = newX;
            curY = newY;
            repaint();
            return true;
        } else {
            ghostPiece = newPiece;
            ghostX = newX;
            ghostY = newY;
            repaint();
            return true;
        }
    }
Snario




PostPosted: Sun Jun 10, 2012 6:50 pm   Post subject: RE:Help adding a few features to Tetris

Is there any way I can put this entire application inside of a Desktop Application rather easily?

By which I mean the entire application is inside a box and outside of the box I can add some buttons and such.
Sponsor
Sponsor
Sponsor
sponsor
Snario




PostPosted: Mon Jun 11, 2012 2:17 pm   Post subject: RE:Help adding a few features to Tetris

Also could anyone tell me how I could go about adding a main menu and screen to display all of the high scores? I know how to do all of the leaderboard stuff, but I don't know how to display it.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: