private void SaveGame() { _gamepause = true; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream("savegame.dat", FileMode.Create)) { try { _mycannon = new gameobjser(_cannon.Position.X, _cannon.Position.Y, _cannon.Center.X, _cannon.Center.Y, _cannon.Velocity.X, _cannon.Velocity.Y, _cannon.Rotation, _cannon.Alive); _mycannonballs = new gameobjser[MaxCannonBalls]; for (int i = 0; i < _mycannonballs.Length; i++) { _mycannonballs[i] = new gameobjser(_cannonballs[i].Position.X, _cannonballs[i].Position.Y, _cannonballs[i].Center.X, _cannonballs[i].Center.Y, _cannonballs[i].Velocity.X, _cannonballs[i].Velocity.Y, _cannonballs[i].Rotation, _cannonballs[i].Alive); } _myenemies = new gameobjser[MaxEnimies]; for (int i = 0; i < _myenemies.Length; i++) { _myenemies[i] = new gameobjser(_enemies[i].Position.X, _enemies[i].Position.Y, _enemies[i].Center.X, _enemies[i].Center.Y, _enemies[i].Velocity.X, _enemies[i].Velocity.Y, _enemies[i].Rotation, _enemies[i].Alive); } mysave = new savegame(_mycannon, _myenemies, _mycannonballs, _score, _health); formatter.Serialize(fs, mysave); } catch (Exception e) { Debug.WriteLine(e); } } _gamepause = false; }
private void LoadGame() { _gamepause = true; BinaryFormatter formatter = new BinaryFormatter(); using (FileStream fs = new FileStream("savegame.dat", FileMode.Open)) { try { mysave = (savegame)formatter.Deserialize(fs); _score = mysave._myscore; _health = mysave._myhealth; _cannon.Center = new Vector2(mysave._mycannon.Centerx, mysave._mycannon.Centery); _cannon.Alive = mysave._mycannon.Alive; _cannon.Position = new Vector2(mysave._mycannon.Positionx, mysave._mycannon.Positiony); _cannon.Rotation = mysave._mycannon.Rotation; _cannon.Velocity = new Vector2(mysave._mycannon.Velocityx, mysave._mycannon.Velocityy); try { for (int i = 0; i < MaxCannonBalls; i++) { _cannonballs[i].Center = new Vector2(mysave._mycannonballs[i].Centerx, mysave._mycannonballs[i].Centery); _cannonballs[i].Alive = mysave._mycannonballs[i].Alive; _cannonballs[i].Position = new Vector2(mysave._mycannonballs[i].Positionx, mysave._mycannonballs[i].Positiony); _cannonballs[i].Rotation = mysave._mycannonballs[i].Rotation; _cannonballs[i].Velocity = new Vector2(mysave._mycannonballs[i].Velocityx, mysave._mycannonballs[i].Velocityy); } } catch (Exception e) { Debug.WriteLine(e); } try { for (int i = 0; i < MaxEnimies; i++) { _enemies[i].Center = new Vector2(mysave._myenemies[i].Centerx, mysave._myenemies[i].Centery); _enemies[i].Alive = mysave._myenemies[i].Alive; _enemies[i].Position = new Vector2(mysave._myenemies[i].Positionx, mysave._myenemies[i].Positiony); _enemies[i].Rotation = mysave._myenemies[i].Rotation; _enemies[i].Velocity = new Vector2(mysave._myenemies[i].Velocityx, mysave._myenemies[i].Velocityy); } } catch (Exception e) { Debug.WriteLine(e); } } catch (Exception e) { Debug.WriteLine(e); } } _gamepause = false; }