Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Game Programming > High Low Game


Reply
 
Thread Tools Display Modes
  #1  
Old 06-10-2012, 12:24 AM
jlslickt78 jlslickt78 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 5
Default High Low Game


So I pretty much built the game, that was the easy part. I am having trouble entering a name on my first form(startup) and keeping track of the score on second form(game) and displaying it both combined on the first page with the current users score. Also the way it set up now if you are running the game for the first time it says can't find LowScores.Text.

Start Up
Code:
Public Class StartUp


    Private Sub txtName_keydown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.Visible = False
            Game.Visible = True
        End If
    End Sub
    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnPlay_Click(sender As Object, e As System.EventArgs) Handles btnPlay.Click
        Me.Visible = False
        Game.Visible = True

        ' writes a name to a sequential access file

        ' declare a StreamWriter variable
        Dim outFile As IO.StreamWriter

        ' open the file for append
        outFile = IO.File.AppendText("LowScores.txt")

        ' write the name on a separate line in the file
        outFile.WriteLine(txtName.Text)

        ' close the file
        outFile.Close()
        ' clear the Name box, then set the focus
        txtName.Text = String.Empty
        txtName.Focus()
    End Sub

    Private Sub StartUp_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        ' reads names from a sequential access file 
        ' and displays them in the interface

        ' declare variables
        Dim inFile As IO.StreamReader
        Dim strName As String

        ' clear previous names from the Contestants box
        lblLowScores.Text = String.Empty

        ' determine whether the file exists
        If IO.File.Exists("LowScores.txt") = True Then
            'open the file for input
            inFile = IO.File.OpenText("LowScores.txt")
            'process the loopinstructions until the end of the file
            Do Until inFile.Peek = -1
                'read a name
                strName = inFile.ReadLine
                'display the name
                lblLowScores.Text = lblLowScores.Text &
                    strName & ControlChars.NewLine
            Loop
            'close the file
            inFile.Close()
        Else
            MessageBox.Show("Can't find the Low Scores.txt file",
                            "Show lowest scores",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information)
        End If
    End Sub
End Class
Game
Code:
'High Low - Game


Imports System.Text.RegularExpressions

Public Class Game

    Public Num As New Random
    Public int As Integer
    Public intCount As Integer
    Private Sub main()
        If txtNum.Text <> Nothing Then
            If Convert.ToInt32(txtNum.Text) = int Then
                Me.Size = New Size(350, 300)
                lblInstructions.Hide()
                If MessageBox.Show("Good job.  you got it in " & intCount & " tries.  Would you like to play again??", "you won!!", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = DialogResult.Yes Then
                    Me.Size = New Size(350, 300)
                    lblInstructions.Show()
                    txtNum.Text = Nothing
                    intCount = 1
                    int = Num.Next(1, 10)
                ElseIf DialogResult.No Then
                    StartUp.Visible = True
                    Me.Close()
                    Me.Size = New Size(350, 300)
                    lblInstructions.Show()
                End If
            txtNum.Text = Nothing
                intCount = 1
        ElseIf Convert.ToInt32(txtNum.Text) < int Then
            MessageBox.Show("Higher")
            txtNum.Text = Nothing
                intCount += 1
        ElseIf Convert.ToInt32(txtNum.Text) > int Then
            MessageBox.Show("Lower")
                txtNum.Text = Nothing
                intCount += 1
        End If
        Else
            MessageBox.Show("enter some text")
        End If

    End Sub

    Private Sub Game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        intCount = 1
        int = Num.Next(1, 10)
    End Sub

    Private Sub txtbox1_keydown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNum.KeyDown
        If e.KeyCode = Keys.Enter Then
            main()
        End If
    End Sub

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartOver.Click
        txtNum.Text = Nothing
        intCount = 1
        int = Num.Next(1, 10)
    End Sub

    Private Sub txtBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtNum.TextChanged
        txtNum.Text = Regex.Replace(txtNum.Text, "([aA-zZ]|\s|\e|\(|\)|\!|\@|\#|\$|\%|\^|\~|\`|\,|\:|\;|\&|\<|\>|\?|\'|\"")", "")
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        StartUp.Close()
        Me.Close()
    End Sub
End Class

Last edited by passel; 06-10-2012 at 11:06 PM.
Reply With Quote
  #2  
Old 06-10-2012, 11:11 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
Default

Certainly looks like VB.Net.
Not sure why you would scroll down and post in the Legacy VB (VB6 (1998) and earlier) General forum.
I've moved the post to .Net Game Programming forum.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 06-11-2012, 09:32 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

Good background information is in Bucky's Passing variables and controls using WinForms and VB .NET. The short story: if you want to send information from one form to another, you're going to have to do so via properties or method parameters.

I've attached a project that demonstrates the "get a name from one form to the other" aspect of your question. When you put the button on StartForm, it gathers all of its information into a PlayerInformation type. Before showing the StartForm, it puts that PlayerInformation into the form's property. When the StartForm loads, it uses the information from that property to populate its own controls.

There's a million ways to do this, but as long as you understand the basic "information is sent via property sets and method parameters" point.
Attached Files
File Type: zip statedemo.zip (14.3 KB, 13 views)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 06-11-2012, 09:30 PM
jlslickt78 jlslickt78 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 5
Default

Thanks for your help. I will try it out and let you know how it goes.
Reply With Quote
  #5  
Old 06-12-2012, 08:29 AM
jlslickt78 jlslickt78 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 5
Default

I figured it out with your help and the info. in Bucky's passing variables. It seems to work for the most part, it did create a few kinks that have to be worked out.
Reply With Quote
  #6  
Old 06-12-2012, 01:28 PM
jlslickt78 jlslickt78 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 5
Default

So for some reason the score always returns 0 instead of the number of tries. The username works fine. But, I am having a hard time getting the intCount to display as thin my label.
Reply With Quote
  #7  
Old 06-12-2012, 03:13 PM
jlslickt78 jlslickt78 is offline
Newcomer
 
Join Date: Jun 2012
Posts: 5
Default

sorry, never-mind I figured it out. Thanks for the help though.
Reply With Quote
Reply

Tags
high low, scores, vb 2010, visual basic


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->