-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpeechtrix.cs
446 lines (393 loc) · 11.9 KB
/
Speechtrix.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
using System;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Diagnostics;
using System.Drawing;
namespace Speechtrix
{
public class Speechtrix
{
const int maxY = 32;
const int maxX = 64;
const int nextLevelLines = 10;
bool newBlock;
bool game;
bool[,] gamefield; //matris med positioner, där vi använder 16(?) som standardbredd (position (0,0) är högst upp i vänstra hörnet)
int speed; //hastighet för fallande block i ms(?)
int level; //kanske för reglera speed/hur mycket poäng man får/vilka block som kommer
int score;
int linesToNextLevel; //lines needed to be removed until reaching next level
int startX, startY, endX, endY, height, width;
Graphics g;
Thread tt;
Thread graphicsThread;
LogicBlock current, next, rotateCheck;
Random rand;
SpeechThread sp;
public Speechtrix()
{
rotateCheck = new LogicBlock();
rand = new Random();
gamefield = new bool[maxX,maxY];
linesToNextLevel = nextLevelLines;
startY = 0; startX = 0;
height = 20; width = 10;
endY = height; endX = width;
Debug.Print("creating speechtrix");
g = new Graphics(width, height, this);
graphicsThread = new Thread(new ThreadStart(Graphics.Run));
graphicsThread.Start();
sp = new SpeechThread(this);
Thread.Sleep(3000);
Debug.Print("created graphics");
}
public static void Main(string[] args)
{
// Console.WriteLine("Would you like to play?");
// if (Console.Read() == "y")
// {
Speechtrix speechtrix = new Speechtrix();
speechtrix.newGame();
// }
}
void newGame() //nollställer tid, score, level, tömmer matris gamefield
{
for (int x = startX; x < endX; x++)
for (int y = startY; y < endY; y++)
gamefield[x, y] = false;
speed = 1000;
level = 1;
score = 0;
linesToNextLevel = nextLevelLines;
game = true;
TimerThread timer = new TimerThread(ref g);
tt = new Thread(new ThreadStart(timer.Run));
tt.Start();
updateScore(score);
play();
}
void copyNextToCurrent()
{
current.nr = next.nr;
current.state = next.state;
current.bxs = next.bxs;
current.bys = next.bys;
current.x = next.x;
current.y = next.y;
current.movable = next.movable;
current.color = next.color;
}
void initiateNewNext()
{
next.nr = (short)rand.Next(0, 7);
next.state = (short)rand.Next(0, 4);
next.y = 0; // (short)-next.bys;
next.x = (short)(width / 2 - next.bxs / 2);
next.movable = true;
}
/*************************************************/
void play()
{
current = new LogicBlock();
next = new LogicBlock();
initiateNewNext();
newBlock = true;
for (short i=0; !checkEnd() && game; i++)
{
if (newBlock)
{
newBlock = false;
copyNextToCurrent();
initiateNewNext();
g.setNext(next);
}
g.setBlock(current); //update falling block graphically
if (checkRowBelow())
{
lockBlock();
checkFullLine();
}
else if (current.movable) //om man inte tryckt ner så att block låsts
{
current.y++; //falling
Thread.Sleep(speed);
}
}
endGame();
}
/*************************************************/
void lockBlock()
{
current.movable = false;
for (int x = 0; x < current.bxs; x++)
{
for (int y = 0; y < current.bys; y++)
{
if (current.getState()[x, y])
{
gamefield[current.x + x, current.y + y] = true;
}
}
}
g.lockBlock(current, next);
if (current.y == 0)
game = false;
newBlock = true;
}
int[][] getRealCoord()
{
int[][] co = new int[4][];
int count = 0;
for (int x=0; x < current.bxs; x++)
{
for (int y=0; y < current.bys; y++)
{
if (current.getState()[x,y])
{
co[count] = new int[2]{
x+current.x,
y+current.y
};
count++;
}
}
}
return co;
}
bool checkRowBelow()
{
int[][] coord = new int[4][] {new int[2]{0,0},new int[2]{0,0},new int[2]{0,0},new int[2]{0,0}};
coord = getRealCoord();
for (int i = 0; i < 4; i++) //gå igenom blockets 4 (x,y)-koordinater
if (gamefield[coord[i][0], coord[i][1]+1] //om det på raden under en koordinat finns sparat block
|| coord[i][1] == height-1) //om y-koordinat är på sista raden
return true;
return false;
}
void newLevel() //ändra level -> ändrar speed/poängsätt...
{
level++;
linesToNextLevel = nextLevelLines;
if (speed > 100)
setSpeed(speed-50);
}
void setSpeed(int ms) //sätter fallhastighet
{
speed = ms;
}
bool checkEnd() //när ett block inte får plats i gamefield
{
bool end = false;
return end;
}
void endGame() //när spelet tar slut
{
tt.Abort();
checkHighscore();
showMenu();
}
void checkHighscore()
{
}
void showMenu()
{
}
void updateScore(int lines) //hur många lines som tagit bort
{
linesToNextLevel -= lines;
if (linesToNextLevel <= 0)
{
newLevel();
linesToNextLevel += nextLevelLines;
}
switch (lines)
{
case 1: score += level * 10 * lines; break;
case 2: score += level * 20 * lines; break;
case 3: score += level * 40 * lines; break;
case 4: score += level * 100 * lines; break;
default: break;
}
g.setScore(score);
}
void checkFullLine() //kolla om det finns några rader i matrisen där alla är true, isf anropa deleteLine för varje rad
{
// ingen poäng med att kolla alla rader, man behöver bara current-blocks rader eftersom att det bara är de som kan ha blivit fulla
int[] deleteLines = new int[4] { 999, 999, 999, 999 };
int count = 0;
int deletedLines = 0;
short minY = current.y;
short maxY = (short) (current.y+current.bys);
for (int i = minY; i < maxY; i++)
{
int j = startX;
for (; j<width && gamefield[j,i]; j++) ; //as long as gamefield is all true on a row
if (j==width) //if whole row true
{
deletedLines++;
deleteLine(i);
deleteLines[count] = i;
count++;
}
}
if (deletedLines > 0)
{
g.removeRow(deleteLines);
updateScore(deletedLines);
}
}
void deleteLine(int lineNumber) //i matrisen, flytta alla bool-värden från rader ovanför lineNumber en rad nedåt, anropar updateScore
{
for (int y = lineNumber; y > startY; y--)
{
for (int x = startX; x < endX; x++)
{
gamefield[x, y] = gamefield[x, y - 1];
}
}
sp.speak("Jippee!");
}
bool canGoLeft()
{
if (current.x <= 0)
return false;
rotateCheck.nr = current.nr;
rotateCheck.state = current.state;
rotateCheck.x = current.x;
rotateCheck.x--;
for (int i = 0; i < rotateCheck.bxs; i++)
for (int j = 0; j < rotateCheck.bys; j++)
if (rotateCheck.getState()[i, j] && gamefield[i + rotateCheck.x, j + current.y])
return false;
return true;
}
bool canGoRight()
{
if (current.x >= width - current.bxs)
return false;
rotateCheck.nr = current.nr;
rotateCheck.state = current.state;
rotateCheck.x = current.x;
rotateCheck.x++;
for (int i = 0; i < rotateCheck.bxs; i++)
for (int j = 0; j < rotateCheck.bys; j++)
if (rotateCheck.getState()[i, j] && gamefield[i + rotateCheck.x, j + current.y])
return false;
return true;
}
bool canRotate()
{
short newXsize = Blocks.sizes[0][current.nr, ((current.state+1) % 4)];
short newYsize = Blocks.sizes[1][current.nr, ((current.state+1) % 4)];
rotateCheck.nr = current.nr;
rotateCheck.state = (short)((current.state + 1) % 4);
for (int i = 0; i < rotateCheck.bxs; i++)
for (int j = 0; j < rotateCheck.bys; j++)
if (rotateCheck.getState()[i, j] && gamefield[i + current.x, j + current.y])
return false;
if (current.x + newXsize <= width)
return true;
else
return false;
}
public void keyUp()
{
if (canRotate())
{
current.state = (short) ((current.state+1) % 4);
g.setBlock(current);
}
}
public void keyDown()
{
if (current.movable)
{
//current.y++;
/***as long as possible***/
while (current.y < height)
{
if (!checkRowBelow())
current.y++;
else
break;
}
lockBlock();
g.setBlock(current);
checkFullLine();
}
}
public void keyLeft()
{
if (canGoLeft())
{
current.x--;
g.setBlock(current); // makes the graphics bug a bit
}
}
public void keyRight()
{
if (canGoRight())
{
current.x++;
g.setBlock(current); // makes the graphics bug a bit
}
}
public void keyN()
{
if (game)
{
tt.Abort();
g.resetColors();
}
newGame();
}
public void understand()
{
g.drawUnderstanding(Color.Green);
}
public void quit()
{
Environment.Exit(0);
}
public void notUnderstand()
{
g.drawUnderstanding(Color.Red);
}
}
class TimerThread
{
Graphics g;
int hours;
int minutes;
int seconds;
public TimerThread(ref Graphics _g)
{
Debug.Print("creating TimerThread");
g = _g;
hours = 0;
minutes = 0;
seconds = 0;
}
public void Run()
{
while (true)
{
Thread.Sleep(1000);
seconds++;
if (seconds == 60)
{
minutes++;
seconds = 0;
}
if (minutes == 60)
{
hours++;
minutes = 0;
}
g.setTime(hours, minutes, seconds);
}
}
}
}