making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations
making an array in a loop change picture box locations making an array in a loop change picture box locations
making an array in a loop change picture box locations
Go Back  Xtreme Visual Basic Talk > > > making an array in a loop change picture box locations


Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2011, 03:14 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default making an array in a loop change picture box locations


Hi, I have made a For next loop with an array. I want all my picture boxes to follow each other within the loop. I have currently been using old code, but not in a loop or array format. The old code works. The array code only makes the 2nd picture box follow the FIRST one. It doesn't loop the rest. I have checked the code and cannot determine any mistakes, else I'm missing out on something I haven't been taught.

old code, working fine:

Code:
' snake body trail movement

        imgsnake52.Location = imgsnake51.Location
        imgsnake51.Location = imgsnake50.Location
        imgsnake50.Location = imgsnake49.Location


        imgsnake49.Location = imgsnake48.Location
        imgsnake48.Location = imgsnake47.Location
        imgsnake47.Location = imgsnake46.Location
        imgsnake46.Location = imgsnake45.Location
        imgsnake45.Location = imgsnake44.Location
        imgsnake44.Location = imgsnake43.Location
        imgsnake43.Location = imgsnake42.Location
        imgsnake42.Location = imgsnake41.Location
        imgsnake41.Location = imgsnake40.Location
        imgsnake40.Location = imgsnake39.Location


        imgsnake39.Location = imgsnake38.Location
        imgsnake38.Location = imgsnake37.Location
        imgsnake37.Location = imgsnake36.Location
        imgsnake36.Location = imgsnake35.Location
        imgsnake35.Location = imgsnake34.Location
        imgsnake34.Location = imgsnake33.Location
        imgsnake33.Location = imgsnake32.Location
        imgsnake32.Location = imgsnake31.Location
        imgsnake31.Location = imgsnake30.Location
        imgsnake30.Location = imgsnake29.Location


        imgsnake29.Location = imgsnake28.Location
        imgsnake28.Location = imgsnake27.Location
        imgsnake27.Location = imgsnake26.Location
        imgsnake26.Location = imgsnake25.Location
        imgsnake25.Location = imgsnake24.Location
        imgsnake24.Location = imgsnake23.Location
        imgsnake23.Location = imgsnake22.Location
        imgsnake22.Location = imgsnake21.Location
        imgsnake21.Location = imgsnake20.Location
        imgsnake20.Location = imgsnake19.Location




        imgsnake19.Location = imgsnake18.Location
        imgsnake18.Location = imgsnake17.Location
        imgsnake17.Location = imgsnake16.Location
        imgsnake16.Location = imgsnake15.Location
        imgsnake15.Location = imgsnake14.Location
        imgsnake14.Location = imgsnake13.Location
        imgsnake13.Location = imgsnake12.Location
        imgsnake12.Location = imgsnake11.Location
        imgsnake11.Location = imgsnake10.Location


        imgsnake10.Location = imgsnake9.Location
        imgsnake9.Location = imgsnake8.Location
        imgsnake8.Location = imgsnake7.Location
        imgsnake7.Location = imgsnake6.Location
        imgsnake6.Location = imgsnake5.Location
        imgsnake5.Location = imgsnake4.Location
        imgsnake4.Location = imgsnake3.Location
        imgsnake3.Location = imgsnake2.Location
        imgsnake2.Location = imgsnake.Location
        imgsnake.Location = New Point(imgsnake.Location.X + Player1_Speed_X, imgsnake.Location.Y + Player1_Speed_Y)

New code with a for next loop and an array.

Code:
 For Each bodyArrayItem In BodyArray ' only makes the 2nd snakeimg follow, DOES NOT loop the rest of the body

            BodyArray(1).Location = New Point(BodyArray(0).Location)

        Next bodyArrayItem


        imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y)
As I said it only loops once. I have tried making a counter and putting it in a while / end while loop until all picture boxes are following - but it's still the same result. only the 2nd picture box follows the first. that's it. thank you.
Reply With Quote
  #2  
Old 05-24-2011, 09:03 AM
AtmaWeapon's Avatar
AtmaWeaponmaking an array in a loop change picture box locations AtmaWeapon is offline
Fabulous Florist

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

