-
Aug 27th, 2014, 04:12 AM
#1
Thread Starter
New Member
How To Shoot Picture Box 'Bullet' On Key Press?
I am doing a school assignment where I have to code a basic game in vb 2008/2010. I am nearly finished and I made a maze type game where the user controls a character with the arrow keys and shoots obstacles to get to the end of the level. I have been playing around with different ways to do it, but have decided that the easiest way to shoot bullets is to just used wasd to shoot in the respective directions until it collides with a wall or and obstacle and then have it return to a position until a wasd key is pressed again. I've finished and the code seems right to me, however it is really glitchy for some reason. It will work once or twice but then it will start to twitch or go to random positions and not run across the screen like it is supposed to.
Here is what I have so far in the key down procedure for it
Case Keys.A
tmrBulletLeft.Start()
ptbBullet.Visible = True
Case Keys.D
tmrBulletRight.Start()
ptbBullet.Visible = True
Case Keys.W
tmrBulletUp.Start()
ptbBullet.Visible = True
Case Keys.S
tmrBulletDown.Start()
ptbBullet.Visible = True
And then there is a timer for each key direction with basically the same code in each
Private Sub tmrBulletLeft_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrBulletLeft.Tick
For Each objTemp In arrWalls
If ptbBullet.Bounds.IntersectsWith(objTemp.Bounds) Then
blnBulletCollision = True
tmrBulletLeft.Stop()
'to move the bullet from the path once it has been collided with
'ptbBullet.Left = ptbCharacter.Left + 10ptbBullet.Left -= 4
'ptbBullet.Top = ptbCharacter.Top + 25
ptbBullet.Left = 227
ptbBullet.Top = 239
ptbBullet.Visible = False 'so the bullet is not visible once moved
End If
Next
For Each objTemp In arrObstacles
If ptbBullet.Bounds.IntersectsWith(objTemp.Bounds) Then
blnBulletCollision = True
tmrBulletLeft.Stop()
intScore = intScore + 50
'to move the obstacle from the path once it has been collided with
objTemp.Left = 20
objTemp.Top = 125
objTemp.visible = False 'so the obstacles are not visible once moved
'to move the bullet from the path once it has been collided with
ptbBullet.Left = ptbCharacter.Left + 10
ptbBullet.Top = ptbCharacter.Top + 25
ptbBullet.Visible = False 'so the bullet is not visible once moved
End If
Next
End Sub
The only difference is the first line of the procedure which for the D key is
ptbBullet.Left += 4
The W key
ptbBullet.Top-= 4
And the S key
ptbBullet.Top += 4
It all seems right to me but for whatever reason it just isn't working correctly.
Please give me something haha, I've been scratching my head for a while and am gettng desperate and the assignment is due soon
Thank you in advance
-
Aug 27th, 2014, 09:33 PM
#2
Re: How To Shoot Picture Box 'Bullet' On Key Press?
In the future please wrap you code in tags. Those look like this:
[code]'Hello world[/code]
Would translate to:
As far as your question. The reason why it's so glitchy is because while you have the basics of VB.Net down, you do not have the basics of game programming down. What you should do is run everything in a game loop and inside the game loop is where you apply any physics(moving the bullet) and drawing(render the bullet in the new location). I have several lessons on game loops here on VBForums, just search my name and game loop.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|