我想做一个类似于http://www.i-am-bored.com/bored_link.cfm?link_id=24495的游戏,但到目前为止我一直没有成功。我想我应该使用 int[] 作为块的 x 和 y 位置......但我不确定如何轻松做到这一点。
这是我的代码(我知道它有点乱)。Panel 是 JPanel,Block 是左右移动的块,DeadBlock 是静止的并且已经放置的块。
游戏很简单,但我很难放置死块。
package com.valgriz.game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Panel extends JPanel implements Runnable, KeyListener {
private Thread thread;
private final int ARRAY_NUMS = 999;
private int sleepTime = 200;
private int rights = 5;
private int lefts = 0;
private int dbCurrent;
private int drawThisMany;
private boolean isSpaced;
private Block block;
private DeadBlock[] dBlock = new DeadBlock[ARRAY_NUMS];
private int[] dbX = new int[ARRAY_NUMS];
private int[] dbY = new int[ARRAY_NUMS];
public Panel() {
addKeyListener(this);
setDoubleBuffered(true);
setFocusable(true);
setBackground(Color.black);
block = new Block();
drawThisMany = 0;
drawThisMany += rights;
for (int i = 0; i < dBlock.length; i++) {
dBlock[i] = new DeadBlock();
}
dbCurrent = 0;
// Thread
thread = new Thread(this);
thread.start();
}
@Override
public void run() {
while (true) {
update();
try {
thread.sleep(sleepTime);
} catch (Exception e) {
}
}
}
public void update() {
block.update();
repaint();
}
public void paint(Graphics g) {
super.paint(g);
for (int i = lefts; i < rights; i++) {
int bX = block.getX();
if (bX == getWidth() - block.getDx() * rights) {
block.setDx(block.getDx() * -1);
}
if (bX == 0) {
block.setDx(block.getDx() * -1);
}
g.drawImage(block.getBlock(), i * block.getWidth() + block.getX(),
getHeight() - block.getDy() * block.getY(), this);
dbPaint(g);
}
// Toolkit
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
////// This code is useless
public void dbPaint(Graphics g) {
for (int i = 0; i < drawThisMany; i++) {
dbX[i] = block.getX() + i * block.getX();
dbY[i] = getHeight() - block.getY() * block.getWidth();
g.drawImage(dBlock[i].getImage(), dbX[i], dbY[i], this);
}
}
public void spaced() {
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE) {
spaced();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
package com.valgriz.game;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.net.URL;
import javax.swing.ImageIcon;
public class Block {
private ImageIcon bii;
private Image block;
private int x;
private int y;
private int dX;
private int dY;
private int pos;
private int width;
public Block() {
bii = new ImageIcon(this.getClass().getResource(
"/raw/images/ss_block.png"));
block = bii.getImage();
dX = getBlock().getWidth(null);
dY = getBlock().getHeight(null);
width = getBlock().getWidth(null);
x = 0;
y = 1;
}
public void update() {
x += dX;
}
public Image getBlock() {
return block;
}
public int getDx() {
return dX;
}
public void setDx(int dX) {
this.dX = dX;
}
public int getDy() {
return dY;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getWidth() {
return width;
}
}
package com.valgriz.game;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class DeadBlock {
private Block block;
private ImageIcon bii;
private Image dBlock;
private int x;
private int y;
public DeadBlock() {
bii = new ImageIcon(this.getClass().getResource(
"/raw/images/ss_block.png"));
dBlock = bii.getImage();
}
public void paint(Graphics g) {
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Image getImage() {
return dBlock;
}
}
什么是正确的方向,我怎样才能使这个代码工作?