# [.net] How to do action until something else happens (snake game on console)

This topic is 3391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 user then i have switch to go to the case of the char (then snake go to that direction)
            ConsoleKeyInfo z = new ConsoleKeyInfo();



##### Share on other sites
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)}

##### Share on other sites
well my snake goes in alot stupider way...

but i didnt get your point...

yes i have main loop
inside the loop i have switch

my point that i want to do something until user press a key
then i do something else

in that way snake goes automatic

so i have main loop
inside it a switch with 4 cases (w , a ,s ,d)
evey case for every direction

btw 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).

##### Share on other sites
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. :)

##### Share on other sites
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/9977341326884

the way i see it
i need to do something like that:
int test;
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]

##### Share on other sites
Quote:
 Original post by Skibulahttp://www.f2h.co.il/9977341326884

Could you extract the relevant parts of the projects and post them using the [source] tags?

##### Share on other sites
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 can
the whole program (my classes are on diffrent files)

every thing that got to do with timer doesnt work
its just my testing :)

 tags?[/quote]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();        }           }}

##### Share on other sites
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);

##### Share on other sites
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 1
in theory ofcruse :-)

##### Share on other sites
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 (:

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632623
• Total Posts
3007504
• ### Who's Online (See full list)

There are no registered users currently online

×