📄 贪吃蛇界面.java.bak
字号:
btHelp.setBackground(Color.green);
btHelp.setForeground(Color.red);
//监听关于按钮
btHelp.addKeyListener(this);
btHelp.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
helpArea.setVisible(true);
}
}
);//结束监听关于按钮
panel12.add(btStart);
panel12.add(btParse);
panel12.add(btExit);
panel12.add(btHelp);
panel11.setBackground(Color.cyan);
panel12.setBackground(Color.cyan);
Icon snakePicture = new ImageIcon( "加油.gif" );
picture1 = new JLabel(snakePicture);
picture2= new JLabel(snakePicture);
//****************************** 加入容器 **********************************************
panel1.add(panel11,BorderLayout.NORTH);
panel1.add(panel12,BorderLayout.CENTER);
panel1.add(picture1,BorderLayout.WEST);
panel1.add(picture2,BorderLayout.EAST);
//**************************** panel2——游戏界面 ************************************************************
panel2.setBackground(Color.blue);
//加入容器
container.add(panel1,BorderLayout.SOUTH);
container.add(desk,BorderLayout.CENTER);
requestFocus();
}//构造函数结束
//***********************************************************************************************************************
//**************************************************************************************************************************
//*************************************************************************************************************************
//**************************************changeTheLookAndFeel方法********************************************************
private void changeTheLookAndFeel( int value )
{
try {
UIManager.setLookAndFeel(
looks[ value ].getClassName() );
SwingUtilities.updateComponentTreeUI( this );
}
catch ( Exception exception ) {
exception.printStackTrace();
}
}
//***************************************风格事件处理*****************************************************
private class ItemHandler implements ItemListener {
public void itemStateChanged( ItemEvent event )
{
for ( int count = 0; count < radio.length; count++ )
if ( radio[ count ].isSelected() ) {
changeTheLookAndFeel( count );
}
}
}
//****************************************键盘监听***********************************************************
public void keyPressed(KeyEvent event){
if (event.getKeyCode() == KeyEvent.VK_UP)
{direction = UP;}
if (event.getKeyCode() == KeyEvent.VK_DOWN)
{direction = DOWN;}
if (event.getKeyCode() == KeyEvent.VK_LEFT)
{direction = LEFT;}
if (event.getKeyCode() == KeyEvent.VK_RIGHT)
{direction = RIGHT;}
}
public void keyReleased(KeyEvent event){}
public void keyTyped(KeyEvent event){}
//******************************************速度监听************************************************************
private class listener implements ActionListener{
private long pause;
listener(long pause){
if ( speedtime > 25 )
{ this.pause = pause; }
else{ pause = 25;}
}
public void actionPerformed(ActionEvent e){
speedtime=pause;
}
}
//*****************************************newGame方法定义 ******************************************************
public void newGame(long speedtime)
{
helpArea.setVisible(false);
panelset.setVisible(false);
cardManager.show(desk,"游戏界面");
snakelength = 3; //初始化蛇身长度
score = 0 ;//分数归零
throughbody = 1 ;//穿身宝物为1
throughwall = 1 ;//穿墙宝物为1
parse = false ; //暂停初始化
isEnd = false ;//
thread3.Hour = 0;
thread3.Minute = 0 ;
thread3.Second = 0 ;
btParse.setText("暂停");
textField1.setText(Integer.toString(score));
textField2.setText(Integer.toString(throughbody));
textField3.setText(Integer.toString(throughwall));
for(int i=0;i<ROW;i++){
for(int j=0;j<COLUMES;j++){
playpanels[i][j].setBackground(colors[backBox.getSelectedIndex()]);
panel2.add(playpanels[i][j]);
}
}
this.speedtime = speedtime;
direction = RIGHT;
lastdirection = RIGHT;
//初始化蛇身
for (int i = 0;i < 25 ;i++ )
{
for (int j =0;j < 50 ;j++ )
{
snake[i][j] = 0;
}
}
repaint();
rows = new int[25];
columes = new int[50];
//最初食物位置
int x = (int)(Math.random()*25);
int y = (int)(Math.random()*50);
while (snake[x][y] != 0)
{
x = (int)(Math.random()*25);
y = (int)(Math.random()*50);
}
snake[x][y] = 2;
playpanels[x][y].setBackground(Color.yellow);
for (int i = 0;i <= snakelength ;i++ )
{
rows[i] = 1;
columes[i] = snakelength-i;
}
//设置墙壁
for ( int count = 0; count < map.length; count++ )
{
if ( map[ count ].isSelected() ) {
changeMap( count );}
}
//线程开始
try
{thread.start();}
catch (IllegalThreadStateException illegalThreadStateException)
{}
}
//****************************************地图监听***********************************************************
private class ItemMapHandler implements ItemListener{
public void itemStateChanged( ItemEvent event )
{
for ( int count = 0 ; count < map.length ; count++ )
if (map[count].isSelected() ){
for(int i=0;i<ROW;i++){
for(int j=0;j<COLUMES;j++){
playpanels[i][j].setBackground(colors[backBox.getSelectedIndex()]);
}
}
changeMap(count);
}
//最初食物位置
int x = (int)(Math.random()*25);
int y = (int)(Math.random()*50);
while (snake[x][y] ==1 || snake[x][y] ==5)
{
x = (int)(Math.random()*25);
y = (int)(Math.random()*50);
}
snake[x][y] = 2;
playpanels[x][y].setBackground(Color.yellow);
}
}
//**************************************changeMap方法定义******************************************************
public void changeMap( int i ){
//地图0
if ( i == 0 )
{}
//地图1
else if ( i == 1 )
{
for (int a = 5 ; a < 20 ; a++ )
{ snake[a][25] = 5 ;
playpanels[a][25].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
//地图2
else if ( i == 2 )
{
for (int a = 10 ; a < 40 ;a++ )
{ snake[8][a] = 5 ;
snake[16][a] = 5 ;
playpanels[8][a].setBackground(Color.white);
playpanels[16][a].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
//地图3
else if ( i == 3 )
{
for ( int a = 5 ; a < 20 ; a++ )
{ snake[a][10] = 5 ;
playpanels[a][10].setBackground(Color.white);
snake[a][40] = 5 ;
playpanels[a][40].setBackground(Color.white);
}
for ( int b = 15 ; b < 35 ; b++ )
{ snake[8][b] = 5 ;
playpanels[8][b].setBackground(Color.white);
snake[16][b] = 5 ;
playpanels[16][b].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
//地图4
else if ( i == 4 )
{
for ( int a = 5 ; a <= 20 ; a++ )
{
snake[a][a] = 5 ;
playpanels[a][a].setBackground(Color.white);
snake[a][25-a] = 5 ;
playpanels[a][25-a].setBackground(Color.white);
snake[a][a+20] = 5 ;
playpanels[a][a+20].setBackground(Color.white);
snake[a][45-a] = 5 ;
playpanels[a][45-a].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
//地图5
else if ( i == 5 )
{
for ( int a = 5 ; a < 20 ; a++ )
{
for ( int b = 2 ; b < 24 ; b++ )
{
snake[a][b*2] = 5 ;
playpanels[a][b*2].setBackground(Color.white);
}
}
cardManager.show(desk,"游戏界面");
}
//地图6
else if ( i == 6 )
{
for ( int a = 5 ; a <= 45 ; a++ )
{
snake[5][a] = 5 ;
playpanels[5][a].setBackground(Color.white);
snake[20][a] = 5 ;
playpanels[20][a].setBackground(Color.white);
}
for ( int a = 5 ; a <= 10 ; a++ )
{
snake[a][5] = 5 ;
playpanels[a][5].setBackground(Color.white);
}
for ( int a = 15 ; a <= 20 ; a++ )
{
snake[a][5] = 5 ;
playpanels[a][5].setBackground(Color.white);
}
for ( int a = 5 ; a <= 20 ; a++ )
{
snake[a][45] = 5 ;
playpanels[a][45].setBackground(Color.white);
}
for ( int a = 10 ; a <= 15 ; a++ )
{
snake[a][15] = 5 ;
playpanels[a][15].setBackground(Color.white);
}
for ( int a = 15 ; a <= 35 ; a++ )
{
snake[10][a] = 5 ;
playpanels[10][a].setBackground(Color.white);
snake[15][a] = 5 ;
playpanels[15][a].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
//地图7
else if ( i == 7 )
{
for ( int a = 1 ; a < 6 ; a++ )
{
for ( int b = 0 ; b < 16 ; b++ )
{
snake[a*4][b*3+2] = 5;
playpanels[a*4][b*3+2].setBackground(Color.white);
}
}
cardManager.show(desk,"游戏界面");
}
//地图8
else if ( i == 8 )
{
for ( int a = 5 ; a <= 20 ; a++ )
{
snake[a][a] = 5 ;
playpanels[a][a].setBackground(Color.white);
snake[a][25-a] = 5 ;
playpanels[a][25-a].setBackground(Color.white);
snake[a][a+20] = 5 ;
playpanels[a][a+20].setBackground(Color.white);
snake[a][45-a] = 5 ;
playpanels[a][45-a].setBackground(Color.white);
}
for ( int a = 8 ; a <= 17 ; a++ )
{
snake[a][5] = 5;
playpanels[a][5].setBackground(Color.white);
snake[a][20] = 5;
playpanels[a][20].setBackground(Color.white);
snake[a][25] = 5;
playpanels[a][25].setBackground(Color.white);
snake[a][40] = 5;
playpanels[a][40].setBackground(Color.white);
snake[5][a] = 5;
playpanels[5][a].setBackground(Color.white);
snake[5][a+20] = 5;
playpanels[5][a+20].setBackground(Color.white);
snake[20][a] = 5;
playpanels[20][a].setBackground(Color.white);
snake[20][a+20] = 5;
playpanels[20][a+20].setBackground(Color.white);
}
cardManager.show(desk,"游戏界面");
}
}
//************************************************timeThread线程部分*****************************************************************
public class timeThread extends Thread
{
int Hour = 0;
int Minute = 0 ;
int Second = 0 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -