-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighScore.cs
72 lines (64 loc) · 1.86 KB
/
HighScore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
namespace Pacman
{
internal class HighScore : Obj
{
private const string FILE_NAME = "Score.data";
private bool newHighScore;
public int highScore;
public HighScore()
{
this.IsAlive = true;
try
{
FileStream fileStream = new FileStream("Score.data", FileMode.Open, FileAccess.Read);
using (BinaryReader binaryReader = new BinaryReader((Stream) fileStream))
this.highScore = binaryReader.ReadInt32();
fileStream.Close();
}
catch (IOException ex)
{
this.highScore = 0;
}
}
public override void LoadContent(ContentManager content)
{
}
public override void Update(GameTime gameTime)
{
if (!this.IsAlive)
return;
if (Items.Player.Score > this.highScore)
{
this.highScore = Items.Player.Score;
this.newHighScore = true;
}
if (!(this.newHighScore & Items.Pacman.IsGameOver))
return;
this.WriteHighScore();
}
public override void Draw(SpriteBatch spriteBatch)
{
if (!this.IsAlive)
return;
spriteBatch.DrawString(PacmanGame.HudFont, "HIGH SCORE = " + this.highScore.ToString(), new Vector2((float) (170 - this.highScore.ToString().Length), 546f), Color.Red);
}
private void WriteHighScore()
{
try
{
FileStream fileStream = new FileStream("Score.data", FileMode.OpenOrCreate, FileAccess.Write);
using (BinaryWriter binaryWriter = new BinaryWriter((Stream) fileStream))
binaryWriter.Write(this.highScore);
fileStream.Close();
}
catch (IOException ex)
{
}
this.newHighScore = false;
}
}
}