-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormGameBoard.cs
531 lines (512 loc) · 24.8 KB
/
FormGameBoard.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LevelClass;
namespace SurvivalOfTheLuckiest
{
public partial class FormGameBoard : Form
{
List<Player> players;
GameBoard GameBoard;
Level MainLevel;
FormEvent FormEvent;
float width_ratio;
float height_ratio;
Graphics gDraw;
Bitmap mainBitmap;
Bitmap diceBitmap;
Bitmap displayBitmap;
Graphics gDisplay;
Graphics gDice;
int timerCount = 0;
bool MoveFlag = false;
bool RollFlag = true;
int moveCount = 0;
int randNum = 0;
int numTurns = 1;
bool dragonsword = true;
int[] Lose;
int[] Battle;
public FormGameBoard(List<Player> players)
{
InitializeComponent();
float screenWidth = Screen.PrimaryScreen.Bounds.Width;
float screenHeight = Screen.PrimaryScreen.Bounds.Height;
//screenWidth = 1920;
//screenHeight = 1080;
width_ratio = (screenWidth / this.Width);
height_ratio = (screenHeight / this.Height);
SizeF scale = new SizeF(width_ratio, height_ratio);
foreach (Control control in this.Controls)
{
control.Scale(scale);
}
foreach (Control control in this.Controls)
{
control.Font = new Font(control.Font.Name, control.Font.SizeInPoints * height_ratio * width_ratio, control.Font.Style);
}
this.players = players;
MainLevel = new Level();
GameBoard = new GameBoard(players, MainLevel);
GameBoard.Initialize();
mainBitmap = new Bitmap(pbMain.Width, pbMain.Height);
gDraw = Graphics.FromImage(mainBitmap);
diceBitmap = new Bitmap(pbDice.Width, pbDice.Height);
gDice = Graphics.FromImage(diceBitmap);
displayBitmap = new Bitmap(pbPlayers.Width, pbPlayers.Height);
gDisplay = Graphics.FromImage(displayBitmap);
Lose = new int[GameBoard.Count];
Battle = new int[GameBoard.Count];
//FormEvent FormEvent = new FormEvent(12);
//FormEvent.StartPosition = FormStartPosition.CenterScreen;
//*************************//
}
private void FormGameBoard_Load(object sender, EventArgs e)
{
MainLevel.Read(@"FinalLevel.lvl");
RefreshPlayers();
Random rand = new Random();
int randNum = rand.Next(6) + 1;
NextTurn(GameBoard[GameBoard.Active]);
//Player player1 = new Player("Player 1", 1);
//Player player2 = new Player("Player 2", 2);
//player1.Power = 1;
//player2.Power = 2;
//FormBattle formBattle = new FormBattle(player1,player2);
//formBattle.ShowDialog();
foreach (Control control in panelPlayer.Controls)
{
control.Font = new Font(control.Font.Name, control.Font.SizeInPoints * height_ratio * width_ratio, control.Font.Style);
}
FormStory FormStory = new FormStory();
FormStory.StartPosition = FormStartPosition.CenterScreen;
FormStory.ShowDialog();
}
public void RefreshPlayers()
{
Bitmap BackgroundImage = (Bitmap)pbMain.BackgroundImage;
using (Graphics g = Graphics.FromImage(BackgroundImage))
{
g.DrawImage(MainLevel.GetImage(), 0, 0);
}
pbMain.BackgroundImage = BackgroundImage;
gDraw.Clear(Color.Transparent);
for (int i = 0; i < players.Count; i++)
{
if (GameBoard[i].CurrentPosition.X == 6 && GameBoard[i].CurrentPosition.Y == 3)
gDraw.DrawImage(GameBoard[i].Avatar, GameBoard[i].CurrentPosition.X * (pbMain.Width / 15), (GameBoard[i].CurrentPosition.Y * (pbMain.Height / 10)) - 20, GameBoard[i].Avatar.Width * 0.85f * width_ratio, GameBoard[i].Avatar.Height * 0.85f * height_ratio);
else
gDraw.DrawImage(GameBoard[i].Avatar, GameBoard[i].CurrentPosition.X * (pbMain.Width / 15), GameBoard[i].CurrentPosition.Y * (pbMain.Height / 10), GameBoard[i].Avatar.Width * 0.85f * width_ratio, GameBoard[i].Avatar.Height * 0.85f * height_ratio);
}
pbMain.Image = mainBitmap;
}
public void NextTurn(Player player)
{
lblName.Text = player.Name;
lblName.Font = new Font(lblName.Font.Name, lblName.Font.Size, lblName.Font.Style);
Bitmap Avatar = player.getAvatar(0);
pbAvatar.Size = new Size((int)(Avatar.Size.Width * 6 / 5 * width_ratio), (int)(Avatar.Size.Height * 6 / 5 * height_ratio));
pbAvatar.BackgroundImage = Avatar;
pbDice.Image = drawDice(gDice, diceBitmap, (int)(5 * (width_ratio + height_ratio) / 2), new Pen(Color.Black), new SolidBrush(Color.Black), 0);
Bitmap power = new Bitmap(Image.FromFile("power" + player.Power + ".png"));
pbPower.BackgroundImage = power;
lblPower.Font = new Font(lblPower.Font.Name, lblPower.Font.Size, lblPower.Font.Style);
lblPower.Text = "Power: " + player.Power + "/4";
for (int i = 0; i < GameBoard.Count; i++)
{
Bitmap currentAvatar;
if (i == GameBoard.Active)
currentAvatar = GameBoard[i].getAvatar(1);
else
currentAvatar = GameBoard[i].getAvatar(-1);
if (GameBoard[i].AvatarNumber == 3)
gDisplay.DrawImage(currentAvatar, 25 * width_ratio, (i * 72 + 20 + i * 15) * height_ratio, 65 * width_ratio, 72 * height_ratio);
else if (GameBoard[i].AvatarNumber == 1)
gDisplay.DrawImage(currentAvatar, 8 * width_ratio, (i * 72 + 20 + i * 15) * height_ratio, 60 * width_ratio, 72 * height_ratio);
else if (GameBoard[i].AvatarNumber == 0)
gDisplay.DrawImage(currentAvatar, 10 * width_ratio, (i * 72 + 20 + i * 15) * height_ratio, 65 * width_ratio, 72 * height_ratio);
else
gDisplay.DrawImage(currentAvatar, 15 * width_ratio, (i * 72 + 15 + i * 15) * height_ratio, 60 * width_ratio, 72 * height_ratio);
gDisplay.DrawString(GameBoard[i].Name, new Font("Times New Roman", 14 * (width_ratio + height_ratio) / 2, FontStyle.Regular), new SolidBrush(Color.Black), 80 * width_ratio, (i * 72 + 20 + i * 15) * height_ratio);
gDisplay.DrawImage(Image.FromFile("power" + GameBoard[i].Power + ".png"), 80 * width_ratio, ((i * 72 + 20 + i * 15) + 20) * height_ratio, 70 * width_ratio, 20 * height_ratio);
}
pbPlayers.Image = displayBitmap;
//width_ratio = 1;
//height_ratio = 1;
}
Bitmap drawDice(Graphics gDice, Bitmap diceBitmap, int iDiceDistance, Pen myPen, Brush myBrush, int randNum)
{
gDice.Clear(Color.White);
gDice.FillRectangle(new SolidBrush(Color.Red), 0, 0, pbDice.Width - 1, pbDice.Height - 1);
gDice.DrawRectangle(myPen, 0, 0, pbDice.Width - 1, pbDice.Height - 1);
if (randNum == 1 || randNum == 3 || randNum == 5)
{
gDice.FillEllipse(myBrush, pbDice.Width / 2 - iDiceDistance, pbDice.Height / 2 - iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
if (randNum == 3 || randNum == 5)
{
gDice.FillEllipse(myBrush, iDiceDistance, pbDice.Height - 3 * iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
gDice.FillEllipse(myBrush, pbDice.Width - 3 * iDiceDistance, iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
if (randNum == 5)
{
gDice.FillEllipse(myBrush, iDiceDistance, iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
gDice.FillEllipse(myBrush, pbDice.Width - 3 * iDiceDistance, pbDice.Height - 3 * iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
}
}
}
if (randNum == 2 || randNum == 4 || randNum == 6)
{
gDice.FillEllipse(myBrush, iDiceDistance, iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
gDice.FillEllipse(myBrush, pbDice.Width - 3 * iDiceDistance, pbDice.Height - 3 * iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
if (randNum == 4 || randNum == 6)
{
gDice.FillEllipse(myBrush, iDiceDistance, pbDice.Height - 3 * iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
gDice.FillEllipse(myBrush, pbDice.Width - 3 * iDiceDistance, iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
if (randNum == 6)
{
gDice.FillEllipse(myBrush, pbDice.Width / 2 - iDiceDistance, iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
gDice.FillEllipse(myBrush, pbDice.Width / 2 - iDiceDistance, pbDice.Height - 3 * iDiceDistance, iDiceDistance * 2, iDiceDistance * 2);
}
}
}
if (randNum == 0)
{
gDice.DrawString("Roll", new Font("Times New Roman", 14 * (width_ratio + height_ratio) / 2, FontStyle.Italic), new SolidBrush(Color.Black), iDiceDistance * width_ratio, pbDice.Height / 2 - 2 * iDiceDistance * height_ratio);
}
return diceBitmap;
}
private void timerDice_Tick(object sender, EventArgs e)
{
if (timerCount > 15)
{
timerDice.Stop();
timerCount = 0;
moveCount = randNum;
MoveFlag = true;
RollFlag = false;
//************************************************
return;
}
Random rand = new Random();
randNum = rand.Next(6) + 1;
pbDice.Image = drawDice(gDice, diceBitmap, (int)(5 * (width_ratio + height_ratio) / 2), new Pen(Color.Black), new SolidBrush(Color.Black), randNum);
timerCount++;
}
private void pbDice_Click_1(object sender, EventArgs e)
{
if (RollFlag)
timerDice.Start();
}
public Keys SameDirection(Keys direction)
{
switch (direction)
{
//case Keys.Up:
// return Keys.Up;
//case Keys.Down:
// return Keys.Down;
//case Keys.Left:
// return Keys.Left;
//case Keys.Right:
// return Keys.Right;
case Keys.W:
return Keys.W;
case Keys.S:
return Keys.S;
case Keys.A:
return Keys.A;
case Keys.D:
return Keys.D;
default:
return Keys.Delete;
}
}
public Keys OppositeDirection(Keys direction)
{
switch (direction)
{
//case Keys.Up:
// return Keys.Down;
//case Keys.Down:
// return Keys.Up;
//case Keys.Left:
// return Keys.Right;
//case Keys.Right:
// return Keys.Left;
case Keys.W:
return Keys.S;
case Keys.S:
return Keys.W;
case Keys.A:
return Keys.D;
case Keys.D:
return Keys.A;
default:
return Keys.Delete;
}
}
private void FormGameBoard_KeyDown(object sender, KeyEventArgs e)
{
if (!MoveFlag)
return;
else
{
if (GameBoard.CheckMove(e.KeyData))
{
GameBoard.Move(e.KeyData, moveCount);
moveCount--;
}
else
{
if (GameBoard[GameBoard.Active].CurrentPosition.X == 5 && GameBoard[GameBoard.Active].CurrentPosition.Y == 3 && (e.KeyData == Keys.D || e.KeyData == Keys.Right) && (GameBoard.PreviousDirection[GameBoard.Active] != Keys.A && GameBoard.PreviousDirection[GameBoard.Active] != Keys.Left))
{
GameBoard[GameBoard.Active].X += 2;
GameBoard[GameBoard.Active].setAvatar(1);
moveCount--;
if (moveCount == 0)
{
GameBoard.PreviousDirection[GameBoard.Active] = Keys.Delete;
GameBoard.LastDirection[GameBoard.Active] = e.KeyData;
}
else
GameBoard.PreviousDirection[GameBoard.Active] = e.KeyData;
}
if (GameBoard[GameBoard.Active].CurrentPosition.X == 7 && GameBoard[GameBoard.Active].CurrentPosition.Y == 3 && (e.KeyData == Keys.A || e.KeyData == Keys.Left) && (GameBoard.PreviousDirection[GameBoard.Active] != Keys.D && GameBoard.PreviousDirection[GameBoard.Active] != Keys.Right))
{
GameBoard[GameBoard.Active].X -= 2;
GameBoard[GameBoard.Active].setAvatar(0);
moveCount--;
if (moveCount == 0)
{
GameBoard.PreviousDirection[GameBoard.Active] = Keys.Delete;
GameBoard.LastDirection[GameBoard.Active] = e.KeyData;
}
else
GameBoard.PreviousDirection[GameBoard.Active] = e.KeyData;
}
}
//dragon
if (GameBoard[GameBoard.Active].X == 12 && GameBoard[GameBoard.Active].Y == 7)
{
RefreshPlayers();
if (DialogResult.Yes == MessageBox.Show("Are you sure you want to fight the dragon? If you lose, you will respawn and lose all your bonus power.", "Are you sure", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
{
Player player1 = GameBoard[GameBoard.Active];
Player dragon = new Player("Dragon", 4);
dragon.setAvatar(0);
dragon.Power = 3;
FormBattle formBattle = new FormBattle(player1, dragon);
formBattle.StartPosition = FormStartPosition.CenterScreen;
formBattle.StartPosition = FormStartPosition.CenterScreen;
formBattle.ShowDialog();
formBattle.Lose.CurrentPosition = formBattle.Lose.StartPosition;
formBattle.Lose.Power = 1;
if (formBattle.Lose.Name == "Dragon")
{
FormWinner FormWinner = new FormWinner(GameBoard[GameBoard.Active].Name);
FormWinner.StartPosition = FormStartPosition.CenterScreen;
FormWinner.ShowDialog();
this.Close();
}
else
{
formBattle.Lose.CurrentPosition = formBattle.Lose.StartPosition;
formBattle.Lose.Power = 1;
RefreshPlayers();
}
}
moveCount = 0;
MoveFlag = false;
RollFlag = true;
if (GameBoard.Active == GameBoard.Count-1)
{
numTurns++;
}
if (GameBoard.Active + 1 == GameBoard.Count)
GameBoard.Active = 0;
else
GameBoard.Active++;
//skip the players turn if necessary
while (GameBoard.Skip[GameBoard.Active])
{
if (GameBoard.Active == GameBoard.Count-1)
{
numTurns++;
}
GameBoard.Skip[GameBoard.Active] = false;
if (GameBoard.Active + 1 == GameBoard.Count)
{
GameBoard.Active = 0;
}
else
GameBoard.Active++;
}
NextTurn(GameBoard[GameBoard.Active]);
if (autoRollToolStripMenuItem.Checked)
pbDice_Click_1(sender, e);
return;
}
if (moveCount <= 0)
{
RefreshPlayers();
Point currentPosition = GameBoard[GameBoard.Active].CurrentPosition;
//other player
if (GameBoard.OtherPlayers(GameBoard[GameBoard.Active]))
{
Player player1 = GameBoard.GetOtherPlayer(GameBoard[GameBoard.Active]);
Player player2 = GameBoard[GameBoard.Active];
Battle[GameBoard.Active]++;
for (int i = 0; i < GameBoard.Count; i++)
{
if (i == GameBoard.Active)
continue;
if (GameBoard[i].CurrentPosition == GameBoard[GameBoard.Active].CurrentPosition)
{
Battle[i]++;
break;
}
}
FormBattle formBattle = new FormBattle(player1, player2);
formBattle.StartPosition = FormStartPosition.CenterScreen;
formBattle.ShowDialog();
Lose[GameBoard.Active]++;
formBattle.Lose.CurrentPosition = formBattle.Lose.StartPosition;
formBattle.Lose.Power = 1;
currentPosition = GameBoard[GameBoard.Active].CurrentPosition;
//count battles lost
for (int i = 0; i < GameBoard.Count; i++)
{
if (GameBoard[i] == formBattle.Lose)
Lose[i]++;
}
}
RefreshPlayers();
//powerups
if (GameBoard.Board[currentPosition.X, currentPosition.Y] == 2)
{
Random rand = new Random();
int eventnumber = rand.Next(17);
FormEvent = new FormEvent(eventnumber, dragonsword, GameBoard[GameBoard.Active].Power);
FormEvent.StartPosition = FormStartPosition.CenterScreen;
FormEvent.ShowDialog();
if (eventnumber < 5)
{
GameBoard[GameBoard.Active].Power++;
}
else if (eventnumber == 5 || eventnumber == 6)
{
GameBoard[GameBoard.Active].Power--;
}
else if (eventnumber == 7)
{
if (dragonsword == true)
{
GameBoard[GameBoard.Active].Power += 2;
dragonsword = false;
}
else
{
GameBoard[GameBoard.Active].Power++;
}
}
else if (eventnumber == 8 || eventnumber == 9) //roll again
{
if (GameBoard.Active - 1 < 0)
GameBoard.Active = GameBoard.Count - 1;
else
GameBoard.Active--;
if (GameBoard.Active == GameBoard.Count - 1)
numTurns--;
}
else if (eventnumber == 10) //lose next turn
{
GameBoard.Skip[GameBoard.Active] = true;
}
else if (eventnumber == 11) //advance 1
{
moveCount = 1;
GameBoard.PreviousDirection[GameBoard.Active] = SameDirection(GameBoard.LastDirection[GameBoard.Active]);
pbDice.Image = drawDice(gDice, diceBitmap, 5, new Pen(Color.Black), new SolidBrush(Color.Black), 1);
return;
}
else if (eventnumber == 12) //go back 2
{
moveCount = 2;
GameBoard.PreviousDirection[GameBoard.Active] = OppositeDirection(GameBoard.LastDirection[GameBoard.Active]);
pbDice.Image = drawDice(gDice, diceBitmap, 5, new Pen(Color.Black), new SolidBrush(Color.Black), 2);
return;
}
else if (eventnumber == 13) //go back 1
{
moveCount = 1;
GameBoard.PreviousDirection[GameBoard.Active] = OppositeDirection(GameBoard.LastDirection[GameBoard.Active]);
pbDice.Image = drawDice(gDice, diceBitmap, 5, new Pen(Color.Black), new SolidBrush(Color.Black), 1);
return;
}
else if (eventnumber == 14) //advance 2
{
moveCount = 2;
GameBoard.PreviousDirection[GameBoard.Active] = SameDirection(GameBoard.LastDirection[GameBoard.Active]);
pbDice.Image = drawDice(gDice, diceBitmap, 5, new Pen(Color.Black), new SolidBrush(Color.Black), 2);
return;
}
else if (eventnumber == 15) //respawn
{
players[GameBoard.Active].CurrentPosition = players[GameBoard.Active].StartPosition;
RefreshPlayers();
}
else //lose next turn, +1 power
{
players[GameBoard.Active].Power++;
GameBoard.Skip[GameBoard.Active] = true;
}
}
MoveFlag = false;
RollFlag = true;
if (GameBoard.Active == GameBoard.Count-1)
{
numTurns++;
}
if (GameBoard.Active + 1 == GameBoard.Count)
GameBoard.Active = 0;
else
GameBoard.Active++;
//skip the players turn if necessary
while (GameBoard.Skip[GameBoard.Active])
{
if (GameBoard.Active == GameBoard.Count-1)
{
numTurns++;
}
GameBoard.Skip[GameBoard.Active] = false;
if (GameBoard.Active + 1 == GameBoard.Count)
{
GameBoard.Active = 0;
}
else
GameBoard.Active++;
}
NextTurn(GameBoard[GameBoard.Active]);
if (autoRollToolStripMenuItem.Checked)
pbDice_Click_1(sender, e);
return;
}
RefreshPlayers();
}
}
private void timerAnimate_Tick(object sender, EventArgs e)
{
}
private void autoRollToolStripMenuItem_Click(object sender, EventArgs e)
{
if (autoRollToolStripMenuItem.Checked == true)
autoRollToolStripMenuItem.Checked = false;
else
autoRollToolStripMenuItem.Checked = true;
}
}
}