-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
244 lines (215 loc) · 7.07 KB
/
Game.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using SplashKitSDK;
public class Game
{
private Playfield TetrisGrid = new Playfield(20, 10);
public Tetromino _CurrentShape;
public Tetromino _CollisionTester;
public bool Quit = false;
public bool _GameOver = false;
public Timer gameTimer;
public List<Tetromino> BagOfPieces = new List<Tetromino>();
public Game(Window gameWindow)
{
FillBagOfPieces();
_CurrentShape = ChooseNewShape();
_CollisionTester = _CurrentShape.Clone();
gameTimer = SplashKit.CreateTimer("gameTicks");
gameTimer.Start();
gameTimer.Pause();
}
private void MoveLeft()
{
if (gameTimer.IsPaused)
{
gameTimer.Start();
}
Console.WriteLine("Moving left");
if (_CollisionTester.MoveShape(Direction.Left) && !_CollisionTester.CheckOverlaps())
{
_CurrentShape.MoveShape(Direction.Left);
}
else
{
_CollisionTester.MoveShape(Direction.Right);
}
}
private void MoveRight()
{
if (gameTimer.IsPaused)
{
gameTimer.Start();
}
Console.WriteLine("Moving right");
if (_CollisionTester.MoveShape(Direction.Right) && !_CollisionTester.CheckOverlaps())
{
_CurrentShape.MoveShape(Direction.Right);
}
else
{
_CollisionTester.MoveShape(Direction.Left);
}
}
private void MoveDown()
{
if (gameTimer.IsPaused)
{
gameTimer.Start();
}
Console.WriteLine("Moving down");
if (_CollisionTester.MoveShape(Direction.Down) && !_CollisionTester.CheckOverlaps())
{
_CurrentShape.MoveShape(Direction.Down);
}
else
{
_CollisionTester.MoveShape(Direction.Up);
_CurrentShape.AssignToGrid();
TetrisGrid.ClearLines();
_CurrentShape = ChooseNewShape();
_CollisionTester = _CurrentShape.Clone();
_CurrentShape.GenerateShape();
_CollisionTester.GenerateShape();
}
}
private void RotatePiece()
{
if (gameTimer.IsPaused)
{
gameTimer.Start();
}
Console.WriteLine("Rotating shape");
_CollisionTester.RotateClockwise();
if (!_CollisionTester.CheckOverlaps())
{
_CurrentShape.RotateClockwise();
}
else
{
_CollisionTester.RotateCounterClockwise();
}
}
public void HandleInput()
{
SplashKit.ProcessEvents();
// Manual block generation for debugging/cheating
if (SplashKit.KeyTyped(KeyCode.IKey))
{
Console.WriteLine("Generating I");
_CurrentShape = new IShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new IShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.OKey))
{
Console.WriteLine("Generating O");
_CurrentShape = new OShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new OShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.TKey))
{
Console.WriteLine("Generating T");
_CurrentShape = new TShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new TShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.SKey))
{
Console.WriteLine("Generating S");
_CurrentShape = new SShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new SShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.ZKey))
{
Console.WriteLine("Generating Z");
_CurrentShape = new ZShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new ZShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.JKey))
{
Console.WriteLine("Generating J");
_CurrentShape = new JShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new JShape(3, 0, TetrisGrid._Grid);
}
if (SplashKit.KeyTyped(KeyCode.LKey))
{
Console.WriteLine("Generating L");
_CurrentShape = new LShape(3, 0, TetrisGrid._Grid);
_CollisionTester = new LShape(3, 0, TetrisGrid._Grid);
}
// Movement and rotation
if (SplashKit.KeyTyped(KeyCode.DownKey))
{
MoveDown();
}
if (SplashKit.KeyTyped(KeyCode.LeftKey))
{
MoveLeft();
}
if (SplashKit.KeyTyped(KeyCode.RightKey))
{
MoveRight();
}
if (SplashKit.KeyTyped(KeyCode.UpKey))
{
RotatePiece();
}
if (SplashKit.KeyTyped(KeyCode.EscapeKey))
{
Quit = true;
}
}
public void FillBagOfPieces()
{
BagOfPieces.Add(new IShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new OShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new TShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new SShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new ZShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new JShape(3, 0, TetrisGrid._Grid));
BagOfPieces.Add(new LShape(3, 0, TetrisGrid._Grid));
}
public Tetromino ChooseNewShape()
{
int rndIndex = SplashKit.Rnd(BagOfPieces.Count);
Tetromino selectedShape;
selectedShape = BagOfPieces[rndIndex];
BagOfPieces.Remove(BagOfPieces[rndIndex]);
if (BagOfPieces.Count == 0)
{
FillBagOfPieces();
}
return selectedShape;
}
public void UpdateGame()
{
// Determine if we have any blocks in the row second from the top of screen (spawn area)
for (int col = 1; col < TetrisGrid._Grid.GetLongLength(1) - 1; col++)
{
if (TetrisGrid._Grid[1, col].Type == BlockType.Filled)
{
_GameOver = true;
}
}
if (_GameOver == true)
{
// Restart the game and print your score when you lose
Console.WriteLine($"{TetrisGrid.clearedCounter} lines cleared");
TetrisGrid.clearedCounter = 0;
gameTimer.Reset();
gameTimer.Pause();
TetrisGrid = new Playfield(20, 10);
_GameOver = false;
}
// Since we refresh at 60 frames per second, our ticks are incremented by 16 or 17 due to floating point rounding
// Due to this rounding, there may be "double" movements or "skipped" movements. I could find a solution.
if (gameTimer.Ticks > 0 && gameTimer.Ticks % 500 < 17)
{
MoveDown();
}
}
public void DrawGame()
{
TetrisGrid.DrawBlocks();
_CurrentShape.DrawShape();
}
}