Question: C# PROGRAMMING QUESTION please help. Have you ever wondered how a game implements a configurable control scheme? Of course, there are many solutions, but a

C# PROGRAMMING QUESTION please help.

Have you ever wondered how a game implements a configurable control scheme? Of course, there are many solutions, but a common one is to associate an input command (keyboard, mouse, or gamepad) with an action (usually a piece of code). This requires you to store a list of mappings.

E.g:

W key -> Move forward

S Key -> Move backward

In this assignment you will create such a mapping using delegates to store the function to be called. In C++ you would have used a function pointer to accomplish this.

Download and run the DelegateKeypress sample code(BELOW). You will see a game hard coded with the arrow keys to move the player around on the console.

There are TBD comments explaining the changes you should make to the code. You can either create the mappings in code or you can ask the user to press the 4 required keys to set up the mapping.

The data structure you need is quite simple. You need a structure that stores the ConsoleKey and the action to be run. Then you need a collection of those since there are many mappings. Choose a collection that maps to the usage.

Then during the main loop you will search the collection looking for an entry that matches the key that was pressed. If you find one you will call the action through the delegate.

DELEGATE KEYPRESS PROGRAM....PLEASE RESPOND WITH CORRECTED CODE IN C#:

namespace DelgateKeypress { class Program { private static int x=20; private static int y=20;

//TBD: You will need to define a data structure to store the association //between the KeyPress and the Action the key should perform

private static void Main(string[] args) { //TBD: Set up your control scheme here. It should look something like this: // myControls.Add(ConsoleKey.W, Up) // myControls.Add(ConsoleKey.S, Down) //or you can ask the user which keys they want to use //etc

while (true) { Console.SetCursorPosition(x, y); Console.Write("@");

var key = Console.ReadKey(true);

int oldX = x; int oldY = y;

//TBD: Replace the following 4 lines by looking up the key press in the data structure //and then performing the correct action if (key.Key == ConsoleKey.UpArrow) Up(); if (key.Key == ConsoleKey.DownArrow) Down(); if (key.Key == ConsoleKey.LeftArrow) Left(); if (key.Key == ConsoleKey.RightArrow) Right();

Console.SetCursorPosition(oldX, oldY); Console.Write("+");

} }

private static void Right() { x++; }

private static void Left() { x--; }

private static void Down() { y++; }

private static void Up() { y--; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!