![]() With that in place, we can now implement our DrawSnake() method: private void DrawSnake()įoreach(SnakePart snakePart in snakeParts)įill = (snakePart.IsHead ? snakeHeadBrush : snakeBodyBrush) We also define a List, which will keep a reference to all parts of the snake. We define two SolidColorBrush'es, one for the body and one for the head. Private SolidColorBrush snakeHeadBrush = Brushes.YellowGreen Private SolidColorBrush snakeBodyBrush = Brushes.Green This simple class will contain information about each part of the snake: Where is the element positioned in our game area, which UIElement (a Rectangle, in our case) represents the part, and is this the head-part of the snake or not? We'll use all of it later, but first, inside our Window class, we need to define a couple of fields to be used in our DrawSnake() method (and later in other methods as well): public partial class SnakeWPFSample : Window Let's start with the SnakePart class, which you would normally define in a new file ( SnakePart.cs, for instance): using System.Windows We'll draw the snake with a method called DrawSnake() - the method is actually quite simple, but it does require quite a bit of extra stuff, including a new class called SnakePart, as well as some extra fields on the Window class. Once again, we'll use the WPF Rectangle class to form a snake of a certain length, with each element being the same width and height as the background squares, or as we call it: The SnakeSquareSize constant! Creating the Snake With that in place, it's now time to create the actual snake and then make it move around the area. In the last article, we made a nice area for the snake in our SnakeWPF implementation to move around. If you want to get the complete source code for the game at once, to get started modifying and learning from it right now, consider downloading all our samples! Creating & moving the Snake It makes sense to start with the Introduction and then work your way through the articles one by one, to get the full understanding. In this article series, we're building a complete Snake game from scratch.
0 Comments
Leave a Reply. |