贪吃蛇Java源码分析
来源:不详 责任编辑:栏目编辑 发表时间:2013-07-01 05:46 点击:次
关键字:贪吃蛇Java源码import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//=============================================
//Main Class
//=============================================
public class GreedSnake implements KeyListener
{
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;//计分牌
SnakeModel snakeModel=null;// 蛇
public static final int canvasWidth=200;
public static final int canvasHeight=300;
public static final int nodeWidth=10;
public static final int nodeHeight=10;
//----------------------------------------------------------------------
//GreedSnake():初始化游戏界面
//----------------------------------------------------------------------
public GreedSnake()
{
//设置界面元素
mainFrame=new JFrame("GreedSnake");
Container cp=mainFrame.getContentPane();
labelScore=new JLabel("Score:");
cp.add(labelScore,BorderLayout.NORTH);
paintCanvas=new Canvas();
paintCanvas.setSize(canvasWidth+1,canvasHeight+1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas,BorderLayout.CENTER);
JPanel panelButtom=new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 帮助信息
labelHelp=new JLabel("PageUp, PageDown for speed;",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.NORTH);
labelHelp=new JLabel("ENTER or R or S for start;",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.CENTER);
labelHelp=new JLabel("SPACE or P for pause",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.SOUTH);
cp.add(panelButtom,BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
//----------------------------------------------------------------------
//keyPressed():按键检测
//----------------------------------------------------------------------
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(snakeModel.running) switch(keyCode)
{
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 减速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暂停或继续
break;
default:
}
//重新开始
if(keyCode==KeyEvent.VK_R || keyCode==KeyEvent.VK_S
|| keyCode==KeyEvent.VK_ENTER)
{
snakeModel.running=false;
begin();
}
}
//----------------------------------------------------------------------
//keyReleased():空函数
//----------------------------------------------------------------------
public void keyReleased(KeyEvent e)
{
}
//----------------------------------------------------------------------
//keyTyped():空函数
//----------------------------------------------------------------------
public void keyTyped(KeyEvent e)
{
}
//----------------------------------------------------------------------
//repaint():绘制游戏界面(包括蛇和食物)
//----------------------------------------------------
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
//=============================================
//Main Class
//=============================================
public class GreedSnake implements KeyListener
{
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;//计分牌
SnakeModel snakeModel=null;// 蛇
public static final int canvasWidth=200;
public static final int canvasHeight=300;
public static final int nodeWidth=10;
public static final int nodeHeight=10;
//----------------------------------------------------------------------
//GreedSnake():初始化游戏界面
//----------------------------------------------------------------------
public GreedSnake()
{
//设置界面元素
mainFrame=new JFrame("GreedSnake");
Container cp=mainFrame.getContentPane();
labelScore=new JLabel("Score:");
cp.add(labelScore,BorderLayout.NORTH);
paintCanvas=new Canvas();
paintCanvas.setSize(canvasWidth+1,canvasHeight+1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas,BorderLayout.CENTER);
JPanel panelButtom=new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 帮助信息
labelHelp=new JLabel("PageUp, PageDown for speed;",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.NORTH);
labelHelp=new JLabel("ENTER or R or S for start;",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.CENTER);
labelHelp=new JLabel("SPACE or P for pause",JLabel.CENTER);
panelButtom.add(labelHelp,BorderLayout.SOUTH);
cp.add(panelButtom,BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
//----------------------------------------------------------------------
//keyPressed():按键检测
//----------------------------------------------------------------------
public void keyPressed(KeyEvent e)
{
int keyCode=e.getKeyCode();
if(snakeModel.running) switch(keyCode)
{
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 减速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暂停或继续
break;
default:
}
//重新开始
if(keyCode==KeyEvent.VK_R || keyCode==KeyEvent.VK_S
|| keyCode==KeyEvent.VK_ENTER)
{
snakeModel.running=false;
begin();
}
}
//----------------------------------------------------------------------
//keyReleased():空函数
//----------------------------------------------------------------------
public void keyReleased(KeyEvent e)
{
}
//----------------------------------------------------------------------
//keyTyped():空函数
//----------------------------------------------------------------------
public void keyTyped(KeyEvent e)
{
}
//----------------------------------------------------------------------
//repaint():绘制游戏界面(包括蛇和食物)
//----------------------------------------------------
相关新闻>>
- 发表评论
-
- 最新评论 更多>>