In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "Java how to achieve a simple gluttonous snake game", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Java how to achieve a simple gluttonous snake game" bar!
Program design idea
1 Data class
Function: connect the statics folder and convert the pictures in the static resource kit into icons for easy drawing on the panel.
Implementation: use the class.getResource (String path) method.
The code is as follows:
Package com.snake;import javax.swing.*;import java.net.URL;public class Data {/ / Snake head public static URL upUrl = Data.class.getResource ("/ statics/up.png"); public static ImageIcon up = new ImageIcon (upUrl); public static URL downUrl = Data.class.getResource ("/ statics/down.png"); public static ImageIcon down = new ImageIcon (downUrl); public static URL leftUrl = Data.class.getResource ("/ statics/left.png") Public static ImageIcon left = new ImageIcon (leftUrl); public static URL rightUrl = Data.class.getResource ("/ statics/right.png"); public static ImageIcon right = new ImageIcon (rightUrl); / / Snake body public static URL bodyUrl = Data.class.getResource ("/ statics/body.png"); public static ImageIcon body = new ImageIcon (bodyUrl); / / Food public static URL foodUrl = Data.class.getResource ("/ statics/food.png") Public static ImageIcon food = new ImageIcon (foodUrl);}
2 StartGame class
Function: create a game window and add a game panel to the window.
Implementation: create a game window using the JFrame class and add an instantiated object of the GamePanel class using its add () method.
The code is as follows:
Package com.snake;import javax.swing.*;import java.awt.*;public class StartGame {public static void main (String [] args) {/ / create the game window JFrame frame = new JFrame ("Java- gluttonous Snake Mini Game"); / / title frame.setSize (900720); / / window size frame.setLocationRelativeTo (null); / / window display middle screen frame.setResizable (false) / / fixed window size frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / set form closing event frame.add (new GamePanel ()); / / add game content frame.setVisible (true); / / set form visibility}}
3 GamePanel class
Function: to achieve the dynamic page of the game.
Achieve:
(1) init () method: initialize the position of the snake.
(2) eat () method: random seed is used to randomly determine the position of food, and the position of food can not coincide with the position of small snake.
(3) inherit the JPanel class, override the paintComponent (Graphics g) method, draw the title bar, the position of the snake (draw the snake's head according to the direction snake head direction variable), the snake body, the integral bar, the game reminder item and the failure judgment item in the method.
(4) implement the keyPressed (KeyEvent e) method in the KeyListener interface, obtain the keyboard input, and change the game state or the direction variable of the snake head direction according to the keyboard input.
(5) implement the actionPerformed (ActionEvent e) method in the ActionListener interface, perform the snake movement operation according to the game state and direction variables (note that the direct turn-back operation is disabled), and determine the food and death. Use the Timer timer to make the game change dynamically, and use the repaint () method to update the interface in real time.
The code is as follows:
Package com.snake;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 [] snakeX = new int [500]; / / Snake Abscissa int [] snakeY = new int [500]; / / Snake Vertical coordinate int foodX;// Food Abscissa int foodY / / Food snake vertical coordinate int length;// length of gluttonous snake String direction;// gluttonous snakehead direction int score;// integral Random r = new Random (); Timer timer = new Timer (100heroine this); boolean isStart; boolean isFail; / / constructor public GamePanel () {init (); this.setFocusable (true); this.addKeyListener (this); timer.start () } private void init () {length=3; snakeX [0] = 100 X snakeY [0] = 100; snakeX [1] = 75 x snakeY [1] = 100; snakeX [2] = 50 x snakeY [2] = 100; direction = "R"; eat (foodX,foodY); isStart = false; isFail = false; score = 0 } private void eat (int x 25*r.nextInt int y) {x = 25 + 25*r.nextInt (34); y = 75 + 25*r.nextInt (24); for (int I = 0; I)
< length; i++) { if(snakeX[i]==x&&snakeY[i]==y){ x = 25 + 25*r.nextInt(34); y = 75 + 25*r.nextInt(24); } } foodX = x;foodY = y; } protected void paintComponent(Graphics g) { super.paintComponent(g); this.setBackground(Color.white);//设置背景板为白色 //画标题 g.setColor(Color.GREEN); g.setFont(new Font("幼圆",Font.BOLD,50)); g.drawString("贪吃蛇游戏",300,60); //绘制游戏区域 g.setColor(Color.GRAY); g.fillRect(25,75,850,600); //画贪吃蛇头部 if(direction=="R"){ Data.right.paintIcon(this,g,snakeX[0],snakeY[0]); } else if(direction=="L"){ Data.left.paintIcon(this,g,snakeX[0],snakeY[0]); } if(direction=="U"){ Data.up.paintIcon(this,g,snakeX[0],snakeY[0]); } else if(direction=="D"){ Data.down.paintIcon(this,g,snakeX[0],snakeY[0]); } //画身体 for (int i = 1; i < length ; i++) { Data.body.paintIcon(this,g,snakeX[i],snakeY[i]); } //画食物 Data.food.paintIcon(this,g,foodX,foodY); //绘制积分栏 g.setColor(Color.BLACK); g.setFont(new Font("幼圆",Font.BOLD,20)); g.drawString("长度:"+length,730,30); g.drawString("得分:"+score,730,60); //游戏开始提醒 if(isStart==false){ g.setColor(Color.BLACK); 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(); } //判断方向 if(keyCode==KeyEvent.VK_LEFT&&direction!="R"){ direction = "L"; } else if(keyCode==KeyEvent.VK_RIGHT&&direction!="L"){ direction = "R"; } else if(keyCode==KeyEvent.VK_UP&&direction!="D"){ direction = "U"; } else if(keyCode==KeyEvent.VK_DOWN&&direction!="U"){ direction = "D"; } } @Override public void keyReleased(KeyEvent e) { } @Override public void keyTyped(KeyEvent e) { } @Override public void actionPerformed(ActionEvent e) { //判断游戏状态 if(isStart&&!isFail){ //移动身体 for (int i = length-1; i >0; snakeY -) {snakeX [I] = snakeX [I-1]; snakeY [I] = snakeY [I-1];} / / move the head if (direction== "R") {snakeX [0] + = 25; if (snakeX [0] > 850) {snakeX [0] = 25 }} else if (direction== "L") {snakeX [0]-= 25; if (snakeX [0])
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.