?? snakegame.java
字號:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.geom.*;
class GameFrame extends JFrame
{
private Toolkit tempKit;
private int horizontalGrid, verticalGrid;
private int singleWidthX, singleHeightY;
private int cooPos;
private Snake mainSnake;
private LinkedList eatedBean;
{
eatedBean = new LinkedList();
}
private Iterator snakeSq;
public javax.swing.Timer snakeTimer;
private int direction = 2;
private int score;
private String info;
private Point bean, eatBean;
{
bean = new Point();
}
private boolean flag;
private JMenuBar infoMenu;
private JMenu[] tempMenu;
private JMenuItem[] tempMenuItem;
private JRadioButtonMenuItem[] levelMenuItem, versionMenuItem;
private JLabel scoreLabel;
{
scoreLabel = new JLabel();
}
private Graphics2D g;
private ImageIcon snakeHead;
{
snakeHead = new ImageIcon( "LOGO.gif" );
}
private ConfigMenu configMenu;
private boolean hasStoped = true;
public GameFrame()
{
this.tempKit = this.getToolkit();
this.setSize( tempKit.getScreenSize() );
this.setGrid( 60, 40, 5 );
this.getContentPane().setBackground( ColorGroup.COLOR_BACK );
this.setUndecorated( true );
this.setResizable( false );
this.addKeyListener( new KeyHandler() );
GameFrame.this.snakeTimer = new javax.swing.Timer( 80, new TimerHandler() );
this.getContentPane().add( scoreLabel, BorderLayout.SOUTH );
this.scoreLabel.setFont( new Font( "Fixedsys", Font.BOLD, 15 ) );
this.scoreLabel.setText( "Pause[SPACE] - Exit[ESC]" );
this.configMenu = new ConfigMenu( this );
this.setVisible( true );
}
public void setGrid( int temp1, int temp2, int temp3 )
{
this.horizontalGrid = temp1;
this.verticalGrid = temp2;
this.singleWidthX = this.getWidth() / temp1;
this.singleHeightY = this.getHeight() / temp2;
this.cooPos = temp3;
}
private class KeyHandler extends KeyAdapter
{
public void keyPressed( KeyEvent e )
{
if( e.getKeyCode() == 27 )
{
snakeTimer.stop();
if( JOptionPane.showConfirmDialog( null, "Are you sure to exit?" ) == 0 )
{
System.exit( 0 );
}
snakeTimer.start();
}
else if( e.getKeyCode() == 37 && mainSnake.snakeDirection != 2 )//left
{
direction = 4;
}
else if( e.getKeyCode() == 39 && mainSnake.snakeDirection != 4 )//right
{
direction = 2;
}
else if( e.getKeyCode() == 38 && mainSnake.snakeDirection != 3 )//up
{
direction = 1;
}
else if( e.getKeyCode() == 40 && mainSnake.snakeDirection != 1 )//down
{
direction = 3;
}
else if( e.getKeyCode() == 32 )
{
if( !hasStoped )
{
if( !flag )
{
snakeTimer.stop();
configMenu.setVisible( true );
configMenu.setMenuEnable( false );
flag = true;
}
else
{
snakeTimer.start();
configMenu.setVisible( false );
configMenu.setMenuEnable( true );
flag = false;
}
}
}
}
}
private class TimerHandler implements ActionListener
{
public synchronized void actionPerformed( ActionEvent e )
{
Point temp = (Point) mainSnake.getLast();
snakeSq = mainSnake.iterator();
while ( snakeSq.hasNext() )
{
Point tempPoint = (Point)snakeSq.next();
if( temp.equals( tempPoint ) && snakeSq.hasNext() != false )
{
snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog( null,
"Your Score is " + score + "\n\nYou Loss!" );
}
}
System.out.println( temp.x + " " + temp.y );
if( (temp.x == 0 && direction == 4) ||
(temp.x == horizontalGrid-1 && direction == 2) ||
(temp.y == 0 && direction == 1) ||
(temp.y == verticalGrid-1 && direction == 3) )
{
snakeTimer.stop();
stopGame();
JOptionPane.showMessageDialog( null,
"Your Score is " + score + "\n\nYou Loss!" );
}
if( direction != mainSnake.snakeReDirection )
{
moveSnake( direction );
}
mainSnake.drawSnake( getGraphics(), singleWidthX, singleHeightY, cooPos );
drawBeanAndEBean( getGraphics() );
}
}
public void stopGame()
{
this.hasStoped = true;
this.snakeTimer.stop();
Graphics2D g = (Graphics2D) GameFrame.this.getGraphics();
g.setColor( ColorGroup.COLOR_BACK );
super.paint( g );
configMenu.setVisible( true );
}
public void resetGame()
{
System.gc();
this.hasStoped = false;
Graphics2D g = (Graphics2D) GameFrame.this.getGraphics();
g.setColor( ColorGroup.COLOR_BACK );
super.paint( g );
this.mainSnake = new Snake();
this.createBean( bean );
this.eatedBean.clear();
mainSnake.drawSnake( getGraphics(), singleWidthX, singleHeightY, cooPos );
this.snakeTimer.start();
this.direction = 2;
this.score = 0;
this.scoreLabel.setText( "Pause[SPACE] - Exit[ESC]" );
}
private void moveSnake( int direction )
{
if( mainSnake.checkBeanIn( this.bean ) )
{
this.score += 100;
this.scoreLabel.setText( this.info + " Current Score:" + this.score );
this.eatedBean.add( new Point(this.bean) );
this.createBean( this.bean );
}
mainSnake.changeDirection( (Point) mainSnake.getLast(), direction );
Point temp = (Point) mainSnake.getFirst();
if( eatedBean.size() != 0 )
{
if( eatedBean.getFirst().equals( temp ) )
{
eatedBean.remove( 0 );
}
else
{
mainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y,
singleWidthX, singleHeightY, cooPos );
mainSnake.removeTail();
}
}
else
{
mainSnake.clearEndSnakePiece( getGraphics(), temp.x, temp.y,
singleWidthX, singleHeightY, cooPos );
mainSnake.removeTail();
}
}
private void drawBeanAndEBean( Graphics g )
{
g.setColor( ColorGroup.COLOR_BEAN );
this.drawPiece( g, this.bean.x, this.bean.y );
g.setColor( ColorGroup.COLOR_EATEDBEAN );
snakeSq = eatedBean.iterator();
while ( snakeSq.hasNext() )
{
Point tempPoint = (Point)snakeSq.next();
this.drawPiece( g, tempPoint.x, tempPoint.y );
}
}
private void drawPiece( Graphics g, int x, int y )
{
g.fillRoundRect( this.singleWidthX * x + 1,
this.singleHeightY * y + 1,
this.singleWidthX - 2,
this.singleHeightY - 2,
this.cooPos,
this.cooPos );
}
private void createBean( Point temp )
{
LP:
while( true )
{
temp.x = (int) (Math.random() * this.horizontalGrid);
temp.y = (int) (Math.random() * this.verticalGrid);
snakeSq = mainSnake.iterator();
while ( snakeSq.hasNext() )
{
if( snakeSq.next().equals( new Point( temp.x, temp.y ) ) )
{
continue LP;
}
}
break;
}
}
}
//*************//
class ConfigDialog extends JDialog
{
private Container c;
private JFrame owner;
private OwnPanel[] panel = new OwnPanel[4];
Box box1, box2;
private JButton commitButton, cancelButton;
Color[] color = new Color[4];
public ConfigDialog( Frame owner )
{
this.owner = (JFrame) owner;
this.setSize( 400, 200 );
this.setResizable( false );
this.setTitle( "Config Your Game" );
this.c = this.getContentPane();
this.c.setBackground( Color.WHITE );
this.c.setLayout( new FlowLayout() );
this.box1 = Box.createVerticalBox();
for( int i = 0; i < panel.length; i++ )
{
panel[i] = new OwnPanel();
panel[i].addActionListener( new ActionHandler() );
this.box1.add( panel[i] );
this.box1.add( Box.createVerticalStrut( 4 ) );
}
this.panel[0].setText( " Background" );
this.panel[1].setText( " Snake" );
this.panel[2].setText( " Bean" );
this.panel[3].setText( " EatedBean" );
this.panel[0].setBack( ColorGroup.COLOR_BACK );
this.panel[1].setBack( ColorGroup.COLOR_SNAKE );
this.panel[2].setBack( ColorGroup.COLOR_BEAN );
this.panel[3].setBack( ColorGroup.COLOR_EATEDBEAN );
this.box2 = Box.createHorizontalBox();
this.commitButton = new JButton( "確定" );
this.commitButton.setFont( Font.getFont( "Fixedsys" ) );
this.commitButton.addActionListener( new ActionHandler() );
this.cancelButton = new JButton( "取消" );
this.cancelButton.setFont( Font.getFont( "Fixedsys" ) );
this.cancelButton.addActionListener( new ActionHandler() );
this.box2.add( this.commitButton );
this.box2.add( Box.createHorizontalStrut( 20 ) );
this.box2.add( this.cancelButton );
this.box1.add( this.box2 );
this.c.add( this.box1, BorderLayout.NORTH );
this.setLocation( ( this.getToolkit().getScreenSize().width - this.getWidth() )/2,
( this.getToolkit().getScreenSize().height - this.getHeight() )/2 );
this.setVisible( true );
}
public void setOwnerColor( Color temp )
{
this.owner.getContentPane().setBackground( temp );
}
private class ActionHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
for( int i = 0; i < color.length; i++ )
{
if( e.getSource() == panel[i].reButton() )
{
color[i] = JColorChooser.showDialog( ConfigDialog.this,
"Choose BackGround Color",
Color.WHITE );
if( color[i] != null )
{
panel[i].setBack( color[i] );
}
}
}
if( e.getSource() == commitButton )
{
color[0] = (color[0]==null?ColorGroup.COLOR_BACK:color[0]);
color[1] = (color[1]==null?ColorGroup.COLOR_SNAKE:color[1]);
color[2] = (color[2]==null?ColorGroup.COLOR_BEAN:color[2]);
color[3] = (color[3]==null?ColorGroup.COLOR_EATEDBEAN:color[3]);
ConfigDialog.this.setVisible( false );
ColorGroup.setCOLOR_BACK( color[0] );
owner.getContentPane().setBackground( color[0] );
ColorGroup.setCOLOR_SNAKE( color[1] );
ColorGroup.setCOLOR_BEAN( color[2] );
ColorGroup.setCOLOR_EATEDBEAN( color[3] );
ConfigDialog.this.dispose();
}
else if( e.getSource() == cancelButton )
{
ConfigDialog.this.setVisible( false );
ConfigDialog.this.dispose();
}
}
}
}
class OwnPanel extends JPanel
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -