Tip:Bar rectangle can move.
1.Introduce the statics
image resource file
2. Write the main startup class of the program
Create a new window JFrame
form class, and give it a width and height, which are respectively 900 long and 720 wide. Set the setResizable
function for the window to be non-resizable, set it to false, and display the window.
Since it is greedy for snakes, there must be some attributes and methods in this core class. We can think about it, snakes definitely have a position, and snakes also have a length. Here, the small body of the snake is in our material. Think of it as a square.
How do snakes grow up? There is no doubt that food is eaten, so we also set the position for food. In order to enhance the experience of the game, it is necessary to add a scoring mechanism, and of course the score attribute of the game. After thinking about this, let the little snake move, and there must be a Timer Timer class to move.
Drawing componentsCreate the GamePanel
class to inherit the JPanel
class. All our game screens are provided by the canvas component. The paintComponent
function is the core method of the entire game. Without the entire method, everything is blank. On this canvas All images are dependent on the paintComponent()
function.
alt+insert
calls the override method
and writes this function, then sets the background color of the panel, draws the header information area, food, snake, game prompts, failure judgment, and the game area, etc. are all added.
Here comes the problem again, so how do we judge whether the game is in the starting state, whether the snake can move, and how to solve a series of problems such as how to grow longer after eating food? This reminds me of a monitor, and we can control the moving direction of the snake through the keyboard, so this is also a monitor, right? You can't make the little snake change direction or grow up with your willpower, can you? So here we have to introduce the interface of keyboard monitoring and event monitoring.
* public class GamePanel extends JPanel implements KeyListener, ActionListener {}
And rewrite related methods; actionPerformed
function, keyTyped
function, keyPressed
means the method of pressing the keyboard, and keyReleased
means the method of releasing the keyboard.
The actionPerformed
function first judges whether the state is started, if it starts, then the snake will move, how to move? Except for the head, everything else must move forward, that is, the body moves.
It was said before that the body of the little snake is square, so every time the little snake moves, the next one will move forward, which is equivalent to replacing the previous one.
Eating food: How can we judge that the little snake has eaten food? Think of it this way, when the snake has the same food as the head, even if it has eaten the food, the length will be increased by one after eating the food, and the length will be increased by one if it is eaten. The corresponding score should also be increased, and the default increase is 10 points. After eating the food, it is impossible for the food to remain in place, right? So at this time, when it comes to introducing Random random = new Random()
Random
object, randomly let food appear at any position on the game interface, this position is roughly estimated as follows.
The end of the game: So how to judge the end, of course, if the head and the body collide with each other, it will fail. For the same reason, as long as the snakeX[i] = = snakeX[0] && snakeY[i] ** snakeY[ 0]
means the game is over.
Finally, there is the repain
function, which needs to constantly update the page to make the animation move. repaint();