-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (115 loc) · 4.02 KB
/
Program.cs
File metadata and controls
138 lines (115 loc) · 4.02 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Threading;
using System.Collections.Generic;
using Config;
using Core;
using GameObject;
namespace Game
{
public static class Program
{
public static int updateIteration = 0;
static void Main()
{
Console.SetBufferSize(Console.WindowWidth, Console.WindowHeight);
Console.CursorVisible = false;
Console.Title = "CSharp Snake by JustAnCore";
if (File.Exists("highscore.txt"))
{
using (StreamReader reader = new StreamReader("highscore.txt"))
{
int.TryParse(reader.ReadLine(), out Cfg.HIGH_SCORE);
}
}
Start();
}
public static void Start()
{
Snake.Initialize(new Position(5, 10));
Apple.Initialize();
Renderer.RenderBorders();
Update();
}
public static void Update()
{
while (!Cfg.GAMEOVER)
{
while (Console.KeyAvailable)
{
Cfg.CURRENT_KEY = Console.ReadKey(intercept: true).Key;
}
if (Cfg.SCORE > Cfg.HIGH_SCORE) Cfg.HIGH_SCORE = Cfg.SCORE;
Renderer.RenderUI();
Snake.Move();
Renderer.Clear();
Renderer.RenderGameObjects();
Thread.Sleep(Cfg.RENDER_DELAY);
updateIteration++;
}
using (StreamWriter writer = new StreamWriter("highscore.txt"))
{
writer.WriteLine(Cfg.HIGH_SCORE);
}
while (Cfg.GAMEOVER)
{
Renderer.RenderUI();
while (Console.KeyAvailable)
{
Cfg.CURRENT_KEY = Console.ReadKey(intercept: true).Key;
}
if (Cfg.CURRENT_KEY == ConsoleKey.R) { Restart(); break; }
}
}
public static void Restart()
{
Console.Clear();
Snake.tail.Clear();
Cfg.GAMEOVER = false;
Cfg.SCORE = 0;
Start();
}
}
public static class Renderer
{
public static void Clear()
{
for (int y = 1; y < Cfg.SCENE_HEIGHT - 1; y++)
{
for (int x = 1; x < Cfg.SCENE_WIDTH - 1; x++)
{
RenderCore.DrawPixel(' ', new Position(x, y));
}
}
}
public static void RenderBorders()
{
for(int y = 0; y < Cfg.SCENE_HEIGHT; y++)
{
for (int x = 0; x < Cfg.SCENE_WIDTH; x++)
{
if(y == 0 || y == Cfg.SCENE_HEIGHT - 1)
{
RenderCore.DrawPixel(Cfg.CHR_WALL, new Position(x, y));
}else if(x == 0 || x == Cfg.SCENE_WIDTH - 1)
{
RenderCore.DrawPixel(Cfg.CHR_WALL, new Position(x, y));
}
}
}
}
public static void RenderUI()
{
if (Cfg.GAMEOVER) RenderCore.DrawText("GAME OVER - Press R to restart", new Position(0, Cfg.SCENE_HEIGHT + 1), ConsoleColor.Red);
else RenderCore.DrawText(" ", new Position(0, Cfg.SCENE_HEIGHT + 1), ConsoleColor.Red);
RenderCore.DrawText("SCORE: " + Cfg.SCORE + " | HIGH SCORE: " + Cfg.HIGH_SCORE , new Position(0, Cfg.SCENE_HEIGHT + 2), ConsoleColor.Green);
RenderCore.DrawText("-----------", new Position(0, Cfg.SCENE_HEIGHT + 3), ConsoleColor.Gray);
RenderCore.DrawText("Iteration: " + Program.updateIteration, new Position(0, Cfg.SCENE_HEIGHT + 4), ConsoleColor.Gray);
RenderCore.DrawText("Current key: " + Cfg.CURRENT_KEY, new Position(0, Cfg.SCENE_HEIGHT + 5), ConsoleColor.Gray);
}
public static void RenderGameObjects()
{
Snake.RenderSelf();
Apple.RenderSelf();
}
}
}