第一个类
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
}
添加回答
举报
0/150
提交
取消