分享
 
 
 

GreedySnake贪吃蛇-测试版

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*------------------------------------------------------------------------------

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

//---------------------------------------------------------------

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有