gpt4 book ai didi

java - 如何让俄罗斯方 block 每秒下落(错误)

转载 作者:行者123 更新时间:2023-12-01 18:09:44 24 4
gpt4 key购买 nike

初级 Java 开发人员。尝试制作一个俄罗斯方 block 小程序作为我个人项目的一部分。

我现在可以在屏幕上绘制俄罗斯方 block ,但我无法让它每秒垂直向下。

代码:

 public class InitialScreen extends JApplet implements ActionListener {
public JPanel cards = new JPanel();
private JPanel introPanel = new JPanel();
public CardLayout c1 = new CardLayout();

public void init() {
initiateIntroScreen();
//game();
add(cards, BorderLayout.NORTH);
setSize(500, 100);
}



private void initiateIntroScreen() {
Frame title = (Frame)this.getParent().getParent();


cards.setLayout(c1);

JLabel centralWords = new JLabel("Click the following button options: 'Play' or 'Instructions'.");
JButton playBtn = new JButton("Play!");
JButton instructionsBtn = new JButton("Instructions!");

introPanel.add(centralWords);
introPanel.add(playBtn);
introPanel.add(instructionsBtn);
cards.add(introPanel,"1");

playBtn.addActionListener(this);
playBtn.addActionListener(new MainGame(cards,c1));


}

@Override
public void actionPerformed(ActionEvent e) {
setSize(300,410);
getContentPane().setBackground(Color.BLACK);
}

这就是 JApplet 的初始屏幕。有两个按钮。当您按下“播放”按钮时,它将进入主游戏屏幕。

public class MainGame extends JApplet implements ActionListener {
private JPanel cards;
private CardLayout c1;
private JPanel gamePanel = new JPanel();

public MainGame(JPanel cards, CardLayout c1) {
this.c1 = c1;
this.cards = cards;
gamePanel.add(new Tetris_Block(new int[10][20]));
}


@Override
public void actionPerformed(ActionEvent e) {
JLabel scoreLbl = new JLabel("Score:");
gamePanel.add(scoreLbl);
cards.add(gamePanel,"game");
c1.show(cards,"game");

}

这是玩俄罗斯方 block 的游戏画面。在构造函数中,它调用俄罗斯方 block block 。

public class Tetris_Block extends JComponent implements ActionListener {
static Color[] colors =
{darkGray, green, blue, red,
yellow, magenta, pink, cyan};
int[][] a;
int w, h;
static int horizontalPos, verticalPos = 0;
static int size = 20;
private int verticalPos1 = 1;

public Tetris_Block(int[][] a) {
this.a = a;
w = a.length;
h = a[0].length;
square_Block();
startTimer();
}

private void nextMove() {
verticalPos++;
verticalPos1++;
}

public void square_Block(){ //Horizontal || Vertical || Colour

//Horizontal never changes for this as I just want the blocks to go down.
a[0][verticalPos] = 3;
a[0][verticalPos1] = 3;
a[1][verticalPos] = 3;
a[1][verticalPos1] = 3;
}


@Override
public void actionPerformed(ActionEvent e) {
nextMove();
square_Block();
System.out.println(verticalPos);
}


public void startTimer(){
Timer timer = new Timer(1000,this);
timer.start();
}


public void paintComponent(Graphics g) {
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
g.setColor(colors[a[i][j]]);
g.fill3DRect(i * size, j * size,
size, size, true);

}
}
}


public Dimension getPreferredSize() {
return new Dimension(w * size, h * size);
}

我的目标是使垂直位置每秒增加 1(因此它以秒为间隔向下移动窗口。

我不认为计时器功能有问题。当我打印verticalPos时,它每秒打印出递增的值,但是它只是在屏幕上显示新位置 - 这就是问题所在。

现在的窗口图像。

[img] /image/sHQVA.png

最佳答案

首先在 Tetris_BlockactionPerformed 方法中添加对 repaint 的调用

@Override
public void actionPerformed(ActionEvent e) {
nextMove();
square_Block();
System.out.println(verticalPos);
// This is important
repaint();
}

这将在事件队列上安排一个绘制事件,最终将调用您的 paintComponent 方法(间接)

这将使方 block 开始移动。您将遇到的下一个问题是您实际上并没有从之前的位置“移除” block ,因此它会继续在屏幕上流血/生长

Bleeding blocks

您可以通过将 block 的颜色传递给 square_Block 来解决这个问题,例如......

public void square_Block(int color) { //Horizontal || Vertical || Colour

//Horizontal never changes for this as I just want the blocks to go down.
a[0][verticalPos] = color;
a[0][verticalPos1] = color;
a[1][verticalPos] = color;
a[1][verticalPos1] = color;
}

然后“休息”当前位置的 block ,更新位置然后设置新的 block 颜色;

@Override
public void actionPerformed(ActionEvent e) {
square_Block(0);
nextMove();
square_Block(3);
System.out.println(verticalPos);
repaint();
}

Falling

关于java - 如何让俄罗斯方 block 每秒下落(错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33926848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com