嗨,我的程序类似于俄罗斯方块游戏,它试图创建每隔几秒钟向上浮动的新“形状”。
我的目标是创建许多将继续向上浮动的新对象。我让我的代码工作的唯一方法是预先定义一些“行”并让它们在到达顶部后循环回到页面底部。但我不想这样做。
我的目标是制作一个俄罗斯方块游戏,其中无限数量的终端将从天而降。也许我可以预先定义最大数量的终端并将它们存储在数据结构中并从中随机选择..但我想知道是否还有另一种方法。
public class Board {
private int x1, x2, y1, y2;
private Line line;
private Timer timer;
private long second;
private Line[] lines = { new Line(0, 500, 500, 500),
new Line(0, 600, 500, 600), new Line(0, 700, 500, 700),
new Line(0, 800, 500, 800), new Line(0, 900, 500, 900) };
private Dude dude;
public Board() {
init();
}
public void init() {
dude = new Dude(230, 10);
line = new Line(0, 400, 500, 400);
final JPanel board = new MyBoard();
final JFrame frame = new JFrame();
frame.add(board);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
second = 1;
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
second++;
System.out.println(second);
line.moveD();
frame.repaint();
if (line.isTop()) {
line.setXY(0, 400, 500, 400);
}
// board.repaint();
for (int i = 0; i < lines.length; i++) {
if (lines[i].isTop()) {
lines[i].setXY(0, 500, 500, 500);
}
lines[i].moveD();
}
int i = 0;
dude.twitch();
}
});
timer.start();
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
System.out.println("r");
dude.moveR();
frame.repaint();
// Right arrow key code
} else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("l");
dude.moveL();
frame.repaint();
// Left arrow key code
} else if (e.getKeyCode() == KeyEvent.VK_UP) {
System.out.println("u");
dude.moveU();
frame.repaint();
// Up arrow key code
} else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
// Down arrow key code
System.out.println("d");
dude.moveD();
frame.repaint();
}
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
});
}
class MyBoard extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
dude.draw(g);
line.draw(g);
g.drawLine(x1, y1, x2, y2);
* for(int i=0;i<lines.length;i++){ lines[i].draw(g); }
g.setColor(Color.gray);
g.fillOval(dude.getX(), dude.getY(), 10, 10);
}
}
}
package Shapes;
import java.awt.Graphics;
public class Line {
private int x1, y1, x2, y2;
public Line(int initx1, int inity1, int initx2, int inity2) {
x1 = initx1;
y1 = inity1;
x2 = initx2;
y2 = inity2;
}
public void draw(Graphics g) {
int xone = x1;
int xtwo = x2;
int yone = y1;
int ytwo = y2;
g.drawLine(xone, yone, xtwo, ytwo);
}
public boolean isTop() {
return y1 == 0;
}
public void setXY(int initx1, int inity1, int initx2, int inity2) {
x1 = initx1;
y1 = inity1;
x2 = initx2;
y2 = inity2;
}
public void moveD() {
if (y1 != -10) {
y1 += -10;
y2 += -10;
}
}
}
本质上,我想制作这个网站所做的大量图形对象。 http://mangaarun.blogspot.com/2011/12/creating-paint-brush-application-in.html 我省略了“Dude”是什么,因为它只是一个人的图画。