java贪吃蛇Snake部分代码
import java.awt.Color;import java.awt.Graphics;
public class Snake {
private Node head = null;
private Node tail = null;
private int size = 0;
private Node n = new Node(20,5,Dir.L);
public 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;
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 U:
node = new Node(head.row-1,head.col,head.dir);
break;
case R:
node = new Node(head.row,head.col+1,head.dir);
break;
case D:
node = new Node(head.row+1,head.col,head.dir);
break;
}
node.next = head;
head = node;
size++;
}
public void draw(Graphics g) {
if(size <= 0) return ;
for(Node n = head; n != null; n = n.next) {
n.draw(g);
}
}
public class Node {
int w = Yard.BLOCK_SIZE;
int h = Yard.BLOCK_SIZE;
int row,col;
Dir dir = Dir.L;
Node next = 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.BLACK);
g.fillRect(Yard.BLOCK_SIZE*col, Yard.BLOCK_SIZE*row, w, h);
g.setColor(c);
}
}
}
昨天发了一个贪吃蛇小游戏的Yard的部分代码,有人反映说部分代码太少了,这一点我是知道的,因为我也在学习中,所以说呢。。喜欢的可以跟着我的代码更新,我们可以互相学习,一起努力!:keai:keai{:1_919:}
yy852 发表于 2016-11-13 21:42
是吗 看来是高手啊!!
专心做哦~理清思路、架构好分好模块、写完检查好代码、测试调试,弄完充分玩够再分享才好,之后可以再优化改进。也可以适当了解和参考别人的(编程还是要多看和积累的,之后可以再做自己的改进),貌似这个200+行代码就成了吧 wqg 发表于 2016-11-14 21:09
ecplic啊
我用的也是那个啊 我感觉你可能没有看完我的代码吧 你可以到我的帖子中看看,现在我已经发了大概有四个这个游戏的代码了吧,还没有更新完呢,因为我也在学习中。你看看哪里不对发过来,我帮你看看。 小白问一下 这个可以要来干嘛 白风 发表于 2016-11-12 20:42
小白问一下 这个可以要来干嘛
你想要干嘛?? 看看好像还不错 看看好像还不错 不错 虽然不怎么懂也还是学习下 感觉好高大上的样子貌似还有个60行的贪吃蛇代码是不是和楼主的一样呢
tiancaizaizuo 发表于 2016-11-13 06:33
感觉好高大上的样子貌似还有个60行的贪吃蛇代码是不是和楼主的一样呢
什么意思??可以切磋一下。 以为很复杂,没想到这么少 看起来挺不错 的支持一下