2 xyxxzsky xyxxzsky 于 2015.06.03 12:10 提问

求救啊,java贪吃蛇问题(要达到的效果是出现一个蓝色格子在动)

java贪吃蛇问题(要达到的效果是出现一个蓝色格子在动)
第一个类

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;

public class Snake {
Node head = null;
Node tail = null;
int size = 0;
Dir dir = Dir.D;
Node n = new Node(30, 30, Dir.D);
public Snake(){
head = n;
tail = n;
size = 1;
}

class Node{
    int row, col;
    Dir dir;
    Node next = null;
    Node prev = null;
    public static final int nodeW = SnakeMain.BLOCK;
    public static final int nodeH = SnakeMain.BLOCK;
    Node(int row, int col, Dir dir ){
        this.row = row;
        this.col = col;
        this.dir = dir;
    }    

    void draw(Graphics g){
        Color c = g.getColor();
        g.setColor(Color.BLUE);
        g.fillRect(SnakeMain.BLOCK * col, SnakeMain.BLOCK * row, nodeW, nodeH);
        g.setColor(c);    
    }
}

public void draw(Graphics g){
    if(size <= 0) return;

    for(Node m = head; m != null; m = m.next){
        m.draw(g);
    }
    move();
}

private void move() {
    addToHead();
    deleteFromTail();


}

private void deleteFromTail() {
    if(size == 0) return;
    tail = tail.prev;
    tail.next = null;
}
public void addToTail(){
    Node node = null;
    switch(tail.dir){
    case L:
        node = new Node(tail.row, tail.col + 1, tail.dir);
        break;
    case R:
        node = new Node(tail.row, tail.col - 1, tail.dir);
        break;
    case U:
        node = new Node(tail.row + 1, tail.col, tail.dir);
        break;
    case D:
        node = new Node(tail.row - 1, tail.col, tail.dir);
        break;
    }
    tail.next = node;
    node.prev =  tail;
    tail = node;
    size++;
}

public void addToHead(){
    Node node = null;
    switch(head.dir){
    case L:
        node = new Node(head.row, head.col - 1, head.dir);
        break;
    case R:
        node = new Node(head.row, head.col + 1, head.dir);
        break;
    case U:
        node = new Node(head.row - 1, head.col, head.dir);
        break;
    case D:
        node = new Node(head.row + 1, head.col, head.dir);
        break;
    }
    node.next = head;
    head.prev = node;
    head = node;
    size++;
}

public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();
    switch(key){
    case KeyEvent.VK_UP:
        head.dir  = dir.U;
        break;
    case KeyEvent.VK_DOWN:
        head.dir = dir.D;
        break;
    case KeyEvent.VK_LEFT:
        head.dir = dir.L;
        break;
    case KeyEvent.VK_RIGHT:
        head.dir = dir.R;
        break;
    }   
 }

}

第二个类
import java.awt.*;
import java.awt.event.*;

public class SnakeMain extends Frame {
public static final int BLOCK = 10;
public static final int ROW = 60;
public static final int COLS = 60;
Snake sn = new Snake();
Image offScreenImage = null;
public static void main(String[] args) {
SnakeMain sm = new SnakeMain();
sm.launch();
}

public void launch(){
    setSize(BLOCK * ROW, BLOCK * COLS);
    setBackground(Color.LIGHT_GRAY);
    setResizable(false);
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    });
    addKeyListener(new KeyMonitor());
    setVisible(true);
    new Thread(new PaintThread()).start();
}

public void paint(Graphics g){
    Color c = g.getColor();
    g.setColor(Color.YELLOW);
    for(int i = 1; i < ROW; ++i){
        g.drawLine(0, i * BLOCK, BLOCK * ROW, i * BLOCK);
    }
    for(int j = 1; j < COLS; ++j){
        g.drawLine(j * BLOCK, 0, j * BLOCK, BLOCK * COLS);
    }
    g.setColor(c);
    sn.draw(g);
}

public void update (Graphics g){
    if(offScreenImage == null) 
        offScreenImage = this.createImage(BLOCK * ROW, BLOCK * COLS);
    Graphics gr = offScreenImage.getGraphics();
    paint(gr);
    g.drawImage(offScreenImage, 0, 0, null);
}

public class PaintThread implements Runnable{

    public void run() {
        while(true){
            repaint(); 
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

public class KeyMonitor extends KeyAdapter{

    @Override
    public void keyPressed(KeyEvent e) {
        sn.keyPressed(e);
    }

}

}

第三个类

public enum Dir {
D, U, L, R
}

3个回答

jiangs111
jiangs111   2015.06.04 15:04

你需要补充的第三个类?具体点的?具体点的?

qq_26788389
qq_26788389 回复xyxxzsky: 我也遇到了这个问题,请问你解决了吗?
2 年多之前 回复
xyxxzsky
xyxxzsky 这是我写贪吃蛇的第一个步骤,不用补充,就是想让以上代码运行起来实现 !只有一个格子! 能被我控制。(但是我运行起来格子只增不减),求大神拯救。
2 年多之前 回复
jiangs111
jiangs111   2015.06.04 15:04

你需要补充的第三个类?具体点的?具体点的?

datou431
datou431   Rxr 2015.06.18 15:30

需要补充的第三个类。。。

Csdn user default icon
上传中...
上传图片
插入图片