Let's say you have 40 boxes and 40 quarters. I give you these instructions:
Quote:
For each quarter in your bag of quarters, place a quarter in box #1.
When you're done, 40 quarters are in box #1. I told you to repeat an identical action 40 times, so the result was 40 times the effect of doing that action once.

If instead I want you to put a quarter in a different box each time, I need to include instructions about how to decide which box to act on:
Quote:
Start counting at 1. For each quarter in the bag of quarters, drop the quarter in the box with the current number. After you drop the quarter, count to the next number.
This should put 1 quarter in each of the 40 boxes because every time you drop a quarter, the instructions for deciding which box to use make you pick another box.

Right now, your code says this:
Quote:
For each picture box in the BodyArray collection, set the 2nd box's location to the first box's location.
This is just like my first set of instructions: you are repeating the same action over and over. You need to change the instructions for how to figure out which box to use. For Each loops aren't so good at this, but For loops work fine:
Code:
For segmentIndex As Integer = 0 To BodyArray.Length - 1
    BodyArray(segmentIndex).Location = BodyArray(segmentIndex + 1).Location
Next
But beware! That code has an off-by-one error. If you have 40 boxes, the last index is 39. This loop will run with segmentIndex = 39, then try to get the location of box #40. This doesn't exist. It's easy to fix, just quit one item earlier:
Code:
For segmentIndex As Integer = 0 To BodyArray.Length - 2
There's still a problem. That will set box #39's location to box #40's location, but nothing sets box #40's location. You'll have to move that last box by hand since its location isn't really based on the other items in the array.
__________________
.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 05-26-2011, 12:16 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by AtmaWeapon View Post
Let's say you have 40 boxes and 40 quarters. I give you these instructions:

When you're done, 40 quarters are in box #1. I told you to repeat an identical action 40 times, so the result was 40 times the effect of doing that action once.

If instead I want you to put a quarter in a different box each time, I need to include instructions about how to decide which box to act on:

This should put 1 quarter in each of the 40 boxes because every time you drop a quarter, the instructions for deciding which box to use make you pick another box.

Right now, your code says this:

This is just like my first set of instructions: you are repeating the same action over and over. You need to change the instructions for how to figure out which box to use. For Each loops aren't so good at this, but For loops work fine:
Code:
For segmentIndex As Integer = 0 To BodyArray.Length - 1
    BodyArray(segmentIndex).Location = BodyArray(segmentIndex + 1).Location
Next
But beware! That code has an off-by-one error. If you have 40 boxes, the last index is 39. This loop will run with segmentIndex = 39, then try to get the location of box #40. This doesn't exist. It's easy to fix, just quit one item earlier:
Code:
For segmentIndex As Integer = 0 To BodyArray.Length - 2
There's still a problem. That will set box #39's location to box #40's location, but nothing sets box #40's location. You'll have to move that last box by hand since its location isn't really based on the other items in the array.
tried this method. It didn't work still the same thing happening. The second box follows the first. That's it.
Reply With Quote
  #4  
Old 05-26-2011, 05:18 AM
PlausiblyDamp's Avatar
PlausiblyDampmaking an array in a loop change picture box locations PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If you step through the code in a debugger does it seem to be doing what you expect (i.e. is it looping over all items in the array and assigning the location from the following item correctly)?

If not what is it doing that you don't expect. Have you used Atma's code exactly or has it been adapted to your application? If the latter could we see your code?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 05-26-2011, 08:10 AM
AtmaWeapon's Avatar
AtmaWeaponmaking an array in a loop change picture box locations AtmaWeapon is offline
Fabulous Florist

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

I can't tell you what's wrong if I can't see your code. Perhaps you didn't do things properly, perhaps I explained it wrong. Without code there is no answer.
__________________
.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
  #6  
Old 06-11-2011, 09:05 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Hi there, after do a little more fiddling, i've come to the same result just the 2nd picture box following the first picture box, that's it.

new code:

Code:
  ' snake body

        Dim NextItemCounter As Integer = 1 ' Next picturebox
        Dim FollowCounter As Integer = 0 ' current picturebox


        For SnakeIndex2 As Integer = 1 To 51 ' for all snake pictureboxes
            BodyArray(NextItemCounter).Location = BodyArray(FollowCounter).Location ' Next picturebox location = Current Picturebox location
            NextItemCounter += 1 ' increment Next picture box integer
            FollowCounter += 1 ' increment Follow counter
        Next SnakeIndex2 ' Next snakeIndex

        imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y)
Something has to be wrong, otherwise it would work.


EDIT

Code:
  ' snake body

        Dim NextItemCounter As Integer = 1 ' Next picturebox
        Dim FollowCounter As Integer = 0 ' current picturebox


        For SnakeIndex2 As Integer = 1 To 51 ' for all snake pictureboxes

            If imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y) Then
                BodyArray(NextItemCounter).Location = BodyArray(FollowCounter).Location ' Next picturebox location = Current Picturebox location ' logic of this line is telling me there is something wrong here.
            End If
           
        Next SnakeIndex2 ' Next snakeIndex 


        imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y)
This code makes each snake picture box appear, though they do not follow each other, they just stay in there default spot on the form.

Why is this so much more complex to do? but it works so easily using the individual code in my first post?

Last edited by 45minutes; 06-11-2011 at 09:28 PM.
Reply With Quote
  #7  
Old 06-12-2011, 03:01 AM
PlausiblyDamp's Avatar
PlausiblyDampmaking an array in a loop change picture box locations PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

It would seem you are never updating the NextItemCounter variable, this will therefore always be set to 1 - hence you are only modifying the first item in the array.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #8  
Old 06-12-2011, 04:27 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

tried it with updating counters, doesn't work!!!! making me extremely frustrated now.
Reply With Quote
  #9  
Old 06-12-2011, 04:33 AM
PlausiblyDamp's Avatar
PlausiblyDampmaking an array in a loop change picture box locations PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

How are you updating it though? What is it doing now? What is the purpose of the NextItemCounter and the FollowCounter variables?

In Atma's code he managed to do this with only a single loop counter segmentIndex, your code seems to have increased this to three SnakeIndex2 , NextItemCounter and FollowCounter - are all of these required? If so are you sure you are updating all three of them correctly and at the correct times?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 06-12-2011, 07:09 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Code:
Dim NextItemCounter As Integer = 1 ' Next picturebox
        Dim FollowCounter As Integer = 0 ' current picturebox


        For SnakeIndex2 As Integer = 1 To 51 ' for all snake pictureboxes

            If imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y) Then
                BodyArray(NextItemCounter).Location = BodyArray(FollowCounter).Location ' Next picturebox location = Current Picturebox location
                NextItemCounter += 1
                FollowCounter += 1
            End If
            
        Next SnakeIndex2 ' Next snakeIndex 

        imgsnake.Location = New Point(imgsnake.Location.X + Speed_X, imgsnake.Location.Y + Speed_y)
Reply With Quote
  #11  
Old 06-12-2011, 09:26 AM
AtmaWeapon's Avatar
AtmaWeaponmaking an array in a loop change picture box locations AtmaWeapon is offline
Fabulous Florist

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

Remember waaaaaay back when I talked about the difference between asking someone to do the same thing multiple times or changing what they do based on how many tasks have been done? I wasn't telling a happy little story for your amusement. It was meant to teach you a point.

This loop prints the number 1 50 times, because nothing in the loop changes the value of the number that will be printed:
Code:
Dim counter As Integer = 1
For i As Integer = 1 To 50
    Console.WriteLine(counter)
Next
Make one tiny change and the loop prints 1-50. Note how each loop interval I update the counter variable first thing; this is why you don't get 50 "1" prints:
Code:
Dim counter As Integer = 1
For i As Integer = 1 To 50
    counter = i
    Console.WriteLine(counter)
Next
When writing a loop, it helps to think of it in three pieces:
  • The loop itself has to know how many times to run and establish some variables that will be used by the loop.
  • Some of the code needs to change on every loop iteration; it should use the loop variables to calculate which things it should do.
  • Some of the code does the same work on each different item generated by the changing parts.
