Question Review Required

james_h

Member
Joined
Aug 21, 2012
Messages
15
Programming Experience
Beginner
Hi Guys,

I am brand new to programming and I have been teaching myself VB 2010 for 2 weeks and would appreciate someone that knows what they are doing to have a look to see what they think of my efforts on this little project. It all seems to work with no problems, so I guess that is a good start. I am guessing there may be more efficient ways to achieve the end results maybe?

Not sure if this is the best way to go about this?

Here is the code for the project.

Public Class Form1


    Dim wins As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        RadioButton2.Checked = True
        ComboBox1.Text = 1
        Label2.Text = 0


    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick


        Dim rnd As New Random


        PictureBox1.Location = New Point(rnd.Next(1, 250), rnd.Next(60, 250))


    End Sub




    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged


        If RadioButton1.Checked = True Then
            Label4.Visible = False
            If wins >= 10 Then
                PictureBox1.BackgroundImage = Image.FromFile("C:\Users\james\Documents\Visual Studio 2010\Icons\Bird-red.png")
            End If
            Timer1.Start()
        End If


    End Sub


    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged


        If RadioButton2.Checked = True Then
            Timer1.Stop()
            PictureBox1.Location = New Point(165, 250)
        End If


    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged


        If ComboBox1.Text = 1 Then
            Timer1.Interval = 1000
        ElseIf ComboBox1.Text = 2 Then
            Timer1.Interval = 750
        ElseIf ComboBox1.Text = 3 Then
            Timer1.Interval = 650
        ElseIf ComboBox1.Text = 4 Then
            Timer1.Interval = 500
        ElseIf ComboBox1.Text = 5 Then
            Timer1.Interval = 350
        End If


    End Sub


    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click


        Label2.Text = CInt(wins)


        If RadioButton1.Checked = True And Timer1.Interval = 1000 Then
            RadioButton2.Checked = True
            MessageBox.Show("Congrats!! You have won 1 coin!!")
            wins += 1
            Label2.Text = wins
        ElseIf RadioButton1.Checked = True And Timer1.Interval = 750 Then
            RadioButton2.Checked = True
            MessageBox.Show("Congrats!! You have won 2 coins!!")
            wins += 2
            Label2.Text = wins
        ElseIf RadioButton1.Checked = True And Timer1.Interval = 650 Then
            RadioButton2.Checked = True
            MessageBox.Show("Congrats!! You have won 3 coins!!")
            wins += 3
            Label2.Text = wins
        ElseIf RadioButton1.Checked = True And Timer1.Interval = 500 Then
            RadioButton2.Checked = True
            MessageBox.Show("Congrats!! You have won 4 coins!!")
            wins += 4
            Label2.Text = wins
        ElseIf RadioButton1.Checked = True And Timer1.Interval = 350 Then
            RadioButton2.Checked = True
            MessageBox.Show("Congrats!! You have won 5 coins!!")
            wins += 5
            Label2.Text = wins
        Else
            MessageBox.Show("The Game isn't running! Start Game to Win!!!")
        End If


    End Sub
End Class

And a screen shot of the actual game

GUI.jpg

Thanks for having a look guys.
 
Last edited by a moderator:
Tell me if I understand this correctly?
The program checks if wins is >=10 and if Ten = False and if this is the case
then it changes Ten to True and displays the message, but if when it checks Ten is True
then it just skips the message and exits the if statement hence only ever displaying the
message one time?

Well actually, as mentioned above, it checks to see if wins is 10 or more and if so also checks the Ten flag. By setting the Ten flag to True the first time that the condition is fulfilled you ensure that it cannot ever be fulfilled again.

This ....
[FONT=Verdana]Dim rnd As New Random[/FONT]


... goes ....

[FONT=Verdana]Public Class Form1[/FONT]



'here
[FONT=Verdana]Dim wins As Integer[/FONT]
[FONT=Verdana]Dim Ten As Boolean = False[/FONT]
[FONT=Verdana]Dim Twenty As Boolean = False[/FONT]
 
Last edited:
Well actually, as mentioned above, it checks to see if wins is 10 or more and if so also checks the Ten flag. By setting the Ten flag to True the first time that the condition is fulfilled you ensure that it cannot ever be fulfilled again.

This ....
Dim rnd As New Random


... goes ....

Public Class Form1



'here
Private rnd As New Random
Dim wins As Integer
Dim Ten As Boolean = False
Dim Twenty As Boolean = False

Oh OK that was much simpler then I was making it in my mind!

Why does that variable have a prefix of Private rather then Dim??? Both Dim & Private seem to work as a prefix!

So the advantage of having that as a public variable over a private variable is?

So that the variable does not need to be re-created each time that process is ran, instead it just has the data held in it changed each time. Is this correct?

Thanks in advance :)
 
Last edited:
Back
Top