From 998f3357a77661cdd85aa1f419bed5054aa21ede Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 28 Feb 2017 15:18:11 -0500 Subject: [PATCH] fix Snakey NRE --- .../Applications/Snakey.Designer.cs | 1 + ShiftOS.WinForms/Applications/Snakey.cs | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ShiftOS.WinForms/Applications/Snakey.Designer.cs b/ShiftOS.WinForms/Applications/Snakey.Designer.cs index b44ea8c..1b21792 100644 --- a/ShiftOS.WinForms/Applications/Snakey.Designer.cs +++ b/ShiftOS.WinForms/Applications/Snakey.Designer.cs @@ -90,6 +90,7 @@ this.Controls.Add(this.tableLayoutPanel1); this.Name = "Snakey"; this.Size = new System.Drawing.Size(317, 329); + this.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.OnKeyPress); this.ResumeLayout(false); this.PerformLayout(); diff --git a/ShiftOS.WinForms/Applications/Snakey.cs b/ShiftOS.WinForms/Applications/Snakey.cs index 2f01b88..2e437b5 100644 --- a/ShiftOS.WinForms/Applications/Snakey.cs +++ b/ShiftOS.WinForms/Applications/Snakey.cs @@ -40,9 +40,10 @@ namespace ShiftOS.WinForms.Applications for (int x = 0; x < 10; x++) { - if (head != null) break; + if (head != null) continue; for (int y = 0; y < 10; y++) { + if (head != null) continue; if (snakemap[x, y] == 2) { head = tableLayoutPanel1.GetControlFromPosition(x, y); @@ -51,8 +52,6 @@ namespace ShiftOS.WinForms.Applications } } - if (head == null) return; - int headX = int.Parse(head.Name.Split('b')[1]); // NRE was here int headY = int.Parse(head.Name.Split('b')[2]); @@ -86,7 +85,7 @@ namespace ShiftOS.WinForms.Applications break; } - if (newHeadX > 9 | newHeadX < 0 | newHeadY > 9 | newHeadY < 0) return; + if (newHeadX > 9 || newHeadX < 0 || newHeadY > 9 || newHeadY < 0) return; snakemap[newHeadX, newHeadY] = 2; tableLayoutPanel1.GetControlFromPosition(newHeadX, newHeadY).BackgroundImage = headImg; snakemap[headX, headY] = 1; @@ -97,23 +96,23 @@ namespace ShiftOS.WinForms.Applications } } - private void OnKeyPress(object sender, KeyPressEventArgs e) + private void OnKeyPress(object sender, PreviewKeyDownEventArgs e) { - switch (e.KeyChar) + switch (e.KeyCode) { - case (char)Keys.Up: + case Keys.Up: snakedirection = 3; break; - case (char)Keys.Down: + case Keys.Down: snakedirection = 1; break; - case (char)Keys.Left: + case Keys.Left: snakedirection = 0; break; - case (char)Keys.Right: + case Keys.Right: snakedirection = 2; break; @@ -205,5 +204,7 @@ namespace ShiftOS.WinForms.Applications return -1; } + + } }