import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.util.Random; public class Egg { int row,col; int w = Yard.BLOCK_SIZE , h = Yard.BLOCK_SIZE ; private static Random r = new Random(); private Color color = Color.GREEN; public Egg(int row, int col) { this.row = row; this.col = col; } public Egg() { this(r.nextInt(Yard.ROWS-3),r.nextInt(Yard.COLS-3)); } public void reAppear() { this.col = r.nextInt(Yard.COLS-1); this.row = r.nextInt(Yard.ROWS-1); } public Rectangle getRect() { return new Rectangle(Yard.BLOCK_SIZE * this.col,Yard.BLOCK_SIZE * this.row,this.w,this.h); } public void draw(Graphics g) { Color c = g.getColor(); g.setColor(color); g.fillOval(Yard.BLOCK_SIZE * col , Yard.BLOCK_SIZE * row, w, h); if(color ==Color.GREEN) color = Color.RED; else color = Color.GREEN; g.setColor(c); } }
import java.awt.Color; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.event.KeyEvent; public class Snake { Node head = null; Node tail = null; int size = 0; /*public Snake(Node node) { this.head = node; this.tail = node; this.size = 1; }*/ private Node n = new Node(20,40,Dir.L); private Yard y; public Snake(Yard y ) { head = n; tail = n; size = 1; this.y = y; } public void eat(Egg e) { if(this.getRec().intersects(e.getRect())) { e.reAppear(); this.addToHead(); y.setScore(y.getScore()+5); } } public Rectangle getRec() { return new Rectangle(Yard.BLOCK_SIZE * head.col,Yard.BLOCK_SIZE * head.row,head.w,head.h); } public void addToTail(){ Node node = null; switch(tail.dir) { case L: node = new Node(tail.row,tail.col+1,tail.dir); break; case V: 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(head.dir) { case L: node = new Node(head.row,head.col-1,head.dir); break; case V: 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.prev = node; head = 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(); checkDead(); } private void checkDead() { if(head.row < 2 || head.col < 0|| head.row >Yard.ROWS-1 || head.col > Yard.COLS-1) { y.stop(); } for(Node n = head.next;n !=null;n = n.next) { if(head.row == n.row && head.col == n.col) { y.stop(); } } } private void deleteFromTail() { if(size==0) return ; tail = tail.prev; tail.next = null; } private class Node{ int w = Yard.BLOCK_SIZE; int h = Yard.BLOCK_SIZE; int row , col; 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.BLACK); g.fillRect(Yard.BLOCK_SIZE * col, Yard.BLOCK_SIZE * row, w, h); g.setColor(c); } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); switch(key) { case KeyEvent.VK_LEFT: if(head.dir != Dir.R) head.dir = Dir.L; break; case KeyEvent.VK_UP: if(head.dir != Dir.D) head.dir = Dir.V; break; case KeyEvent.VK_RIGHT: if(head.dir != Dir.L) head.dir = Dir.R; break; case KeyEvent.VK_DOWN: if(head.dir != Dir.V) head.dir = Dir.D; break; } } }