private void gameLoop(int lvl = 1) { currentGame = new Game(); currentGame.initGame(); dir = currentGame.currentOrientation; timer.Interval = 200 / (int)Math.Sqrt(lvl); timer.Enabled = true; timer.Start(); }
private void updateMap() { int s = 0; for (int i = 0; i < Space.W; i++) { for (int j = 0; j < Space.H; j++) { if (currentGame.Map[j, i].type == CellType.HEAD || currentGame.Map[j, i].type == CellType.SNAKE) { s++; } if (currentGame.Map[j, i].type != displayedMap[j, i].type) { currentGame.Map[j, i].Location = new Point(i * 20, j * 20); currentGame.Map[j, i].Size = new Size(20, 20); if (currentGame.Map[j, i].type == CellType.HEAD) { while (imgDir != dir) { currentGame.Map[j, i].Image.RotateFlip(RotateFlipType.Rotate270FlipNone); if (imgDir == Space.Orientation.EAST) { imgDir = Space.Orientation.NORTH; } else { imgDir++; } } } foreach (Cell c in gamePanel.Controls) { if (c.Location == new Point(i * 20, j * 20)) { c.Dispose(); } } if (currentGame.Map[j, i].type != CellType.EMPTY) { Console.WriteLine("Map : ( " + j + " ; " + i + " )"); Console.WriteLine("Type = " + currentGame.Map[j, i].type); gamePanel.Controls.Add(currentGame.Map[j, i]); displayedMap[j, i] = currentGame.Map[j, i]; } } } } size = s; }
/** * Moves snakes in direction and handle snake growing **/ private void move(Space.Orientation dir) { Coord currentHead = Snake.First.Value; Map[currentHead.y, currentHead.x] = new Cell(CellType.SNAKE); Snake.AddFirst(currentHead + Space.Vec[(int)dir]); Map[Snake.First.Value.y, Snake.First.Value.x] = new Cell(CellType.HEAD); if (toGrow == 0) { Map[Snake.Last.Value.y, Snake.Last.Value.x] = new Cell(CellType.EMPTY); Snake.RemoveLast(); } else { toGrow--; currentScore += 1; } }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { switch (keyData) { case Keys.Right: case Keys.D: if (dir != Space.Orientation.WEST && dirRead) { dir = Space.Orientation.EAST; dirRead = false; } return(true); case Keys.Left: case Keys.Q: if (dir != Space.Orientation.EAST && dirRead) { dir = Space.Orientation.WEST; dirRead = false; } return(true); case Keys.Up: case Keys.Z: if (dir != Space.Orientation.SOUTH && dirRead) { dir = Space.Orientation.NORTH; dirRead = false; } return(true); case Keys.Down: case Keys.S: if (dir != Space.Orientation.NORTH && dirRead) { dir = Space.Orientation.SOUTH; dirRead = false; } return(true); default: return(base.ProcessCmdKey(ref msg, keyData)); } }
public MainForm() { this.KeyPreview = true; InitializeComponent(); displayedMap = new Cell[Space.H, Space.W]; for (int i = 0; i < Space.W; i++) { for (int j = 0; j < Space.H; j++) { displayedMap[j, i] = new Cell(CellType.EMPTY); } } buffer = new List <Cell>(); dirRead = false; state = gameState.STOP; gamePanel.BackColor = Color.Black; gamePanel.BorderStyle = BorderStyle.FixedSingle; imgDir = Space.Orientation.NORTH; size = 0; best = new ScoreList(); pseudoIn = new PseudoInput(); }
/** * To be called **/ public bool update(Space.Orientation dir) { Console.WriteLine("Update"); Console.WriteLine(currentOrientation); Coord next = Snake.First.Value + Space.Vec[(int)dir]; foreach (Coord c in Snake) { Console.WriteLine("( " + c.y + " ; " + c.x + " )"); } switch (Map[next.y, next.x].type) { case CellType.FRUIT: fruitEaten(); move(dir); Console.WriteLine("FRUIT"); break; case CellType.EMPTY: move(dir); Console.WriteLine("EMPTY"); break; case CellType.SNAKE: case CellType.WALL: Console.WriteLine("WALL/SNAKE"); return(false); break; default: Console.WriteLine("????"); break; } return(true); }