From 2ac1c82c4cdb010a6d775a7a72944026320db6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Jurczyk?= Date: Sat, 3 Oct 2020 14:29:06 +0200 Subject: [PATCH] Added 60% keyboards support (eg. Razer Huntsman Mini), fixed bugs --- Chroma.cs | 176 ++++++++++++++++++++++++++----------- MainForm.Designer.cs | 24 +++-- MainForm.cs | 23 ++++- Properties/AssemblyInfo.cs | 4 +- Snake.cs | 18 +++- 5 files changed, 184 insertions(+), 61 deletions(-) diff --git a/Chroma.cs b/Chroma.cs index 25cf359..02d7e2c 100644 --- a/Chroma.cs +++ b/Chroma.cs @@ -74,15 +74,15 @@ private static async void MainLoop() case Snake.GameState.Paused: blinkState = !blinkState; - _grid[Key.Pause] = blinkState ? Color.Green : Color.Black; + _grid[Snake.Is60Percent ? Key.OemPeriod: Key.Pause] = blinkState ? Color.Green : Color.Black; await ShowBoard(); await Task.Delay(250); break; case Snake.GameState.Finished: blinkState = !blinkState; - _grid[Key.Escape] = blinkState ? Color.White : Color.Black; - _grid[Key.End] = blinkState ? Color.Green : Color.Black; + _grid[Snake.Is60Percent ? Key.OemTilde : Key.Escape] = blinkState ? Color.White : Color.Black; + _grid[Snake.Is60Percent ? Key.OemApostrophe : Key.End] = blinkState ? Color.Green : Color.Black; await ShowBoard(finishReset); finishReset = false; await Task.Delay(250); @@ -102,6 +102,14 @@ internal static async Task ShowBoard(bool reset = false) switch (Snake.State) { + case Snake.GameState.SelectKeyboardSize when reset: + _grid[Key.D6] = Color.Green; + _grid[Key.R] = Color.Green; + MainForm.Instance.statusLabel.Text = + // ReSharper disable once LocalizableElement + "Please select keyboard size.\r\nR - Regular or TE/TKL\r\n6 - 60%"; + break; + case Snake.GameState.SelectMode: switch (Snake.Speed) { @@ -148,7 +156,7 @@ internal static async Task ShowBoard(bool reset = false) if (reset) { - _grid[Key.Escape] = Color.White; + _grid[Snake.Is60Percent ? Key.OemTilde : Key.Escape] = Color.White; _grid[Key.Enter] = Color.Green; MainForm.Instance.statusLabel.Text = @"Please select speed using numeric keys (1-5) and press ENTER."; @@ -161,7 +169,7 @@ internal static async Task ShowBoard(bool reset = false) ShowSnakeBoard(); MainForm.Instance.statusLabel.Text = - $@"Game will begin in {Snake.Countdown} seconds."; + $@"Game will begin in {Snake.Countdown} second{(Snake.Countdown == 1 ? "" : "s")}."; if (Snake.Countdown > 2f) { @@ -184,34 +192,32 @@ internal static async Task ShowBoard(bool reset = false) if (reset) { - _grid[Key.Up] = DarkGold; - _grid[Key.Down] = DarkGold; - _grid[Key.Left] = DarkGold; - _grid[Key.Right] = DarkGold; + if (Snake.Is60Percent) + { + _grid[Key.I] = DarkGold; + _grid[Key.J] = DarkGold; + _grid[Key.K] = DarkGold; + _grid[Key.L] = DarkGold; + + _grid[Key.OemApostrophe] = Color.Black; + _grid[Key.OemPeriod] = Color.Black; + } + else + { + _grid[Key.Up] = DarkGold; + _grid[Key.Down] = DarkGold; + _grid[Key.Left] = DarkGold; + _grid[Key.Right] = DarkGold; - _grid[Key.End] = Color.Black; - _grid[Key.Pause] = Color.Black; + _grid[Key.End] = Color.Black; + _grid[Key.Pause] = Color.Black; + } } break; case Snake.GameState.InProgress: - ShowSnakeBoard(); - if (reset) { - _grid[Key.F12] = Color.White; - _grid[Key.F11] = Color.White; - _grid[Key.F10] = Color.White; - _grid[Key.F9] = Color.White; - _grid[Key.F8] = Color.White; - _grid[Key.F7] = Color.White; - _grid[Key.F6] = Color.White; - _grid[Key.F5] = Color.White; - _grid[Key.F4] = Color.White; - _grid[Key.F3] = Color.White; - _grid[Key.F2] = Color.White; - _grid[Key.F1] = Color.White; - _grid[Key.Escape] = Color.White; _grid[Key.LeftControl] = Color.White; _grid[Key.LeftWindows] = Color.White; _grid[Key.LeftAlt] = Color.White; @@ -220,32 +226,87 @@ internal static async Task ShowBoard(bool reset = false) _grid[Key.Function] = Color.White; _grid[Key.RightMenu] = Color.White; _grid[Key.RightControl] = Color.White; + + if (!Snake.Is60Percent) + { + _grid[Key.F12] = Color.White; + _grid[Key.F11] = Color.White; + _grid[Key.F10] = Color.White; + _grid[Key.F9] = Color.White; + _grid[Key.F8] = Color.White; + _grid[Key.F7] = Color.White; + _grid[Key.F6] = Color.White; + _grid[Key.F5] = Color.White; + _grid[Key.F4] = Color.White; + _grid[Key.F3] = Color.White; + _grid[Key.F2] = Color.White; + _grid[Key.F1] = Color.White; + + _grid[Key.Up] = Gold; + _grid[Key.Down] = Gold; + _grid[Key.Left] = Gold; + _grid[Key.Right] = Gold; - _grid[Key.Up] = Gold; - _grid[Key.Down] = Gold; - _grid[Key.Left] = Gold; - _grid[Key.Right] = Gold; - - _grid[Key.End] = DarkGreen; - _grid[Key.Pause] = DarkGreen; - + _grid[Key.End] = DarkGreen; + _grid[Key.Pause] = DarkGreen; + + _grid[Key.Escape] = Color.White; + } + else _grid[Key.OemTilde] = Color.White; + MainForm.Instance.statusLabel.Text = @"Game is in progress."; } + + ShowSnakeBoard(); + + if (Snake.Is60Percent) + { + if (_grid[Key.I] == Color.Black) + _grid[Key.I] = Gold; + + if (_grid[Key.J] == Color.Black) + _grid[Key.J] = Gold; + + if (_grid[Key.K] == Color.Black) + _grid[Key.K] = Gold; + + if (_grid[Key.L] == Color.Black) + _grid[Key.L] = Gold; + + if (_grid[Key.OemApostrophe] == Color.Black) + _grid[Key.OemApostrophe] = DarkGreen; + + if (_grid[Key.OemPeriod] == Color.Black) + _grid[Key.OemPeriod] = DarkGreen; + } break; case Snake.GameState.Paused: if (reset) { - ShowSnakeBoard(); - - _grid[Key.Up] = DarkGold; - _grid[Key.Down] = DarkGold; - _grid[Key.Left] = DarkGold; - _grid[Key.Right] = DarkGold; + if (Snake.Is60Percent) + { + _grid[Key.I] = Gold; + _grid[Key.J] = Gold; + _grid[Key.K] = Gold; + _grid[Key.L] = Gold; - _grid[Key.End] = DarkGreen; - _grid[Key.Pause] = Color.Green; + _grid[Key.OemApostrophe] = DarkGreen; + _grid[Key.OemPeriod] = Color.Green; + } + else + { + _grid[Key.Up] = DarkGold; + _grid[Key.Down] = DarkGold; + _grid[Key.Left] = DarkGold; + _grid[Key.Right] = DarkGold; + + _grid[Key.End] = DarkGreen; + _grid[Key.Pause] = Color.Green; + } + + ShowSnakeBoard(); MainForm.Instance.statusLabel.Text = @"Game paused. Press PAUSE key to unpause."; @@ -255,19 +316,32 @@ internal static async Task ShowBoard(bool reset = false) case Snake.GameState.Finished: if (reset) { - _grid[Key.Up] = Color.Black; - _grid[Key.Down] = Color.Black; - _grid[Key.Left] = Color.Black; - _grid[Key.Right] = Color.Black; - - _grid[Key.Escape] = Color.White; - _grid[Key.End] = Color.Green; - _grid[Key.Pause] = Color.Black; + if (Snake.Is60Percent) + { + _grid[Key.I] = Color.Black; + _grid[Key.J] = Color.Black; + _grid[Key.K] = Color.Black; + _grid[Key.L] = Color.Black; + _grid[Key.OemApostrophe] = Color.Green; + _grid[Key.OemPeriod] = Color.Black; + _grid[Key.OemTilde] = Color.White; + } + else + { + _grid[Key.Up] = Color.Black; + _grid[Key.Down] = Color.Black; + _grid[Key.Left] = Color.Black; + _grid[Key.Right] = Color.Black; + + _grid[Key.End] = Color.Green; + _grid[Key.Pause] = Color.Black; + _grid[Key.Escape] = Color.White; + } + MainForm.Instance.statusLabel.Text = @"Game over. Press END key to restart."; } - break; } diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs index 8f11160..375e531 100644 --- a/MainForm.Designer.cs +++ b/MainForm.Designer.cs @@ -31,6 +31,7 @@ private void InitializeComponent() { this.statusLabel = new System.Windows.Forms.Label(); this.authorLabel = new System.Windows.Forms.Label(); + this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // statusLabel @@ -38,7 +39,7 @@ private void InitializeComponent() this.statusLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))); this.statusLabel.Location = new System.Drawing.Point(12, 9); this.statusLabel.Name = "statusLabel"; - this.statusLabel.Size = new System.Drawing.Size(776, 82); + this.statusLabel.Size = new System.Drawing.Size(776, 121); this.statusLabel.TabIndex = 1; this.statusLabel.Text = "Loading"; // @@ -46,30 +47,43 @@ private void InitializeComponent() // this.authorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))); this.authorLabel.ForeColor = System.Drawing.Color.Red; - this.authorLabel.Location = new System.Drawing.Point(584, 120); + this.authorLabel.Location = new System.Drawing.Point(584, 130); this.authorLabel.Name = "authorLabel"; - this.authorLabel.Size = new System.Drawing.Size(204, 44); + this.authorLabel.Size = new System.Drawing.Size(204, 35); this.authorLabel.TabIndex = 2; - this.authorLabel.Text = "Created by Łukasz Jurczyk\r\nBuild Date: 2020/05/20"; + this.authorLabel.Text = "Created by Łukasz Jurczyk\r\nBuild Date: 2020/10/03"; + // + // label1 + // + this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte) (0))); + this.label1.ForeColor = System.Drawing.Color.Red; + this.label1.Location = new System.Drawing.Point(12, 143); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(319, 22); + this.label1.TabIndex = 3; + this.label1.Text = "Only US (QWERTY) keyboard layout is supported!"; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(800, 174); + this.Controls.Add(this.label1); this.Controls.Add(this.authorLabel); this.Controls.Add(this.statusLabel); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.KeyPreview = true; this.MaximizeBox = false; this.Name = "MainForm"; - this.Text = "RazerSnake"; + this.Text = "RazerSnake v. 1.1.0"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.Load += new System.EventHandler(this.Form1_Load); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown); this.ResumeLayout(false); } + private System.Windows.Forms.Label label1; + private System.Windows.Forms.Label authorLabel; internal System.Windows.Forms.Label statusLabel; diff --git a/MainForm.cs b/MainForm.cs index 9567ab9..ad568f2 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -31,25 +31,41 @@ private async void Form1_KeyDown(object sender, KeyEventArgs e) { e.SuppressKeyPress = true; if (!_ready) return; - + switch (e.KeyCode) { case Keys.Up: + case Keys.I: Snake.Dir = Snake.Direction.Up; break; case Keys.Down: + case Keys.K: Snake.Dir = Snake.Direction.Down; break; case Keys.Left: + case Keys.J: Snake.Dir = Snake.Direction.Left; break; case Keys.Right: + case Keys.L: Snake.Dir = Snake.Direction.Right; break; + case Keys.R when Snake.State == Snake.GameState.SelectKeyboardSize: + Snake.Is60Percent = false; + Snake.State = Snake.GameState.SelectMode; + await Chroma.ShowBoard(true); + break; + + case Keys.D6 when Snake.State == Snake.GameState.SelectKeyboardSize: + Snake.Is60Percent = true; + Snake.State = Snake.GameState.SelectMode; + await Chroma.ShowBoard(true); + break; + case Keys.Escape when Snake.State == Snake.GameState.SelectMode || Snake.State == Snake.GameState.Finished: Environment.Exit(0); break; @@ -87,26 +103,31 @@ private async void Form1_KeyDown(object sender, KeyEventArgs e) break; case Keys.Pause when Snake.State == Snake.GameState.InProgress: + case Keys.OemPeriod when Snake.State == Snake.GameState.InProgress: Snake.State = Snake.GameState.Paused; await Chroma.ShowBoard(true); break; case Keys.Pause when Snake.State == Snake.GameState.Paused: + case Keys.OemPeriod when Snake.State == Snake.GameState.Paused: Snake.Countdown = 3; Snake.State = Snake.GameState.Countdown; await Chroma.ShowBoard(true); break; case Keys.End when Snake.State == Snake.GameState.InProgress || Snake.State ==Snake.GameState.Paused || Snake.State ==Snake.GameState.Finished: + case Keys.Oem7 when Snake.State == Snake.GameState.InProgress || Snake.State ==Snake.GameState.Paused || Snake.State ==Snake.GameState.Finished: Snake.State = Snake.GameState.SelectMode; await Chroma.ShowBoard(true); break; case Keys.Insert when Snake.State == Snake.GameState.SelectMode && _testSequence < 2: + case Keys.OemQuestion when Snake.State == Snake.GameState.SelectMode && _testSequence < 2: _testSequence++; break; case Keys.Home when _testSequence == 2: + case Keys.Oem6 when _testSequence == 2: Testing = true; await Chroma.MapTest(); Testing = false; diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 67ca697..f43ab7a 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.1.0.0")] +[assembly: AssemblyFileVersion("1.1.0.0")] \ No newline at end of file diff --git a/Snake.cs b/Snake.cs index 18ebf0c..81e6ac7 100644 --- a/Snake.cs +++ b/Snake.cs @@ -14,6 +14,7 @@ internal enum Direction : byte internal enum GameState : byte { + SelectKeyboardSize, SelectMode, Countdown, InProgress, @@ -56,10 +57,11 @@ internal static byte Speed } } } - + + internal static bool Is60Percent; internal static byte Countdown = 3; internal static ushort SleepTime = 400; - internal static GameState State = GameState.SelectMode; + internal static GameState State = GameState.SelectKeyboardSize; internal static Direction Dir; private static byte _snakeLength, _speed = 3; private static readonly Random Rng = new Random(); @@ -151,10 +153,22 @@ internal static void NextStep() switch (Board[x][y]) { + case Right when Dir == Direction.Left && Board[x-1][y] == DoubleRight: + x -= 2; + break; + + case Right when Dir == Direction.Left: + x -= 1; + break; + case Right: x++; break; + case DoubleRight when Dir == Direction.Left: + x -= 1; + break; + case DoubleRight: x += 2; break;