using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using SnakeApp.Objects; using SnakeGame.Common; namespace SnakeApp { public partial class SnakeWindow : Form { public const int BLOCKS_WIDE = 32; public const int BLOCKS_HIGH = 19; public static Color BACK_COLOR = Color.Black; private Panel[][] displayGrid; public int gameWidth; public int gameHeight; public bool running; public bool appdead; Game game; MenuPanel menuPanel; public SnakeWindow() { InitializeComponent(); this.gameWidth = this.displayPanel.Width; this.gameHeight = this.displayPanel.Height - this.topWindowBackground.Height; displayPanel.Visible = false; createDisplayGrid(); running = false; appdead = false; } private void createDisplayGrid() { this.displayGrid = new Panel[BLOCKS_WIDE][]; for (int i = 0; i < BLOCKS_WIDE; i++) { this.displayGrid[i] = new Panel[BLOCKS_HIGH]; for (int j = 0; j < BLOCKS_HIGH; j++) { Panel cell = this.displayGrid[i][j] = new Panel(); cell.BackColor = BACK_COLOR; cell.Parent = this.displayPanel; cell.Width = (gameWidth / BLOCKS_WIDE) - 2; cell.Height = (gameHeight / BLOCKS_HIGH) - 2; cell.Location = new Point( 1+(i * (cell.Width+2)), 1+(j * (cell.Height+2)) + this.topWindowBackground.Height); } } } private void lll(object sender, EventArgs e) { if (menuPanel.go == true) { menuPanel.Dispose(); startGame(); } } private void SnakeWindow_Load(object sender, EventArgs e) { this.SetClientSizeCore(this.displayPanel.Width, this.displayPanel.Height); menu(); } private void SnakeWindow_KeyDown(object sender, KeyEventArgs e) { switch (e.KeyData) { case Keys.F2: if (!running) { startGame(); } break; case Keys.Up: game.up(); break; case Keys.Down: game.down(); break; case Keys.Left: game.left(); break; case Keys.Right: game.right(); break; case Keys.Escape: Application.Exit(); break; } } public void startGame() { game = new Game(displayGrid, scoreDisplay, levelDisplay, BLOCKS_WIDE, BLOCKS_HIGH); game.OnFinished+=new OnFinished(game_done); running = true; GameOver.Visible = false; displayPanel.Visible = true; System.Threading.Thread t = new System.Threading.Thread(game.run); t.Start(this); } public void game_done() { running = false; menu(); } public void menu() { if (appdead) return; Invoke( new MethodInvoker(delegate { if(menuPanel!=null){ menuPanel.Dispose(); menuPanel=null; } menuPanel = new MenuPanel(); menuPanel.Location = new Point(0,0); menuPanel.Parent = this; menuPanel.VisibleChanged += new EventHandler(this.lll); displayPanel.Visible = false; }) ); } private void SnakeWindow_FormClosing(object sender, FormClosingEventArgs e) { appdead = true; } } }