First Step VB6 - Sprites and Movement
Now the Ball image and the Paddle image both move, and any object like this that will move or perform some action will be saved as a "Sprite". So we need to declare (dim) both these things as "Sprites" and also while we are at it, set the x and y coordinates of the objects. To do so open up the GameLogic file and underneath LoadResources() add the following code:
Dim ball As Sprite Set ball = Graphics.CreateSprite(GameImage("Ball")) 'Position of ball ball.setX 400 ball.setY 300 Dim Paddle As Sprite Set Paddle = Graphics.CreateSprite(GameImage("Paddle")) 'Position of paddle Paddle.setX 400 Paddle.setY 550
Now to make these appear we have to draw them within the Loop. We can add this below where we draw the background picture. Add the following 2 lines of text:
Graphics.DrawSprite ball Graphics.DrawSprite Paddle
If you hit make and run the program now the paddle and the ball are seen in front of the background but will not be moving yet.
Getting the ball moving
In order to get our ball moving we need to add the following code below the y coordinate for the ball like this:
ball.setY 300 'Movement of the ball ball.SetMovementX 1 ball.SetMovementY 1
Remember you already have the first line of code from above. This code sets the speed at which the ball moves, it will increase it's x and y position on the screen by 1 every time it runs.
Then we need to add something to our loop so every time the program goes through the loop the ball moves a bit and not just once when the program starts. Add this line below SwinGame.Graphics.ClearScreen() in the Do loop:
Now when you run the program (make, then run) you should see the ball move off to the bottom right hand side of the screen... into nowhere....really fast...but we can fix that next by adding some tests to our game loop and changing the speed from 1 to 0.5 as well if it is too fast.
Testing for the outside border
To get the ball bouncing off the outside of the screen we need to add some tests to our game loop. We will use four "If" statements to test for the four different walls. Remember that our screen is 800x600 with the top left corner coordinates equaling 0,0 and bottom right 800,600.
When an outside border is hit we want the ball to bounce off. In game programming terms this means reversing the y and x movement depending on whether it has hit a top/bottom or side border. Top and bottom borders we reverse the Y movement, side border we reverse the X movement. Add the following If statement to your game loop below "Graphics.MoveSprite(ball)".
If ball.GetX + ball.GetWidth > 800 Then 'if the ball's position plus it's width is beyond the right hand side border ball.SetMovementX = -ball.GetMovementX 'reverse the x angle of movement End If
This code tests the right hand side border, you still need to test for the other 3 borders which means adding 3 more If statements below that one.
Now you do this...
See if you can figure out what the other 3 if statements should be. If you can't figure it out have a look at the solution here.
- First Step VB6 - Getting started and compiling "Hello World"
- Adding an Image - How to add an image and draw it
- Sprites and Movement - Getting the image to move
- Playing Sounds - Loading and playing a sound
- Keyboard Input - Moving a sprite with keys
- Sprite Collisions - Adding a "bounce" to the game
- Scoring - Adding a score to your game
- Mouse Input - Clicking sprites in your game