-
February 11th, 2012, 09:06 PM
#1
Roguelike Console App Control Problems
I'm working on a Roguelike console app for C#. I have several key features working such as movement arithmetic but I am unable to coordinate things so my sprite moves around the screen properly.
If you'll compile and run the code you'll notice you have to enter in a "w", "a", "s", or "d" key twice before you can start moving the character. After that, the character keeps moving in one direction whether you enter in a different key or not. Not only that, but the Console.ReadKey(false) method doesn't work and you have to enter another key and press enter in order to move the character again. Other problems persist as well, like the character disappearing from the screen.
Instead of what I have, I want smooth movement. For example, if the user presses the "w" key, it should move the character "up" without the user having to hit enter. Likewise, "a" and "d" should move the character left and right to allow for movement as seen in the original Rogue.
I've tried using Console.ReadKey(false) but it doesn't work to move the character without having to hit enter. The code seems to be a mess and I would like to know how I can eliminate those two Console.ReadLine methods so the user doesn't have to enter in two characters to start the game properly.
Can anybody help me make sense of this?
Code:
using System;
class Sample
{
protected static int origRow;
protected static int origCol;
protected static void WriteAt(string s, int x, int y)
{
try
{
Console.SetCursorPosition(origCol + x, origRow + y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
Console.Clear();
Console.WriteLine(e.Message);
}
}
public static void Main()
{
Console.ReadKey(false);
int sr = 10;
int sc = 10;
Console.ReadKey(false);
ConsoleKeyInfo theKey = Console.ReadKey(false);
string choice = Console.ReadLine();
do
{
Console.Clear();
origRow = Console.CursorTop;
origCol = Console.CursorLeft;
string input = Console.ReadLine();
WriteAt("@", sr, sc);
Console.ReadKey(false);
switch (theKey.KeyChar)
{
case 'W': case 'w' :
sc = sc - 1;
WriteAt("@", sr, sc);
break;
case 'S': case 's' :
sc = sc + 1;
WriteAt("@", sr, sc);
break;
case 'A': case 'a' :
sr = sr - 1;
WriteAt("@", sr, sc);
break;
case 'D': case 'd' :
sr = sr + 1;
WriteAt("@", sr, sc);
break;
}
} while (choice != "Q");
}
}
-
February 11th, 2012, 09:20 PM
#2
Re: Roguelike Console App Control Problems
A good habit to get into when first learning .Net is to check out other methods and properties in the class you are using (or planning to use).
See Console Class in msdn.
There are 4 methods related to reading, including the ReadLine method you are using.
See if any of the other read related methods will do what you want (see the description on the right of each method).
Hint: there are two of them that will do what you are looking for. One of them might work better than the other.
-
February 13th, 2012, 08:21 PM
#3
Re: Roguelike Console App Control Problems
Originally Posted by Arjay
A good habit to get into when first learning .Net is to check out other methods and properties in the class you are using (or planning to use).
See Console Class in msdn.
There are 4 methods related to reading, including the ReadLine method you are using.
See if any of the other read related methods will do what you want (see the description on the right of each method).
Hint: there are two of them that will do what you are looking for. One of them might work better than the other.
I see the 4 read methods you speak of. I also am looking at the write methods (not cursor methods), would these be able to help me?
-
February 15th, 2012, 06:31 PM
#4
Re: Roguelike Console App Control Problems
-
February 15th, 2012, 07:26 PM
#5
Re: Roguelike Console App Control Problems
Originally Posted by Krunchyman
I see the 4 read methods you speak of. I also am looking at the write methods (not cursor methods), would these be able to help me?
Why don't you try them and see? The best way to understand this stuff is by hands on experience. Type in the code and see what happens. Just remember to keep backups so you can revert back if you hose your code.
Always use [code][/code] tags when posting code.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|