单翡
2019-07-31 21:41
采纳率: 100%
浏览 508
已采纳

本人写的贪吃蛇代码,求教大神哪里错了?

本人初学JAVA,写了一个简易的贪吃蛇,运行的时候有时候吃到豆子能变长,有时候吃豆子不能变长不知道为什么,代码如下

Snake类:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.*;

public class Snake extends JPanel implements KeyListener{
    private int direction=South;

    ArrayList<Integer> Bufferbody=new ArrayList<Integer>();
    ArrayList<Integer> body=new ArrayList<Integer>();
    int foodX=300;
    int foodY=300;
    int headX=100;
    int headY=100;
    int souse=0;
    int O=0;
    public static final int South=0;
    public static final int North=1;
    public static final int East=2;
    public static final int West=3;
    Random rand=new Random();
    private Image iBuffer;
    private Graphics gBuffer;
    public void setBufferbody() {
        Bufferbody.add(0,100);
        Bufferbody.add(1,100);
    }
    @Override
    public void paint(Graphics g) {
        // TODO Auto-generated method stub
        super.paint(g);
        setBackground(Color.gray);
        g.setColor(Color.BLUE);
        g.fillRect(600, 0, 100, 600);
        Font font=new Font("微软雅黑",Font.BOLD,30);
        g.setFont(font);
        g.setColor(Color.BLACK);
        g.drawString("分数", 612, 100);
        if(souse==0)
        g.drawString(""+souse, 630, 150);
        if(souse>0&&souse<=90)
        g.drawString(""+souse, 623, 150);
        if(souse>90)
        g.drawString(""+souse, 612, 150);
        g.fillRect(headX, headY,10 ,10 );
        while(foodX==headX&&foodY==headY) {
            foodX=rand.nextInt(60)*10;
            foodY=rand.nextInt(56)*10;
        }
        g.setColor(Color.YELLOW);
        g.fillRect(foodX, foodY, 10, 10);
        if(O==0) {
            g.setColor(Color.red);
            g.drawString("按A开始游戏", 200, 300);
        }
        for(int i=0;i<body.size();i++) {
            Bufferbody.set(i+2,body.get(i));
        }
        for(int i=0;i<body.size();i++) {
            body.set(i,Bufferbody.get(i));
        }
        for(int i=0;i<body.size();i=i+2) {
            g.setColor(Color.green);
            g.fillRect(body.get(i), body.get(i+1), 10, 10);
        }
        if(O==-1) {
            g.setColor(Color.red);
            g.drawString("Game Over",200, 200);
            g.drawString("按R重新游戏", 200, 300);
        }
    }
    public void update(Graphics g) {
        if(iBuffer==null) {
            iBuffer=createImage(700,600);
            gBuffer=getGraphics();
        }
        gBuffer.setColor(getBackground());
        gBuffer.fillRect(0, 0, 700, 600);
        paint(gBuffer);
        g.drawImage(iBuffer, 0, 0, this);
    }
    public void move() {
        new Thread() {
            public void run() {
                while(true) {
                    System.out.print("");
                if(O==1) {
                    switch(direction) {
                    case South:
                        Bufferbody.set(0,headX);
                        Bufferbody.set(1,headY);
                        headY=headY+10;
                        repaint();
                        break;
                    case North:
                        Bufferbody.set(0,headX);
                        Bufferbody.set(1,headY);                                                                    
                        headY=headY-10;
                        repaint();
                        break;
                    case East:
                        Bufferbody.set(0,headX);
                        Bufferbody.set(1,headY);
                        headX=headX+10;
                        repaint();
                        break;
                    case West:
                        Bufferbody.set(0,headX);
                        Bufferbody.set(1,headY);                        
                        headX=headX-10;
                        repaint();
                        break;
                    }
                if(headX==foodX&&headY==foodY) {
                    Bufferbody.add(0);  
                    Bufferbody.add(0);  
                    body.add(Bufferbody.get(Bufferbody.size()-2));
                    body.add(Bufferbody.get(Bufferbody.size()-1));
                    souse=souse+10;
                    repaint();
                }
                if(headX<0||headY<0||headX==600||headY==560) {
                    O=-1;
                    repaint();
                    break;
                }
                for(int i=0;i<body.size();i=i+2) {
                    if(headX==body.get(i)&&headY==body.get(i+1)) {
                        O=-1;
                        repaint();
                        break;
                    }
                }
                }
                try {
                Thread.sleep(140);
                } catch (InterruptedException e) {
                    // TODO: handle exception
                }
                }
            }
        }.start();
    }
    @Override
    public void keyPressed(KeyEvent a) {
        // TODO Auto-generated method stub
        if(a.getKeyCode()==65&&O==0) {
            O=1;
            repaint();
            }
        if(O==1) {
        switch(a.getKeyCode()) {
        case KeyEvent.VK_UP:
            if(direction==East||direction==West)
            direction=North;
            break;
        case KeyEvent.VK_RIGHT:
            if(direction==South||direction==North)
            direction=East;
            break;
        case KeyEvent.VK_DOWN:
            if(direction==East||direction==West)
            direction=South;
            break;
        case KeyEvent.VK_LEFT:
            if(direction==South||direction==North)
            direction=West;
            break;
    }
        }
        if(a.getKeyCode()==82&&O==-1) {
            direction=South;
            foodX=300;
            foodY=300;
            headX=100;
            headY=100;
            souse=0;
            Bufferbody.clear();
            body.clear();
            setBufferbody();
            O=0;
            move();
            repaint();
        }
        if(a.getKeyCode()==32&&O==1) {
            O=0;
            repaint();
            }
        if(a.getKeyCode()==KeyEvent.VK_ESCAPE) {
        System.exit(0);
    }
        }
    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub

    }
}

World类:

import javax.swing.*;

public class World extends JFrame{
public static void main(String[] args) {
JFrame f=new JFrame();
Snake s=new Snake();
f.setSize(700,600);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(3);
f.setTitle("Snake");
f.add(s);
s.setBufferbody();
s.move();
f.addKeyListener(s);
f.setVisible(true);
}
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • 毕小宝 2019-08-01 09:19
    已采纳

    我测了两遍,看到的蛇是变长了的,能否改进一下,给蛇身画上竖线以便确定是否变长了。

    打赏 评论

相关推荐 更多相似问题