Hi guys!
I am trying to code the famous Frogger game in C# - Console. I got to a point where the "cars" (reporesented as ">" and "<" wont move unless the frog (an "X") moves (console.ReadKey waits for the key pressing).
Is there anyway I could make teh cars to run on their own (keep moving non-stop) while the frog waits for the key press? I've read about thread but not sure how to apply it.
Here is my code so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Frogger
{
class Program
{
static void Main(string[] args)
{
sbyte bytCar1X;
sbyte bytCar1Y;
sbyte bytCar2X;
sbyte bytCar2Y;
sbyte bytCar3X;
sbyte bytCar3Y;
sbyte bytFrogX;
sbyte bytFrogY;
Console.Clear();
//Random deeclarations
Random rndRandom = new Random();
bytCar1X = Convert.ToSByte(rndRandom.Next(1, 79));
bytCar1Y = Convert.ToSByte(rndRandom.Next(1, 6));
bytCar2X = Convert.ToSByte(rndRandom.Next(1, 79));
bytCar2Y = Convert.ToSByte(rndRandom.Next(7, 15));
bytCar3X = Convert.ToSByte(rndRandom.Next(1, 79));
bytCar3Y = Convert.ToSByte(rndRandom.Next(16, 22));
bytFrogX = Convert.ToSByte(rndRandom.Next(1, 79));
bytFrogY = 23;
do
{
Console.Clear();
//Car 1 movement
bytCar1X = bytCar1X +=1;
if(bytCar1X > 79)
{
bytCar1X = 1;
}
Console.SetCursorPosition(bytCar1X, bytCar1Y);
Console.WriteLine(">");
//Car 2 movement
bytCar2X = bytCar2X -= 1;
if (bytCar2X < 0)
{
bytCar2X = 79;
}
Console.SetCursorPosition(bytCar2X, bytCar2Y);
Console.WriteLine("<");
//Car 3 movement
bytCar3X = bytCar3X += 1;
if (bytCar3X > 79)
{
bytCar3X = 1;
}
Console.SetCursorPosition(bytCar3X, bytCar3Y);
Console.WriteLine(">");
//Frog keys
Console.SetCursorPosition(bytFrogX, bytFrogY);
Console.WriteLine("X");
ConsoleKeyInfo CKIKey = Console.ReadKey(true);
Console.SetCursorPosition(0, 0);
if(CKIKey.Key == ConsoleKey.UpArrow)
{
bytFrogY = (bytFrogY -= 1);
}
Console.SetCursorPosition(0, 0);
if (CKIKey.Key == ConsoleKey.DownArrow)
{
bytFrogY = (bytFrogY += 1);
}
Console.SetCursorPosition(0, 0);
if (CKIKey.Key == ConsoleKey.LeftArrow)
{
bytFrogX = (bytFrogX -= 1);
}
Console.SetCursorPosition(0, 0);
if (CKIKey.Key == ConsoleKey.RightArrow)
{
bytFrogX = (bytFrogX += 1);
}
//Frog Respawn and Crashes
if (bytFrogY == 0)
{
bytFrogY = 23;
}
if ((bytFrogX == bytCar1X) && (bytFrogY == bytCar1Y))
{
Console.Clear();
Console.WriteLine("CRASH! Press enter to continue");
Console.ReadLine();
}
if ((bytFrogX == bytCar2X) && (bytFrogY == bytCar2Y))
{
Console.Clear();
Console.WriteLine("CRASH! Press enter to continue");
Console.ReadLine();
}
if ((bytFrogX == bytCar3X) && (bytFrogY == bytCar3Y))
{
Console.Clear();
Console.WriteLine("CRASH! Press enter to continue");
Console.ReadLine();
}
} while (1 == 1);
}
}
}
Thanks in advance.
Victor.
Loading
Wim SturkenboomPosted Oct 22, 2014, 9:48 AM
from http://msdn.microsoft.com/en-us/library/system.console.keyavailable.aspx
ConsoleKeyInfo cki = new ConsoleKeyInfo();
do
{
Console.WriteLine("\nPress a key to display; press the 'x' key to quit.");
// Your code could perform some useful task in the following loop. However,
// for the sake of this example we'll merely pause for a quarter second.
while (Console.KeyAvailable == false)
Thread.Sleep(250); // Loop until input is entered.
cki = Console.ReadKey(true);
Console.WriteLine("You pressed the '{0}' key.", cki.Key);
} while (cki.Key != ConsoleKey.X);
from http://msdn.microsoft.com/en-us/library/system.consolekeyinfo%28v=vs.110%29.aspx
ConsoleKeyInfo cki = new ConsoleKeyInfo();
// Prevent example from ending if CTL+C is pressed.
Console.TreatControlCAsInput = true;
Console.WriteLine("Press any combination of CTL, ALT, and SHIFT, and a console key.");
Console.WriteLine("Press the Escape (Esc) key to quit: \n");
do
{
cki = Console.ReadKey();
Console.Write(" --- You pressed ");
if ((cki.Modifiers & ConsoleModifiers.Alt) != 0) Console.Write("ALT+");
if ((cki.Modifiers & ConsoleModifiers.Shift) != 0) Console.Write("SHIFT+");
if ((cki.Modifiers & ConsoleModifiers.Control) != 0) Console.Write("CTL+");
Console.WriteLine(cki.Key.ToString());
} while (cki.Key != ConsoleKey.Escape);