2016-04-24 97 views
0

遊戲是一個簡單的堆積塊遊戲,遊戲編譯和運行良好。我遇到的問題是,越靠近玩家獲得的頂部,塊應該移動得越快。問題在於無論在哪個街區,它們都以閃電般的速度移動。我想我只是在看一個簡單的錯誤。會喜歡任何形式的幫助。無法弄清楚我的遊戲發生了什麼

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) {} 
} 
+1

你浪費你的'時間'嗎? :-)我看不到任何用法。 – Turo

回答

0

你有沒有定時控制邏輯在遊戲主循環,它似乎是在方法go():在do .. while環這就要求forward()draw()沒有任何與時間相關。您需要跟蹤上一次遊戲迭代更新所花費的時間(以毫秒爲單位),並等待它小於預先設定的遊戲速度的時間間隔。

還有一個遞歸調用go()方法的問題,最終很可能會耗盡堆棧內存。實施它的方式,作爲重新啓動遊戲的一種方式,將允許重新啓動,並且您可能永遠不會注意到它,但這不是一個好設計。你應該有一個主要的遊戲循環,當遊戲結束或贏取時退出,然後當玩家按下開始按鈕之類的按鈕時由GUI重新開始。

相關問題