📄 snakegame.java
字号:
{
private JLabel temp1;
private JTextField temp2;
private JButton temp3;
OwnPanel()
{
temp1 = new JLabel();
temp1.setFont( Font.getFont( "Fixedsys" ) );
temp2 = new JTextField();
temp3 = new JButton( "Change" );
temp3.setFont( Font.getFont( "Fixedsys" ) );
temp2.setEditable( false );
temp2.setColumns( 10 );
this.add( temp1 );
this.add( temp2 );
this.add( temp3 );
this.setLayout( new GridLayout( 1, 3 ) );
}
public void setButtonName( String temp )
{
temp3.setName( temp );
}
public void setBack( Color temp )
{
temp2.setBackground( temp );
}
public void setText( String temp )
{
temp1.setText( temp );
}
public Object reButton()
{
return temp3;
}
public void addActionListener( ActionListener ac )
{
temp3.addActionListener( ac );
}
}
//*******************//
class ConfigMenu extends JMenuBar
{
GameFrame owner;
JMenu[] menu;
JMenuItem[] menuItem;
JRadioButtonMenuItem[] speedItem, modelItem, standardItem;
private UIManager.LookAndFeelInfo looks[];
public ConfigMenu( GameFrame owner )
{
this.owner = owner;
owner.setJMenuBar( this );
String[] menu_name = {"Snake Game", "Game Configure", "Game Help"};
menu = new JMenu[menu_name.length];
for( int i = 0; i < menu_name.length; i++ )
{
menu[i] = new JMenu( menu_name[i] );
menu[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
this.add( menu[i] );
}
String[] menuItem_name = {"Start Game", "Stop Game", "Exit Game",
"Game Color",
"About..."};
menuItem = new JMenuItem[menuItem_name.length];
for( int i = 0; i < menuItem_name.length; i++ )
{
menuItem[i] = new JMenuItem( menuItem_name[i] );
menuItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
menuItem[i].addActionListener( new ActionHandler() );
}
menu[0].add( menuItem[0] );
menu[0].add( menuItem[1] );
menu[0].addSeparator();
menu[0].add( menuItem[2] );
menu[1].add( menuItem[3] );
menu[2].add( menuItem[4] );
String[] inner_menu_name = {"Game Speed", "Window Model", "Game Standard "};
JMenu[] inner_menu = new JMenu[inner_menu_name.length];
for( int i = 0; i < inner_menu_name.length; i++ )
{
inner_menu[i] = new JMenu( inner_menu_name[i] );
inner_menu[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
menu[1].add( inner_menu[i] );
}
ButtonGroup temp1 = new ButtonGroup();
String[] speedItem_name = {"Speed-1", "Speed-2", "Speed-3", "Speed-4", "Speed-5"};
speedItem = new JRadioButtonMenuItem[speedItem_name.length];
for( int i = 0; i < speedItem_name.length; i++ )
{
speedItem[i] = new JRadioButtonMenuItem( speedItem_name[i] );
inner_menu[0].add( speedItem[i] );
speedItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
speedItem[i].addItemListener( new ItemHandler() );
temp1.add( speedItem[i] );
}
ButtonGroup temp2 = new ButtonGroup();
String[] modelItem_name = { "Linux", "Mac", "Windows" };
modelItem = new JRadioButtonMenuItem[modelItem_name.length];
for( int i = 0; i < modelItem_name.length; i++ )
{
modelItem[i] = new JRadioButtonMenuItem( modelItem_name[i] );
inner_menu[1].add( modelItem[i] );
modelItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
modelItem[i].addItemListener( new ItemHandler() );
temp2.add( modelItem[i] );
}
ButtonGroup temp3 = new ButtonGroup();
String[] standardItem_name = { "60 * 40", "45 * 30", "30 * 20" };
standardItem = new JRadioButtonMenuItem[standardItem_name.length];
for( int i = 0; i < standardItem_name.length; i++ )
{
standardItem[i] = new JRadioButtonMenuItem( standardItem_name[i] );
inner_menu[2].add( standardItem[i] );
standardItem[i].setFont( new Font( "Courier", Font.PLAIN, 12 ) );
standardItem[i].addItemListener( new ItemHandler() );
temp3.add( standardItem[i] );
}
looks = UIManager.getInstalledLookAndFeels();
}
private class ActionHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
if( e.getSource() == menuItem[0] )
{
owner.resetGame();
ConfigMenu.this.setVisible( false );
}
else if( e.getSource() == menuItem[1] )
{
owner.stopGame();
ConfigMenu.this.setVisible( true );
ConfigMenu.this.setMenuEnable( true );
}
else if( e.getSource() == menuItem[2] )
{
System.exit( 0 );
}
else if( e.getSource() == menuItem[3] )
{
ConfigDialog temp = new ConfigDialog( owner );
temp.setVisible( true );
}
else if( e.getSource() == menuItem[4] )
{
JOptionPane.showMessageDialog( null, "Sanke Game 2.0 Version!\n\n" +
"Author: FinalCore\n\n" );
}
}
}
private class ItemHandler implements ItemListener
{
public void itemStateChanged( ItemEvent e )
{
for( int i = 0; i < speedItem.length; i++ )
{
if( e.getSource() == speedItem[i] )
{
owner.snakeTimer.setDelay( 150 - 30 * i );
}
}
if( e.getSource() == standardItem[0] )
{
owner.setGrid( 60, 40, 5 );
}
else if( e.getSource() == standardItem[1] )
{
owner.setGrid( 45, 30, 10 );
}
else if( e.getSource() == standardItem[2] )
{
owner.setGrid( 30, 20, 15 );
}
for( int i = 0; i < modelItem.length; i++ )
{
if( e.getSource() == modelItem[i] )
{
try
{
UIManager.setLookAndFeel( looks[i].getClassName() );
}catch(Exception ex){}
}
}
}
}
public void setMenuEnable( boolean temp )
{
menu[1].setEnabled( temp );
}
}
class ColorGroup
{
static Color COLOR_BACK = Color.WHITE;
static Color COLOR_SNAKE = new Color( 43, 110, 187 );
static Color COLOR_BEAN = Color.BLUE;
static Color COLOR_EATEDBEAN = Color.CYAN;
static void setCOLOR_BACK( Color temp )
{
COLOR_BACK = temp;
}
static void setCOLOR_SNAKE( Color temp )
{
COLOR_SNAKE = temp;
}
static void setCOLOR_BEAN( Color temp )
{
COLOR_BEAN = temp;
}
static void setCOLOR_EATEDBEAN( Color temp )
{
COLOR_EATEDBEAN = temp;
}
}
//****************//
final class MenuGet
{
public static JMenu getMenu( String temp )
{
JMenu reMenu = new JMenu( temp );
reMenu.setFont( Font.getFont( "Fixedsys" ) );
return reMenu;
}
}
final class MenuItemGet
{
public static JMenuItem getMenuItem( String temp )
{
JMenuItem reMenuItem = new JMenuItem( temp );
reMenuItem.setFont( Font.getFont( "Fixedsys" ) );
return reMenuItem;
}
}
//***********************//
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 )
{
g.setColor( ColorGroup.COLOR_BACK );
g.fillRoundRect( singleWidthX * temp1 + 1,
singleHeightY * temp2 + 1,
singleWidthX - 2,
singleHeightY - 2,
cooPos,
cooPos );
}
}
//*********************//
public class SnakeGame
{
public static void main( String[] args )
{
JDialog.setDefaultLookAndFeelDecorated( true );
GameFrame temp = new GameFrame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -