Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:09:27+00:00 2026-06-12T14:09:27+00:00

I have here a code snippet of my Tetris project. private class Game implements

  • 0

I have here a code snippet of my Tetris project.

private class Game implements Runnable  {
    private int numDropped = -1;
    public void setCount(int count){
        count++;
    }
    public int getCount(){
        return count;
    }
    public void run() {
        int column = 4, style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
        while (onPlay) {
            if (piece != null) {
                if (piece.isAlive()) {
                    try {
                        Thread.currentThread().sleep(100);
                    } 
                    catch (InterruptedException ie) {
                        ie.printStackTrace();
                    }
                    continue;
                }
            }

            checkFullLine();  
            if (isGameOver()) {
                playItem.setEnabled(true);
                pauseItem.setEnabled(true);
                resumeItem.setEnabled(false);
                rightPanel.setPlayButtonEnable(true);
                rightPanel.setPauseButtonLabel(true);

                displayGameOver();
                return;
            }
            piece = new Piece(style, -1, column, board);
            piece.start();
            style = Piece.SHAPES[(int) (Math.random() * 7)][(int) (Math.random() * 4)];
            rightPanel.setTipStyle(style);
            numDropped = numDropped+1;
            RightPanel.scoreTextField.setText(numDropped+"");
        }
    }
}

The class Game is an inner class, by the way. Whenever a new piece goes down, the value of numDropped increments (as displayed in a JTextField) but then after a short while goes back to zero. Did I misplace the

numDropped = numDropped+1;
RightPanel.scoreTextField.setText(numDropped+"");

? Or because of something else like being static and stuff. Please help me. I’m kinda new in Java. Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:09:28+00:00Added an answer on June 12, 2026 at 2:09 pm

    The code you have posted will definitely never update the scoreTextField to a value that is smaller than the value it used earlier.

    The first thing I would suspect is that there is some other code that updates scoreTextField to a value that is not equal to numDropped.

    Update

    In your additionally posted code I see this:

    RightPanel.scoreTextField = new JTextField(numDropped+"");
    

    This is wrong; delete it and uncomment the line above, that sets the text on the existing text field.

    Moreover, you’ve got this code:

    timer = new Timer(
                    500, new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {
                            scoreTextField.setText("" + game.getScore());
                            int scoreForLevelUpdate = game.getScoreForLevelUpdate();
                            if (scoreForLevelUpdate >= Tetris.PER_LEVEL_SCORE && scoreForLevelUpdate > 0)
                                game.levelUpdate();
                        }
                    }
            );
    
            timer.start();
    

    Clearly this will overwrite your scoreTextField with the game score, whereas in the Game‘s loop you are writing the number of dropped elements. Judging from the name of your variable scoreTextField, you should change the code in Game to update some other text field and not the one used to display score.

    Yet another thing that is wrong in your design is this:

    Thread thread = new Thread(new Game());
    thread.start();
    

    You are starting another thread to control the gameplay. You must not update any Swing components outside the main GUI thread, the so-called Event Dispatch Thread (EDT). My suggestion is to redesign your code similar to what you have done above with your timer variable: don’t start another thread; instead schedule another Timer with the interval of 100 milliseconds, and implement the associated actionPerformed based on your current code in while (onPlay) — all you’ll need to change is that you don’t need the loop anymore. Of course, Thread.sleep(100) will also be redundant. When the game is over, just cancel your timer task.

    Not directly related to the problem of this question, but this code

    public void setCount(int count){
        count++;
    }
    

    is also wrong: it doesn’t write to the instance variable count, but just increments the supplied argument, immediately discarding it (it’s just a local variable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with two constructors (C#). Here is the code snippet: public
Here my code snippet: class Request { public: Request(void); ……….. } Request::Request(void) { qDebug()<<Request:
Here is a code snippet which I have written: let Foo (a : (int
I have a small code snippet for deleting element in linked list. Here is
does anyone here have a code snippet: how do I convert SDDL to SECURITY_DESCRIPTOR
FULL CLASS CODE HERE: http://pastebin.com/rdjDGLJS EDIT: Latest code snippet taken from original posters comment
I'm getting an ActivityNotFoundException in my Android Application.. Here's the code snippet I have
Here is html code snippet <li style=opacity: 1;> <a id=LinkDisplay class=optionsDropDown style=color:#FF0000;display:none href=javascript:showThisLink('LinkId');> </li>
Here is a code snippet where in I have to select a proper date
Here is a code snippet for an asp:Wizard control I have on an aspx

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.