How to write Snake Mini Game in Java
Editor to share with you how to use Java to write gluttonous Snake Mini Game, I believe most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Design ideas:
By the startup method main method of GameStart, Date stores external data, and GamePanel draws gluttonous snakes and listens for input.
GamePanel:
Import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Random;public class GamePanel extends JPanel implements KeyListener,ActionListener {int lenth; int [] snakeX = new int [600]; / / Snake coordinates X int [] snakeY = new int [600]; / / Snake coordinates Y String fx / / manipulate the upper and lower left and right boolean isStart = false; Timer timer = new Timer (100Magine this); / / define a food int foodx; int foody; Random random = new Random (); / / add a death judgment boolean isFail = false; boolean isPass = false; / / whether the game is cleared / / points system int score / / Constructor public GamePanel () {init (); / / get keyboard listening events this.setFocusable (true); this.addKeyListener (this); timer.start (); / / Let the time move} / / initialize the game public void init () {lenth = 3; snakeX [0] = 100; snakeY [0] = 100 / / head coordinates snakeX [1] = 75; snakeY [1] = 100 random.nextInt / first body coordinates snakeX [2] = 50; snakeY [2] = 100 score / second body coordinates fx = "R"; foodx = 25 + 25 * random.nextInt (34); foody = 75 + 25 * random.nextInt (24); score = 0 } / / Sketchboard drawing interface and snake / / Graphics: brush @ Override protected void paintComponent (Graphics g) {super.paintComponent (g); / / clear screen this.setBackground (Color.BLUE); / / set the color of the background / / draw the advertising bar Date.header.paintIcon of the head (this, g, 25,11) / draw the game area g.fillRect (25,75,850,600); / / draw a static snake if (fx.equals ("R")) {Date.right.paintIcon (this,g,snakeX [0], snakeY [0]);} else if (fx.equals ("L")) {Date.left.paintIcon (this,g,snakeX [0], snakeY [0]) } else if (fx.equals ("U")) {Date.up.paintIcon (this,g,snakeX [0], snakeY [0]);} else if (fx.equals ("D")) {Date.down.paintIcon (this,g,snakeX [0], snakeY [0]);} for (int I = 1; I)
< lenth; i++) { Date.body.paintIcon(this,g,snakeX[i],snakeY[i]); // 蛇的身体长度通过了lenth控制 } // 画食物 Date.food.paintIcon(this,g,foodx,foody); // 画积分 g.setColor(Color.WHITE); g.setFont(new Font("微软雅黑",Font.BOLD,18)); // 设置字体 g.drawString("您的积分是:" + score,700,50); g.drawString("您的长度是:" + lenth,700,35); // 游戏提示:是否开始 if (isStart == false){ // 画一个文字String 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("摁下空格开始游戏", 200, 300); } if (isPass) { g.setColor(Color.YELLOW); g.setFont(new Font("微软雅黑", Font.BOLD, 40));//设置字体 g.drawString("游戏通关!", 350, 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(); //刷新界面 } // 键盘控制走向 if(keyCode == KeyEvent.VK_LEFT){ fx = "L"; }else if (keyCode == KeyEvent.VK_UP){ fx = "U"; }else if (keyCode == KeyEvent.VK_DOWN){ fx = "D"; }else if (keyCode == KeyEvent.VK_RIGHT){ fx = "R"; } } // 定时器 :监听时间流动 执行定时操作 @Override public void actionPerformed(ActionEvent e) { //随着分数增加,蛇的运动速度加快,难度增大,并判断是否已通关 int delay = 100 - score / 5; timer.setDelay(delay); if (delay 0; i--) { snakeX[i] = snakeX[i-1]; snakeY[i] = snakeY[i-1]; } // 通过控制方向 让头部移动 if (fx.equals("R")){ snakeX[0] = snakeX[0] + 25; if (snakeX[0]>{isFail = true;}} else if (fx.equals ("L")) {snakeX [0] = snakeX [0]-25; if (snakeX [0])
< 25) { isFail = true;} } else if (fx.equals("U")) { snakeY[0] = snakeY[0] - 25; if (snakeY[0] < 75) { isFail = true; } }else if (fx.equals("D")) { snakeY[0] = snakeY[0] + 25; if (snakeY[0] >{isFail = true;}} / / if the head of the snake coincides with the food coordinates if (snakeX [0] = = foodx & & snakeY [0] = = foody) {/ / length plus one lenth++; snakeX [lenth-1] = foodx-1; snakeY [lenth-1] = foody-1 Score = score + 10; / / regenerate food foodx = 25 + 25 * random.nextInt (34); foody = 75 + 25 * random.nextInt (24);} / / end judgment for (int I = 1; I < lenth) If +) {if (snakeX [0] = = snakeX [I] & snakeY [0] = = snakeY [I]) {isFail = true; break;}} / / refresh interface repaint ();} timer.start () / / Let the time move} / /-do not use the following: @ Override public void keyTyped (KeyEvent e) {/ / Keyboard press to pop up} @ Override public void keyReleased (KeyEvent e) { / / release a key}} above is all the content of the article "how to write gluttonous Snake Mini Game with Java" 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!