标题:java贪吃蛇问题求助
只看楼主
b465513006
Rank: 2
等 级:论坛游民
威 望:1
帖 子:70
专家分:48
注 册:2011-3-18
结帖率:73.33%
已结贴  问题点数:20 回复次数:4 
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 贪吃蛇 教程 
2011-11-07 00:56
b465513006
Rank: 2
等 级:论坛游民
威 望:1
帖 子:70
专家分:48
注 册:2011-3-18
得分:0 
没人帮忙看一下嘛
2011-11-07 13:44
baifenghan
Rank: 8Rank: 8
等 级:贵宾
威 望:10
帖 子:258
专家分:952
注 册:2006-3-17
得分:20 
建议两点:
1.首先定义一个蛇的类,用于描述蛇,一边准确的画出蛇。
2.将蛇在的上一个位置用背景色重新绘制就可以了。
2011-11-07 23:44
b465513006
Rank: 2
等 级:论坛游民
威 望:1
帖 子:70
专家分:48
注 册:2011-3-18
得分:0 
回复 3楼 baifenghan
我是初学者,能告诉我怎么重新绘制吗?需要加入什么代码
2011-11-08 01:23
linMop
Rank: 1
等 级:新手上路
帖 子:6
专家分:7
注 册:2011-12-9
得分:0 
在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);
             }
         }
     }
 
2011-12-11 22:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-354476-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.107159 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved