Sign in to follow this  
Skibula

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

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();
          

            z = Console.ReadKey();


Share this post


Link to post
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 velocity
snake_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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by WanMaster
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. :)



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;
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Skibula
http://www.f2h.co.il/9977341326884

That 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?

Share this post


Link to post
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)

http://www.turboupload.com/rmkqpfv9qvm8/SnakeProject0206.zip.html

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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
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);


i dont understand how this property make the snake go more steps then 1
in theory ofcruse :-)

Share this post


Link to post
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 (:

Share this post


Link to post
Share on other sites
i pasted the code u gave me and it doesnt compile

it give me error
Error 1 Use of unassigned local variable 'keyInfo'
on this line
while (keyInfo.Key != System.ConsoleKey.Escape);



i changed this to this
while (true)

just to test it

and now its half the way

casue i still need to press the key if i want to move...
i need to hold the key
(i want for 1 key press it will do that until i press another key...

Share this post


Link to post
Share on other sites
i just wanted to say to all of you that took part of this post


thanks to you guys i have figured it up

im now using timer event


t = new Timer(250);
then main loop

while (true)
{

t.Dispose();//clean the old timer for reuse(i dont know how
to empty time signal)
t = new Timer(250);
t.Elapsed += new ElapsedEventHandler(t_Elapsed);
t.Start();
z = Console.ReadKey();


}

static void t_Elapsed(object sender, ElapsedEventArgs e)
{
GoToMove(z.KeyChar);//inside this function there is a
case for evey direction
}



any way thanks everybody for helping me out
:-)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this