/*------------------------------------------------------------------------------
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
//---------------------------------------------------------------
}
本文地址:http://com.8s8s.com/it/it13750.htm