Java Swing中的蛇游戏 - 我的蛇只会生长



我正在尝试进行经典的蛇游戏,我设法使蛇移动,但是当它移动时,它变得无限长,因为它永远不会擦除尾巴。

我一直在使用 validate()repaint(),但没有任何作用。

问题

这是我的代码:

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class controls extends JPanel implements KeyListener, ActionListener { // Amb aquestes dues implementacions, fem que el programa pugui rebre per teclat
// Mida serp
private int[] longXserp = new int[750];
private int[] longYserp = new int[750];
// Longitut inicial de la serp
private int serplong = 3;
// Moviments que fem
private int moviments = 0;
// Controls
private boolean esquerra = false;
private boolean dreta = false;
private boolean amunt = false;
private boolean avall = false;
// Gràfics de moviment
private ImageIcon serpesquerra;
private ImageIcon serpdreta;
private ImageIcon serpamunt;
private ImageIcon serpavall;
private Timer timer;
private int velocitatserp = 100;
private ImageIcon serp;
public controls() {
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    timer = new Timer(velocitatserp, this);
    timer.start();
}
public void paint(Graphics g) { // Mètode amb el que imprimim per pantalla. Ha d'anomenar-se "paint", o si no, no funcionarà
    if(moviments == 0) {
        longXserp[2] = 50;
        longXserp[1] = 75;
        longXserp[0] = 100;
        longYserp[2] = 100;
        longYserp[1] = 100;
        longYserp[0] = 100;
    }

    serpdreta = new ImageIcon("src/grafics/serpdreta.png");
    serpdreta.paintIcon(this, g, longXserp[0], longYserp[0]);
    for(int a = 0; a < serplong; a++) {
        if(a == 0 && esquerra) {
            serpesquerra = new ImageIcon("src/grafics/serpesquerra.png");
            serpesquerra.paintIcon(this, g, longXserp[a], longYserp[a]);
        }
        if(a == 0 && dreta) {
            serpdreta = new ImageIcon("src/grafics/serpdreta.png");
            serpdreta.paintIcon(this, g, longXserp[a], longYserp[a]);
        }
        if(a == 0 && amunt) {
            serpamunt = new ImageIcon("src/grafics/serpamunt.png");
            serpamunt.paintIcon(this, g, longXserp[a], longYserp[a]);
        }
        if(a == 0 && avall) {
            serpavall = new ImageIcon("src/grafics/serpavall.png");
            serpavall.paintIcon(this, g, longXserp[a], longYserp[a]);
        }
        if(a != 0) {
            serp = new ImageIcon("src/grafics/serp.png");
            serp.paintIcon(this, g, longXserp[a], longYserp[a]);
        }
    }
    g.dispose();
}

@Override
public void keyPressed(KeyEvent ke) {
    if(ke.getKeyCode() == KeyEvent.VK_RIGHT){ // Si polses la tecla X, la seva variable boolean es posa en true
        moviments++;
        dreta = true;
        if(!esquerra) {
            dreta = true;
        }
        else {
            dreta = false;
            esquerra = true;
        }
        amunt = false;
        avall = false;
    }
    if(ke.getKeyCode() == KeyEvent.VK_LEFT){
        moviments++;
        esquerra = true;
        if(!dreta) {
            esquerra = true;
        }
        else {
            esquerra = false;
            dreta = true;
        }
        amunt = false;
        avall = false;
    }
    if(ke.getKeyCode() == KeyEvent.VK_UP){
        moviments++;
        amunt = true;
        if(!avall) {
            amunt = true;
        }
        else {
            amunt = false;
            avall = true;
        }
        esquerra = false;
        dreta = false;
    }
    if(ke.getKeyCode() == KeyEvent.VK_DOWN){
        moviments++;
        avall = true;
        if(!amunt) {
            avall = true;
        }
        else {
            amunt = true;
            avall = false;
        }
        esquerra = false;
        dreta = false;
    }
}
@Override
public void keyReleased(KeyEvent ke) {
    // TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent ke) {
    // TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent ae) {
    timer.start();
    if(dreta) {
        for(int d = serplong-1; d>=0; d--) {
            longYserp[d+1] = longYserp[d];
        }
        for(int e = serplong; e>= 0; e--) {
            if(e==0) {
                longXserp[e] = longXserp[e] + 25;
            }
            else {
                longXserp[e] = longXserp[e-1];
            }
            if(longXserp[e] > 850) {
                longXserp[e] = 25;
            }
        }
        repaint(); // Mètode per refrescar els gràfics tornant a cridar al mètode paint(). Es usa quan es realitzan canvis sobre els gràfics
    }
    if(esquerra) {
        for(int d = serplong-1; d>=0; d--) {
            longYserp[d+1] = longYserp[d];
        }
        for(int d = serplong; d>= 0; d--) {
            if(d==0) {
                longXserp[d] = longXserp[d] - 25;
            }
            else {
                longXserp[d] = longXserp[d-1];
            }
            if(longXserp[d] < 25) {
                longXserp[d] = 850;
            }
        }
        repaint();
    }
    if(amunt) {
        for(int d = serplong-1; d>=0; d--) {
            longXserp[d+1] = longXserp[d];
        }
        for(int d = serplong; d>= 0; d--) {
            if(d==0) {
                longYserp[d] = longYserp[d] - 25;
            }
            else {
                longYserp[d] = longYserp[d-1];
            }
            if(longYserp[d] < 75) {
                longYserp[d] = 625;
            }
        }
        repaint();
    }
    if(avall) {
        for(int d = serplong-1; d>=0; d--) {
            longXserp[d+1] = longXserp[d];
        }
        for(int d = serplong; d>= 0; d--) {
            if(d==0) {
                longYserp[d] = longYserp[d] + 25;
            }
            else {
                longYserp[d] = longYserp[d-1];
            }
            if(longYserp[d] > 625) {
                longYserp[d] = 75;
            }
        }
        repaint();
    }
}   
}

我一直在试图弄清楚几天的问题。上述代码有什么问题?

public void paint(Graphics g) { ..

这应该是:

public void paintComponent(Graphics g) { 
    super.paintComponent(g); ..

注意两个重要区别:

  1. 覆盖paintComponent的任何类别或扩展JComponent的类别。
  2. 自定义绘画删除先前的图纸时,请调用超级方法。

其他提示现在更仔细地查看代码:

  1. serpdreta = new ImageIcon("src/grafics/serpdreta.png");不应以油漆方法进行这种类型的资源加载,预计将在最少的时间内完成。取而代
  2. 但是,当应用程序时,该路径可能会失败。变成一个罐子。src目录通常不包括。
  3. 应用程序资源将在部署时成为嵌入式资源,因此明智的做法是开始访问它们,就像现在一样。必须通过URL而不是文件访问嵌入式资源。查看信息。嵌入式资源的页面,用于如何形成URL。
  4. 如果应用程序。需要绘画图像,创建Image而不是ImageIcon
  5. 要在加载图像时更好的反馈,请使用ImageIO.read(..)而不是创建ImageIcon,因为它将默默失败,而ImageIO将提供有关任何问题的有用信息。
  6. 对于挥杆,我们通常使用键绑定,而不是较低级别的KeyListener

相关内容

  • 没有找到相关文章

最新更新