static void SaveGame(DataSave x) { FileStream fs = new FileStream(@"dataGame.ser", FileMode.OpenOrCreate, FileAccess.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); try { bf.Serialize(fs, x); } catch (Exception e) { Console.WriteLine(e.Message); } finally { fs.Close(); } }
static void Main(string[] args) { rating = GetRating(); MainMenu(); DataSave data = DataGet(); Game.Set(data); Console.CursorVisible = false; Console.SetWindowSize(70, 20); Thread t = new Thread(Move); t.Start(); while (!Game.Gameover) { if (Game.Gameover == true) { break; } ConsoleKeyInfo btn = Console.ReadKey(); if (btn.Key == ConsoleKey.RightArrow && Game.snake.dir != 3) { Game.snake.dir = 1; } if (btn.Key == ConsoleKey.DownArrow && Game.snake.dir != 4) { Game.snake.dir = 2; } if (btn.Key == ConsoleKey.LeftArrow && Game.snake.dir != 1) { Game.snake.dir = 3; } if (btn.Key == ConsoleKey.UpArrow && Game.snake.dir != 2) { Game.snake.dir = 4; } } }
static DataSave DataGet() { FileStream fs = new FileStream(@"dataGame.ser", FileMode.OpenOrCreate, FileAccess.ReadWrite); BinaryFormatter bf = new BinaryFormatter(); DataSave res = new DataSave(); try { res = (DataSave)bf.Deserialize(fs); } catch (Exception e) { Console.WriteLine(e.Message); } finally { fs.Close(); } return(res); }
public static void Set(DataSave x) { if (x.GameOver == false) { level = x.level; snake = x.snake; wall = x.wall; food = x.food; score = x.score; Gameover = x.GameOver; } else { snake = new Snake(); wall = new Wall(); food = new Food(); score = 0; Gameover = false; level = 0; } }