Hey, I'm currently attempting to write a classic snake sort of game, where the user directs the snake around the screen using the keyboard's arrow keys. So far, i've managed to sort out the code that dictates the movement and the keyboard controls successfully, but have come across problems upon trying to merge these pieces of code.
To move the snake, i'm using individual background worker loops for each arrow key as such:
publicvoid worker_DoWork(object sender,DoWorkEventArgs e){for(int i =0; i <1000; i++){if(abortloop ==true){
worker2 =null;
worker3 =null;
worker4 =null;break;}while(Snake1.Top-5>=8){{this.Invoke((MethodInvoker)delegate(){Snake1.Top=Snake1.Top-5;Refresh();});System.Threading.Thread.Sleep(500);}}}}
where the background worker is called from the keyboard input:
elseif(e.KeyCode==Keys.W){
abortloop2 =true;
abortloop3 =true;
abortloop4 =true;if(worker ==null&& abortloop ==true){
abortloop =false;
worker =newBackgroundWorker();
worker.DoWork+=newDoWorkEventHandler(worker_DoWork);
worker.RunWorkerAsync();}}
All this code produces no fatal errors to the program so to speak, however, upon commanding the snake to move in another direction (in this case, let's say it's currently moving downwards and i press the right arrow key), the snake will begin to move in the appropriate direction but also carry moving in the same direction as before (upon pressing the right arrow key, the snake then starts moving diagonally, rather than just to the right as i would want it to). This problem simply isn't down to there being 4 separate background workers, as the same problem occurs if i just use 1 background worker for all of the key inputs.
If anybody here could help out, that'd be fantastic.
Thanks in advance!