Your code fails to update imgsnake on each loop iteration. While you do update the appropriate array indices, the only picture box you ever move is this one picturebox.

Now that leads me to ask a question: what the heck is BodyArray? All this time I think I've been assuming it's an array of picture boxes. But it can't be an array of picture boxes because you're still fiddling around with imgSnake.

You should have an array of picture boxes. Each loop iteration, you should use the loop counter to get the picture box you want. When your loop code appears to execute only once, or multiple times for the same item, double-checking that you change the item each time is the "see if it's plugged in" of the programming world: no one checks it because they assume it's not the problem but it takes less than 5 seconds to verify so why not check the easy things first?

Also, please stop posting responses that say, "I tried it, doesn't work" without posting the code or going into excruciating detail about what's not working. You're asking us to figure out what's wrong with your code. That's hard when all we know is "it doesn't work". Imagine if someone were delivering you a new car and they called you to say, "Yeah, I'm at the address. You're not here. I'm taking the car back." But you're standing in your own house and he's not there! Which would you rather do:
  • Play 20 questions?
    "But... do you see a red brick house?"
    "Nope"
    "But... do you see my neighbor's house? It's white?"
    "Yeah, I see a white house but not a red brick one. Car's going back. Bye."
    ...
  • Have him tell you, "I'm at 824 Sycamore street" so you can reply, "Oh, that's the problem! I'm at 24 Sycamore street; you're a few blocks to the west!"
It's very frustrating to post an example that we have tested carefully and get a "doesn't work" response with no other information.
__________________
.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
  #12  
Old 06-12-2011, 08:28 PM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

BodyArray is an array of pictureboxes. I just prefer to use the first snake img as imgsnake, because I do not need to loop it in anyway. It's juts everything after the first snake img that needs looping. I will work on my code and will read your post again to fully understand it. will let you know how I go. Thank you.
Reply With Quote
  #13  
Old 06-14-2011, 06:10 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Okay well I had a play around with some new code. I figured that a do while .. loop will be best for this function. As I need 51 pictureboxes to continuously follow each other 1 after the other, until it's game over. However this new code I made just seems to freeze my whole program when P1movement.enabled = True

Code:
        Dim i As Integer = 1 ' starts at second picture box because 1st is already creating a new point

        Do While P1movement.Enabled = True ' when this timer is true

            If BodyArray(0).Location = New Point(BodyArray(0).Location.X + Speed_X, BodyArray(0).Location.Y + Speed_y) Then ' if head of snake = new point then...
                BodyArray(i + 1).Location = BodyArray(i).Location ' make bodyarray(0+1).location = bodyarray(0).location (this is on the first cycle)
            End If
            i += 1 ' increment 
        Loop
I need to somehow cap the (i) to not go over 50, otherwise index goes out of array bounds. A for loop will do this from 1 to 50, through it will only do it the whole cycle 1 to 50 times, then stops. I need a continuous cycle. I not sure if I get your logic, or maybe I didn't explain myself better the first time. this is the hardest function I'm finding to do.


EDIT: Okay, after re-reading your initial 40 coins statement, I do not understand how to do this in code. Like I think what I'm doing now is logically right. I made another FOR .. Next loop, but still only the 2nd img follows the head. I'm losing my patience with this, I'll probably just end up using my old code.

Code:
  Sub SnakeBodyLocation()

        ' snake body images following each other

        For Index As Integer = 1 To 51 ' I want 51 picture picutres to follow the first

            BodyArray(Index).Location = BodyArray(Index - 1).Location ' example first cycle: Bodyarray(1).location = bodyarray(0).location

        Next Index ' increment index so it goes on to the next picturebox

        BodyArray(0).Location = New Point(BodyArray(0).Location.X + Speed_X, BodyArray(0).Location.Y + Speed_y) ' head of snake is always going to a new point

    End Sub

Last edited by 45minutes; 06-14-2011 at 06:46 AM.
Reply With Quote
  #14  
Old 06-14-2011, 06:17 AM
DrPunk's Avatar
DrPunkmaking an array in a loop change picture box locations DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

