-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cs
83 lines (69 loc) · 2.12 KB
/
Controller.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace SnakeMess
{
class Controller
{
private Model model;
private View view;
private Direction lastDirection;
private Direction newDirection;
private bool pause;
private bool gameOver;
public Controller()
{
Console.CursorVisible = false;
model = new Model();
view = new View(model);
newDirection=Direction.DOWN;
lastDirection=newDirection;
GameLoop();
}
public void GameLoop(){
Stopwatch gametimer = new Stopwatch();
gametimer.Start();
while(!gameOver){
UserInput();
if(gametimer.ElapsedMilliseconds < 100 || pause)
continue;
gametimer.Restart();
model.MoveSnake(newDirection);
lastDirection = newDirection;
if (model.CheckAppleCollision()) {
model.PlaceApple();
} else {
model.RemoveSnakePart();
}
if (model.CheckDeathCollision())
{
gameOver = true;
continue;
}
model.AddSnakeHead();
view.UpdateView();
}
}
public void UserInput()
{
if ( Console.KeyAvailable )
{
InputHandler.Instance.UpdateInput();
if (InputHandler.Instance.Up() && lastDirection != Direction.DOWN)
newDirection = Direction.UP;
else if (InputHandler.Instance.Right() && lastDirection != Direction.LEFT)
newDirection = Direction.RIGHT;
else if (InputHandler.Instance.Down() && lastDirection != Direction.UP)
newDirection = Direction.DOWN;
else if (InputHandler.Instance.Left() && lastDirection != Direction.RIGHT)
newDirection = Direction.LEFT;
else if (InputHandler.Instance.Space())
pause = !pause;
else if (InputHandler.Instance.Escape())
gameOver = true;
}
}
}
}