XNa advanced programming: Xbox 360 and Windows 2-2

Source: Internet
Author: User


2.2 Game Ideas

It sounds silly to discuss the idea of a game like Pong, but I will try to generalize it. One thing that is important before you start coding is that you should carefully consider what your game looks like and how it feels. The larger the game project, the more important the design process is. But I'm not saying that you can't write games or applications without any idea.ProgramIn fact, many beginners start by writing things, and then step by step improve until it looks great. The problem is that it is difficult to find the best solution during the first attempt.

Pong's game logic is very simple and does not require too much text or images (as shown in 2-1 ).CodeAnd the idea of writing a game becomes very simple. Write down all the components required by the game (such as balls, rackets, and screen borders) and your initial thoughts.


Figure 2-1


Write down your thoughts!

Writing down your ideas does not mean that you have to write down all your ideas and draw UML diagrams frantically until everything is ready. I am very opposed to this method, because you cannot think of everything. In addition, beginners generally do not know how to pre-design the entire game in the best way. However, you can have a clear picture of your game in your mind, and you can start work after you spend enough time designing your mind.

If there are other people working with you in your project, it will be more difficult to explain the concept of the game and keep everyone's work synchronized. For this reason, you have to develop a habit of writing your game ideas on just one page. You can't just show this to others. You have to consider other things. When you write down your thoughts, you will find it more complicated than you think, and you have to consider a lot of related things.

For example, in pong games, you know that you need two rackets and a ball that is moving between the rackets. However, it may be only after you write down your initial idea and the texture material you want to use that will you think of a more detailed gameplay-how many lives each player has? How can I check if the ball hits the edge of the racket and how to deal with it? Does the speed of a racket affect the speed and direction of the ball? When gamers are bored, do they need to increase the speed of the game at regular intervals? Of course, these aspects can be improved after you finish the first version, but it is easier to take all these aspects into account before you start to write complex code.

As an example, I will write down the design concept of the pong game you will develop in this chapter. You may use 5 to 10 pages to write more complex games, but you don't want to write a 100 page idea without a line of code, it takes a lot of time and it is difficult to manage these ideas. Using "Agile Methodology", you will often update your ideas and modify some details. I usually start to draw a UML diagram after the code completes at least 50%, the overview charts and documents are not updated until the end of the project.

Pong game ideas

This game is a simple clone version of pong game, supporting one or two players. In single player mode, the computer controls another racket. In double player mode, two players can use gamepad or the same keyboard on the PC, you can also play on Xbox 360.

The game uses 2D images and sound files for the ball and racket to support special effects when the ball hits the racket or dies once. There is also a very simple game menu to choose from:

    • Single-user mode
    • Double Mode
    • Exit the game

This menu uses a simple texture image. The background is a dark area, and most of the images are bright. You should try to make the game interesting enough.

Game features and gameplay

The game has two screens: menus and games. The menu contains several menu options, including basic balls, two rackets, and a simple scoring system. See a simple sketch to understand the task you will complete (Figure 2-2 ):
Figure 2-2

Technologies used

Obviously, you need to use xNa game studio express without using other class libraries. Because it is only a simple sprites rendering, 3D models and shaders are not required, in addition, games can run on both Windows and Xbox 360 platforms.

As you can see, I managed to put everything on a single page. This is a practice when I write about the concept of a game: first, put everything on one page, and then write some code, if your group or external partners (for example, if you want to show your game ideas to a game publisher) if necessary, you can write a five to ten-page White Paper containing some initial ideas and sketches.

I never talk about coding at the game conception stage. I just need to determine what technology I want to use. Now it is time to consider specific implementation issues. Do not consider your actual capabilities when thinking about a game, because it will limit you, even if you leave them behind!

A complete example of a game concept is the concept of a rocket Commander game (see Figure 2-3). You can view the PDF files in the folder in this chapter. It has four pages and a cover, which contains all the ideas of the rocket Commander. From the cover, you can see that it is very different from the concept of the game, but the basic idea of the game is reflected.
Figure 2-3

Agile Methodology

For software development, the Agile Methodology (rocket Commander) is a concept. Its main idea is to avoid large planning stages, because it brings a lot of risks, it is also difficult to follow unrealistic project schedules. For small projects, you may only need to repeat them for a short period of time, but as the project grows, you will find that you are always solving the problem, these problems may take months or even years to solve, and all of them are difficult to plan.

In fact, do not design everything as detailed as possible, as long as you have some rough ideas. Every part of the concept in the entire agile development process will be repeated many times, and the design is more detailed, the Code is designed with unit testing, and then the actual code is compiled for implementation, finally, test and write the document. Such repetition should not be more than a few weeks, and all parts of a large project will repeat the process.

Agile Methods also include other ideas, such as how to make developers and customers, testers, project managers, and designers better collaborate. Several rules can be used in the agile framework to avoid overall confusion. Because the design phase has been shortened, it seems that there is no detailed design for the project, much like the chaotic strong code. But in fact, the design process runs through more projects and is more useful than designing everything at the beginning and solving all the errors produced at that stage.