It's probably freezing your program because the program is tied up running your loop (the interface runs on the same thread, so it can't do anything while it's running your loop).

An application.doevents inside the loop will give the form a chance to update the user interface.

For your cycle of i, all you need to do is check the value of i and if it's 50 then you set i back to 1, otherwise you increment i.
__________________
There are no computers in heaven!
Reply With Quote
  #15  
Old 06-14-2011, 06:48 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Default

Quote:
Originally Posted by DrPunk View Post
It's probably freezing your program because the program is tied up running your loop (the interface runs on the same thread, so it can't do anything while it's running your loop).

An application.doevents inside the loop will give the form a chance to update the user interface.

For your cycle of i, all you need to do is check the value of i and if it's 50 then you set i back to 1, otherwise you increment i.
Yes I think you're right. I took all code out of that while loop and it still froze my program. So I'm not going to use it. This For next loop though, I do not understand what I'm not doing right. This is more work than I thought.

Tried doing what you said, set it back to 1, but the program just freezes and crashes.


Code:
 Sub SnakeBodyLocation()

        ' snake body images following each other


        For Index As Integer = 1 To 51 ' I want 51 picture picutres to follow the first

            BodyArray(Index).Location = BodyArray(Index - 1).Location ' example first cycle: Bodyarray(1).location = bodyarray(0).location
            If Index = 51 Then ' this freezes my program
                Index = 1 ' this freezes my program
            End If ' this freezes my program
        Next Index ' increment index so it goes on to the next picturebox

        BodyArray(0).Location = New Point(BodyArray(0).Location.X + Speed_X, BodyArray(0).Location.Y + Speed_y) ' head of snake is always going to a new point

    End Sub

Last edited by 45minutes; 06-14-2011 at 06:54 AM.
Reply With Quote
  #16  
Old 06-14-2011, 06:57 AM
DrPunk's Avatar
DrPunkmaking an array in a loop change picture box locations DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Stick with what you've got. I wanted to give you pointers rather than code, but...

Code:
Dim i As Integer = 1 ' starts at second picture box because 1st is already creating a new point

        Do While P1movement.Enabled = True ' when this timer is true

            If BodyArray(0).Location = New Point(BodyArray(0).Location.X + Speed_X, BodyArray(0).Location.Y + Speed_y) Then ' if head of snake = new point then...
                BodyArray(i + 1).Location = BodyArray(i).Location ' make bodyarray(0+1).location = bodyarray(0).location (this is on the first cycle)
            End If

            ' Increment i like a never ending for loop
            if i = 50 Then
                i = 1 ' set i back to 1
            else
                i += 1 ' increment 
            endif

            ' Give the user interface a chance to respond to any events, i.e. fix the freezing
            application.doevents
        Loop
The blue text is the only part that's changed.
__________________
There are no computers in heaven!
Reply With Quote
  #17  
Old 06-14-2011, 08:43 AM
AtmaWeapon's Avatar
AtmaWeaponmaking an array in a loop change picture box locations AtmaWeapon is offline
Fabulous Florist

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

An infinite loop with DoEvents() is not the appropriate way to get something to happen continuously in VB .NET. It works fine as a stopgap, but I think there's more natural ways.

Windows Forms is event-driven. It's easier to understand what that means if we start with code that's not event-driven; I'll call this "traditional" code. In a traditional game, everything revolves around the "game loop". Using C as the example to make it clear it's different:
Code:
int main()
{
    while(gameIsRunning)
    {
        GetKeyboardInput();
        UpdateGameState();
        Render();
    }
}
The game loop runs forever. It checks input, updates the data structures that represent the game, then renders. This works because while the loop is infinite it's monitoring input so the program remains responsive. It turns out Windows applications in C have their own version of a game loop. Simplified it looks like this:
Code:
int WinMain(...)
{
    // a ton of code here to set up a form and display it
    // literally it can be 50 lines

    while(GetMessage(&msg, ...) != WM_QUIT)
    {
        DispatchMessage(&msg)
    }
}
This is key to understanding how event-driven programming works. When you do something with the keyboard, Windows generates a "message" that it inserts into a "message queue" for your program. GetMessage() pulls a message out of the queue. DispatchMessage() figures out which control the message was intended for and sends the message to that control. Messages aren't just in response to keyboard input; if a window needs to re-render itself it invalidates itself; this generates a WM_PAINT message that causes the form to repaint itself. Timer ticks generate WM_TIMER messages that cause the appropriate handlers to get called. If there's no messages in the queue, your program patiently waits for one to be generated. This continues until a "quit" message is found.

