Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApplication6
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public enum Direction
  16. { Up, Down, Left, Right };
  17. public Random rand = new Random();
  18. private int x, y, SnakeLength;
  19. private Direction direction;
  20. private bool FoodExists;
  21. Graphics g;
  22. Rectangle foodfield = new Rectangle();
  23. List<Rectangle> snake = new List<Rectangle>();
  24.  
  25. public Form1()
  26. {
  27. InitializeComponent();
  28. direction = Direction.Down;
  29. snake.Add(new Rectangle(1, 0, 20, 20));
  30. snake.Add(new Rectangle(21, 0, 20, 20));
  31. snake.Add(new Rectangle(41, 0, 20, 20));
  32. SnakeLength = 3;
  33. g = CreateGraphics();
  34. DrawSnake();
  35. }
  36.  
  37. private void Form1_KeyDown(object sender, KeyEventArgs e)
  38. {
  39. if(e.KeyCode==Keys.Down)
  40. {
  41. direction = Direction.Down;
  42. }
  43. if (e.KeyCode == Keys.Up)
  44. {
  45. direction = Direction.Up;
  46. }
  47. if (e.KeyCode == Keys.Right)
  48. {
  49. direction = Direction.Right;
  50. }
  51. if (e.KeyCode == Keys.Left)
  52. {
  53. direction = Direction.Left;
  54. }
  55. }
  56.  
  57. private void timer1_Tick(object sender, EventArgs e)
  58. {
  59. Refresh();
  60. timer1.Enabled = false;
  61. switch (direction)
  62. {
  63. case (Direction.Down):
  64. {
  65. y = 20;
  66. x = 0;
  67. break;
  68. }
  69. case (Direction.Up):
  70. {
  71. y = -20;
  72. x = 0;
  73. break;
  74. }
  75. case (Direction.Right):
  76. {
  77. y = 0;
  78. x = 20;
  79. break;
  80. }
  81. case (Direction.Left):
  82. {
  83. y = 0;
  84. x = -20;
  85. break;
  86. }
  87. }
  88. Rectangle Prev = snake[0];
  89. Rectangle Next;
  90. for (int i = 0; i < snake.Count - 1; i++)
  91. {
  92.  
  93. if (i == 0)
  94. {
  95.  
  96. snake[i] = new Rectangle(snake[i].X + x, snake[i].Y + y, 20, 20);
  97. }
  98. if (!(snake[i + 1].IsEmpty))
  99. {
  100. Next = snake[i + 1];
  101. snake[i + 1] = Prev;
  102. Prev=Next;
  103. }
  104. }
  105. if (snake[0]==foodfield)
  106. {
  107. snake.Add(foodfield);
  108. SnakeLength++;
  109. FoodExists = false;
  110. }
  111. if ((snake[0].X < 0 || snake[0].X > 510 || snake[0].Y < 0 || snake[0].Y > 700) || EatItSelf())
  112. {
  113. MessageBox.Show("Игра закончена\n Длина змейки равна: " + SnakeLength.ToString());
  114. Close();
  115. return;
  116. }
  117. DrawSnake();
  118. timer1.Enabled = true;
  119. }
  120. private void DrawSnake()
  121. {
  122. for (int i = 0; i < snake.Count; i++)
  123. {
  124. if (i == 0)
  125. g.FillRectangle(Brushes.Red, snake[i]);
  126. else
  127. g.FillRectangle(Brushes.Gray, snake[i]);
  128. g.DrawRectangle(Pens.Green, snake[i]);
  129. }
  130. }
  131. private bool EatItSelf()
  132. {
  133. int count = 0;
  134. foreach (Rectangle a in snake)
  135. if (a == snake[0]) count++;
  136. if (count > 1 && foodfield != snake[0])
  137. return true;
  138. else
  139. return false;
  140. }
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement