A c # console game (source code parsing)

Source: Internet
Author: User
Tags map class

A few days ago, I published the game exe file, and sorted out the code-download the source code in the past few days, so I came up to write blogs immediately. Now, the question is starting.

The program class diagram is as follows:

 

Program: Program class -- main method

Game: Game-Game running and basic role information calling

From: Map class-depicts game borders and prompts

Tank: Tanks-Tank rendering, behavior, and collision handling

EnemyTank: enemy tanks-tank rendering, behavior, and collision handling

Bullet: Bullet class-Bullet drawing, behavior, collision Processing

Point: Location Structure-record tank and bullet positions

Direction: recording the Direction of tanks and bullets

Of course, this game is mainly composed of map, tank drawing, action, collision, bullet drawing, action, and collision. The following describes one by one:

1. Map

/// <Summary> /// draw a border /// </summary> public void DrawBorder () {Console. foregroundColor = ConsoleColor. gray; Console. cursorVisible = false; Console. setCursorPosition (Left, Top); Console. write ("substring" + new string ('struct', width) + "substring"); for (int I = 0; I <= height; I ++) {if (I! = Height) {Console. setCursorPosition (left, top + I + 1); Console. write ("signature" + new string ('', 2 * width) +" signature ");} else {Console. setCursorPosition (left, top + I + 1); Console. write ("signature" + new string ('signature', width) + "signature") ;}} Config. config. func ();}

Console. ForegroundColor -- get or set the color of the current Console
Console. CursorVisible -- used to indicate whether the cursor is visible
Console. SetCursorPosition (,) -- set the cursor position
(The above three will be used for subsequent plotting, so I will not explain the usage)

Looking at the code, I can know that the person will get a frame pattern. This is the map of the game.

2. Get Keyboard Events

/// <Summary> /// obtain the keyboard button status /// </summary> /// <returns> </returns> public bool StartInput () {ConsoleKeyInfo key; while (true) {key = Console. readKey (true); if (! HandleInput (key) {return false;} return true ;}

This method is enabled and loaded in the Game class. It is used to control the upper, lower, and lower sides of the tank and to launch bullets. The Console is used. readKey (true); the pressed Keyboard can be obtained, and the content is continuously circulating. The HandleInput (key) method is used to control tank behavior and is not executed once, if the tank takes one step or launches a bullet, the system jumps out of the modification method when false is returned.

3. Collision judgment between objects

/// <Summary> /// determine whether a border or enemy tank is hit. // </summary> /// <param name = "type"> </param> // /<returns> </returns> public int Collide (MoveType type) {int tan = 0; // left if (type = MoveType. left) {if (x <= 1) {tan = x;} else {if (From. map [x-2, Y-1] = 3 | From. map [x-2, y] = 3 | From. map [x-2, y + 1] = 3) {tan = x ;}else {x-= 1; tan = x ;}}} // right if (type = MoveType. right) {if (x> = From. width-width + 1) {tan = x;} else {if (From. map [x + 2, Y-1] = 3 | From. map [x + 2, y] = 3 | From. map [x + 2, y + 1] = 3) {tan = x;} else {x + = 1; tan = x ;}}} // if (type = MoveType. up) {if (y <= 1) {tan = y;} else {if (From. map [x-1, y-2] = 3 | From. map [x, y-2] = 3 | From. map [x + 1, y-2] = 3) {tan = y;} else {y-= 1; tan = y ;}}} // if (type = MoveType. down) {if (y> From. height-3) {tan = y;} else {if (From. map [x-1, y + 2] = 3 | From. map [x, y + 2] = 3 | From. map [x + 1, y + 2] = 3) {tan = y;} else {y + = 1; tan = y ;}} return tan ;}

Collision between objects. For example, how can a tank interact with an enemy tank to determine whether to prevent them from blocking each other, and how can a bullet hit an enemy tank to determine whether to cause it to explode. This is indeed a headache, but if you carefully pay attention to every tank or bullet draw, where From. Map [,] I assign values to different values. I assign values to my tank. 2. assign values to enemy tanks. 3. assign values to bullets. 1. Then, after clearing the trajectory, all values are assigned 0. Then, we can see the above judgment conditions, I think everyone knows how to control their collision judgment.

The above three problems are typical program problems. Drawing, running, and collision. Everyone knows about other things, so not all of them are taken out here. Of course, there are some things you should pay attention to when you see them: for example, when drawing maps, tanks, and moving them, I use two cursor positions, the length is always at the position of a cursor. And to From. when adding values in the array position in Map [,], note that the array starts with 0, while the Map content starts with 1, so each time they add a value, I need to subtract 1.

Now, this article is written. The code is all open-source. If any of you are interested, you can modify the code yourself (this mini-game is very welcome ). Another problem that occurred immediately after the zzd user raised an opinion on the enemy's tank. I also forgot to change it (too lazy and don't want to change it any more ), if you are not familiar with the details, you can add QQ Group 253674268. We welcome you to join us.

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.