The winter is really cold in the northeast. Yesterday in the bedroom house a day, idle to find the horse soldier Teacher's greedy snake video, follow the teacher completed the small game. Put it here and leave a souvenir!
/*************** Yard class ********************/
ImportJava.awt.Color;ImportJava.awt.Font;ImportJava.awt.Frame;ImportJava.awt.Graphics;ImportJava.awt.Image;ImportJava.awt.event.KeyAdapter;Importjava.awt.event.KeyEvent;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent; Public classYardextendsFrame {paintthread paintthread=NewPaintthread (); Private BooleanGameover=false; Public Static Final intRows=30; Public Static Final intCols=30; Public Static Final intBlock_size=15; Private intScore=0; Snake s=NewSnake ( This); Egg e=NewEgg (); Image Offscrenimage=NULL; Public voidlauch () { This. setlocation (200, 200); This. SetSize (cols*block_size,rows*block_size); This. Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {system.exit (0); } }); This. setvisible (true); This. Addkeylistener (Newkeymonitor ()); NewThread (NewPaintthread ()). Start (); } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub NewYard (). Lauch (); } Public voidStop () {Gameover=true;} @Override Public voidPaint (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); //Draw Line for(inti=1;i<rows;i++) {G.drawline (0, Block_size*i, cols*block_size,block_size*i); } for(inti=1;i<cols;i++) {g.drawline (block_size*i, 0, Block_size*i, block_size*ROWS); } g.setcolor (Color.yellow); g.DrawString ("Score:" +getscore (), 10, 60); G.setcolor (c); S.eat (e); E.draw (g); S.draw (g); if(Gameover) {g.setcolor (color.yellow); G.setfont (NewFont ("Arial", font.bold| font.hanging_baseline,50)); g.DrawString ("GAME over!", 100, 180); Paintthread.gameover (); } } Private classPaintthreadImplementsrunnable{Private Booleanruning =true; @Override Public voidrun () { while(runing) {repaint (); Try{Thread.Sleep (100); }Catch(interruptedexception e) {e.printstacktrace (); } } } Public voidGameover () {runing=false; }} @Override Public voidUpdate (Graphics g) {if(Offscrenimage = =NULL) {Offscrenimage= This. CreateImage (cols*block_size,rows*block_size); } Graphics Goff=Offscrenimage.getgraphics (); Paint (Goff); G.drawimage (Offscrenimage,0, 0,NULL);}Private classKeymonitorextendskeyadapter{ Public voidkeypressed (KeyEvent e) {s.keypressed (e); }} Public intGetscore () {returnscore;} Public voidSetScore (intscore) { This. score =score;}}
/********************** Snake class **************************/
Import Java.awt.color;import java.awt.graphics;import Java.awt.rectangle;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);p rivate Yard y; Public Snake (Yard y) {head=n; Tail=n; size=1; This.y=y; } public void Addtotail () {//Added to the tail 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;//new tail 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); BreakCase 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; Move (); for (Node n=head;n!=null;n=n.next) {N.draw (g); }} private void Move () {//TODO auto-generated Method Stubaddtohead ();d eletefromtail (); Checkdead ();} private void Checkdead () {//TODO auto-generated method stubif (head.row<2| | head.col<0| | head.row>yard.rows| | Head.col>yard.cols) {y.stop ();} for (Node n=head.next;n!=null;n=n.next) {//and own body collision if (Head.row==n.row&&head.col==n.col) {y.stop ();}}} private void Deletefromtail () {//TODO auto-generated method stubif (size==0) return;tail=tail.prev;tail.next=null;} Class Node{int W=yard.block_size;int h=yard.block_size;int row,col;dir dir=dir.d; 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 Eat (Egg e) {if (This.getrect (). intersects (E.getrect ())) {e.reappear (); This.addtohead (); Y.setscore ( Y.getscore () +5);}} Private Rectangle GetRect () {return new Rectangle (yard.block_size*head.col,yard.block_size*head.row,head.w,head.h);} public void keypressed (KeyEvent e) {//TODO auto-generated method Stubint key=e.getkeycode (), switch (key) {case KeyEvent.VK_LEFT:if (HEAD.DIR!=DIR.R) head.dir=dir.l;break;case KeyEvent.VK_RIGHT:if (HEAD.DIR!=DIR.L) head.dir= Dir.r;break;case KeyEvent.VK_UP:if (HEAD.DIR!=DIR.D) head.dir=dir.u;break;case KeyEvent.VK_DOWN:if (head.dir!=dir.u ) Head.dir=dir.d;break;}}}
/********************* eggs (for snakes to eat) *******************/
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; int 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) +3,r.nextint (Yard.cols));} public void reappear () {this.row=r.nextint (yard.rows-3) +3;this.col=r.nextint (yard.cols);} Public Rectangle GetRect () {return new Rectangle (YARD.BLOCK_SIZE*COL,YARD.BLOCK_SIZE*ROW,W,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); G.setcolor (c); if (color==color.green) Color=color.red;else Color=color.green;} public int GetRow () {return row;} public void Setrow (int row) {this.row = row;} public int Getcol () {return col;} public void Setcol (int col) {this.col = col;}}
/********************** enumeration (direction) *****************************/
public enum Dir { l,u,r,d;}
From the video view, this is the teacher classroom completed, very envious of the teacher's ability. At the same time, also found that the teacher is doing, some places are not too ideal to deal with.
。。。
Java Study Diary-------------------------------------snake