Advertisement
Guest User

PC01 Week 2 Project

a guest
Jun 20th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Threading;
  7.  
  8. namespace snaaaaaaaake
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. int[,] grid = new int[10, 10];
  15. Random rng = new Random();
  16.  
  17. ConsoleColor snakeColour = ConsoleColor.Green;
  18. ConsoleColor powerUpColour = ConsoleColor.Red;
  19.  
  20. int cRow = 4;
  21. int cCol = 4;
  22. int dir = 0;
  23. int size = 1;
  24.  
  25. grid[cRow, cCol] = size;
  26. AddPowerUp(rng, grid);
  27.  
  28. int millisecondsPerUpdate = 200;
  29.  
  30. while (true)
  31. {
  32. Update(rng, grid, ref cRow, ref cCol, ref dir, ref size, ref millisecondsPerUpdate);
  33. Thread.Sleep(millisecondsPerUpdate);
  34. RenderGame(grid, snakeColour, powerUpColour, size);
  35.  
  36. }
  37.  
  38. }
  39.  
  40. public static void AddPowerUp(Random rng, int[,] grid)
  41. {
  42. int rRow = rng.Next(grid.GetLength(0));
  43. int rCol = rng.Next(grid.GetLength(1));
  44. if (grid[rRow, rCol] > 0)
  45. {
  46. AddPowerUp(rng, grid);
  47. }
  48. else
  49. {
  50. grid[rRow, rCol] = -1;
  51. }
  52. }
  53.  
  54. public static void RenderGame(int[,] grid, ConsoleColor snakeColour, ConsoleColor powerUpColour, int size)
  55. {
  56. Console.Clear();
  57. Console.WriteLine("score: " + size);
  58. for (int x = 0; x < grid.GetLength(0); x++)
  59. {
  60. for (int y = 0; y < grid.GetLength(1); y++)
  61. {
  62. if (grid[x, y] == 0)
  63. {
  64. Console.ForegroundColor = ConsoleColor.White;
  65. Console.Write(" ");
  66. }
  67. else if (grid[x, y] < 0)
  68. {
  69. Console.ForegroundColor = powerUpColour;
  70. Console.Write("X");
  71. }
  72. else
  73. {
  74. Console.ForegroundColor = snakeColour;
  75. Console.Write("O");
  76. }
  77. }
  78. Console.WriteLine();
  79. }
  80.  
  81. }
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement