xyxxzsky 2015-06-03 04:10 采纳率: 33.3%
浏览 1964

求救啊,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条回答 默认 最新

  • 408985552 2015-06-04 07:04
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的