java贪吃蛇代码注释求解

首先http://blog.csdn.net/leasystu/article/details/7576116完整代码在此求详细解释一下整体思路以及InterFace类里... 首先 http://blog.csdn.net/leasystu/article/details/7576116 完整代码在此
求详细解释一下整体思路以及InterFace类里面每一行的内容。
若采纳会有追加分数,谢谢!
展开
 我来答
3322178
2015-01-05 · TA获得超过214个赞
知道答主
回答量:158
采纳率:0%
帮助的人:89.6万
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
public class InterFace extends JFrame {
/**
* WIDTH:宽
* HEIGHT:高
* SLEEPTIME:可以看作蛇运动的速度
* L = 1,R = 2, U = 3, D = 4 左右上下代码
*/
public static final int WIDTH = 800, HEIGHT = 600, SLEEPTIME = 200, L = 1,R = 2, U = 3, D = 4;
BufferedImage offersetImage= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR);;
Rectangle rect = new Rectangle(20, 40, 15 * 50, 15 * 35);
Snake snake;
Node node;
public InterFace() {
//创建"蛇"对象
snake = new Snake(this);
//创建"食物"对象
createNode();
this.setBounds(100, 100, WIDTH, HEIGHT);
//添加键盘监听器
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode());
switch (arg0.getKeyCode()) {
//映射上下左右4个键位
case KeyEvent.VK_LEFT:
snake.dir = L;
break;
case KeyEvent.VK_RIGHT:
snake.dir = R;
break;
case KeyEvent.VK_UP:
snake.dir = U;
break;
case KeyEvent.VK_DOWN:
snake.dir = D;
}
}
});
this.setTitle("贪吃蛇 0.1 By : Easy");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
//启动线程,开始执行
new Thread(new ThreadUpadte()).start();
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) offersetImage.getGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.black);
g2d.drawRect(rect.x, rect.y, rect.width, rect.height);
//如果蛇碰撞(吃)到食物,则创建新食物
if (snake.hit(node)) {
createNode();
}
snake.draw(g2d);
node.draw(g2d);
g.drawImage(offersetImage, 0, 0, null);
}
class ThreadUpadte implements Runnable {
public void run() {
//无限重绘画面
while (true) {
try {
Thread.sleep(SLEEPTIME);
repaint(); //
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* 创建食物
*/
public void createNode() {
//随机食物的出现位置
int x = (int) (Math.random() * 650) + 50,y = (int) (Math.random() * 500) + 50;
Color color = Color.blue;
node = new Node(x, y, color);
}
public static void main(String args[]) {
new InterFace();
}
}
/**
* 节点类(包括食物和蛇的身躯组成节点)
*/
class Node {
int x, y, width = 15, height = 15;
Color color;
public Node(int x, int y, Color color) {
this(x, y);
this.color = color;
}
public Node(int x, int y) {
this.x = x;
this.y = y;
this.color = color.black;
}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.drawRect(x, y, width, height);
}
public Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
}
/**
* 蛇
*/
class Snake {
public List<Node> nodes = new ArrayList<Node>();
InterFace interFace;
int dir=InterFace.R;
public Snake(InterFace interFace) {
this.interFace = interFace;
nodes.add(new Node(20 + 150, 40 + 150));
addNode();
}
/**
* 是否碰撞到食物
* @return true 是 false 否
*/
public boolean hit(Node node) {
//遍历整个蛇体是否与食物碰撞
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).getRect().intersects(node.getRect())) {
addNode();
return true;
}
}
return false;
}
public void draw(Graphics2D g2d) {
for (int i = 0; i < nodes.size(); i++) {
nodes.get(i).draw(g2d);
}
move();
}
public void move() {
nodes.remove((nodes.size() - 1));
addNode();
}
public synchronized void addNode() {
Node nodeTempNode = nodes.get(0);
//如果方向
switch (dir) {
case InterFace.L:
//判断是否会撞墙
if (nodeTempNode.x <= 20) {
nodeTempNode = new Node(20 + 15 * 50, nodeTempNode.y);
}
nodes.add(0, new Node(nodeTempNode.x - nodeTempNode.width,
nodeTempNode.y));
break;
case InterFace.R:
//判断是否会撞墙
if (nodeTempNode.x >= 20 + 15 * 50 - nodeTempNode.width) {
nodeTempNode = new Node(20 - nodeTempNode.width, nodeTempNode.y);
}
nodes.add(0, new Node(nodeTempNode.x + nodeTempNode.width,
nodeTempNode.y));
break;
case InterFace.U:
//判断是否会撞墙
if (nodeTempNode.y <= 40) {
nodeTempNode = new Node(nodeTempNode.x, 40 + 15 * 35);
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y - nodeTempNode.height));
break;
case InterFace.D:
//判断是否会撞墙
if (nodeTempNode.y >= 40 + 15 * 35 - nodeTempNode.height) {
nodeTempNode = new Node(nodeTempNode.x,40 - nodeTempNode.height);
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height));
break;
}
}
}
匿名用户
2015-01-05
展开全部
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
public class InterFace extends JFrame {
public static final int WIDTH = 800, HEIGHT = 600, SLEEPTIME = 100, L = 1,R = 2, U = 3, D = 4; //width and height is for windows setup
//sleeptime is the interval for screen to be updated or respond to our operation
BufferedImage offersetImage= new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_3BYTE_BGR);; //generate the empty picture for the operation
Rectangle rect = new Rectangle(20, 40, 15 * 50, 15 * 35); //setup Rectangle with x=20, y=40 and height =525 width = 750
Snake snake;
Node node;
public InterFace() {
snake = new Snake(this); //generate the snake
createNode(); //generate the first target node
this.setBounds(100, 100, WIDTH, HEIGHT); //set the place of this program in the screen
//add key listener function for the program
this.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode()); //get the key press event and check which side to be change
switch (arg0.getKeyCode()) {
case KeyEvent.VK_LEFT:
snake.dir = L;
break;
case KeyEvent.VK_RIGHT:
snake.dir = R;
break;
case KeyEvent.VK_UP:
snake.dir = U;
break;
case KeyEvent.VK_DOWN:
snake.dir = D;
}
}
});
this.setTitle("Snack 0.1 By: Easy");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
new Thread(new ThreadUpadte()).start(); //call the endless loop to run the program
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) offersetImage.getGraphics();
g2d.setColor(Color.white);
g2d.fillRect(0, 0, WIDTH, HEIGHT); //fill the whole picture with white
g2d.setColor(Color.black); //change to use black
g2d.drawRect(rect.x, rect.y, rect.width, rect.height); //draw the rect for related operation now
if (snake.hit(node)) {
createNode(); //generate the new node after snake eat the former one
}
snake.draw(g2d); //draw the snake
node.draw(g2d); //draw the node
g.drawImage(offersetImage, 0, 0, null); //show the whole picture to people now
}
//the endless loop to executed the program with multi thread operations
class ThreadUpadte implements Runnable {
public void run() {
while (true) {
try {
Thread.sleep(SLEEPTIME);
repaint(); //use repaint to re-draw the picture again, system will call the paint function above to do that
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//generate a node with random address for snack to eat
public void createNode() {
int x = (int) (Math.random() * 650) + 50,y = (int) (Math.random() * 500) + 50;
Color color = Color.blue;
node = new Node(x, y, color);
}
//the main function for the whole program
public static void main(String args[]) {
new InterFace();
}
}
class Node { //the class for the small node to be eated by snack
int x, y, width = 15, height = 15;
Color color;
public Node(int x, int y, Color color) {
this(x, y);
this.color = color;
}
public Node(int x, int y) {
this.x = x;
this.y = y;
this.color = color.black;
}
public void draw(Graphics2D g2d) {
g2d.setColor(color);
g2d.drawRect(x, y, width, height);
}
public Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
}
class Snake {
public List<Node> nodes = new ArrayList<Node>(); //snack is formed by several nodes together
InterFace interFace;
int dir=InterFace.R;
public Snake(InterFace interFace) {
this.interFace = interFace;
nodes.add(new Node(20 + 150, 40 + 150)); //the start place for the snack is x=170,y=190
addNode(); //move the snack now
}
public boolean hit(Node node) {
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).getRect().intersects(node.getRect())) {
addNode();
return true;
}
}
return false;
}
public void draw(Graphics2D g2d) {
for (int i = 0; i < nodes.size(); i++) {
nodes.get(i).draw(g2d);
}
move();
}
//move to a new place
public void move() {
nodes.remove((nodes.size() - 1));
addNode();
}
//the below function are used each timestamp of SLEEPTIME to update the current picture
public synchronized void addNode() {
Node nodeTempNode = nodes.get(0);
switch (dir) {
case InterFace.L:

if (nodeTempNode.x <= 20) {
//this is just to show the case the head of snack hit the left side and switch to show it on the right side
nodeTempNode = new Node(20 + 15 * 50, nodeTempNode.y);
System.out.println("hit the left side of windows now.");
}
nodes.add(0, new Node(nodeTempNode.x - nodeTempNode.width,
nodeTempNode.y));

break;
case InterFace.R:

if (nodeTempNode.x >= 20 + 15 * 50 - nodeTempNode.width) {
//this is just to show the case the head of snack hit the right side and switch to show it on the left side
nodeTempNode = new Node(20 - nodeTempNode.width, nodeTempNode.y);
System.out.println("hit the right side of windows now.");
}
nodes.add(0, new Node(nodeTempNode.x + nodeTempNode.width,
nodeTempNode.y));
break;
case InterFace.U:

if (nodeTempNode.y <= 40) {
//this is just to show the case the head of snack hit the upper side and switch to show it on the down side
nodeTempNode = new Node(nodeTempNode.x, 40 + 15 * 35);
System.out.println("hit the upper side of windows now.");
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y - nodeTempNode.height));

break;
case InterFace.D:

if (nodeTempNode.y >= 40 + 15 * 35 - nodeTempNode.height) {
//this is just to show the case the head of snack hit the Down side and switch to show it on the upper side
nodeTempNode = new Node(nodeTempNode.x,40 - nodeTempNode.height);
System.out.println("hit the Down side of windows now.");
}
nodes.add(0, new Node(nodeTempNode.x, nodeTempNode.y + nodeTempNode.height));

break;
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式