I will not talk too much about agile methodologies in this book, but I will use unit tests and simple concepts, unit test design, and then code repeatedly throughout all the projects in this book. If you want to learn more about unit testing, I recommend you visit Martin Fowler's site: http://martinfowler.com.

Martin has also written several books. I recommend refactoring: improving the design of existing code. You can also find it on the website, you can also find more links and information about the agile software development process on his site.

Solve the first problem

Well, you already have the idea of a game, but you haven't spent any time looking for pictures and sound files, and how to implement them and how to deal with the problem of hitting a racket with a ball? Do you need to design a simple physical engine for the ball?

After the design phase, all these problems will emerge, and for more complex projects, it may take weeks or even months to solve some basic problems. For example, I made the first one. NET 2.0 game rocket Commander. I want to display thousands of minor planets on the screen. There are millions in total, and I want them to collide with each other and detect each other, but how to optimize it is not very clear. After initial tests, I found that I could not even render more than 100 minor planets without significantly degrading performance. At this time, I did not rewrite my game ideas, nor is there a poor, boring game of an Minor Planet (if not, it is not a real game-this is the principle of the rocket Commander game ), instead, I tried to solve this problem. I classified the minor by shaders and material settings and rendered them. If the minor is invisible, I cleared them as early as possible, in addition, only the processing of physical processes is applied to adjacent minor planets.

After a week of effort and optimization, the minor can be rendered, and the performance is good, but the physical process is still very slow. Even with the best optimization processing, it still seems too much to update 5000 to 10000 of the planets within the visible range of each frame to view their collisions, however, I still want to make it work normally after the collision with each other. If I only check a certain number of minor planets for each frame,AlgorithmThere is often no collision effect, and collision occurs only when one minor enters the other, resulting in more trouble than removing the physical process. I almost gave up, because I have spent 50% of the time in the project to solve this problem. However, I came up with another idea to divide the whole space into several regions, with each region having a specific number of minor planets, each time an minor leaves a region, it is removed from the region and added to a new region, so that the minor only needs to detect collisions in their own and adjacent areas. This may sound like there are still a lot of collision detection, but the use of "sector logic" does indeed increase the performance of the physical process by 100 times, and the results will be more obvious after optimization.

Now, in most of my games, I spend more than half of my time dealing with the initial challenges and splitting them into smaller ones to solve them, I will reassemble these small fragments later, and soon the game will be completed some days later. Although this is a way to complete a game as quickly as possible, there will be a sense of frustration if you cannot see the running results of the game for a long time. In the past, I always repeatedly modified my game until I was satisfied with the results, which indeed took a long time. Thanks to the agile development process, now I can solve these problems and there is no need to repeat the endless improvement cycle.

What does this mean for your pong game? The game is very simple. You don't need to spend a lot of time constantly improving it for 100 times. Just implement a basic version of pong game, a hard-coded racket for the computer, move up and down at the left of the screen as the ball moves, and control the racket on the right. After you have improved and tested the single player mode, you may want to support the dual player mode, but this is very difficult because you hardcoded the implementation part of the computer racket. You may comment out the code or simply delete it, and modify the code of the gameplay part again. After testing the dual-game mode, you may want to add another menu, and you can adjust and modify the code again to continue.

Although this is a good practice and forces you to constantly refactor your code (refactoring means modifying the code design without changing the functionality makes it easier to maintain, refer to the refactoring book written by Martin Fowler recommended earlier). However, if you focus only on one part of the game at a time, it is easier to use several versions for testing at the same time. In the next part of this chapter, you will do this. I hope you can understand what I mean. With unit testing, code refactoring is more natural and makes your code easier to use. It also allows you to see the running results faster.

The next question you need to solve is how to handle the physical motion of the ball and how to detect the part where the ball hits the racket.

Create texture

Before coding, You need to display the texture of game elements on the screen. Even if you use dummy bitmap or images from other places, it is important to consider the final image effect, such as the size of the ball and racket.

Again, your pong game is quite simple, however, a more complex game may require an artistic idea, a lot of sketches, and a person who can manage these design works and complete them in a timely manner so that game programmers can implement them. Looking back at the early days of computer games, almost all games were completed by one person. Although this is the easiest way, today no formal game can be developed by only one person. Most excellent art designers are not programmers, and most programmers are not good art designers. It doesn't matter if you only make a simple game, but the larger your project, you need more time to find partners, experienced art designers and model makers, and effectively organize them.

To view a 2-4 game image, you need three texture files: (1) spacebackground. Dds watermark, used to modify the racket and ball.
Figure 2-4

In addition, you need to import two audio files (pongballhit.wavand pongballlost.wav) to your project, and add a simple xact project to play the sound. For more information about audio processing and xact, see Chapter 9.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.