ShiftOS_TheReturn/unusedcode/Snakey.cs

364 lines
11 KiB
C#
Raw Permalink Normal View History

2017-02-27 17:08:50 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ShiftOS.Engine;
2017-02-28 21:11:49 +00:00
using System.Diagnostics;
2017-02-27 17:08:50 +00:00
namespace ShiftOS.WinForms.Applications
{
[Launcher("Snakey", true, "al_snakey", "Games")]
[RequiresUpgrade("snakey")]
[WinOpen("snakey")]
[DefaultIcon("iconSnakey")]
public partial class Snakey : UserControl, IShiftOSWindow
{
private int[,] snakemap = null; // 0 - Nothing, 1 - Body, 2 - Head, 3 - Tail
2017-03-01 14:59:19 +00:00
private int[,] snakepartlist = null;
2017-02-27 23:25:23 +00:00
private int snakedirection = 0; // 0 - Left, 1 - Down, 2 - Right, 3 - Up
2017-02-28 16:05:59 +00:00
private Timer snakeupdater = new Timer();
2017-03-01 14:59:19 +00:00
private int snakelength = 0;
2017-02-28 16:05:59 +00:00
private bool extending = false;
2017-02-27 21:31:55 +00:00
2017-02-27 17:08:50 +00:00
public Snakey()
{
InitializeComponent();
}
public void OnLoad()
{
2017-02-28 16:05:59 +00:00
snakeupdater.Interval = 250;
snakeupdater.Tick += updateSnake;
2017-02-27 21:31:55 +00:00
makeGrid();
2017-02-27 17:08:50 +00:00
}
2017-02-28 16:05:59 +00:00
private void updateSnake(object sender, EventArgs e)
{
2017-02-28 21:11:49 +00:00
PictureBox head = null;
2017-02-28 22:41:18 +00:00
PictureBox tail = null;
2017-02-28 16:05:59 +00:00
for (int x = 0; x < 10; x++)
{
2017-02-28 21:11:49 +00:00
if (head != null) break;
2017-02-28 16:05:59 +00:00
for (int y = 0; y < 10; y++)
{
2017-02-28 21:11:49 +00:00
if (head != null) break;
2017-02-28 16:05:59 +00:00
if (snakemap[x, y] == 2)
{
2017-02-28 21:11:49 +00:00
head = (PictureBox)tableLayoutPanel1.GetControlFromPosition(x, y);
2017-02-28 16:05:59 +00:00
break;
}
}
}
2017-02-28 22:41:18 +00:00
for (int x = 0; x < 10; x++)
{
if (tail != null) break;
for (int y = 0; y < 10; y++)
{
if (tail != null) break;
if (snakemap[x, y] == 3)
{
tail = (PictureBox)tableLayoutPanel1.GetControlFromPosition(x, y);
break;
}
}
}
2017-02-28 21:11:49 +00:00
int headX = int.Parse(head.Name.Split('b')[1]);
2017-02-28 16:05:59 +00:00
int headY = int.Parse(head.Name.Split('b')[2]);
int newHeadX = headX;
int newHeadY = headY;
2017-02-28 22:41:18 +00:00
int tailX = int.Parse(tail.Name.Split('b')[1]);
int tailY = int.Parse(tail.Name.Split('b')[2]);
int newTailX = tailX;
int newTailY = tailY;
2017-02-28 16:05:59 +00:00
Image headImg = null;
switch (snakedirection)
{
case 0:
newHeadX = headX - 1;
headImg = Properties.Resources.SnakeyHeadL;
break;
case 1:
newHeadY = headY + 1;
headImg = Properties.Resources.SnakeyHeadD;
break;
case 2:
newHeadX = headX + 1;
headImg = Properties.Resources.SnakeyHeadR;
break;
case 3:
newHeadY = headY - 1;
headImg = Properties.Resources.SnakeyHeadU;
break;
default:
break;
}
2017-02-28 20:18:11 +00:00
if (newHeadX > 9 || newHeadX < 0 || newHeadY > 9 || newHeadY < 0) return;
2017-02-28 22:18:46 +00:00
int newheadlocation = snakemap[newHeadX, newHeadY];
2017-02-28 16:05:59 +00:00
snakemap[newHeadX, newHeadY] = 2;
2017-02-28 21:11:49 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(newHeadX, newHeadY)).Image = headImg;
2017-02-28 16:05:59 +00:00
snakemap[headX, headY] = 1;
2017-02-28 21:11:49 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(headX, headY)).Image = Properties.Resources.SnakeyBody;
2017-02-28 16:05:59 +00:00
if (!extending)
{
2017-03-01 23:21:56 +00:00
tail.Image = Properties.Resources.SnakeyBG; //mikey its here
snakemap[tailX, tailY] = 0;
tableLayoutPanel1.Refresh();
2017-03-01 14:59:19 +00:00
snakepartlist[newHeadX, newHeadY] = snakelength;
bool[,] exemptlist = new bool[10, 10];
2017-03-01 14:59:19 +00:00
bool splassigned = false;
for (int s = snakelength; s > 0; s--)
{
splassigned = false;
for (int x = 0; x < 10; x++)
{
if (splassigned) break;
for (int y = 0; y < 10; y++)
{
if (splassigned) break;
if (exemptlist[x, y]) continue;
2017-03-01 14:59:19 +00:00
if (x == newHeadX && y == newHeadY) continue;
if (snakepartlist[x, y] == snakelength)
{
snakepartlist[x, y]--;
exemptlist[x, y] = true;
2017-03-01 14:59:19 +00:00
splassigned = true;
break;
}
}
}
}
2017-02-28 22:41:18 +00:00
Image tailImg = null;
switch (tailDirection())
{
case 0:
newTailX = tailX - 1;
break;
case 1:
newTailY = tailY + 1;
break;
case 2:
newTailX = tailX + 1;
break;
case 3:
newTailY = tailY - 1;
break;
2017-02-28 16:05:59 +00:00
2017-02-28 22:41:18 +00:00
default:
break;
}
switch (nextTailDirection(newTailX, newTailY))
{
2017-03-01 14:59:19 +00:00
case 0:
tailImg = Properties.Resources.SnakeyTailL;
break;
case 1:
tailImg = Properties.Resources.SnakeyTailD;
break;
case 2:
tailImg = Properties.Resources.SnakeyTailR;
break;
case 3:
tailImg = Properties.Resources.SnakeyTailU;
break;
2017-02-28 22:41:18 +00:00
default:
break;
}
2017-03-01 14:59:19 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(newTailX, newTailY)).Image = tailImg;
snakemap[newTailX, newTailY] = 3;
2017-03-01 14:59:19 +00:00
}
if (extending)
{
snakepartlist[newHeadX, newHeadY] = snakelength;
extending = false;
2017-02-28 16:05:59 +00:00
}
2017-02-28 22:18:46 +00:00
if (newheadlocation == 1)
{
gameover();
}
2017-03-01 14:59:19 +00:00
if (newheadlocation == 4)
{
extending = true;
snakelength++;
placefruit();
}
2017-02-28 22:18:46 +00:00
}
2017-02-28 22:41:18 +00:00
private int nextTailDirection(int x, int y)
{
2017-03-01 14:59:19 +00:00
if (snakepartlist[x - 1, y] == 2) return 0;
if (snakepartlist[x, y + 1] == 2) return 1;
if (snakepartlist[x + 1, y] == 2) return 2;
if (snakepartlist[x, y - 1] == 2) return 3;
2017-02-28 22:41:18 +00:00
return -1;
}
2017-02-28 22:18:46 +00:00
private void gameover()
{
throw new NotImplementedException();
2017-02-28 16:05:59 +00:00
}
2017-02-28 21:11:49 +00:00
private void OnKeyDown(object sender, PreviewKeyDownEventArgs e)
2017-02-27 23:25:23 +00:00
{
2017-02-28 20:18:11 +00:00
switch (e.KeyCode)
2017-02-27 23:25:23 +00:00
{
2017-02-28 20:18:11 +00:00
case Keys.Up:
2017-02-27 23:25:23 +00:00
snakedirection = 3;
break;
2017-02-28 20:18:11 +00:00
case Keys.Down:
2017-02-27 23:25:23 +00:00
snakedirection = 1;
break;
2017-02-28 20:18:11 +00:00
case Keys.Left:
2017-02-27 23:25:23 +00:00
snakedirection = 0;
break;
2017-02-28 20:18:11 +00:00
case Keys.Right:
2017-02-27 23:25:23 +00:00
snakedirection = 2;
break;
default:
break;
}
2017-02-28 21:11:49 +00:00
Debug.Print("Snake Direction Value: " + snakedirection.ToString());
2017-02-27 23:25:23 +00:00
}
2017-02-27 21:31:55 +00:00
private void makeGrid()
2017-02-27 17:08:50 +00:00
{
2017-02-27 21:31:55 +00:00
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
tableLayoutPanel1.Controls.Add(newPicBox(x, y), x, y);
}
}
2017-02-27 17:08:50 +00:00
}
2017-02-27 21:31:55 +00:00
private PictureBox newPicBox(int x, int y)
2017-02-27 17:08:50 +00:00
{
2017-02-27 21:31:55 +00:00
PictureBox picBox = new PictureBox();
2017-02-27 17:08:50 +00:00
2017-02-27 21:31:55 +00:00
picBox.Size = new System.Drawing.Size(20, 20);
picBox.Image = Properties.Resources.SnakeyBG;
picBox.Name = "pb" + x.ToString() + "b" + y.ToString();
2017-02-28 22:18:46 +00:00
picBox.Margin = new Padding(0);
2017-02-27 21:31:55 +00:00
return picBox;
2017-02-27 17:08:50 +00:00
}
2017-02-27 21:31:55 +00:00
public void OnSkinLoad() { }
2017-02-28 21:11:49 +00:00
public bool OnUnload()
{
snakeupdater.Stop();
return true;
}
2017-02-27 21:31:55 +00:00
public void OnUpgrade() { }
2017-02-28 16:05:59 +00:00
private void button1_Click(object sender, EventArgs e)
{
if (snakemap != null)
{
clearGame();
makeGrid();
}
2017-02-28 17:11:40 +00:00
snakemap = new int[10, 10];
2017-03-01 14:59:19 +00:00
snakepartlist = new int[10, 10];
2017-02-28 16:05:59 +00:00
snakemap[5, 5] = 2;
2017-03-01 14:59:19 +00:00
snakepartlist[5, 5] = 4;
2017-02-28 17:27:16 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(5, 5)).Image = Properties.Resources.SnakeyHeadL;
2017-03-01 14:59:19 +00:00
snakemap[6, 5] = 1;
snakepartlist[6, 5] = 3;
((PictureBox)tableLayoutPanel1.GetControlFromPosition(6, 5)).Image = Properties.Resources.SnakeyBody;
snakemap[7, 5] = 1;
snakepartlist[7, 5] = 2;
((PictureBox)tableLayoutPanel1.GetControlFromPosition(7, 5)).Image = Properties.Resources.SnakeyBody;
2017-02-28 16:05:59 +00:00
snakemap[8, 5] = 3;
2017-03-01 14:59:19 +00:00
snakepartlist[8, 5] = 1;
2017-02-28 17:27:16 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(8, 5)).Image = Properties.Resources.SnakeyTailL;
2017-03-01 14:59:19 +00:00
snakelength = 4;
2017-02-28 22:18:46 +00:00
placefruit();
2017-02-28 16:05:59 +00:00
snakeupdater.Start();
}
2017-02-28 22:18:46 +00:00
private void placefruit()
{
Random rnd1 = new Random();
while (true)
{
int fruitX = rnd1.Next(10);
int fruitY = rnd1.Next(10);
if (snakemap[fruitX, fruitY] == 0)
{
snakemap[fruitX, fruitY] = 4;
2017-02-28 22:19:11 +00:00
((PictureBox)tableLayoutPanel1.GetControlFromPosition(fruitX, fruitY)).Image = Properties.Resources.SnakeyFruit;
2017-02-28 22:18:46 +00:00
break;
}
}
}
2017-02-28 16:05:59 +00:00
private void clearGame()
{
snakemap = null;
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
tableLayoutPanel1.Controls.Remove(tableLayoutPanel1.GetControlFromPosition(x, y));
}
}
}
2017-02-28 22:41:18 +00:00
private int tailDirection()
2017-02-28 16:05:59 +00:00
{
PictureBox tail = null;
for (int x = 0; x < 10; x++)
{
if (tail != null) break;
for (int y = 0; y < 10; y++)
{
if (snakemap[x, y] == 3)
{
tail = (PictureBox)tableLayoutPanel1.GetControlFromPosition(x, y);
break;
}
}
}
if (tail.Image == Properties.Resources.SnakeyTailL) return 0;
if (tail.Image == Properties.Resources.SnakeyTailD) return 1;
if (tail.Image == Properties.Resources.SnakeyTailR) return 2;
if (tail.Image == Properties.Resources.SnakeyTailU) return 3;
2017-02-28 16:05:59 +00:00
return -1;
}
2017-02-28 22:18:46 +00:00
2017-02-27 17:08:50 +00:00
}
}