java贪吃蛇游戏框根据面向对象的思想建立蛇类、食物类、周围石头类、面板类、控制中心类、监听器类、组建类。
Food.java
public class Food {
//蛇是否吃到了食物方法
public boolean isSnakeEatFood(){
System.out.println("蛇是否吃到了食物");
return false;
}
//绘制食物方法
public void drawFood(){
System.out.println("绘制食物");
}
}
Ground.java
public class Ground {
//蛇是否吃到了石头方法
public boolean isSnakeEatrRock(){
System.out.println("蛇是否吃到了石头");
return false;
}
//绘制周围石头方法
public void drawRock(){
System.out.println("绘制周围石头");
}
}
Snake.java
public class Snake {
//监听
private Set
//蛇移动的方法
public void move(){
System.out.println("蛇移动");
}
//蛇改变方向的方法
public void changeDirecion(){
System.out.println("蛇改变方向");
}
//蛇吃食物的方法
public void eatFood(){
System.out.println("蛇吃食物");
}
//蛇是否吃到自己方法
public boolean isEatBody(){
System.out.println("蛇是否吃到自己");
return false;
}
//绘制蛇的方法
public void drawSnake(){
System.out.println("绘制蛇");
}
//游戏开始后蛇不断移动的线程类
class SnakeDriver implements Runnable{
public void run() {
while(true){
move();
//遍历监听组
for(SnakeListener l:listeners){
l.snakeMoved(Snake.this);
}
try {
//每隔一秒钟移动一次
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//启动线程方法
public void start(){
new Thread(new SnakeDriver()).start();
}
//增加监听的方法
public void addSnakelistener(SnakeListener l){
if(l !=null){
this.listeners.add(l);
}
}
}
GamePanel.java
public class GamePanel extends JPanel{
private Snake snake;
private Food food;
private Ground ground;
//显示绘制的图像方法
public void display(Snake snake,Food food,Ground ground){
System.out.println("显示绘制的图像");
this.snake=snake;
this.food=food;
this.ground=ground;
this.repaint();
}
//重新显示绘制图像方法
protected void paintComponent(Graphics g) {
// TODO Auto-generated method stub
this.ground.drawRock();
this.food.drawFood();
this.snake.drawSnake();
}
}
SnakeListener.java 监听蛇移动接口
public interface SnakeListener {
void snakeMoved(Snake snake);
}
Controller.java 控制中心处理业务逻辑,处理键盘事件和实现监听接口
public class Controller extends KeyAdapter implements SnakeListener{
private Snake snake;
private Food food;
private Ground ground;
private GamePanel gamepanel;
public Controller(Snake snake, Food food, Ground ground, GamePanel gamepanel) {
super();
this.snake = snake;
this.food = food;
this.ground = ground;
this.gamepanel = gamepanel;
}
//处理键盘事件
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
snake.changeDirecion();
break;
case KeyEvent.VK_DOWN:
snake.changeDirecion();
break;
case KeyEvent.VK_LEFT:
snake.changeDirecion();
break;
case KeyEvent.VK_RIGHT:
snake.changeDirecion();
break;
}
}
//实现监听接口
public void snakeMoved(Snake snake) {
// TODO Auto-generated method stub
gamepanel.display(snake, food, ground);
}
//开始新游戏方法
public void gameStart(){
snake.start();
}
}
Game.java 组建类
public class Game {
public static void main(String[] args) {
Snake snake =new Snake();
Food food =new Food();
Ground ground=new Ground();
GamePanel gamepanel=new GamePanel();
Controller controller=new Controller(snake,food,ground,gamepanel);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.add(gamepanel, BorderLayout.CENTER);
gamepanel.addKeyListener(controller);
snake.addSnakelistener(controller);
frame.setVisible(true);
controller.gameStart();
}
}
框架搭建完成,接下来就是一步一步实现这些方法,最后完成效果如下: