Outline of snake writing
Thanks:
ThanksRonnie MooneyAfter learning his video, you can find the video in [C #] Creating a Snake Game-Less than an Hour, please bring your own ladder ).
WriteInput
Static class
- Initialize static variables
private static readonly Hashtable _keys = new Hashtable()
- Writing Method
public static void ChangeState(Keys key, bool state)
- Writing Method
public static bool Pressed(Keys key)
WriteSnakePart
Class
This class has only two variables.X
AndY
And a constructor, the role of the constructor isX
AndY
Assign values.
WriteDirection
Enumeration
WriteSnakeForm
Form
- Add
PictureBox
Control, namedpbCanvas
And set the size to 320*240, and the background colorLightSkyBlue
- Resize a form
- Add
SnakeForm_KeyDown
AndSnakeForm_KeyUp
Event
- Add
pbCanvas_Paint
Event. The event processing function is called.draw()
- Add
_score
Variable
- Add
_gameover
Variable
- Add
_direction
Variable
- Add
_snake
List Variables
- Add
_food
Variable
- Add
_gameLoop
Variable
- Add
_snakeLoop
Variable
- Add
_snakeRate
Variable
- Initialize each variable and set the callback method and callback interval of the timer variable (GameLoop: 1000/60,And start the timer to call
startGame
- Writing Method
startGame()
- Reset variable
- Instantiation
head
And add_snake
- Call
generateFood
- Write
generateFood()
- Generate a random
SnakePart
And assigned_food
To ensure that the newly generated_food
Not a snake
- Writing Method
gameover()
- Writing Method
draw()
- Writing Method
updateGame()
- Writing Method
updateSnake()