Tai game snake xenzia java




















This is a FIFO approach, i. We need to create methods for. Game Credits to the classes we've made so far the forthcoming section is actually surprisingly easy!

We need a class Game, which contains data members to take care of the snake and the board. We need a set of fixed directions for movement, and a direction member to determine movement. Along with a boolean to check for the game to be over! One characteristic is that, even though direction of movement is somewhat characteristic to the snake, the position is actually attributed to the cell, which is a part of the board.

Since we have the snake as well as access to current values here, and have also added direction members here, it seems right to and modular to maintain directions here. You are welcome to compile and run them together, maybe even develop a friendly interface and actually play it! You see, the way to solve a problem is logically breaking it into smaller problems. Now go ahead and maybe build another game, or break down your own game into smaller fragments and implement it!

Share this. This class would be responsible for rendering the contents of the game board to the screen each frame, and for calling methods of the game model when receiving input. It would also update the game model each tick. Following the idea of separating the game model from the rendering, there are many places where the code could be made more object oriented.

It is not mandatory to write code in an object oriented way, but it is especially helpful when you are a beginner, and I personally find that it makes code easier to write and understand. Below the SnakeWindow class there would be the SnakeGame class that would be responsible for the rules of the game. This class would have things like the speed of the game, or the speed that food spawns.

It would have fields for the Snake object as well as the Board object. The window would update the SnakeGame each tick, and the game object would call the necessary methods of the snake and the board.

The Board object could have a simple int[][] , and each position on the board could have a 0 for empty space, a 1 for a wall, and a 2 if a food is present. I think it would be best if it were responsible for the creation of food, so it could have a variable such as maxFood , and the Board would be called each tick to create the food.

The window class would get information about the Snake and the Board from the game object and display this information on the screen.

Related to this, I would remove all information about the dotSize from the game model and keep this information in the window. It's best to treat the board as discrete [x][y] locations and let the window be responsible for what size to draw them. I would put the collision methods for the walls and food in the SnakeGame class, checking the positions of the snake against the positions of walls and food in the Board object.

You could put the collision method for the snake against itself in this class, or alternatively put it inside the Snake class. For clarity though I would put it in the game class to keep all of the main game logic and rules in a central location.

The fact that the max position of the food is 40 but then that number is multiplied by the dot size is confusing. It is also strange that the Food object has a field for the max position on the board at all. It means that this class is dependent on information that is actually contained in another class. I think it would be better to initialize the food object with values for the width and height of the board.

Shouldn't there only be one instance of the Snake object? I'm not sure, but it appears that you are creating a snake object every time you create a food object.

If you don't want food to spawn on spaces occupied by the snake, then I would pass a reference to the snake into the food object and allow it to check against those positions.

But really, I do not think that a Food class is even necessary. I think it would be better to let the Board be responsible for creating the food, and to do so it could just change an x and y position to the integer that represents food. I find it strange that the snake needs to have arrays that are the size of the entire board.

Additionally, it seems to me that each joint of the snake should have a discrete x and y position. It doesn't make sense to me to keep track of these in two separate arrays. I believe I would recommend using a Point or Joint class here to keep track of the x and y values. You could have an array or list of these objects, and when moving you could iterate over the list and move each point the way that it needs to be moved.

I think that the initial size of the snake should either be in the game object or inside the snake object. Is it correct that all of the initial joints are being created on the same location?

I think it would be better to initialize the Snake with a starting x and y position, and have the joints be created in the logical following positions in whatever direction you want. I think that it is the wrong approach to treat the zero index of the array as the head of the snake.

This relies on other areas of your code knowing that you must call the Snake methods with a 0 in order to get the position of the head. I think it would be better to have an explicit getSnakeHead method that would return the x and y position for the head for the purpose of collision detection. I hope that I have not overwhelmed you with information.

If you are going to continue doing game development in Java, I would highly recommend looking into libGDX. It has a much better structure for things such as the render loop, input handling, and screen management and transitions.

We control the head of the snake. We can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.

In the checkCollision method, we determine if the snake has hit itself or one of the walls. The setResizable method affects the insets of the JFrame container on some platforms.

Therefore, it is important to call it before the pack method. Otherwise, the collision of the snake's head with the right and bottom borders might not work correctly. Snake Snake is an older classic video game. Development of Java Sname game The size of each of the joints of a snake is 10 px.



0コメント

  • 1000 / 1000