   [.net] How to do action until something else happens (snake game on console)
  hey all,I need your guys help ones more :-)i makeing a snake game ( i will sell it in the cost of 10000$ per unit :) )for now my snake move one step at the time (with any user klick the snake goes one step to the direction)how i can can make the snake move until i tell him a diffrent direction ?i would like to use events for that but i understand there are no that kind of events on console :(right now im using the concolekeyinfo to get the char from the userthen i have switch to go to the case of the char (then snake go to that direction)  ConsoleKeyInfo z = new ConsoleKeyInfo();  z = Console.ReadKey();
  I assume you have some sort of main loop, checking for input events and updating the game state. You can determine the direction of the snake after a key press, so there's no reason why you would lose that information in the next update. An simple pseudo code example:// let's use some 2d vectors for position and velocitysnake_position = [10, 10]snake_velocity = [0, 0]while (not quiting game){  // check user input and change the velocity (or direction) accordingly  // the sign of the direction depends on the coordinate system used  if (key_left is pressed)    velocity.x = -1  if (key_right is pressed)    velocity.x = 1  if (key_up is pressed)    velocity.y = -1  if (key_down is pressed)    velocity.y = 1   // update the position of the snake  // here we also take into account the time elapsed since the last frame  snake_position.x += snake_velocity.x * elapsed_time  snake_position.y += snake_velocity.y * elapsed_time  // update the screen by drawing the snake at its new position  draw_snake(snake_position)};  well my snake goes in alot stupider way...but i didnt get your point...yes i have main loopinside the loop i have switch my point that i want to do something until user press a key then i do something elsein that way snake goes automaticso i have main loopinside it a switch with 4 cases (w , a ,s ,d)evey case for every directionbtw im not using matrix for the board im using array (i found it easyer to do so ,,, im working on a reson why i did it that way).;  Perhaps it will help if you post more of (the relevant parts of) your code.Unfortunately I don't have time in the coming couple of hours, but I will look at it later. Or someone will give you some pointers. :)   ;Quote:Original post by WanMasterPerhaps it will help if you post more of (the relevant parts of) your code.Unfortunately I don't have time in the coming couple of hours, but I will look at it later. Or someone will give you some pointers. :)here is a link to my project:i know it will look kinda complicated so i hope u guys can help.http://www.f2h.co.il/9977341326884the way i see iti need to do something like that:int test;test = int.parse(console.readline());if in 1 sec user doesnt give any input to test then test = null; and more other stuff...or something like that[Edited by - Skibula on June 5, 2009 10:14:12 AM];Quote:Original post by Skibulahttp://www.f2h.co.il/9977341326884That link leads to page throwing nothing but PHP errors.Could you extract the relevant parts of the projects and post them using the [source] tags?   ;  well i have here alot stuff that got nothing to do with my problem (i trust u to know what is unrelevnt)any way this is my main program class (i have alot more classes on diffrent files)any way i post here another file to download if u canthe whole program (my classes are on diffrent files)http://www.turboupload.com/rmkqpfv9qvm8/SnakeProject0206.zip.htmlevery thing that got to do with timer doesnt workits just my testing :)  tags?using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Xml.Serialization;using System.Timers;namespace SnakeProject//version 3.0{    class Program    {   const int end = 90;   static void Main(string[] args)   {  Console.SetWindowSize(70, 40);  ///============  DataRecord HighestPoint = new DataRecord();  HighestPoint = MaxPoint.LoadData(HighestPoint);  Screen.PrintWellcome(HighestPoint);  int line = 2;//there are 10 lines that count from 0-9  int row = 4;//there are 9 rows that count from 0-8  int points = 0;  List<int> snake = new List<int>();  char[] board = new char[end];  snake.Add(18);  snake.Add(19);  snake.Add(20);  snake.Add(21);  snake.Add(22);  Interaction.emptyarray(board);  Interaction.putsnakeinboard(board, snake);  BoxToEat.PutBoxOnBoard(board);  Screen.PrintScreen(board,points);  Console.WriteLine();  ConsoleKeyInfo z = new ConsoleKeyInfo();  z = Console.ReadKey();  char move;  move = z.KeyChar;//first move  bool flag = true;//flag is the mark when to exit the game if flag false game is over  while (flag == true)  { int head; switch (move) {case 'w'://GO UP    line = line - 1;    if (line < 0)    {   flag = false;   break;    }    //when going up need to see if place is empty or with box or with snake(tail)    head = snake[snake.Count-1];    if (board[head-9] == 'E')    {   snake.Add(head-9);//put the new move in the snake   snake.RemoveAt(0);    }    if (board[head-9] == 'B')    {   snake.Add(head - 9);//put the new move in the snake   points = points + 10;    }    if (board[head - 9] == 'S')    {   flag = false;   break;    }    Interaction.putsnakeinboard(board, snake);    BoxToEat.PutBoxOnBoard(board);    Console.Clear();    Screen.PrintScreen(board,points);   z = Console.ReadKey();    move = z.KeyChar;    break;   case 'a'://GO LEFT    row = row - 1;    if (row < 0)    {   flag = false;   break;    }    //when going left need to see if place is empty or with box or with snake(tail)    head = snake[snake.Count - 1];    if (board[head - 1] == 'E')    {   snake.Add(head - 1);//put the new move in the snake   snake.RemoveAt(0);    }    if (board[head - 1] == 'B')    {   snake.Add(head - 1);//put the new move in the snake   points = points + 10;    }    if (board[head - 1] == 'S')    {   flag = false;   break;    }    Interaction.putsnakeinboard(board, snake);    BoxToEat.PutBoxOnBoard(board);    Console.Clear();    Screen.PrintScreen(board,points);    z = Console.ReadKey();    move = z.KeyChar;    break;case 's'://GO DOWN    line = line + 1;    if (line > 9)    {   flag = false;   break;    }    //when going down need to see if place is empty or with box or with snake(tail)    head = snake[snake.Count - 1];    if (board[head + 9] == 'E')    {   snake.Add(head + 9);//put the new move in the snake   snake.RemoveAt(0);    }    if (board[head + 9] == 'B')    {   snake.Add(head + 9);//put the new move in the snake   points = points + 10;    }    if (board[head + 9] == 'S')    {   flag = false;   break;    }    Interaction.putsnakeinboard(board, snake);    BoxToEat.PutBoxOnBoard(board);    Console.Clear();    Screen.PrintScreen(board,points);    z = Console.ReadKey();    move = z.KeyChar;    break;case 'd'://GO RIGHT    row = row + 1;    if (row > 8)    {   flag = false;   break;    }    //when going right need to see if place is empty or with box or with snake(tail)    head = snake[snake.Count - 1];    if (board[head + 1] == 'E')    {   snake.Add(head + 1);//put the new move in the snake   snake.RemoveAt(0);    }    if (board[head + 1] == 'B')    {   snake.Add(head + 1);//put the new move in the snake   points = points + 10;    }    if (board[head + 1] == 'S')    {   flag = false;   break;    }    Interaction.putsnakeinboard(board, snake);    BoxToEat.PutBoxOnBoard(board);    Console.Clear();    Screen.PrintScreen(board, points);    z = Console.ReadKey();    move = z.KeyChar;    break; }  }  HighestPoint = MaxPoint.UpdateRecord(HighestPoint, points);  Screen.PrintGameOver(HighestPoint, points);   }   static void myTimer_Elapsed(object sender, ElapsedEventArgs e)   {  throw new NotImplementedException();   } }};  You're looking for the System.Console.KeyAvailable property.System.ConsoleKeyInfo keyInfo;do{    if(System.Console.KeyAvailable)   // <---- that's what you want    {   keyInfo = System.Console.ReadKey(true); //true hides the key that was pressed, false will make it show   switch(keyInfo.Key)   {  case System.ConsoleKey.LeftArrow: //blah  break;  case System.ConsoleKey.RightArrow: //blah  break;  case System.ConsoleKey.UpArrow: //blah  break;  case System.ConsoleKey.DownArrow: //blah  break;  //etc   }    }}while(keyInfo.Key != System.ConsoleKey.Escape);;Quote:Original post by capn_midnightYou're looking for the System.Console.KeyAvailable property.System.ConsoleKeyInfo keyInfo;do{    if(System.Console.KeyAvailable)   // <---- that's what you want    {   keyInfo = System.Console.ReadKey(true); //true hides the key that was pressed, false will make it show   switch(keyInfo.Key)   {  case System.ConsoleKey.LeftArrow: //blah  break;  case System.ConsoleKey.RightArrow: //blah  break;  case System.ConsoleKey.UpArrow: //blah  break;  case System.ConsoleKey.DownArrow: //blah  break;  //etc   }    }}while(keyInfo.Key != System.ConsoleKey.Escape);i dont understand how this property make the snake go more steps then 1in theory ofcruse :-);  It's because ReadKey will wait for a keypress before it moves on.If you check if there is a key available first it will only do a readkey if there was a keypress, otherwise it will just keep going.That makes it so your main loop keeps looping whether the user is pressing a key or not (:   