3.42k likes | 12.33k Views
The Snake Game. Lesson 14. How the Snake Game Draws the Snake!. The snake is drawn one pixel at a time. Uses the PSet command. User click on one of the path’s directional command buttons to change the path of the snake.
E N D
The Snake Game Lesson 14
How the Snake Game Draws the Snake! • The snake is drawn one pixel at a time. • Uses the PSet command. • User click on one of the path’s directional command buttons to change the path of the snake.
The variable sngX and sngY, specify the point on the form where the next pixel will appear. • The sngXFactor and sngYFactor specifies the number of pixels that are to be added to • sngX to advance the head of the snake in the X direction. • sngY to advance the head of the snake in the Y direction.
sngXFactor is zero because there is no change to the X coorodinate. • Example: sngXFactor = 0 sngYFactor = 1 sngYFactor is one because with each repetition of the loop the Y coordinate must increase by one pixel.
sngXFactor = 1 sngYFactor = 0 sngXFactor is one because the X coordinate must increase by one pixel with each repetition of the loop. The Y does not change because it is horizontal!
sngXFactor = 0 sngYFactor = -1 Moving the snake up and left requires a negativenumber because it is moving to the top or left.
Move the snake to the left the sngXFactor must be a negative number. This will move the snake to the left. sngXFactor = -1 sngYFactor = 0
How the game works: • The user runs the snake into the wall or into its tail. • The user clicks the Stop Game button. • The user click the Quit button.
Lets make some changes! • Open the snake game from lesson 1. • Change the value of lngSnakeColor to vbRed. • Add the following code to your program: lngSnakeColor = vbRed • Run your program! • What happens?
Let’s create faster button! • Add a command button above the Up button. • Set the Name property to cmdSpeed. • Set the Caption property to Faster. • Double click the Faster command button and add code to toggle the speed at which the snake moves and to change the caption of the command button.
If cmdSpeed.Caption = "Faster" Then lngMySpeed = 100000 cmdSpeed.Caption = "Slower" Else lngMySpeed = 200000 cmdSpeed.Caption = "Faster" End If
Move the code Dim lngMySpeed from the cmdStart section to the General Declaration section. • Save your program! • Run your program! • What happens?