简体   繁体   中英

Can't figure out what's going on with my game

The game is a simple stack up the blocks game, the game compiles and runs fine. The problem I am having is the blocks are supposed to move faster the closer to the top the player gets. The problem is that no matter where the blocks are they move at lightning speed. I think I am just over looking a simple mistake. Would love any kind of help.

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Stacker
  extends JFrame
  implements KeyListener
{
  int iteration = 1;
  static double time = 200.0D;
  static int last = 0;
  static int m = 10;
  static int n = 20;
  JLabel[][] b;
  static int[] length = { 5, 5 };
  static int layer = 19;
  static int[] deltax = new int[2];
  static boolean press = false;
  static boolean forward = true;
  static boolean start = true;

  public static void main(String[] args)
  {
    System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
    new Stacker();
  }

  public Stacker()
  {
    setDefaultCloseOperation(3);
    this.b = new JLabel[m][n];
    setLayout(new GridLayout(n, m));
    for (int y = 0; y < n; y++) {
      for (int x = 0; x < m; x++)
      {
        this.b[x][y] = new JLabel(" ");
        this.b[x][y].setBackground(Color.black);
        add(this.b[x][y]);
        this.b[x][y].setEnabled(true);
        this.b[x][y].setOpaque(true);
        this.b[x][y].setBorder(BorderFactory.createLineBorder(Color.GRAY));
        this.b[x][y].setPreferredSize(new Dimension(40, 30));
      }
    }
    setFocusable(true);
    addKeyListener(this);
    pack();
    setVisible(true);
    go();
  }

  public void go()
  {
    int tmp = 0;
    Component temporaryLostComponent = null;
    do
    {
      if (forward) {
        forward();
      } else {
        back();
      }
      if (deltax[1] == 10 - length[1]) {
        forward = false;
      } else if (deltax[1] == 0) {
        forward = true;
      }
      draw();

    } while (!

      press);
    if (layer > 12) {
      time = 150 - (this.iteration * this.iteration * 2 - this.iteration);
    } else {
      time -= 2.2D;
    }
    this.iteration += 1;
    layer -= 1;
    press = false;
    tmp = check();
    length[0] = length[1];
    length[1] = tmp;
    if ((layer == -1) && (length[1] > 0))
    {
      JOptionPane.showMessageDialog(temporaryLostComponent, "Congratulations! You beat the game!");
      System.exit(0);
    }
    if (length[1] <= 0)
    {
      JOptionPane.showMessageDialog(temporaryLostComponent, "Game over! You reached line " + (18 - layer) + "!");
      System.exit(0);
    }
    last = deltax[1];
    start = false;
    go();
  }

  public int check()
  {
    if (start) {
      return length[1];
    }
    if (last < deltax[1])
    {
      if (deltax[1] + length[1] - 1 <= last + length[0] - 1) {
        return length[1];
      }
      return length[1] - Math.abs(deltax[1] + length[1] - (last + length[0]));
    }
    if (last > deltax[1]) {
      return length[1] - Math.abs(deltax[1] - last);
    }
    return length[1];
  }

  public void forward()
  {
    deltax[0] = deltax[1];
    deltax[1] += 1;
  }

  public void back()
  {
    deltax[0] = deltax[1];
    deltax[1] -= 1;
  }

  public void draw()
  {
    for (int x = 0; x < length[1]; x++) {
      this.b[(x + deltax[0])][layer].setBackground(Color.black);
    }
    for (int x = 0; x < length[1]; x++) {
      this.b[(x + deltax[1])][layer].setBackground(Color.BLUE);
    }
  }

  public void keyPressed(KeyEvent e)
  {
    if (e.getKeyCode() == 32) {
      press = true;
    }
  }

  public void keyReleased(KeyEvent arg0) {}

  public void keyTyped(KeyEvent arg0) {}
}

You have no timing control logic in your main game loop, which it seems to be in the method go() : the do .. while loop which calls forward() and draw() does not have anything that is time dependent. You need to keep track of how long, in milliseconds, it took to do the last game iteration updates and wait if it is less than a pre-defined time interval which sets your game speed.

There is also the issue of a recursive call to the go() method which is likely to run out of stack memory eventually. The way you implemented it, as a way to restart the game, would allow for a number of restarts, and you will probably never notice it, but it is not a good design. You should have a main game loop, which exits when game-over or win and then restarted by the GUI when the player presses something like a start button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM