java小游戏-贪吃蛇

王朝java/jsp·作者佚名  2008-05-31
宽屏版  字体: |||超大  

SnakeGame.Java

package SnakeGame;

import javax.swing.*;

public class SnakeGame

{

public static void main( String[] args )

{

JDialog.setDefaultLookAndFeelDecorated( true );

GameFrame temp = new GameFrame();

}

}

Snake.java

package SnakeGame;

import java.awt.*;

import java.util.*;

class Snake extends LinkedList

{

public int snakeDirection = 2;

public int snakeReDirection = 4;

public Snake()

{

this.add( new Point( 3, 3 ) );

this.add( new Point( 4, 3 ) );

this.add( new Point( 5, 3 ) );

this.add( new Point( 6, 3 ) );

this.add( new Point( 7, 3 ) );

this.add( new Point( 8, 3 ) );

this.add( new Point( 9, 3 ) );

this.add( new Point( 10, 3 ) );

}

public void changeDirection( Point temp, int direction )

{

this.snakeDirection = direction;

switch( direction )

{

case 1://up

this.snakeReDirection = 3;

this.add( new Point( temp.x, temp.y - 1 ) );

break;

case 2://right

this.snakeReDirection = 4;

this.add( new Point( temp.x + 1, temp.y ) );

break;

case 3://down

this.snakeReDirection = 1;

this.add( new Point( temp.x, temp.y + 1 ) );

break;

case 4://left

this.snakeReDirection = 2;

this.add( new Point( temp.x - 1, temp.y ) );

break;

}

}

public boolean checkBeanIn( Point bean )

{

Point temp = (Point) this.getLast();

if( temp.equals( bean ) )

{

return true;

}

return false;

}

public void removeTail()

{

this.remove( 0 );

}

public void drawSnake( Graphics g, int singleWidthX, int singleHeightY, int cooPos )

{

g.setColor( ColorGroup.COLOR_SNAKE );

Iterator snakeSq = this.iterator();

while ( snakeSq.hasNext() )

{

Point tempPoint = (Point)snakeSq.next();

this.drawSnakePiece( g, tempPoint.x, tempPoint.y,

singleWidthX, singleHeightY, cooPos );

}

}

public void drawSnakePiece( Graphics g, int temp1, int temp2,

int singleWidthX, int singleHeightY, int cooPos )

{

g.fillRoundRect( singleWidthX * temp1 + 1,

singleHeightY * temp2 + 1,

singleWidthX - 2,

singleHeightY - 2,

cooPos,

cooPos );

}

public void clearEndSnakePiece( Graphics g, int temp1, int temp2,

int singleWidthX, int singleHeightY, int cooPos )

{

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有