服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

greedysnake贪吃蛇-测试版


 

/*------------------------------------------------------------------------------
development notes:
greedy snake game testing version
based on linklist structure

direction control keys are: w a s d
@danielnw2004-----------------------------------------------------------------*/


import javax.swing.*;   // the java extensions "swing" graphics kit
import java.awt.*;      // the java  abstract windowing toolkit
import java.awt.image.*;// the awt image  package
import java.awt.geom.*; // awt geometry
import java.io.*;       // i/o package
import java.awt.event.*;

public class snake extends jframe implements keylistener
{
 public static final int display_width = 640;
 public static final int display_height = 640;
 public static final int blocknum = 80;

 public int currdir = 1;
 public canvasarea ca = new canvasarea(display_width/8,display_height/8);

 public container con = null;
 public int flag = 0, foodnum = 4, speed = 80;
 public cellitem temp = new cellitem();

 public static void main(string[] args)
 {
  new snake();
 }
 
 public snake() {
  super("greedy snake program testing");
  setdefaultcloseoperation(exit_on_close);
  setresizable(false);
  con = getcontentpane();
  con.setlayout(null);
  addkeylistener( this ); // make this object be the one to listen to keyboard
//----------------------------------------------------------------
//matrix of the canvas
//----------------------------------------------------------------
  ca.canvasinit();

  ca.addfood(ca.castr[30][32],2);
  ca.addfood(ca.castr[18][9],2);
  ca.addfood(ca.castr[10][20],2);
  ca.addfood(ca.castr[10][15],2);
  //ca.displaymatrix();
  ca.addcell(ca.castr[1][0],1);
  ca.addcell(ca.castr[1][1],1);
  ca.addcell(ca.castr[1][2],1);
  ca.addcell(ca.castr[1][3],1);
  ca.addcell(ca.castr[1][4],1);
  ca.addcell(ca.castr[2][4],1);
  ca.moveleft();

  //ca.moveup();
  //ca.moveup();
  //ca.moveup();
  //ca.moveup();
  //ca.moveup();
  //ca.moveup();
  ca.displaysnake();
//---------------------------------------------------------------- 

  displayarea da =
     new displayarea( new rectangle( 0, 0, display_width, display_height ) );

  con.add ( da );

  setvisible(true);
  
  resizetointernalsize( display_width, display_height);

  while(true){
   try{
    thread.sleep( speed );
   }catch( interruptedexception ie ){}
    if ( currdir == 1 ) ca.moveup();
    if ( currdir == 2 ) ca.movedown();
    if ( currdir == 3 ) ca.moveleft();
    if ( currdir == 4 ) ca.moveright();
    if ( foodnum == 0 ) ca.actfood();
    //ca.moveup();
    //ca.movedown();
    //ca.moveleft();
    //ca.moveright();
    da.repaint();
   }

 }
//-----------------------------------------------------------------------------
//keyboard listener
//-----------------------------------------------------------------------------
 public void keypressed( keyevent kev ){}
    public void keyreleased( keyevent kev ){}

    public void keytyped( keyevent kev ){  // useful only for "normal" characters
  if( kev.getkeychar() == 'q' || kev.getkeychar() == 'q' ){  // q or q for "quit"
   system.exit(1);
  }else if( kev.getkeychar() == 'w' || kev.getkeychar() == 'w' ){
   currdir = 1;
   system.out.println("key w pressed, snake goes up");
  }else if( kev.getkeychar() == 's' || kev.getkeychar() == 's' ){
      currdir = 2;
   system.out.println("key s pressed, snake goes down");
  }else if( kev.getkeychar() == 'a' || kev.getkeychar() == 'a' ){
   currdir = 3;
   system.out.println("key a pressed, snake goes left");
  }else if( kev.getkeychar() == 'd' || kev.getkeychar() == 'd' ){
   currdir = 4;
   system.out.println("key d pressed, snake goes right");
  }
    }

 

//----------------------------------------------------------------------------- 
 public void resizetointernalsize( int internalwidth, int internalheight ){
 insets insets = getinsets();
 final int newwidth  = internalwidth  + insets.left + insets.right;
 final int newheight = internalheight + insets.top  + insets.bottom;
 
 runnable resize = new runnable(){ // an anonymous inner class
  public void run(){
      setsize( newwidth, newheight);
  }
 };
 if(!swingutilities.iseventdispatchthread() ){
     try{
  swingutilities.invokeandwait( resize );
     }catch( exception e ) { }
 }
 else{
     resize.run();
 }
 validate();
    }
//----------------------------------------------------------------
//canvasarea class
//----------------------------------------------------------------
 public class canvasarea
 { 
  public int width, height, row, col, cellnum;
  
