墙的代码:import java.awt.*;
import java.awt.event.*;public class Yard extends Frame {
public static final int ROWS = 50;
public static final int COLS = 50;
public static final int BLOCK_SIZE = 10;
Image offScreenImage = null; //双缓冲
Snake s = new Snake();
public static void main(String args[]) {
new Yard().launch();
}
public void launch() {
this.setLocation(200,200);
this.setSize(COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE );
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start();
}
//双缓冲方法
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE);
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0,COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE);
g.setColor(Color.DARK_GRAY);
//画出横线
for(int i=0; i< ROWS; i++) {
g.drawLine(0, BLOCK_SIZE*i, COLS*BLOCK_SIZE, BLOCK_SIZE*i);
}
//画出竖线
for(int i =0; i<COLS; i++) {
g.drawLine(BLOCK_SIZE*i, 0, BLOCK_SIZE*i, ROWS*BLOCK_SIZE);
}
g.setColor(c);
s.draw(g);
}
public class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
s.keyPressed(e);
}
}
}蛇类:import java.awt.*;
import java.awt.event.KeyEvent;public class Snake {
private Node head = null;
private Node tail = null;
private int size = 0;
private Node n = new Node(20,30,Dir.L);
Snake() {
head = n;
tail = n;
size = 1;
}
public void addToTail() {
Node node = null;
switch(tail.dir) {
case L:
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 R:
node = new Node(tail.row,tail.col - 1,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(tail.dir) {
case L:
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 R:
node = new Node(tail.row,tail.col + 1,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 draw(Graphics g) {
if(size <= 0)
return;
for(Node n = head; n != null; n = n.next)
n.draw(g);
move();
}
private void move() {
addToHead();
deleteFromTail();
} private void deleteFromTail() {
if(size == 0)
return;
tail = tail.prev;
tail.next = null;
} private class Node {
int row;
int col;
int h = Yard.BLOCK_SIZE;
int w = Yard.BLOCK_SIZE;
Dir dir = Dir.L;
Node next = null;
Node prev = null; //前指针
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.GREEN);
g.fillRect(Yard.BLOCK_SIZE*row, Yard.BLOCK_SIZE*col, w, h);
g.setColor(c);
}
} public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT:
head.dir = Dir.L;
break;
case KeyEvent.VK_UP:
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT:
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN:
head.dir = Dir.D;
break;
}
}
}方向类:public enum Dir {
L,U,R,D;
}
import java.awt.event.*;public class Yard extends Frame {
public static final int ROWS = 50;
public static final int COLS = 50;
public static final int BLOCK_SIZE = 10;
Image offScreenImage = null; //双缓冲
Snake s = new Snake();
public static void main(String args[]) {
new Yard().launch();
}
public void launch() {
this.setLocation(200,200);
this.setSize(COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE );
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
this.addKeyListener(new KeyMonitor());
new Thread(new PaintThread()).start();
}
//双缓冲方法
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE);
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GRAY);
g.fillRect(0, 0,COLS * BLOCK_SIZE,ROWS * BLOCK_SIZE);
g.setColor(Color.DARK_GRAY);
//画出横线
for(int i=0; i< ROWS; i++) {
g.drawLine(0, BLOCK_SIZE*i, COLS*BLOCK_SIZE, BLOCK_SIZE*i);
}
//画出竖线
for(int i =0; i<COLS; i++) {
g.drawLine(BLOCK_SIZE*i, 0, BLOCK_SIZE*i, ROWS*BLOCK_SIZE);
}
g.setColor(c);
s.draw(g);
}
public class PaintThread implements Runnable {
public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private class KeyMonitor extends KeyAdapter {
public void keyPressed(KeyEvent e) {
s.keyPressed(e);
}
}
}蛇类:import java.awt.*;
import java.awt.event.KeyEvent;public class Snake {
private Node head = null;
private Node tail = null;
private int size = 0;
private Node n = new Node(20,30,Dir.L);
Snake() {
head = n;
tail = n;
size = 1;
}
public void addToTail() {
Node node = null;
switch(tail.dir) {
case L:
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 R:
node = new Node(tail.row,tail.col - 1,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(tail.dir) {
case L:
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 R:
node = new Node(tail.row,tail.col + 1,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 draw(Graphics g) {
if(size <= 0)
return;
for(Node n = head; n != null; n = n.next)
n.draw(g);
move();
}
private void move() {
addToHead();
deleteFromTail();
} private void deleteFromTail() {
if(size == 0)
return;
tail = tail.prev;
tail.next = null;
} private class Node {
int row;
int col;
int h = Yard.BLOCK_SIZE;
int w = Yard.BLOCK_SIZE;
Dir dir = Dir.L;
Node next = null;
Node prev = null; //前指针
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.GREEN);
g.fillRect(Yard.BLOCK_SIZE*row, Yard.BLOCK_SIZE*col, w, h);
g.setColor(c);
}
} public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key) {
case KeyEvent.VK_LEFT:
head.dir = Dir.L;
break;
case KeyEvent.VK_UP:
head.dir = Dir.U;
break;
case KeyEvent.VK_RIGHT:
head.dir = Dir.R;
break;
case KeyEvent.VK_DOWN:
head.dir = Dir.D;
break;
}
}
}方向类:public enum Dir {
L,U,R,D;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货