当前位置: 代码迷 >> Java相关 >> java贪吃蛇问题求助
  详细解决方案

java贪吃蛇问题求助

热度:553   发布时间:2011-11-07 00:56:38.0
java贪吃蛇问题求助
看教程写了个不完整的贪吃蛇程序,可是除了点问题找不到哪里错了,请高手帮忙看下,蛇走过的痕迹不会抹去。。。。。
程序代码:
第一个class 是snakeWin
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class snakeWin extends JPanel implements ActionListener,KeyListener{
    JButton newGame , stopGame;
    int point=0 , Speed=0;
    boolean Start = false;
    Random r = new Random();
    int rx = 0 , ry = 0 ;
    List<snakeAct> list = new ArrayList<snakeAct>();
    public snakeWin() {
        newGame = new JButton("开始");
        stopGame = new JButton("退出");
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        newGame.addActionListener(this);
        stopGame.addActionListener(this);
        this.addKeyListener(this);
        this.add(newGame);
        this.add(stopGame);
    }
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawRect(10 , 40 , 400 , 300);
        g.drawString("分数: " +point, 150, 25);
        g.drawString("速度: " +Speed, 220, 25);
        g.setColor(new Color(0,255,0));
        if(Start){
            g.fillRect(10+rx*10, 40+ry*10, 10, 10);
            g.setColor(new Color(255,0,0));
            for(int i=0 ; i<list.size(); i++){
                g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10);
            }
        }
    }
   

    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==newGame){
            newGame.setEnabled(false);
            Start = true;
            rx = r.nextInt(40);ry = r.nextInt(30);
            snakeAct tempAct = new snakeAct();
            tempAct.setX(20);
            tempAct.setY(15);
            list.add(tempAct);
            requestFocus(true);
            repaint();
        }
        if (e.getSource()==stopGame){
            System.exit(0);
        }
    }
    private void eat(){
        if(rx==list.get(0).getX()&&ry==list.get(0).getY()) {
            rx = r.nextInt(40);ry = r.nextInt(30);
            snakeAct tempAct = new snakeAct();
            tempAct.setX(list.get(list.size()-1).getX());
            tempAct.setY(list.get(list.size()-1).getY());
            list.add(tempAct);
        }
    }
    public void otherMove(){
        snakeAct tempAct = new snakeAct();
        for(int i=0; i<list.size(); i++){
            if(i==1){
                list.get(i).setX(list.get(0).getX());
                list.get(i).setY(list.get(0).getY());
            }else if(i>1){
                tempAct = list.get(i-1);
                list.set(i-1, list.get(i));
                list.set(i, tempAct);
            }
        }
    }
   
    public void move(int x , int y){
        if (minYes(x,y)){
            list.get(0).setX(list.get(0).getX()+x);
            list.get(0).setY(list.get(0).getY()+y);
            eat();
            repaint();
        }else {
           
        }
    }
    public boolean minYes(int x , int y){
        if(!maxYes(list.get(0).getX()+x,list.get(0).getY()+y))
            return false;
        return true;
    }
    public boolean maxYes(int x , int y){
        if(x<0||x>=40||y<0||y>=30) {
            return false;
        }
        return true;
    }
    public void keyPressed(KeyEvent e) {
        if (Start){
            switch (e.getKeyCode()){
            case KeyEvent.VK_UP:
                move(0 , -1);
                break;
            case KeyEvent.VK_DOWN:
                move(0 , 1);
                break;
            case KeyEvent.VK_LEFT:
                move(-1 , 0);
                break;
            case KeyEvent.VK_RIGHT:
                move(1 , 0);
                break;
            default:
                break;
            }
        }
    }
    @Override
    public void keyReleased(KeyEvent e) {
   
    }

    public void keyTyped(KeyEvent e) {
      
    }
   
}

   
    第二个class是snakeMain
package game;
import javax.swing.*;
public class snakeMain extends JFrame {
    public snakeMain() {
        snakeWin win = new snakeWin();
        add(win);
        setTitle("贪吃蛇1.0V~~~杨扬鹏作品");
        setSize(435,390);
        setLocation(200,200);
        setVisible(true);
    }
    public static void main(String args[]){
        new snakeMain();
    }
}第三个class是snakeAct
package game;
public class snakeAct {
    private int x;
    private int y;
    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;
    }
   
}
三个class放在game包里面,下面是问题的截图:
走过得痕迹不会消失
搜索更多相关的解决方案: java  game  贪吃蛇  教程  

----------------解决方案--------------------------------------------------------
没人帮忙看一下嘛
----------------解决方案--------------------------------------------------------
建议两点:
1.首先定义一个蛇的类,用于描述蛇,一边准确的画出蛇。
2.将蛇在的上一个位置用背景色重新绘制就可以了。
----------------解决方案--------------------------------------------------------
回复 3楼 baifenghan
我是初学者,能告诉我怎么重新绘制吗?需要加入什么代码
----------------解决方案--------------------------------------------------------
在public void paintComponent(Graphics g) {
         super.paintComponents(g);
         g.drawRect(10 , 40 , 400 , 300);
         g.drawString("分数: " +point, 150, 25);
         g.drawString("速度: " +Speed, 220, 25);
g.clearRect(0, 0, getWidth(), getHeight());//加这个试一下

         g.setColor(new Color(0,255,0));
         if(Start){
             g.fillRect(10+rx*10, 40+ry*10, 10, 10);
             g.setColor(new Color(255,0,0));
             for(int i=0 ; i<list.size(); i++){
                 g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10);
             }
         }
     }


----------------解决方案--------------------------------------------------------
  相关解决方案