  public cellitem castr[][], flist, ftail, fsearch, fcurrent;
  public cellitem slist, search, current, stail;

  public canvasarea(int w, int h){
   width = w;
   height = h;
   slist = null;
   search = null;
   cellnum = blocknum;
   flist = null;ftail = null;fsearch = null; fcurrent = null;
   castr = new cellitem[w][h];
  }
  
  public void canvasinit() {
   for (col = 0; col < height; col++ )
   {
    for (row = 0; row < width; row++ )
    {
     castr[row][col] = new cellitem();
     castr[row][col].x = row;
     castr[row][col].y = col;
     castr[row][col].type = 0;
    }
   }
   
   for (col = 0; col < height; col++ )
   {
    for (row = 0; row < width; row++ )
    {
     if ((row == 0) && (col == 0))
     {
       castr[row][col].up = castr[row][cellnum-1];
       castr[row][col].down = castr[row][col+1];
       castr[row][col].left = castr[cellnum-1][col];
       castr[row][col].right = castr[1][col];
     }
     if ((row == 0) && (col == (cellnum - 1)) )
     {
      castr[row][col].up = castr[row][col-1];
      castr[row][col].down = castr[row][0];
      castr[row][col].left = castr[cellnum-1][col];
      castr[row][col].right = castr[1][col];
     }
     if ((row == (cellnum - 1)) && (col == 0))
     {
      castr[row][col].up = castr[row][cellnum-1];
      castr[row][col].down = castr[row][1];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[0][col];
     }
     if ((row == (cellnum - 1)) && (col == (cellnum - 1)))
     {
      castr[row][col].up = castr[row-1][col-1];
      castr[row][col].down = castr[row][0];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[0][col];
     }
     //----------------------------------------------------
     //cells on each edge
     //----------------------------------------------------
     // on the top edge
     if ((col == 0) && (0 < row) && (row < cellnum - 1))
     {
      castr[row][col].up = castr[row][cellnum-1];
      castr[row][col].down = castr[row][col+1];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[row+1][col];
     }

     //on the bottom edge
     if ((col == cellnum - 1) && (0 < row) && (row < cellnum - 1))
     {
      castr[row][col].up = castr[row][col-1];
      castr[row][col].down = castr[row][0];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[row+1][col];
     }

     //on the left edge
     if ((row == 0) && (0 < col) && (col < cellnum - 1))
     {
      castr[row][col].up = castr[row][col-1];
      castr[row][col].down = castr[row][col+1];
      castr[row][col].left = castr[cellnum-1][col];
      castr[row][col].right = castr[row+1][col];
     }

     //on the right edge
     if ((row == cellnum - 1) && (0 < col) && (col < cellnum - 1))
     {
      castr[row][col].up = castr[row][col-1];
      castr[row][col].down = castr[row][col+1];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[0][col];
     }
     //cells sitting in the middle area
     if ((0 < row) && (row < cellnum-1) && (0 < col) && (col < cellnum-1))
     {
      castr[row][col].up = castr[row][col-1];
      castr[row][col].down = castr[row][col+1];
      castr[row][col].left = castr[row-1][col];
      castr[row][col].right = castr[row+1][col];
     }
    
    }
   }
  }
//-------------------------------------------------------------------------
//active all food
//-------------------------------------------------------------------------
  public void actfood(){
   double outvalue;
   int x, y;
   fsearch = flist;
   do
   { 
    if (fsearch.down == null) break;
    fsearch.type = 2;

    fsearch = fsearch.down;
   }
   while (fsearch != null);
   fsearch.type = 2;
   foodnum = 4;
  }
//-------------------------------------------------------------------------

  public void displaysnake() {
   search = slist;
   do
   {
    if (search == null) break;
    system.out.print(search.x + " ");
    system.out.println(search.y);
    search = search.down;
   }
   while (search != null);
   
   system.out.println("----------------------------------");
   search = stail;
   do
   {
    if (search == null) break;
    system.out.print(search.x + " ");
    system.out.println(search.y);
    search = search.up;
   }
   while (search != null);

  }


  public void displaymatrix() {
   for (col = 0; col < height; col++ )
   {
    for (row = 0; row < width; row++ )
    {
     system.out.print(castr[row][col].x + " ");
     system.out.println(castr[row][col].y);
    }
   }
  }