This happens in .NET too, but this loop (called the "message loop") is buried deep in the innards of your application; it's automatically generated so you don't have to worry about it. Suppose you have a button with a click handler. When you click it, a few messages like WM_MOUSEUP are generated and sent to the button. The button responds by adding a WM_COMMAND message to the queue. When you set up an event handler on your button, .NET automatically sets things up so its message loop's response co that WM_COMMAND is to call your event handler.

So why am I rambling about this? Since you're program already has a message loop that functions like a game loop, it's not necessary to create *another* loop. Without the DoEvents() call, you never let the message loop get control again and that's why your form freezes. DoEvents() lets the message loop run but breaks the abstractions that .NET puts in place.

Instead of a game loop, consider this pattern that exploits the message loop. Suppose there's a timer called gameTimer with a fast interval like 500ms; 2 updates a second is a leisurely snake strolling through the park.
Code:
Sub gameTimer_Tick(...) Handles gameTimer.Tick
    MoveSnake()
    UpdatePictureBoxes()
End Sub

Sub Handle_KeyPress(...) Handles Me.KeyPress
    ' When the user presses an arrow key, update the data structures
    ' that represent the snake
End Sub

Sub MoveSnake()
   ' Move the head and tail of the snake 
End Sub

Sub UpdatePictureBoxes()
    ' Move the pictureboxes to the locations of the snake segments
End Sub
This actually works just like the original game loop! The keypress handler is equivalent to GetKeyboardInput(): it probably just updates a variable that indicates the snake's direction. MoveSnake() is similar to UpdateGameState(): it updates the data structures that represent the snake to indicate the snake has moved. UpdatePictureBoxes() is equivalent to Render(): it manipulates the controls that make up your display. To me, this pattern is much easier to follow and understand in a WinForms environment than a loop that requires DoEvents(). When DoEvents() is on the table, it's much harder to comprehend where you are when an error occurs.
__________________
.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
  #18  
Old 06-15-2011, 02:42 AM
45minutes 45minutes is offline
Regular
 
Join Date: Jan 2011
Posts: 75
Smile

Hi After some logical thinking, I took a few steps back to see where my problem lied. soon after I realised that I wasn't making a reference for where the current index (i) was meant to be going. I quickly coded a solution, tested it and guess what .... IT WORKS so happy, so satisfying.

code:

Code:
    Sub SnakeBodyLocation() ' controls movement of each individual snake piece

        For i As Integer = 1 To 50 ' Index within array bounds for quick access

            BodyArray(i + 1).Location = BodyArray(i).Location ' make bodyarray(1+1).location = bodyarray(1).location (this is on the first cycle) . Therefore this line makes the next piece in line follow the piece in front.
            BodyArray(i).Location = BodyArray(i - 1).Location ' Bodyarray(1).location = bodyarray(1-1) - this is on first cycle. therefore this line initally follows BodyArray(0).location.

            If i = 50 Then ' Increment i like a never ending for loop
                i = 1 ' set i back to 1
            Else
                i += 1 ' increment 
            End If
        Next

        BodyArray(51).Location = BodyArray(50).Location ' last piece of snake is manually coded to follow the 2nd last snake piece as it cannot be included in the loop for array boundary reasons.
        BodyArray(0).Location = New Point(BodyArray(0).Location.X + Speed_X, BodyArray(0).Location.Y + Speed_y) ' head of snake is always going to a new point.

    End Sub
To all that helped, I thank you very much. Your lives will be rewarded.
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 an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations making an array in a loop change picture box locations
making an array in a loop change picture box locations
making an array in a loop change picture box locations
 
making an array in a loop change picture box locations
making an array in a loop change picture box locations
 
-->