📄 game.java
字号:
package org.han.snake.game;
import java.awt.BorderLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import org.han.snake.MenuListener.MenuListener;
import org.han.snake.controller.Controller;
import org.han.snake.entities.Food;
import org.han.snake.entities.Ground;
import org.han.snake.entities.ShowScore;
import org.han.snake.entities.Snake;
import org.han.snake.util.Global;
import org.han.snake.view.GameMenu;
import org.han.snake.view.GamePanel;
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();
ShowScore showScore = new ShowScore(snake);
Controller controller = new Controller(snake,food,ground,gamepanel,showScore);
GameMenu gameMenu = new GameMenu();
GameMenu helpMenu = new GameMenu();
JMenuBar gameMenuBar = new JMenuBar();
JMenuItem newGameMenuItem = new JMenuItem("开始游戏");
JMenuItem quitGameMenuItem = new JMenuItem("退出");
JMenuItem helpGameMenuItem = new JMenuItem("关于");
javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
frame.setSize(Global.WIDTH*Global.CELL_SIZE,Global.HEIGHT*Global.CELL_SIZE+58);
frame.setLocation(300,200);
gameMenu.setText("菜单");
gameMenu.add(newGameMenuItem);
newGameMenuItem.addMouseListener(new MenuListener(controller));
gameMenu.addSeparator();
gameMenu.add(quitGameMenuItem);
helpMenu.setText("帮助");
helpMenu.add(helpGameMenuItem);
gameMenuBar.add(gameMenu);
gameMenuBar.add(helpMenu);
frame.add(gamepanel,BorderLayout.CENTER);
frame.setJMenuBar(gameMenuBar);
frame.setResizable(false);
frame.setVisible(true);
snake.addSnakeListener(controller);
gamepanel.addKeyListener(controller);
frame.addKeyListener(controller);
controller.newGame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -