/
Display.cs
89 lines (84 loc) · 3.18 KB
/
Display.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Drawing;
namespace DonkeyKong
{
class Display
{
public static readonly Size Size = new Size(80, 40);
private GameModel model;
private GameTiles[,] buffer;
public Display(GameModel model)
{
//Initialization
Console.Title = "Donkey Kong - ASCII";
buffer = new GameTiles[Size.Width, Size.Height];
ClearBuffer();
this.model = model;
}
/// <summary>
/// This will set all elements in the buffer array to GameTiles.Null
/// </summary>
private void ClearBuffer()
{
for (int y = 0; y < buffer.GetLength(1); y++)
{
for (int x = 0; x < buffer.GetLength(0); x++)
{
buffer[x, y] = GameTiles.Null;
}
}
}
/// <summary>
/// Redraws the model in its current state to the console. Will only
/// redraw a position if it has a different value than the last time
/// that position was drawn. This is for performance reasons (to
/// remove flicker and increase speed).
///
/// After calling this method the cursor position will be set to
/// (0, 0) and the console fore and background colors will be set
/// to their defaults.
/// </summary>
public void Update()
{
for (int y = 0; y < Size.Height - 1; y++)
{
for (int x = 0; x < Size.Width; x++)
{
if (model[x, y] != buffer[x, y])
{
buffer[x, y] = model[x, y];
Console.SetCursorPosition(x, y);
switch (buffer[x, y])
{
case GameTiles.Floor:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write('=');
break;
case GameTiles.Mario:
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write((char)1);
break;
case GameTiles.Empty:
Console.Write(' ');
break;
case GameTiles.Ladder:
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write('H');
break;
case GameTiles.DK:
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write((char)2);
break;
case GameTiles.Barrel:
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write('@');
break;
}
}
}
}
Console.ResetColor();
Console.SetCursorPosition(0, 0);
}
}
}