  public void addcell(cellitem s, int t) {
   cellitem temp;
   if (slist == null)
   {
    temp = new cellitem();
    //slist = new cellitem();
    slist = temp;
    slist.x = s.x;
    slist.y = s.y;
    slist.type = t;
    stail = slist;
   }else{
    search = slist;
    //current = slist;
    do
    { 
     if (search.down == null) break;
     current = search;
     search = search.down;
    }
    while (search != null);
    //search.down = null;
    search.down = new cellitem();
    search.down.x = s.x;
    search.down.y = s.y;
    search.down.type = t;
    search.down.up = search;
    stail = search.down;
   }

  }
//---------------------------------------------------------------------
//snake eats food
//---------------------------------------------------------------------
  public void addtotop(cellitem s){
   cellitem temp = new cellitem();
   temp.x = s.x;
   temp.y = s.y;
   temp.type = 1;
   temp.down = slist;
   slist.up = temp;
   slist = temp;
  }
//---------------------------------------------------------------------
//food match
//---------------------------------------------------------------------
  public boolean fmatch(int valuex, int valuey, cellitem s) {
   fsearch = flist;
   do
   { 
    if (fsearch.down == null) break;
    if ((fsearch.x == valuex) && (fsearch.y == valuey)) {
     s.x = valuex;
     s.y = valuey;
     fsearch.type = 0;
     foodnum--;
     //s.type = 1;
     return true;
    }
    fsearch = fsearch.down;
   }
   while (fsearch != null);
   if ((fsearch.x == valuex) && (fsearch.y == valuey)) {
     s.x = valuex;
     s.y = valuey;
     fsearch.type = 0;
     foodnum--;
     //s.type = 1;
     return true;
   }
   return false;
  }
//----------------------------------------------------------------------
//add food
//----------------------------------------------------------------------
  public void addfood(cellitem s, int t) {
   cellitem temp;
   if (flist == null)
   {
    temp = new cellitem();
    //slist = new cellitem();
    flist = temp;
    flist.x = s.x;
    flist.y = s.y;
    flist.type = t;
    ftail = flist;
   }else{
    fsearch = flist;
    //current = slist;
    do
    { 
     if (fsearch.down == null) break;
     fcurrent = fsearch;
     fsearch = fsearch.down;
    }
    while (fsearch != null);
    //search.down = null;
    fsearch.down = new cellitem();
    fsearch.down.x = s.x;
    fsearch.down.y = s.y;
    fsearch.down.type = t;
    fsearch.down.up = fsearch;
    ftail = fsearch.down;
   }

  }

//---------------------------------------------------------------------
//move up
//---------------------------------------------------------------------
  public void moveup(){
   int topx,topy;
   cellitem tempcell = new cellitem();
   topx = slist.x;
   topy = slist.y;
   search = stail;
   //if ((castr[topx][topy].up.x == flist.x) && (castr[topx][topy].up.y == flist.y))
   if (fmatch(castr[topx][topy].up.x, castr[topx][topy].up.y, tempcell))
   {
    addtotop(tempcell);
    //flist = null;
   }else{
   do
   {
    if(search.up == null) break;
     search.x = search.up.x;
     search.y = search.up.y;
     search = search.up;
   }
   while (search != null);
   slist.x = castr[topx][topy].up.x;
   slist.y = castr[topx][topy].up.y;
   }
  }
//---------------------------------------------------------------------
//move down
//---------------------------------------------------------------------
  public void movedown(){
   int topx,topy;
   cellitem tempcell = new cellitem();
   topx = slist.x;
   topy = slist.y;
   search = stail;

   //if ((castr[topx][topy].down.x == flist.x) && (castr[topx][topy].down.y == flist.y))
   if (fmatch(castr[topx][topy].down.x, castr[topx][topy].down.y, tempcell))
   {
    addtotop(tempcell);
    //flist = null;
   }else{

   do
   {
    if(search.up == null) break;
     search.x = search.up.x;
     search.y = search.up.y;
     search = search.up;
   }
   while (search != null);
   slist.x = castr[topx][topy].down.x;
   slist.y = castr[topx][topy].down.y;

   }
  }
//---------------------------------------------------------------------
//move left
//---------------------------------------------------------------------
  public void moveleft(){
   int topx,topy;
   cellitem tempcell = new cellitem();
   topx = slist.x;
   topy = slist.y;
   search = stail;

   //if ((castr[topx][topy].left.x == flist.x) && (castr[topx][topy].left.y == flist.y))
   if (fmatch(castr[topx][topy].left.x, castr[topx][topy].left.y, tempcell))
   {
    addtotop(tempcell);
    //flist = null;
   }else{

   do
   {
    if(search.up == null) break;
     search.x = search.up.x;
     search.y = search.up.y;
     search = search.up;
   }
   while (search != null);
   slist.x = castr[topx][topy].left.x;
   slist.y = castr[topx][topy].left.y;
   }
  }

//---------------------------------------------------------------------
//move right
//---------------------------------------------------------------------
  public void moveright(){
   int topx,topy;
   cellitem tempcell = new cellitem();
   topx = slist.x;
   topy = slist.y;
   search = stail;

   //if ((castr[topx][topy].right.x == flist.x) && (castr[topx][topy].right.y == flist.y))
   if (fmatch(castr[topx][topy].right.x, castr[topx][topy].right.y, tempcell))
   {
    addtotop(tempcell);
    //flist = null;
   }else{

   do
   {
    if(search.up == null) break;
     search.x = search.up.x;
     search.y = search.up.y;
     search = search.up;
   }
   while (search != null);
   slist.x = castr[topx][topy].right.x;
   slist.y = castr[topx][topy].right.y;
   }
  }
//---------------------------------------------------------------------
  public void set(cellitem clist){
   current = clist;
  }

