/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------- Development Notes: Greedy Snake Game Testing Version Based on Linklist Structure
Direction Control Keys Are: Wasd @ DanieLnw2004 -------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------- * /
import javax.swing *;. // the Java eXtensions "Swing" graphics kitimport java.awt *;. // the Java Abstract Windowing Toolkitimport java.awt.image *;. // the AWT image packageimport java.awt.geom. *; // AWT Geometry Import Java.io. *; // I / O PackageImport Java.awt.Event. *;
Public class snake expert {public static final int display_width = 320; public static final int display_height = 320; public static final int blocknum = 40;
Public int currdir = 1; public canvasarea ca = new canvasarea (Display_Width / 8, Display_Height / 8); 0000-00-00-20-00-00-00 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]; 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.moveup (); ca.displaysnake (); // ---------- -------------------------------------------------- ----
Displayarea Da = New Displayarea (New Rectangle (0, 0, Display_Width, Display_HEight);
C. ADD (DA);
SetVisible (TRUE); ResizetointernalSize (Display_Width, Display_Height);
While (True) {Try {thread.sleep (speted);} 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" character "character" == 'q' || kev.getKeychar () == 'q') {// q or q fort Quit "system.exit (1);} else if (kev.getKeychar () == 'w' || kev.getKeychar () == 'w') {if (currdir! = 2) currdir = 1; system. Out.println ("Key W PRESSED, SNAKE GoES UP");} else if (kev.getKeychar () == 's' || kev.getKeychar () == 's') {if (currdir! = 1) Currdir = 2; System.out.Println ("Key s Pressed, Snake Goes Down");} else if (kev.getKeychar () == 'a' || kev.getKeychar () == 'a') {ix (Currdir! = 4) currdir = 3; System.out.Println ("Key a press, snake goes left");} else if (kev.getKeychar () == 'd' || kev.getKeychar () == 'D') {IF (currdir! = 3) currdir = 4; System.out.Println ("Key D Pressed, Snake Goes Right");}}
/ / -------------------------------------------------------------------------------------------- ----------------------------- Public Void ResizetointernAl (int interfacewidth, int interfaceHeight) {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 Castr [ROW] [col] .right = castr [0] [col];} // --------------------------------------------------------------------------------------- ----------------------- // Cells on each edge // ------------------- ------------------------------- // on the top edge f ((color == 0) && (0 // on the left Edge IF ((row == 0) && (0 // on the right Edge IF ((Row == Cellnum - 1) && (0 R = (int) (Math.random () * 10); whele (r> = blocknum) R = (int) () () () () () () () () () () () () () () () () () () () () () () * 10); fsearch.y = r; 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 Public Void AddCell (CellItem S, INT T) {CellItem Temp; IF (slist == null) {TEMP = new cellitem (); // slist = new cellitem (); slist = temp; slist.x = sx; slist. Y = sy; 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 = SX; search.down.y = Sy; 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 = SX; Temp.y = Sy; Temp.Type = 1; Temp.down = slist; slist.up = Temp; Slist = Temp;} / / --------------------------------------------------- -------------------- // Food matCH // ------------------------ ------------------------------------------- Public Boolean Fmatch (int Valuex, int value, cell; {fsearch = flist; do {ife (fsearch.down == null) Break; if (fsearch.x == valuex) && (fsearch.y == value)) {sx = valuex Sy = value; fsearch.type = 0; foodNum--; //s.type = 1; return true;} fsearch = fsearch.down;} while (fsearch! = Null); if ((fsearch.x == Valuex) && (fsearch.y == value)) {sx = valuex; sy = value; fsearch.type = 0; foodnum--; //s.type = 1; return URN 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 = SX; flist.y = sy; flist.type = t; ftail = flist;} else {fsearch = flist; // current = slist; do {i (fsearch.down == null) Break; fcurrent = fsearch; fsearch = Fsearch.down;} while (fsearch! = null); //search.down = null; fsearch.down = new cellitem (); fsearch.down.x = sx; fsearch.down.y = sy; 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; //iff ((Castr [TOPX] [TOPY] .up.x == flist .x) && (castr [topx] [top] .up.y == flist.y)) f (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; Sear CH = stail; // if ((Castr [Topx] [TOPY] .down.x == flist.x) && (castr [topx] [topy] .down.y == flist.y)) f (fmatch (castr [topx] [TOPY] .down.x, castr [topx] [top] .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, top; cellitem tempcell = new cellitem (); Topx = SLIST .x; TOPY = slist.y; search = stail; // IF ((Castr [TopX] .left.x == flist.x) && (castr [Topx] [TOPY] .left.y == flist.y)) f (fmatch (castr [TOPX] [top]. 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] [top] .right.y == flist.y)) f (fmatch (castr [topx] [TOPY] .right.x, castr [topx] [top] .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] [top] .right.y;}} // ---------------- -------------------------------------------------- --- Public void set (CellItem Clist) {current = clist;} public void getandmove (CellItem S, CellItem Clist) {if (current! = Null) {sx = current.x; sy = 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; 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; setopaque (false); setpreferredsize (new Dimension (Bounds.Width, Bounds.Height);} Public void PaintComponent (Graphics G) {Graphics2D G2 = (Graphics2D) G; Graphics2D G3 = (Graphics2D) g; g2.setcolor; 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; colnum; col ) {for (row = 0; row g3.setcolor (color.red); ca.set (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;} }