c# Console-Do something until key pressed - c#

Recently I am trying to make basic games in Console-Application, like to move the cursor with the arrows (maybe as a very basic start of snake and games that you move something all over the Console screen), and I wanted to ask if it's possible to make that the cursor won't stop to move, and just change the direction when I press on some arrow? Is it something with events?
Thanks.

I would suggest something like the following:
bool isRight, isUp; //Used to determine direction
while (gamePlaying)
{
if(Console.KeyAvailable)
{
Console.ReadKey(); //Do your thing in here
}
}
So you check if a key is being pressed, and if it is you read it.
Also, bear in mind if you're making a console game you'll want to double-buffer your screen and use the Win32 API to minimise write calls if possible or your screen with flicker horribly.

I do not believe there is anyway to control the cursor position of the console window. You can read or write text, but that's about it. So if you want to make a console game, it will need to be text-based. You may be able to make an game ASCII art based where new text is printed to the console fast enough that it appears to be animated, but I don't think the console can go fast enough to make this work well.

Related

Ascii console game: player character

I've been working on a game for a while now. It's an ASCII game in the console, using C#. It's a bit like the old top-down zelda games. I've managed to get the map to draw, the movement system works and I've added random encounters.
Since I am still a beginner, I don't know how to use classes yet, so the game is programmed entirely without them.
My player character currently is still just the cursor, but I would like it to be a bit easier to see. It could be a white "#" symbol or something. The map is in color, so after the # symbol moves, the right tile, with the right color should be placed back at where the character was. I've tried quite some times, but I simply haven't been able to figure out how I can get this to work.
So my question is: How do I draw a white "#" character where the cursor is on the map and how do I make sure it doesn't leave the map changed when the "#" symbol moves as the player moves?
If you need any more information, simply tell me and I'll post it.
Thank you in advance!
I believe what you are looking for is a good combination of Console.SetCursorPosition and Console.Clear. Try experimenting with that.

Project to track on screen object - Unsure how to go about it

I've started learning c# and vb.net and have a project related to tracking a moving object on the screen to help me learn some basics.
In essence, I will have a ball on the screen which moves from left to right (only horizontally), and when it reaches a certain point (e.g. 250 pixels from the "mid" point either side) I need to know this, and click an on-screen counter to increase a value (or decrease depending on left or right) and reset the ball to the centre (note that the ball speed will vary from incredible slow to an instant "jump").
I've been asked to look into c# and vb and decide which is best, then use it to create the program. As a complete newbie in both of these, does anyone have a recommendation and a starting point please?
My background is Javascript, HTML and very basic Java.
Thanks!
Not too sure what you define as a "Starting Point" do you mean like different C# libs to use etc. If all you are trying to do is move a ball from left to right basically and count the difference it would be so simple to do in C# XNA.
You can also do it in windows forms too which I really thing shouldn't be too hard at all. Obviously you would create the velocity formulas yourself I don't believe that's what you're asking.
One way to get the screen size in windows forms is:
public Rectangle GetScreen()
{
return Screen.FromControl(this).Bounds;
}
This will return a rectangle with a width and height that you can call, divide both values by 2 and it will give you the center of the screen. Then just add a method that is called every frame to check the balls value from the middle of the screen.
From what I've read I understand what you want to do, but I'm unsure if you just want a windows form or if you'd prefer to use a small game engine like XNA etc.
Either way best of luck and let me know :)

Ask for user input using a SpriteFont on the screen present

For instance, consider a brick breaker game where once the ball falls below the paddle, the user would get a displayed message (using SpriteFont) asking whether he would like to play again. If that condition was met,and the sprite font stated 'Play again, enter 'y' for yes, 'n' for no' how would one proceed to take that input in XNA?
Well, first you're gonna want to have a boolean to see if the game has ended or not. If you didn't have the boolean then the user could press Y or N anytime and the game would restart.
It should be simple to setup one anyways, just have it detect when the ball falls below the paddle and set the bool to true;
Then, the rest should be quite simple to figure out.
if (gameRestart)
{
if (Keyboard.GetState().IsKeyDown(Keys.Y))
{
// code for restart goes here
}
else if (Keyboard.GetState().IsKeyDown(Keys.N))
{
// do nothing, I guess?
}
}
Here is a tutorial on detecting key presses in XNA: http://msdn.microsoft.com/en-us/library/bb203902.aspx

XNA Mouse position is off

I'm working on a miniature golf game in XNA, I originally had everything in Game.cs (main), but I now want it to be more object-oriented, so I made separate class for most of my stuff.
When I had everything in Game.cs, it was working fine, now it doesn't.
What is happening is this:
When my cursor is at the top left corner of the game window, it's like X=200, Y=50.
It's supposed to be X=0, Y=0.
Even when I look for the 0, 0 position, it's way outside the game window.
Does anyone know what could be causing this?
How is that possible? Your cursor position is the mouse position. Simply draw the cursor there.
Unless you are talking about the Windows cursor. In that case, yes, the input data in XNA will not match the Windows cursor movement. They probably apply some modifiers for acceleration, etc. You have to interprete the input data yourself. In other words, draw your own cursor.

Is there a better way to make console games than with Console.Clear()?

I'm messing around with game loops and going to create some games as practice.
Currently I have a steady game loop up where the game is updated as fast as possible and the rendering is updated x times a second (25 currently)
The rendinging method is basically a draw + Console.Clear() and at very high updates the display becomes very jittery since it's not finished drawing when Console.Clear() hits.
Is there a better way of doing something like this?
Can I write whatever data to the console and then replace it with some other data?
Assuming you Write a full screen from topleft again in every loop you could simply replace the Clear() with:
Console.SetCursorPosition(0, 0);
And overwrite the previous screen.
Since you're in C# anyway, you might want to look into the XNA Framework.
I'm guessing your problem arises from Console.Clear() not being optimized for this kind of use, as XNA uses a similar method (Clear() is called on a GraphicsDevice).
If you don't want to try using XNA, then it may possibly be faster to draw a rectangle (solid black or gray or whatever) rather than call Clear() to 'blank' out the screen and then draw over it.
You migth wanna check out the ConsoleLibrary
I didn't use it, but from the article/demos it seems it would allow you to do bunch of neat stuff on the console.

Categories