Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left
Making a picture box move right then left Making a picture box move right then left
Making a picture box move right then left
Go Back  Xtreme Visual Basic Talk > > > Making a picture box move right then left


Reply
 
Thread Tools Display Modes
  #1  
Old 01-20-2011, 09:43 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Question Making a picture box move right then left


I want a picbox to move across a screen then back to where it started (for a space invader game:


the following code is what I have for this task.

Code:
    Private Sub AlienMovement_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlienMovement.Tick

        If Alien1.Location.X > 20 Then
            Alien1.Left += 10 ' makes alien go right
        End If


        If Alien1.Location.X < 870 Then     ' <<<<this part I can't get right
            Alien1.Left -= 10 ' makes alien go left
        End If

    End Sub
I can't make it < because thats not logical, can't make it = becasue that doesn't work, can't make it > becasue then it will only bounce back once. Is there another variable I could use?
Reply With Quote
  #2  
Old 01-21-2011, 08:52 AM
AtmaWeapon's Avatar
AtmaWeaponMaking a picture box move right then left AtmaWeapon is offline
Fabulous Florist

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

The problem you're having suggests that the logic you have chosen to represent the problem is incorrect. Your logic in pseudocode looks like this:
Code:
If the picture box is to the right of the left edge:
    Move it to the right

If the picture is to the left of the right edge:
    Move it to the left
Think about those rules for a minute. Imagine trying to move a piece on a game board following those rules. You would always be oscillating the piece both left and right, because it's always to the right of the left edge and always to the left of the right edge. It doesn't get better if you change the second rule:
Code:
If the picture is to the right of the right edge:
    Move it to the left
End If
This would let the picture move left until it passes the right edge, then you'd oscillate at the right edge as you continuously pass it then are placed back to the left.

When neither <, >, or = will work in a situation you know you're doing it the wrong way and it's time to try something different. Here's one way to describe moving an object back and forth:
Code:
Store the direction of travel in some variable.
Move the object.
If the object is to the right of the right edge:
    Change the direction of the object to "left".
If the object is to the left of the left edge:
    Change the direction of the object to "right".
See if you don't have better luck with that implementation.
__________________
.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
  #3  
Old 01-21-2011, 04:13 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
The problem you're having suggests that the logic you have chosen to represent the problem is incorrect. Your logic in pseudocode looks like this:
Code:
If the picture box is to the right of the left edge:
    Move it to the right

If the picture is to the left of the right edge:
    Move it to the left
Think about those rules for a minute. Imagine trying to move a piece on a game board following those rules. You would always be oscillating the piece both left and right, because it's always to the right of the left edge and always to the left of the right edge. It doesn't get better if you change the second rule:
Code:
If the picture is to the right of the right edge:
    Move it to the left
End If
This would let the picture move left until it passes the right edge, then you'd oscillate at the right edge as you continuously pass it then are placed back to the left.

When neither <, >, or = will work in a situation you know you're doing it the wrong way and it's time to try something different. Here's one way to describe moving an object back and forth:
Code:
Store the direction of travel in some variable.
Move the object.
If the object is to the right of the right edge:
    Change the direction of the object to "left".
If the object is to the left of the left edge:
    Change the direction of the object to "right".
See if you don't have better luck with that implementation.
When you say right edge, do you mean me using If.alien1.bounds.intersectswith(rightwall) then
alien1.left -= 10
end if

cause I have tried that and it stops "as it should" It gets stuck on the wall, because it has intersected with it and cannot move out.

Is there some simpler way to do this. how did the original game code their alien movement. Because I'm going to have to add 30 or so more aliens, the task may be tedious coding each one.

also the gun fire, I cannot seem to get right. (it fires once, but I don't have the logic to reset it back to the standard position to be fired again.

I will have a go at what you said, see if it helps, if not i'll just upload my program.
Reply With Quote
  #4  
Old 01-21-2011, 05:05 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

I followed your advice, made a separate timer to change direction, no luck. I made it worse I think, now the alien doesn't even move
Attached Files
File Type: zip SpaceInvadeX.zip (68.2 KB, 37 views)
Reply With Quote
  #5  
Old 04-25-2011, 07:02 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

I have never created a game before but steping through your code (F8) it appears that your spaceship (Player 1) is just looping until you move left or right then it does what it is meant to do and returns back to the spaceship loop. (presumably through the use of .Tick which I have never used before)

Unless you run your Alien on a seperate thread or have some way of interupting the spaceship loop to move the alien it is never going to move, probably the same for the bullets.
Reply With Quote
  #6  
Old 04-26-2011, 10:45 PM
CodeCruncher CodeCruncher is offline
Junior Contributor
 
Join Date: Jul 2006
Posts: 355
Default

I had a bit of a play with your code and added a few functions
Now drops down at the end of each line
Now speeds up each line it drops
Now lands so you can be invaded...

Hope this helps.

Code:
Public Class Form1

    Dim AlienDir As String = "Go2Right"
    Dim AlienSpeed As Int16 = 10
    Dim Lefty As Boolean

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

        AlienMove.Enabled = True 'Get that Alien moving

    End Sub

    Private Sub AlienMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlienMove.Tick

        If AlienDir = "Go2Right" Then
            Alien1.Left += AlienSpeed  'Alien goes right
        Else
            Alien1.Left -= AlienSpeed  'Alien goes left
        End If
        If Alien1.Location.X < 20 Or Alien1.Location.X > 850 Then
            If Alien1.Location.X < 20 Then 'The left boundry stop point for Alien.
                AlienDir = "Go2Right"
            Else 'The right boundry stop point for Alien.
                AlienDir = "" 'Alien direction no longer Go2Right, so Alien now goes left.
            End If
            If Alien1.Location.Y < 370 Then 'Sets the ground point where you are considered invaded...
                Alien1.SetBounds(Alien1.Location.X, Alien1.Location.Y + 20, 20, 20) 'Drop alien by its own height "20"
                AlienSpeed += 2 'Increase the speed of the invasion
            Else
                Alien1.SetBounds(Alien1.Location.X, Alien1.Location.Y + 40, 20, 20) 'Move one last time onto terra firma
                AlienMove.Stop() 'On the ground so don't move the Alien any more and exit the timer
                SpaceShipMove.Stop()
                Shoot.Stop()
                MsgBox("Dude you lost and have been seriously invaded!") 'Rub it in :D
            End If
        End If

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

            If e.KeyCode = Keys.Left And Spaceship.Location.X > 20 Then 'Sets the limit of the left boundry for spaceship
                Lefty = True
                SpaceShipMove.Enabled = True
            End If
            If e.KeyCode = Keys.Right And Spaceship.Location.X < 850 Then 'Sets the limit of the right boundry for spaceship
                Lefty = False
                SpaceShipMove.Enabled = True
            End If
            If e.KeyCode = Keys.Space Then
                Shoot.Enabled = True
            End If

    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp

        If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then SpaceShipMove.Enabled = False

    End Sub

    Private Sub SpaceShipMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SpaceShipMove.Tick

        If Lefty = True Then
            Spaceship.Location = New Point(Spaceship.Location.X - 10, 426)
        Else
            Spaceship.Location = New Point(Spaceship.Location.X + 10, 426)
        End If
        If Shoot.Enabled = False Then
            Bullet.Location = New Point(Spaceship.Location.X + 14, 404)
        End If

    End Sub

    Private Sub Shoot_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Shoot.Tick

        If Bullet.Location.Y > 0 Then
            Bullet.Location = New Point(Bullet.Location.X, Bullet.Location.Y - 20)
        Else
            Bullet.Location = New Point(Spaceship.Location.X + 14, 404)
            Shoot.Enabled = False
        End If

    End Sub

End Class
Reply With Quote
Reply


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
Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left Making a picture box move right then left
Making a picture box move right then left
Making a picture box move right then left
 
Making a picture box move right then left
Making a picture box move right then left
 
-->