  public void getandmove(cellitem s, cellitem clist) {
   
   if (current != null)
   {
    s.x = current.x;
    s.y = current.y;
    s.type = current.type;
    current = current.down;
   }else{

    current = clist;
    s.x = -1;
    s.y = -1;
    s.type = -1;
   }   
  }
  

 }

//----------------------------------------------------------------
//cell class
//----------------------------------------------------------------
 public class cellitem{
  public int x,y;
  public int type; //0 means canvas,1 means snake, 2 means food
  public cellitem up,down,left,right;

  public cellitem() {
   x = -1; y = -1;
   type = -1;
   up = null;
   down = null;
   left = null;
   right = null;
  }

 }

//----------------------------------------------------------------
//displayarea class
//----------------------------------------------------------------
 public class displayarea extends jpanel
 {
  int col,row;
  int rownum, colnum, rowlen, collen;
  int i;

  public displayarea( rectangle bounds ){  // default constructor
     setlayout(null);
     setbounds( bounds);
     setopaque(false);
     setpreferredsize( new dimension (bounds.width, bounds.height )  );
  }

  public void paintcomponent( graphics g ){
   graphics2d g2 = (graphics2d)g;
   graphics2d g3 = (graphics2d)g;

   g2.setcolor( color.white );
   g2.fillrect(0,0,display_width,display_height);


   rownum = display_width/8;
   colnum = display_height/8;
   rowlen = 8;
   collen = 8;
   /*
   if (flag == 0)
   {
    g2.setcolor( color.lightgray);
    for (col = 0; col < colnum; col++ )
    {
     for (row = 0; row < rownum; row++ )
     {
      //g2.drawrect(row * rowlen, col * collen, rowlen, collen);
     }
    }
    //flag = 1;
   }
   */
   g2.setcolor( color.lightgray);
   if (flag == 0)
   {
    for (col = 0; col < colnum; col++ )
    {
     g2.drawline(0, col * collen, display_height, col * collen);
    }
    for (row = 0; row < rownum; row++ )
    {
     g2.drawline(row * rowlen, 0, row * rowlen, display_width);
    }
   }
//------------------------------------------------------------------------
//print out food
//------------------------------------------------------------------------

   g3.setcolor( color.red );
   ca.set(ca.flist);
   ca.getandmove(temp, ca.flist);
   while (temp.x != -1)
   {
    if (temp.type == 2){
     g3.fillrect(temp.x * rowlen + 1, temp.y * collen + 1, 7 ,7);
    }
    ca.getandmove(temp, ca.flist);
    if (temp.x == -1) break;
   }

//------------------------------------------------------------------------
//print out the snake
//------------------------------------------------------------------------
   ca.set(ca.slist);
   ca.getandmove(temp, ca.slist);
   g3.setcolor( color.blue);
   while (temp.x != -1)
   {
    g3.fillrect(temp.x * rowlen + 1, temp.y  * collen + 1,7,7);
    ca.getandmove(temp, ca.slist);
    if (temp.x == -1) break;
   }

     }
 
 }

//--------------------------------------------------------------- 
//end of program
//---------------------------------------------------------------
}

扫描关注微信公众号