-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiPlayer.xaml.cs
More file actions
124 lines (115 loc) · 5.21 KB
/
MultiPlayer.xaml.cs
File metadata and controls
124 lines (115 loc) · 5.21 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfApp1.GameClasses;
namespace WpfApp1
{
public partial class MultiPlayer : Window
{
readonly MediaPlayer gameSounds = new MediaPlayer {Volume = 0.1};
readonly MediaPlayer backgroundMusic = new MediaPlayer {Volume = 0.1};
const int moveSize = 30;
BotMoving botMoving;
readonly DispatcherTimer time;
readonly Random rnd = new Random();
int playerLifes = 3, enemyLifes = 3;
readonly int width = SettingsClass.Width; // Ширина окна
readonly int height = SettingsClass.Height; // Высота окна
List<SnakeElement> snakeBody;
List<SnakeElement> snakeBodyEnemy;
Food food;
Directions currentPlayerDirection;
Directions currentEnemyDirections;
readonly Point playerStartPosition;
readonly Point enemyStartPosition;
public MultiPlayer()
{
InitializeComponent();
backgroundMusic.Open(new Uri("sounds/gameMusic.mp3", UriKind.RelativeOrAbsolute));
backgroundMusic.Play();
myGrid.Height = height;
myGrid.Children.Add(new Rectangle
{
Height = height, Width = 100, StrokeDashCap = PenLineCap.Round,
Stroke = Brushes.Gray
});
firstPlayerLifes.Text = firstPlayerLifes.Text + playerLifes;
secondPlayerLifes.Text = secondPlayerLifes.Text + enemyLifes;
Backgr.ImageSource = new BitmapImage(new Uri("images/backgame.png", UriKind.Relative));
Width = width + 100;
foodCanvas.Width = enemyCanvas.Width = canvas.Width = width;
foodCanvas.Height = enemyCanvas.Height = canvas.Height = Height = height;
playerStartPosition = new Point(moveSize, moveSize);
enemyStartPosition = new Point(width / (moveSize + 4) * moveSize, height / (moveSize + 4) * moveSize);
time = new DispatcherTimer();
currentPlayerDirection = Directions.Stay;
currentEnemyDirections = Directions.Stay;
snakeBody = new List<SnakeElement>();
snakeBodyEnemy = new List<SnakeElement>();
snakeBody.Add(new SnakeElement(playerStartPosition));
snakeBodyEnemy.Add(new SnakeElement(enemyStartPosition));
snakeBodyEnemy[0].rectangle.Fill = Brushes.Indigo;
time.Interval =
new TimeSpan(0, 0, 0, 0, SettingsClass.Difficulty); /*you can change speed of the snake here */
time.Tick += time_Tick;
}
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up && currentPlayerDirection != Directions.Down)
currentPlayerDirection = Directions.Up;
if (e.Key == Key.Down && currentPlayerDirection != Directions.Up)
currentPlayerDirection = Directions.Down;
if (e.Key == Key.Left && currentPlayerDirection != Directions.Right)
currentPlayerDirection = Directions.Left;
if (e.Key == Key.Right && currentPlayerDirection != Directions.Left)
currentPlayerDirection = Directions.Right;
if (SettingsClass.Mode == 0)
{
if (e.Key == Key.W && currentEnemyDirections != Directions.Down)
currentEnemyDirections = Directions.Up;
if (e.Key == Key.S && currentEnemyDirections != Directions.Up)
currentEnemyDirections = Directions.Down;
if (e.Key == Key.A && currentEnemyDirections != Directions.Right)
currentEnemyDirections = Directions.Left;
if (e.Key == Key.D && currentEnemyDirections != Directions.Left)
currentEnemyDirections = Directions.Right;
}
}
private void time_Tick(object sender, EventArgs e)
{
botMoving = new BotMoving(food, snakeBody, snakeBodyEnemy,
currentEnemyDirections, width, height, moveSize);
if (SettingsClass.Mode == 1) // против компа
currentEnemyDirections = botMoving.GetNextDirection();
MoveSnake(snakeBody);
MoveSnake(snakeBodyEnemy);
CheckAndIncreaseEnemy(snakeBodyEnemy);
CheckAndIncreasePlayer(snakeBody);
CheckAndChangeDirectory();
DrawSnake(snakeBody, canvas);
DrawSnake(snakeBodyEnemy, enemyCanvas);
CheckForFailsPlayer();
CheckForFailsEnemy();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
AddFoodInCanvas();
AddSnakeInCanvas(snakeBody, canvas);
AddSnakeInCanvas(snakeBodyEnemy, enemyCanvas);
time.Start();
}
private void Window_Closing(object sender, CancelEventArgs e)
{
time.Stop();
backgroundMusic.Stop();
MainWindow.player.Play();
Owner.Show();
}
}
}