How to realize simple GUI gluttonous Snake Mini Game by Java
Editor to share with you how Java to achieve a simple GUI gluttonous Snake Mini Game, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Main startup class
Import javax.swing.*;/** * @ author The Setting sun * @ create 2021-09-11 15:19 * / / main launch class public class StartGame {public static void main (String [] args) {JFrame frame = new JFrame (); frame.setResizable (false); / / window size immutable frame.setSize (900,720); / / window size frame.setLocationRelativeTo (null) / / window centered frame.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); / / window closing event frame.add (new GamePanel ()); frame.setVisible (true); / / window visible}}
Resource class (icon)
Import javax.swing.*;import java.net.URL;/**@author The Setting sun@create 2021-09-11 15:40*/public class Data {public static URL headerURL = Data.class.getResource ("img/header.png"); public static ImageIcon header = new ImageIcon (headerURL); public static URL upURL = Data.class.getResource ("img/tx.png"); public static ImageIcon up = new ImageIcon (upURL); public static URL downURL = Data.class.getResource ("img/down.png") Public static ImageIcon down = new ImageIcon (downURL); public static URL rightURL = Data.class.getResource ("img/right.png"); public static ImageIcon right = new ImageIcon (rightURL); public static URL leftURL = Data.class.getResource ("img/left.png"); public static ImageIcon left = new ImageIcon (leftURL); public static URL bodyURL = Data.class.getResource ("img/body.png"); public static ImageIcon body = new ImageIcon (bodyURL) Public static URL foodURL = Data.class.getResource ("img/food.png"); public static ImageIcon food = new ImageIcon (foodURL);}
Program class
Import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.Random;/** * @ author The Setting sun * @ create 2021-09-11 15:24 * / / Game panel public class GamePanel extends JPanel implements KeyListener, ActionListener {/ / snake int length;// snake length int [] snakeX = new int []; / / snake x coordinate 25 int [] snakeY = new int [500] / / Snake's y coordinate 25 String orient;// initial direction / / Food int foodX; int foodY; Random random = new Random (); boolean isStart = false;// current state of the game: stop boolean isFail = false;// game failed by default, there is no int score;// integral / / timer ms in units Timer timer = new Timer (140, this) / / refresh once in 100ms / / Constructor public GamePanel () {init (); / / initialize setFocusable (true); / / get the focus addKeyListener (this); / / get the keyboard listening event timer.start (); / / start the timer as soon as the game starts} / / initialize public void init () {length = 3 / / the initial length is 3 snakeX [0] = 100; snakeY [0] = 100; snakeX [1] = 75; snakeY [1] = 100; snakeX [2] = 50; snakeY [2] = 100; orient = "R" of the second body / / initial direction to the right / / Let the food be randomly distributed on the interface foodX = 25 + 25 * random.nextInt (34); foodY = 75 + 25 * random.nextInt (24); score = 0 / points} / / draw the panel, which is used to draw @ Override protected void paintComponent (Graphics g) {super.paintComponent (g) / / clear screen setBackground (Color.pink); / / draw static panel Data.header.paintIcon (this, g, 25,11); g.fillRect (25,75,850,600); / / draw integral panel g.setColor (Color.GREEN); g.setFont ("Microsoft Acer", Font.BOLD, 16)) G.drawString ("length:" + length, 750,35); g.drawString ("integral:" + score, 750,50); / / draw food Data.food.paintIcon (this, g, foodX, foodY); / / draw snake switch (orient) {case "R": Data.right.paintIcon (this, g, snakeX [0], snakeY [0]) / / Snake initialization to the right, by judging the change direction break; case "L": Data.left.paintIcon (this, g, snakeX [0], snakeY [0]); / / Snake initialization to the right, by judging the change direction break Case "U": Data.up.paintIcon (this, g, snakeX [0], snakeY [0]); / / Snake initialization to the right, by judging the change direction break; case "D": Data.down.paintIcon (this, g, snakeX [0], snakeY [0]) / / initialize the snake to the right and dynamically add the body for (int I = 1; I) by judging the change direction break;} / /
< length; i++) { Data.body.paintIcon(this, g, snakeX[i], snakeY[i]); }// Data.body.paintIcon(this,g,snakeX[1],snakeY[1]);//第1个身体的坐标// Data.body.paintIcon(this,g,snakeX[2],snakeY[2]);//第2个身体的坐标 //游戏状态 if (isStart == false) { g.setColor(Color.WHITE); g.setFont(new Font("微软雅黑", Font.BOLD, 40)); g.drawString("按下空格开始游戏", 300, 300); } if (isFail) { g.setColor(Color.RED); g.setFont(new Font("微软雅黑", Font.BOLD, 40)); g.drawString("犹豫就会败北!", 300, 300); } } //键盘监听器 @Override public void keyPressed(KeyEvent e) {//按下 int keyCode = e.getKeyCode();//获得键盘按键码 if (keyCode == KeyEvent.VK_SPACE) {//按下空格 if (isFail) { //重新开始 isFail = false; init(); } else { isStart = !isStart;//取反 repaint(); } } //修复了一个蛇可以反向移动的bug if (orient.equals("U") && keyCode == KeyEvent.VK_DOWN){ keyCode = KeyEvent.VK_UP; } if (orient.equals("D") && keyCode == KeyEvent.VK_UP){ keyCode = KeyEvent.VK_DOWN; } if (orient.equals("L") && keyCode == KeyEvent.VK_RIGHT){ keyCode = KeyEvent.VK_LEFT; } if (orient.equals("R") && keyCode == KeyEvent.VK_LEFT){ keyCode = KeyEvent.VK_RIGHT; } //控制蛇改变方向移动 switch (keyCode) { case KeyEvent.VK_UP: orient = "U"; break; case KeyEvent.VK_DOWN: orient = "D"; break; case KeyEvent.VK_LEFT: orient = "L"; break; case KeyEvent.VK_RIGHT: orient = "R"; break; } } //事件监听:需要通过固定事件来刷新,1秒10次(帧率) @Override public void actionPerformed(ActionEvent e) { if (isStart && isFail == false) {//如果游戏是开始状态,就让蛇动起来 //吃食物 if (snakeX[0] == foodX && snakeY[0] == foodY) { //长度+1 length++; score += 10; //再次让食物随机出现 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); } //身体移动 for (int i = length - 1; i >0; snakeX -) {snakeX [I] = snakeX [I-1]; snakeY [I] = snakeY [I-1];} / / towards switch (orient) {case "R": snakeX [0] = snakeX [0] + 25 / / Boundary judgment if (snakeX [0] > 850) {snakeX [0] = 25;} break; case "L": snakeX [0] = snakeX [0]-25 / / Boundary judgment if (snakeX [0])
< 25) { snakeX[0] = 850; } break; case "U": snakeY[0] = snakeY[0] - 25; //边界判断 if (snakeY[0] < 75) { snakeY[0] = 650; } break; case "D": snakeY[0] = snakeY[0] + 25; //边界判断 if (snakeY[0] >{snakeY [0] = 75;} break;} / / failure judgment, GG for if you bump into yourself (int I = 1; I < length) ) {if (snakeX [0] = = snakeX [I] & & snakeY [0] = = snakeY [I]) {isFail = true;}} repaint (); / / Refresh Page} timer.start () / / timer on} @ Override public void keyTyped (KeyEvent e) {} @ Override public void keyReleased (KeyEvent e) {}} these are all the contents of the article "how to make Java easy GUI gluttonous Snake Mini Game". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!