-
May 19th, 2015, 08:02 PM
#1
Thread Starter
New Member
How to fix score increments and randomizing pictureboxes .Top
I am making flappy bird. I have 2 problems i am not to sure what i would do. The first problem is my score is going up to 19 instead of 1, i have found that it is because i have it saying when "bird.left > pipe.left" the score is suppose to go up +1, but it goes up to 19 because the birds ".left" is still greater then the pipes ".left" when it passes so the score keeps going up. So i am wondering how i would get it so the score will stop going up when it sees that the bird is past the pipe. The 2nd problem is that i don't know what i would do to have it so the gap between the top and bottom pipes are always the same but the bottom and top pipes ".Top" will change so the pipes will be randomized. Here is all my code.
Code:
Public Class MainForm
Dim ySpeed As Integer = 0
Dim gravity As Integer = 2
Dim bottomPipes() As PictureBox
Dim topPipes() As PictureBox
Dim score As Integer = 0
Private random As New Random
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim num1, num2 As Integer
num1 = random.Next(1, 400)
num2 = random.Next(450, 710)
' Randomizes the pipes heights
Randomize()
' Timer starts when the form is loaded
Timer1.Start()
' Populate Array
bottomPipes = {pipe1, pipe2, pipe3, pipe4}
topPipes = {topPipe1, topPipe2, topPipe3, topPipe4}
' Randomizes bottom pipes height
For Each bP As PictureBox In bottomPipes
bP.Top = num1
Next
' Randomizes top pipes height
For Each tP As PictureBox In bottomPipes
tP.Top = num2
Next
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
' Bird will bounce when space bar is pressed
If e.KeyCode = Keys.Space Then
ySpeed = -15
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ySpeed += gravity
bird.Top += ySpeed
' Pipes in bottom array moves to the left
For Each p As PictureBox In bottomPipes
p.Left -= 10
' Moves bottom pipes back onto form
If p.Left < -200 Then
p.Left += 1300
End If
Next
' Pipes in top array moves to the left
For Each i As PictureBox In topPipes
i.Left -= 10
' Moves top pipes back onto form
If i.Left < -200 Then
i.Left += 1300
End If
Next
' Adds 1 point to score label when bird passes pipe
' Collision Detection for top and bottom pipes
For index = 0 To 3
If Me.bird.Bounds.IntersectsWith(bottomPipes(index).Bounds) Then
Timer1.Stop()
ElseIf Me.bird.Bounds.IntersectsWith(topPipes(index).Bounds) Then
Timer1.Stop()
End If
Next
' Collision detection for ground
If Me.bird.Bounds.IntersectsWith(ground.Bounds) Then
Timer1.Stop()
End If
' Hides main menu, game over, high score, and high score label when timmer is enabled
If Timer1.Enabled = True Then
mainMenu.Hide()
gameOver.Hide()
highsScore.Hide()
highScoreLabel.Hide()
' Shows main menu, game over, high score, and high score label when timmer is enabled
ElseIf Timer1.Enabled = False Then
mainMenu.Show()
gameOver.Show()
highsScore.Show()
highScoreLabel.Show()
End If
End Sub
Private Sub mainMenu_Click(sender As Object, e As EventArgs) Handles mainMenu.Click
' Restarts game when main menu button is clicked
Application.Restart()
End Sub
End Class
-
May 21st, 2015, 07:38 PM
#2
Re: How to fix score increments and randomizing pictureboxes .Top
Originally Posted by LlamaDrama
The first problem is my score is going up to 19 instead of 1, i have found that it is because i have it saying when "bird.left > pipe.left" the score is suppose to go up +1, but it goes up to 19 because the birds ".left" is still greater then the pipes ".left" when it passes so the score keeps going up.
You could consider adding a Boolean Variable to each 'Bird' which you Set when it's ok to advance the score, but Reset as you add the one. Only reset it when you're next ready to increment the score.
VB.NET Code:
If bird.left > pipe.left And bird.flag then
bird.score + = 1
bird.flag = False
end if.
'Suggestion
If bird.left < pipe.left then
bird.flag = True
end if.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 21st, 2015, 08:46 PM
#3
Thread Starter
New Member
Re: How to fix score increments and randomizing pictureboxes .Top
Is the ".flag" the boolean that you made?
-
May 24th, 2015, 08:09 AM
#4
Re: How to fix score increments and randomizing pictureboxes .Top
Originally Posted by LlamaDrama
Is the ".flag" the boolean that you made?
Hi,
Yes, sorry, 'Flag' is a term we oldies used to use, it means a boolean variable.
I still use the name when I've only the one boolean variable.
Poppa.
Along with the sunshine there has to be a little rain sometime.
-
May 26th, 2015, 09:26 AM
#5
Re: How to fix score increments and randomizing pictureboxes .Top
The issue seems to be that you are checking against the left-most pipe each time, and there are 18 timer ticks after "passing" the pipe before it gets shifted to the far end of the set of pipes.
The approach I would use is to simply keep track of the "next" pipe separately. When you "pass" the pipe (with a simple bird.left > pipe.left comparison) you can increment the score and then find the pipe that follows and set your pipe variable to that pipe. Next timer tick then bird.left > pipe.left will return false because the pipe variable does not hold the leftmost already-passed pipe, but the next pipe that the bird has not yet passed.
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
|