Visual Studio 2003 - VB.net

This is a discussion on Visual Studio 2003 - VB.net within the Tech Board forums, part of the Community Boards category; Hi there, I would like to know if anyone here knows or perhaps remembers "loops" in Visual Basic. I want ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    8

    Question Visual Studio 2003 - VB.net

    Hi there,

    I would like to know if anyone here knows or perhaps remembers "loops" in Visual Basic.

    I want to loop some code which happens to be a 2D game.

    Once my bitmap asteroid hits the bottom of the form1, how can I get the game to restart from the beginning. I'm not using arrays, just a bunch of asteroids moving down the form to eventually trigger game over.

    So In gameover, I have the following. Once this asteroid hits the form, how do I "Loadsettings()" begin the program again.

    Part of my program code.

    Code:
    Dim rocketleft As Boolean
        Dim rocketright As Boolean
        Dim asteroidspeed As Integer
        Dim asteroiddrop As Integer
        Dim asteroiddown As Boolean
        Dim rocketspeed As Integer = 21
        Dim missilespeed As Integer = 22
    
        Private Sub Timermain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timermain.Tick
            moveasteroid()
            checkgameover()
            moverocketship()
            firemissile()
            checkhit()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            loadsettings()
        End Sub
    
        Private Sub loadsettings()
            asteroiddown = True
            asteroidspeed = 2
            asteroiddrop = 40
            checkgameover()
            rocketspeed = 21
            missilespeed = 22
            missile.Visible = False
            firemissile()
            checkhit()
        End Sub
    private sub gameover contains the following

    Code:
    If PictureBox1.Top + PictureBox1.Width >= Me.ClientRectangle.Bottom And PictureBox1.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
    Thank you for any assistance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Personally, I think you would be better off finding a forum with an actual speciality in VB.net for this particular question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    I agree with you there, this is my third forum. First was Vbforums, but I didn't get any help there. I only asked how to loop my code. You'd think someone there would explain an example code to learn.

    I learnt how to create my small game from a tutorial on the youtube site a few weeks back. But the loop was using the game's array code. I tried messing about with the code, without the arrays only for nothing to change.

    I just thought may be somebody here would know the method of doing this.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to tell somebody that the game is over, by setting the value of a flag or suchlike that your main loop can then notice and process appropriately. You have turned off Timermain, so if that's something you can check in main (the name sure sounds suggestive) then there you go.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    I haven't got a loop. Everything loads once, the program has to be closed and opened again for the code to set out the movements etc.

    Are you implying that i must instruct all the asteroids to move again from the beginning, thus enabling the timer to do so somehow?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pocket View Post
    I haven't got a loop.
    Then that's probably your problem. If you want something to happen more than once, you're going to have to put it in a loop.

    Quote Originally Posted by pocket View Post
    Are you implying that i must instruct all the asteroids to move again from the beginning, thus enabling the timer to do so somehow?
    I wish I knew how you got that out of what I wrote. I'm saying that when you have a game over situation, you kill "Timermain", so that is presumably the thing that notifies your main program that the game is over. Once your main notices that, it can then pop up a play again box (or whatever) and then loop the loop back to the beginning. (Note: I'm not at all familiar with VB.net, and so it might be that killing Timermain kills the program -- it looks like, based on your not knowing anything about a loop, that that timer tick loop is an automatic thing. If that is true, then killing Timermain is in fact a bad idea, and you'll have to set up your own flag to communicate to the main program that the game is over.)

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Oh, I posted here for an example on how to loop my code once an object hit the form1 bottom area.

    I know I don't have a loop.

    Yes, the video tutorial explained on looping for it using a msg box. But I don't want any of that. I want it without, sort of self explanatory, the player destroys the objects and the game ends and begins automatically again. The same for the missed objects that hit the form.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by pocket View Post
    Oh, I posted here for an example on how to loop my code once an object hit the form1 bottom area.

    I know I don't have a loop.

    Yes, the video tutorial explained on looping for it using a msg box. But I don't want any of that. I want it without, sort of self explanatory, the player destroys the objects and the game ends and begins automatically again. The same for the missed objects that hit the form.
    You'll have to wrap your main code in a loop -- not any of this stuff. This stuff is already in a loop controlled by Timermain.tick (which happens ... however often). You'll need to wrap the "setting up the initial game board" (which may all be done in LoadSettings) -- "setting up the main timer" (I don't know where you did that) stuff in a loop so that when you reach game over you can restart the game.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Posts
    8
    Code:
      Dim rocketleft As Boolean
        Dim rocketright As Boolean
        Dim asteroidspeed As Integer
        Dim asteroiddrop As Integer
        Dim asteroiddown As Boolean
        Dim rocketspeed As Integer = 21
        Dim missilespeed As Integer = 22
        Private Sub Timermain_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timermain.Tick
            moveasteroid()
            checkgameover()
            moverocketship()
            firemissile()
            checkhit()
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            loadsettings()
        End Sub
        Private Sub loadsettings()
            asteroiddown = True
            asteroidspeed = 2
            asteroiddrop = 40
            checkgameover()
            rocketspeed = 21
            missilespeed = 22
            missile.Visible = False
            firemissile()
            checkhit()
        End Sub
        Private Sub moveasteroid()
            'Asteroid 1
            If asteroiddown = True Then
                PictureBox1.Top += asteroidspeed
            Else
                PictureBox1.Left -= asteroidspeed
            End If
            If PictureBox1.Left + PictureBox1.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox1.Top += asteroiddrop
            End If
            'Asteroid 2
            If asteroiddown = True Then
                PictureBox2.Top += asteroidspeed
            Else
                PictureBox2.Left -= asteroidspeed
            End If
            If PictureBox2.Left + PictureBox2.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox2.Top += asteroiddrop
            End If
            'Asteroid 3
            If asteroiddown = True Then
                PictureBox3.Top += asteroidspeed
            Else
                PictureBox3.Left -= asteroidspeed
            End If
            If PictureBox3.Left + PictureBox3.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox3.Top += asteroiddrop
            End If
            'Asteroid 4
            If asteroiddown = True Then
                PictureBox4.Top += asteroidspeed
            Else
                PictureBox4.Left -= asteroidspeed
            End If
            If PictureBox4.Left + PictureBox4.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox4.Top += asteroiddrop
            End If
            'Asteroid 5
            If asteroiddown = True Then
                PictureBox5.Top += asteroidspeed
            Else
                PictureBox5.Left -= asteroidspeed
            End If
            If PictureBox5.Left + PictureBox5.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox5.Top += asteroiddrop
            End If
            'Asteroid 6
            If asteroiddown = True Then
                PictureBox6.Top += asteroidspeed
            Else
                PictureBox6.Left -= asteroidspeed
            End If
            If PictureBox6.Left + PictureBox6.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox6.Top += asteroiddrop
            End If
            'Asteroid 7
            If asteroiddown = True Then
                PictureBox7.Top += asteroidspeed
            Else
                PictureBox7.Left -= asteroidspeed
            End If
            If PictureBox7.Left + PictureBox7.Width > Me.ClientRectangle.Bottom Then
                asteroiddown = False
                PictureBox7.Top += asteroiddrop
            End If
            'Asteroid 8
            If asteroiddown = True Then
                PictureBox8.Top += asteroidspeed
            Else
                PictureBox8.Left -= asteroidspeed
            End If
        End Sub
        Private Sub firemissile()
            If missile.Visible = True Then
                missile.Top -= missilespeed
            End If
            If missile.Top + missile.Height < Me.ClientRectangle.Top Then
                missile.Visible = False
            End If
        End Sub
        Private Sub checkhit()
            If (missile.Top + missile.Height >= PictureBox1.Top) And (missile.Top <= PictureBox1.Top + PictureBox1.Height) And (missile.Left + missile.Width >= PictureBox1.Left) And (missile.Left <= PictureBox1.Left + PictureBox1.Width) And missile.Visible = True Then
                PictureBox1.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox2.Top) And (missile.Top <= PictureBox2.Top + PictureBox2.Height) And (missile.Left + missile.Width >= PictureBox2.Left) And (missile.Left <= PictureBox2.Left + PictureBox2.Width) And missile.Visible = True Then
                PictureBox2.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox3.Top) And (missile.Top <= PictureBox3.Top + PictureBox3.Height) And (missile.Left + missile.Width >= PictureBox3.Left) And (missile.Left <= PictureBox3.Left + PictureBox3.Width) And missile.Visible = True Then
                PictureBox3.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox4.Top) And (missile.Top <= PictureBox4.Top + PictureBox4.Height) And (missile.Left + missile.Width >= PictureBox4.Left) And (missile.Left <= PictureBox4.Left + PictureBox4.Width) And missile.Visible = True Then
                PictureBox4.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox5.Top) And (missile.Top <= PictureBox5.Top + PictureBox5.Height) And (missile.Left + missile.Width >= PictureBox5.Left) And (missile.Left <= PictureBox5.Left + PictureBox5.Width) And missile.Visible = True Then
                PictureBox5.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox6.Top) And (missile.Top <= PictureBox6.Top + PictureBox6.Height) And (missile.Left + missile.Width >= PictureBox6.Left) And (missile.Left <= PictureBox6.Left + PictureBox6.Width) And missile.Visible = True Then
                PictureBox6.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox7.Top) And (missile.Top <= PictureBox7.Top + PictureBox7.Height) And (missile.Left + missile.Width >= PictureBox7.Left) And (missile.Left <= PictureBox7.Left + PictureBox7.Width) And missile.Visible = True Then
                PictureBox7.Visible = False
                missile.Visible = False
            End If
            If (missile.Top + missile.Height >= PictureBox8.Top) And (missile.Top <= PictureBox8.Top + PictureBox8.Height) And (missile.Left + missile.Width >= PictureBox8.Left) And (missile.Left <= PictureBox8.Left + PictureBox8.Width) And missile.Visible = True Then
                PictureBox8.Visible = False
                missile.Visible = False
            End If
        End Sub
        Private Sub checkgameover()
            If PictureBox1.Top + PictureBox1.Width >= Me.ClientRectangle.Bottom And PictureBox1.Visible = True Then
                Timermain.Enabled = False
                playagain()
            End If
            If PictureBox2.Top + PictureBox2.Width >= Me.ClientRectangle.Bottom And PictureBox2.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox3.Top + PictureBox3.Width >= Me.ClientRectangle.Bottom And PictureBox3.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox4.Top + PictureBox4.Width >= Me.ClientRectangle.Bottom And PictureBox4.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox5.Top + PictureBox5.Width >= Me.ClientRectangle.Bottom And PictureBox5.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox6.Top + PictureBox6.Width >= Me.ClientRectangle.Bottom And PictureBox6.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox7.Top + PictureBox7.Width >= Me.ClientRectangle.Bottom And PictureBox7.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox8.Top + PictureBox8.Width >= Me.ClientRectangle.Bottom And PictureBox8.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space not protected!")
            End If
            If PictureBox8.Top + PictureBox8.Width >= Me.ClientRectangle.Bottom And PictureBox8.Visible = True Then
                Timermain.Enabled = False
                MsgBox("Game Over- Region of space protected!")
            End If
        End Sub
        Private Sub playagain()
            dim result = 
        End Sub
        Private Sub moverocketship()
            If rocketleft = True And rocketship.Left + rocketship.Width > Me.ClientRectangle.Left Then
                rocketship.Left -= rocketspeed
            End If
            If rocketright = True And rocketship.Left + rocketship.Width < Me.ClientRectangle.Width Then
                rocketship.Left += rocketspeed
            End If
        End Sub
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
            If (e.KeyCode = System.Windows.Forms.Keys.Left) Then
                rocketleft = True
                rocketright = False
            End If
            If (e.KeyCode = System.Windows.Forms.Keys.Right) Then
                rocketright = True
                rocketleft = False
            End If
            If (e.KeyCode = System.Windows.Forms.Keys.Down) And missile.Visible = False Then
                missile.Top = rocketship.Top
                missile.Left = rocketship.Left + (rocketship.Width / 2) - (missile.Width / 2)
                missile.Visible = True
            End If
            If (e.KeyCode = System.Windows.Forms.Keys.Up) Then
                Me.Close()
            End If
        End Sub
        Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
            If (e.KeyCode = System.Windows.Forms.Keys.Left) Then
                rocketleft = False
            End If
            If (e.KeyCode = System.Windows.Forms.Keys.Right) Then
                rocketright = False
            End If
        End Sub
    End Class
    Well this the code in total.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio 2003 Add New... problem
    By cboard_member in forum Tech Board
    Replies: 5
    Last Post: 06-19-2006, 03:30 AM
  2. Visual Studio .Net 2003
    By thedoofus in forum C Programming
    Replies: 4
    Last Post: 11-11-2005, 06:25 AM
  3. Arrays in Visual Studio.Net 2003
    By Jorger101 in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2005, 12:07 PM
  4. Visual Studio.Net 2003 and DataGrids
    By snowblind37 in forum Windows Programming
    Replies: 1
    Last Post: 07-01-2004, 09:37 AM
  5. Visual Studio 2003
    By dalek in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 04-27-2004, 03:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21