游戏是块游戏的简单叠加,游戏编译并运行良好。我遇到的问题是块应该移动得越快,越接近玩家获得的顶部。问题是,无论块在哪里,它们都以闪电般的速度移动。我想我只是在看一个简单的错误。会喜欢任何帮助。
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) {}
}
答案 0 :(得分:0)
你的主游戏循环中没有时间控制逻辑,它似乎在方法go()
中:调用do
的{{1}} .. while
循环并且forward()
没有任何时间依赖的东西。您需要跟踪完成最后一次游戏迭代更新所花费的时间(以毫秒为单位),并等待它是否小于设定游戏速度的预定义时间间隔。
还有一个递归调用draw()
方法的问题,最终可能会耗尽堆栈内存。你实现它的方式,作为一种重新启动游戏的方式,将允许重新启动,你可能永远不会注意到它,但它不是一个好的设计。你应该有一个主游戏循环,当玩家按下开始按钮之类的东西时,它会在游戏结束或赢取时退出,然后由GUI重新启动。