我正在尝试创建一个蛇游戏的版本,但它无法正常工作。它现在正在做的是显示一个没有paintComponent
部分中任何组件的黑屏。有时它会在一段时间后显示我的“游戏结束”消息,所以有些东西正在工作但蛇和食物没有显示。
我已经尝试了几个小时来查找任何逻辑错误,但一直无法找到。如果有人能指导我做错了什么,我将非常感激。
下面是我设置电路板的代码,下面是我运行程序的主要类。我把整个代码放在你想要在你自己的设备上运行的情况下,但重要的代码片段围绕它们有两颗星。任何建议或意见将不胜感激。
public class Board2 extends JPanel implements ActionListener{
private int bWidth = 500;
private int bHeight = 500;
private int segmentSize = 10;
private int bodyMax = 2500;
private int randomPos = 29;
private int delay = 140;
private int x[] = new int[bodyMax];
private int y[] = new int[bodyMax];
private int bodySize, foodX, foodY;
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
private boolean inGame = true;
private Timer timer;
private Image head;
private Image food;
private Image body;
Board2(){
addKeyListener(new changeDirection());
setBackground(Color.BLACK);
setPreferredSize(new Dimension(bWidth, bHeight));
loadImages();
init();
}
**private void loadImages(){
ImageIcon headImage = new ImageIcon("Square-Freemason-Symbols-300x300.png");
head = headImage.getImage();
ImageIcon foodImage = new ImageIcon("Square-Freemason-Symbols-300x300.png");
food = foodImage.getImage();
ImageIcon bodyImage = new ImageIcon("Flat1.jpg");
body = bodyImage.getImage();
}**
private void init(){
bodySize = 3;
for(int z = 0; z < bodySize; z++){
x[z] = 50 - z * 10;
y[z] = 50;
}
findFood();
timer = new Timer(delay, this);
timer.start();
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
doDrawing(g);
}
**private void doDrawing(Graphics g){
if(inGame){
g.drawImage(food, foodX, foodY, this);
for(int z = 0; z < bodySize; z++){
if(z == 0){
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(body, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}**
private void gameOver(Graphics g){
String msg = "Game Over";
Font small = new Font("Helvetiva", Font.BOLD, 14);
FontMetrics metr = getFontMetrics(small);
g.setColor(Color.WHITE);
g.setFont(small);
g.drawString(msg, (bWidth - metr.stringWidth(msg)/2), bHeight/2);
}
private void findFood(){
int r = (int) Math.random() * randomPos;
foodX = r * segmentSize;
int s = (int) Math.random() * randomPos;
foodY = s * segmentSize;
}
private void checkFood(){
if(x[0] == foodX && y[0] == foodY){
bodySize += 3;
findFood();
}
}
private void checkCollision(){
for(int z = bodySize; z > 0; z--){
if(z > 4 && x[0] == x[z] && y[0] == y[z]){
inGame = false;
}
}
if(x[0] > bWidth){
inGame = false;
}
if(x[0] < 0){
inGame = false;
}
if(y[0] >bHeight){
inGame = false;
}
if(y[0] < 0){
inGame = false;
}
}
private void Move(){
for(int z = bodySize; z > 0; z--){
x[z] = x[z - 1];
y[z] = y[z - 1];
}
if(leftDirection){
x[0] -= segmentSize;
}
if(rightDirection){
x[0] += segmentSize;
}
if(upDirection){
y[0] -= segmentSize;
}
if(downDirection){
y[0] += segmentSize;
}
}
public void actionPerformed(ActionEvent e){
if(inGame){
checkFood();
checkCollision();
Move();
}
}
public class changeDirection extends KeyAdapter{
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT && !rightDirection){
leftDirection = true;
upDirection = false;
downDirection = false;
}
if(key == KeyEvent.VK_RIGHT && !leftDirection){
rightDirection = true;
upDirection = false;
downDirection = false;
}
if(key == KeyEvent.VK_UP && !downDirection){
upDirection = true;
leftDirection = false;
rightDirection = false;
}
if(key == KeyEvent.VK_DOWN && !upDirection){
downDirection = true;
leftDirection = false;
rightDirection = false;
}
}
}
}
这是主要课程:
public class SnakeV2 extends JFrame{
SnakeV2(){
add(new Board2());
setResizable(false);
pack();
setTitle("SnakeV2");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
@Override
public void run(){
JFrame ex = new SnakeV2();
ex.setVisible(true);
}
});
}
}