Sample code is a great way to learn how to do things in computer programming. The problem for many beginners is that a lot of sample code is written by professionals for professionals. This means several things both good and bad. On the good side students are exposed to good coding practices (usually), technically correct code, and powerful techniques. On the bad side sample code for professionals often is very complicated (perhaps even over engineered) and often presents more new concepts than a beginner can safely handle. The problem than becomes separating what a beginner needs for a specific task and what has been included for more complicated problems. This is why I tend to write a lot of sample code and even better highlight sample code by better programmers/explainers than myself. Matt Gertz of the Visual Basic team is one of those people. If you search my blog for his name you’ll find a number of references to his posts on the Visual Basic Team Blog.
Recently I came across something he posted back in 2007 (Simple Animation in Visual Basic (Matt Gertz)) that explains some simple animation techniques in Visual Basic. He does a great job and I recommend the article. The gerbil code (there is a link to it at the bottom of the article or go here gerbil.zip) includes two simple projects. The Gerbil game and a very simple project that moves a sheep across the screen.
Along with the animation, Matt also includes a couple of other things that are well worth knowing. One of them is states as in state machines. I think that while he doesn’t spend a lot of time on the concept of a state machine the implementation is one that students can easily understand and work with. Once they know about that in practice I think you will find explaining the theory goes quickly and easily. The other thing that a student can pick up easily is drag and drop programming. Students like to do that and it is surprisingly easy once you know the steps.
For what it is worth I created as basic a sample of drag and drop as I could. This code just moves a picture box around the screen in response to pressing, holding and moving the mouse. When you release the mouse button the picture box stays were you leave it.
Try it. Have some fun with it. This week is all about the fun of programming for me.