Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
78 lines (69 loc) · 2.61 KB
/
Copy pathProgram.cs
File metadata and controls
78 lines (69 loc) · 2.61 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
using System;
namespace ConsoleGame
{
class Program
{
static void Main(string[] args)
{
Random rand = new Random();
Console.CursorVisible = false;
// Determine bounds and set starting positions
int rows = Console.BufferHeight;
int cols = Console.BufferWidth;
char cursor = '<';
int characterRow = rows / 2;
int characterCol = cols / 2;
char fruit = '@';
int fruitRow = rand.Next(1, rows);
int fruitCol = rand.Next(1, cols);
int score = 0;
// Code in this loop executes infinitely
// unless Q or CTRL + C is pressed
while (true)
{
// Draw score, character, and fruit
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.Write($"Score: {score}");
Console.SetCursorPosition(characterCol, characterRow);
Console.Write(cursor);
Console.SetCursorPosition(fruitCol, fruitRow);
Console.Write(fruit);
// Capture user input
ConsoleKeyInfo cki = Console.ReadKey(false);
// End game if Q is pressed
if (cki.Key == ConsoleKey.Q)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.CursorVisible = true;
break;
}
// Change character position based on key
// Uses UpdatePosition()
string key = cki.Key.ToString();
key.ToUpper();
int colChange = 0;
int rowChange = 0;
Game.UpdatePosition(key, out colChange, out rowChange);
characterCol += colChange;
characterRow += rowChange;
// Update character symbol
// Uses UpdateCursor()
cursor = Game.UpdateCursor(key);
// Keep character in bounds
// Uses KeepInBounds()
characterCol = Game.KeepInBounds(characterCol, cols);
characterRow = Game.KeepInBounds(characterRow, rows);
// Update score and fruit if player scored
// Uses DidScore()
if (Game.DidScore(characterCol, characterRow, fruitCol, fruitRow))
{
score++;
fruitCol = rand.Next(cols);
fruitRow = rand.Next(rows);
}
}
}
}
}