[Algorithm] Learning in the game, using c# object-oriented features to control the movement of game characters
Recently, Xiaoyue’s life is like a busy symphony. She is so busy every day. Although her schedule is full, she does not feel fulfilled. Instead, she had little time to spend with her family, which she felt a little regretful about. On Friday afternoon, Xiaoyue’s brother suddenly called, his voice full of anxiety.
“Xiaoyue, I have something to ask you.” came his brother’s voice.
Xiao Yue couldn’t help but wonder, what does her brother need her help with? She couldn’t help but ask: “Brother, do you need my help?”
My brother explained: “I have to go on a business trip for a while recently, about a week or so. And my wife is currently taking care of her parents who are hospitalized and has no time to take care of Xiao Ming. I would like to ask you to help take care of Xiao Ming for a while.”
Xiao Yue was stunned. She didn’t expect her brother to entrust her with this important task. This was a big challenge, and she was worried that she wouldn’t be able to do it. However, she knew she couldn’t let her feelings affect her brother’s decision. So she replied firmly: “Okay, brother, I will take good care of Xiao Ming. Don’t worry and go on a business trip.”
The brother on the other end of the phone breathed a sigh of relief and said gratefully: “Thank you, Xiaoyue. I believe you will take good care of Xiao Ming. I will be back as soon as possible.”
In this way, Xiaoyue took over the important task of taking care of Xiaoming. Although she is very busy, she feels that she has to put in more effort and time for her brother and Xiao Ming. In the next few days, Xiao Yue took care of Xiao Ming in every detail, cooking, washing, and helping him with homework, playing the role of an aunt dutifully.
On the weekend afternoon, Xiaoyue sat in front of the computer and concentrated on her work. She wears comfortable clothes and pants, and her long hair is tied up casually, giving people a friendly and natural feeling. Suddenly, there was a knock on the door. Xiaoyue put down her work, opened the door and saw that it was her nephew Xiao Ming.
Xiao Ming is a lively and active child. In the second grade, he still retains the innocence and curiosity of his childhood. He widened his eyes and looked at Xiaoyue’s computer screen curiously: “Sister, what are you doing?” Xiaoyue smiled slightly and corrected: “I am an aunt, not a sister.” Xiao Ming scratched his head awkwardly and hurriedly Changed her words: “Auntie, what are you doing?”
Xiaoyue replied with a smile: “I’m writing a program.”
“Procedure? Can you teach me?” Xiao Ming asked expectantly.
“Of course,” Xiao Yue nodded. She introduced the basic knowledge and rules of programming to Xiao Ming, and then began to teach him step by step how to write a simple game program.
Xiao Yue patiently taught Xiao Ming how to design a tank simulation game. She first introduced him to the basic rules of the game, and then taught him step by step how to use the program to operate the cross keys on the keyboard to control the movement of the tank on the screen. During this process, Xiaoyue always kept smiling, and her gentle words and careful guidance made Xiaoming feel more cordial.
Time passed by unconsciously, and Xiaoyue gradually felt tired in her heart. But whenever she sees Xiao Ming flying freely in the world of programming, her heart is filled with relief and pride. Xiao Ming also gradually began to understand the mysteries of programming. His interest was greatly aroused, and every day was full of new expectations and challenges. Although these days are very tiring and busy, Xiaoyue feels it is very meaningful. Because she knew that this time was precious to Xiao Ming, and she was lucky enough to participate in it, which made her feel extremely satisfied and happy.
Xiaoyue used object-oriented thinking to design a class called PlayerMovement to control the movement of players in the game in real time. This class contains a list of directions that stores the currently pressed arrow keys. The Position property represents the current player’s position, and the Direction property represents the direction of the current player.
Algorithm implementation 1:
1 public enum Direction { Up = 8, Down = 2, Left = 4, Right = 6 } 2 3 public struct Tile 4 { 5 public span> int X { get; } 6 public span> int Y { get; } 7 8 public Tile( int x, int y); 9 } 10 11 public static class Input 12 { 13 // pressed = true, released = false 14 public static bool GetState(Direction direction); 15 }
1 public class PlayerMovement 2 { 3 private span> List directions = new List(); 4 5 public Tile Position { get; private set; } 6 public span> Direction Direction { get; private set; } 7 8 public PlayerMovement( int x, int y) 9 { 10 // Initialize player position 11 this.Position = new Tile(x, y) ; 12 } 13 14 public void Update() 15 { 16 // Get new direction 17 this.directions = GetNewDirections(this.directions); 18 if span> (!this.directions.Any()) return; 19 20 var currentDirection = this.directions.Last(); 21 if span> (this.Direction != currentDirection) 22 { 23 // Update player direction 24 this.Direction = currentDirection; 25 } 26 else span> 27 { 28 // Get new location 29 this.Position = GetNewPosition(this.Position, this .Direction); 30 } 31 } 32 33 private static List GetNewDirections(IEnumerablecurrent) 34 { 35 var span> newState = current.ToList(); 36 37 var currentDirections = GetCurrentDirections().ToList(); 38 39 var directionsToRemove = newState.Except(currentDirections).ToList(); 40 directionsToRemove.ForEach(d =>newState.Remove(d)); 41 42 var directionsToAdd = currentDirections.Except(newState).ToList(); 43 directionsToAdd.ForEach(d =>newState.Add(d)); 44 45 return newState; 46 } 47 48 private static Tile GetNewPosition(Tile pos, Direction dir) 49 { 50 var span> newX = pos.X; 51 var span> newY = pos.Y; 52 53 switch (dir) 71 Release(Direction.Up); 72 73 TestEquality(Direction.Down, 0 , 2); 74 75 Release(Direction.Down); 76 77 TestEquality(Direction.Right, 0 , 2); 78 TestEquality(Direction.Right, 1, 2); 79 TestEquality(Direction.Right, 2, 2); 80 81 Release(Direction.Right); 82 83 TestEquality(Direction.Right, 2 , 2); 84 } 85 86 [Test(Description = "Random Test" span>)] 87 public span> void RandomTest() 88 { 89 int span> x, y; 90 Random rand = new Random(); 91 92 x = rand.Next(200 ) - 100; 93 y = rand.Next(200) - 100; 94 95 _player = new PlayerMovement(x, y); 96 Input.Clear (); 97 98 Press(Direction.Down); 99 Press(Direction .Left); 100 Press(Direction .Right); 101 Press(Direction .Up); 102 103 TestEquality(Direction.Up, x , y); 104 105 for (int i = 0; i < rand.Next(20) + 1; i++) 106 { 107 y++; 108 TestEquality(Direction .Up, x, y); 109 } 110 111 Release(Direction.Left); 112 113 y++; 114 TestEquality(Direction .Up, x, y); 115 116 Release(Direction.Up); 117 118 TestEquality(Direction.Down, x , y); 119 120 Release(Direction.Down); 121 122 TestEquality(Direction.Right, x , y); 123 x++; 124 TestEquality(Direction .Right, x, y); 125 x++; 126 TestEquality(Direction .Right, x, y); 127 128 Release(Direction.Right); 129 130 TestEquality(Direction.Right, x , y); 131 } 132 133 private void Press(Direction dir) { Console.WriteLine("Pressed " + dir); Input.Press(dir); } 134 private span> void Release(Direction dir) { Console.WriteLine("Released " + dir); Input.Release(dir); } 135 } 136 }
(128, 0, 128, 1)”>1; i++)
106 {
107 y++;
108 TestEquality(Direction .Up, x, y);
109 }
110
111 Release(Direction.Left);
112
113 y++;
114 TestEquality(Direction .Up, x, y);
115
116 Release(Direction.Up);
117
118 TestEquality(Direction.Down, x , y);
119
120 Release(Direction.Down);
121
122 TestEquality(Direction.Right, x , y);
123 x++;
124 TestEquality(Direction .Right, x, y);
125 x++;
126 TestEquality(Direction .Right, x, y);
127
128 Release(Direction.Right);
129
130 TestEquality(Direction.Right, x , y);
131 }
132
133 private void Press(Direction dir) { Console.WriteLine(“Pressed ” + dir); Input.Press(dir); }
134 private span> void Release(Direction dir) { Console.WriteLine(“Released “ + dir); Input.Release(dir); }
135 }
136 }