Sign in to follow this  
Followers 0
quick_sliver007

How do I keep arrays from fall out of a loop

12 posts in this topic

here is the code.

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------


#include <GuiConstants.au3>
#include <Array.au3>

Dim $label[10]
$gui = GUICreate("MyGUI", @DesktopWidth, @DesktopHeight - 10)
$label[0] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
;GUICtrlSetBkColor($label[0], 0)
$label[1] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
;GUICtrlSetBkColor($label[1], 0)
$label[2] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
;GUICtrlSetBkColor($label[2], 0)
$label[3] = GUICtrlCreateLabel("3", 200, 230, 10, 10)
;GUICtrlSetBkColor($label[3], 0)
$label[4] = GUICtrlCreateLabel("4", 200, 240, 10, 10)
;GUICtrlSetBkColor($label[4], 0)
$label[5] = GUICtrlCreateLabel("5", 200, 250, 10, 10)
;GUICtrlSetBkColor($label[5], 0)
$label[6] = GUICtrlCreateLabel("6", 200, 260, 10, 10)
;GUICtrlSetBkColor($label[6], 0)
$label[7] = GUICtrlCreateLabel("7", 200, 270, 10, 10)
;GUICtrlSetBkColor($label[7], 0)
$label[8] = GUICtrlCreateLabel("8", 200, 280, 10, 10)
;GUICtrlSetBkColor($label[8], 0)
$label[9] = GUICtrlCreateLabel("9", 200, 290, 10, 10)
;GUICtrlSetBkColor($label[9], 0)



GUISetState()

HotKeySet("{up}", "up")
HotKeySet("{down}", "down")
HotKeySet("{left}", "left")
HotKeySet("{right}", "right")
While 1
    Global $msg = GUIGetMsg()
    Select
        Case $msg = $gui_event_close
            ExitLoop
    EndSelect
WEnd
Exit
Func up()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] - 10)
        _ArrayRotate($label,0,1)
        Sleep(5)
    WEnd
EndFunc ;==>up
Func left()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        
            GUICtrlSetPos($label[ (UBound($label) - 1) ], $pos[0]-10, $pos[1] )
            _ArrayRotate($label,0,1)
    Sleep(5)
    WEnd
    
EndFunc ;==>left
Func right()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0] + 10, $pos[1])
        _ArrayRotate($label,0,1)
    Sleep(5)
    WEnd
EndFunc ;==>right
Func down()
    
    While 1
        Dim $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] + 10)
        
        _ArrayRotate($label,0,1)
        Sleep(5)
    WEnd
EndFunc ;==>down
#CS
_ArrayRotate( $aArray [, $iDir [, $iSteps ] ] )
By Rob Saunders
Params:
     $aArray   The Array to rotate.
     $iDir   The direction to rotate.
               True (not 0) steps forward.
               False (0) steps backward.
     $iSteps   How many steps forward/backward
               to rotate the items.
#CE

Func _ArrayRotate(ByRef $aArray, $iDir = 0, $iSteps = 1)
    Local $iLen = UBound($aArray) - 1
    Local $vStore
    Local $iSC; step counter
    Local $iAC; array counter
    For $iSC = 1 To $iSteps
        If $iDir Then
            $sStore = $aArray[0]
            For $iAC = 0 To $iLen
                If $iAC < $iLen Then
                    $aArray[$iAC] = $aArray[$iAC + 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        Else
            $sStore = $aArray[$iLen]
            For $iAC = $iLen To 0 Step -1
                If $iAC > 0 Then
                    $aArray[$iAC] = $aArray[$iAC - 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        EndIf
    Next
EndFunc
If you run this script, use your arrow keys to move the labels and try to keep it on the gui(I have not done collision dection yet). It is fast with Sleep(5). If you would like you can you can change the sleep to 1000 instead to get a feel of whats going on. Well back to the bug. When the labels are moving this fast, some get left behide. If you play with it at the high speed you see will what I mean. It just drops a label on the gui. It works fine at slow speeds. Is this a bug? Is there some way to fix this. Also while I am at it, how do I get the exit button to work while it in the loop. Do I need to do select case with $msg in the funtions.


.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I take it this does not work with the beta as I get anerror when I press the down key.

Edit: I was able to duplicate the problem by pressing left and right fast.

Edit 2: After doing this over and over again, left/right that is, I got a fatal recursion error warning for the _ArrayRotate() function.

Edited by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

I take it this does not work with the beta as I get anerror when I press the down key.

Edit: I was able to duplicate the problem by pressing left and right fast.

Edit 2: After doing this over and over again, left/right that is, I got a fatal recursion error warning for the _ArrayRotate() function.

<{POST_SNAPBACK}>

I got some recursion errors too like if I hold down the key. Then the labels move very fast the it stops(I get and error) to avoid a stack dumb. I am going to use a last push tracker and disable the arrow in the last direction. The other problem is very real though, all though it take about 5-10 seconds of it moving very fast, with out holding down the key. The problem being that it leaves label(s) behide, like an element of the array falls out. Is this something to do with the speed of Autoit? If so, is there any thing that can be done about it or is this just something I will have to work around. Like setting a speed limit of the labels to a point of where this will not happen.

.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I got some recursion errors too like if I hold down the key. Then the labels move very fast the it stops(I get and error) to avoid a stack dumb. I am going to use a last push tracker and disable the arrow in  the last direction. The other problem is  very real though, all though it take about 5-10 seconds of it moving very fast, with out holding down the key. The problem being that it leaves label(s) behide, like an element of the array falls out. Is this something to do with the speed of Autoit? If so, is there any thing that can be done about it or is this just something I will have to work around. Like setting a speed limit of the labels to a point of where this will not happen.

<{POST_SNAPBACK}>

Very nice job :(

1)Solved the boundary check, keeps the labels nicly in the windw (changes direction automatic)

2)exit button works if u use the onevent modus

No recursion probs for me :

EDIT: Yes, recursion problem :( maybe you can have a ispressed() check in the main loop (dont know if that works) or disable hotkey for direction it is running?

;
----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------


#include <GuiConstants.au3>
#include <Array.au3>
Opt("GuiOnEventMode",1)
Dim $label[10]
$gui = GUICreate("MyGUI", @DesktopWidth, @DesktopHeight - 10)
$label[0] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
;GUICtrlSetBkColor($label[0], 0)
$label[1] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
;GUICtrlSetBkColor($label[1], 0)
$label[2] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
;GUICtrlSetBkColor($label[2], 0)
$label[3] = GUICtrlCreateLabel("3", 200, 230, 10, 10)
;GUICtrlSetBkColor($label[3], 0)
$label[4] = GUICtrlCreateLabel("4", 200, 240, 10, 10)
;GUICtrlSetBkColor($label[4], 0)
$label[5] = GUICtrlCreateLabel("5", 200, 250, 10, 10)
;GUICtrlSetBkColor($label[5], 0)
$label[6] = GUICtrlCreateLabel("6", 200, 260, 10, 10)
;GUICtrlSetBkColor($label[6], 0)
$label[7] = GUICtrlCreateLabel("7", 200, 270, 10, 10)
;GUICtrlSetBkColor($label[7], 0)
$label[8] = GUICtrlCreateLabel("8", 200, 280, 10, 10)
;GUICtrlSetBkColor($label[8], 0)
$label[9] = GUICtrlCreateLabel("9", 200, 290, 10, 10)
;GUICtrlSetBkColor($label[9], 0)

GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")

GUISetState()

HotKeySet("{up}", "up")
HotKeySet("{down}", "down")
HotKeySet("{left}", "left")
HotKeySet("{right}", "right")
Global $clientsize = WinGetClientSize($gui)
Global $speedder = 20
While 1
    Sleep(50)
WEnd
Exit
Func up()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        If $pos[1] > 0 Then
            GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] - 10)
            _ArrayRotate($label,0,1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1,3,1)
    if $test = 1 Then
        right()
    ElseIf $test = 2 Then
        left()
    Else
        down()
    EndIf
EndFunc;==>up
Func left()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        If $pos[0] > 0 Then
            GUICtrlSetPos($label[ (UBound($label) - 1) ], $pos[0]-10, $pos[1] )
            _ArrayRotate($label,0,1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1,3,1)
    if $test = 1 Then
        right()
    ElseIf $test = 2 Then
        up()
    Else
        down()
    EndIf
EndFunc;==>left
Func right()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        If $pos[0] + 10  < $clientsize[0] Then
            GUICtrlSetPos($label[UBound($label) - 1], $pos[0] + 10, $pos[1])
            _ArrayRotate($label,0,1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1,3,1)
    if $test = 1 Then
        down()
    ElseIf $test = 2 Then
        left()
    Else
        up()
    EndIf
EndFunc;==>right
Func down()
    
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        If $pos[1] + 70 < $clientsize[1] Then
            GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] + 10)
            _ArrayRotate($label,0,1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1,3,1)
    if $test = 1 Then
        up()
    ElseIf $test = 2 Then
        left()
    Else
        right()
    EndIf
EndFunc;==>down
#CS
_ArrayRotate( $aArray [, $iDir [, $iSteps ] ] )
By Rob Saunders
Params:
     $aArray   The Array to rotate.
     $iDir   The direction to rotate.
               True (not 0) steps forward.
               False (0) steps backward.
     $iSteps   How many steps forward/backward
               to rotate the items.
#CE

Func _ArrayRotate(ByRef $aArray, $iDir = 0, $iSteps = 1)
    Local $iLen = UBound($aArray) - 1
    Local $vStore
    Local $iSC; step counter
    Local $iAC; array counter
    For $iSC = 1 To $iSteps
        If $iDir Then
            $sStore = $aArray[0]
            For $iAC = 0 To $iLen
                If $iAC < $iLen Then
                    $aArray[$iAC] = $aArray[$iAC + 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        Else
            $sStore = $aArray[$iLen]
            For $iAC = $iLen To 0 Step -1
                If $iAC > 0 Then
                    $aArray[$iAC] = $aArray[$iAC - 1]
                Else
                    $aArray[$iAC] = $sStore
                EndIf
            Next
        EndIf
    Next
EndFunc

Func _Exit()
    Exit
EndFunc
Edited by TuMbLeWeEd

Share this post


Link to post
Share on other sites

Very nice job 

1)Solved the boundary check, keeps the labels nicly in the windw (changes direction automatic)

2)exit button works if u use the onevent modus

No recursion probs for me 

EDIT: Yes, recursion problem  maybe you can have a ispressed() check in the main loop (dont know if that works) or disable hotkey for direction it is running?

<{POST_SNAPBACK}>

Thank you for the work on the script and the praise . I believe I will use the ispressed() function(udf) instead of hotkeyset. I just like it better since it doesn't hog up the keys. I believe I need to write this with onevent mode like you said. It will be my first time using it. I don't think the recusion problem can be helped nor the labels being left behide. I will just have to limit the speed and keep track of the last button pushed. I believe I will block the last direction pushed and the opposite direction pushed so that it can not fold in on its self.

.

Share this post


Link to post
Share on other sites

Sorry to double post. I was just playing with the script and found an Autoit bug. I made some food for the snake to eat and after the snake getts around 40 parts long, it no longer adds the new element(part) to the right place. It just leaves it behide. Does anyone have any ideal how to solve this problem or is this game just too much for Autoit to handle . Here is this script.

;
; AutoIt Version: 3.1.0
; Author:        A.N.Other <myemail@nowhere.com>
;
; Script Function:
;   Template AutoIt script.
;
; ----------------------------------------------------------------------------


#include <GuiConstants.au3>
#include <Array.au3>
opt("GuiOnEventMode", 1)
Dim $label[10]
$gui = GUICreate("MyGUI", 300, 300)
$label[0] = GUICtrlCreateLabel("0", 200, 200, 10, 10)
;GUICtrlSetBkColor($label[0], 0)
$label[1] = GUICtrlCreateLabel("1", 200, 210, 10, 10)
;GUICtrlSetBkColor($label[1], 0)
$label[2] = GUICtrlCreateLabel("2", 200, 220, 10, 10)
;GUICtrlSetBkColor($label[2], 0)
$label[3] = GUICtrlCreateLabel("3", 200, 230, 10, 10)
;GUICtrlSetBkColor($label[3], 0)
$label[4] = GUICtrlCreateLabel("4", 200, 240, 10, 10)
;GUICtrlSetBkColor($label[4], 0)
$label[5] = GUICtrlCreateLabel("5", 200, 250, 10, 10)
;GUICtrlSetBkColor($label[5], 0)
$label[6] = GUICtrlCreateLabel("6", 200, 260, 10, 10)
;GUICtrlSetBkColor($label[6], 0)
$label[7] = GUICtrlCreateLabel("7", 200, 270, 10, 10)
;GUICtrlSetBkColor($label[7], 0)
$label[8] = GUICtrlCreateLabel("8", 200, 280, 10, 10)
;GUICtrlSetBkColor($label[8], 0)
$label[9] = GUICtrlCreateLabel("9", 200, 290, 10, 10)
;GUICtrlSetBkColor($label[9], 0)
$food = GUICtrlCreateLabel("0", 100, 200, 10, 10)
GUISetOnEvent($gui_event_close, "_Exit")

GUISetState()
HotKeySet("{space}", "_sleep")
HotKeySet("{up}", "up")
HotKeySet("{down}", "down")
HotKeySet("{left}", "left")
HotKeySet("{right}", "right")
Global $clientsize = WinGetClientSize($gui)
Global $speedder = 200
While 1
    Sleep(50)
WEnd
Exit
Func up()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        $pos2 = ControlGetPos($gui, "", $food)
        
        
        If $pos[0] = $pos2[0] And $pos[1] = $pos2[1] Then
            _ArrayAdd($label, GUICtrlCreateLabel("X", $pos[0], $pos[1], 10, 10))
            GUICtrlSetPos($food, Round(Random(0, $clientsize[0]), -1), Round(Random(0, $clientsize[1]), -1))
            _ArrayRotate($label, 0, 1)
        EndIf
        
        Sleep($speedder / 2)
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] - 10)
        _ArrayRotate($label, 0, 1)
        Sleep($speedder / 2)
        
    WEnd
    $test = Random(1, 3, 1)
    If $test = 1 Then
        right()
    ElseIf $test = 2 Then
        left()
    Else
        down()
    EndIf
EndFunc  ;==>up
Func left()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        $pos2 = ControlGetPos($gui, "", $food)
        If $pos[0] = $pos2[0] And $pos[1] = $pos2[1] Then
            _ArrayAdd($label, GUICtrlCreateLabel("X", $pos[0] - 10, $pos[1], 10, 10))
            GUICtrlSetPos($food, Round(Random(0, $clientsize[0]), -1), Round(Random(0, $clientsize[1]), -1))
            _ArrayRotate($label, 0, 1)
        EndIf
        
        If $pos[0] > 0 Then
            GUICtrlSetPos($label[ (UBound($label) - 1) ], $pos[0] - 10, $pos[1])
            _ArrayRotate($label, 0, 1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1, 3, 1)
    If $test = 1 Then
        right()
    ElseIf $test = 2 Then
        up()
    Else
        down()
    EndIf
EndFunc  ;==>left
Func right()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        $pos2 = ControlGetPos($gui, "", $food)
        If $pos[0] = $pos2[0] And $pos[1] = $pos2[1] Then
            _ArrayAdd($label, GUICtrlCreateLabel("X", $pos[0] + 10, $pos[1], 10, 10))
            GUICtrlSetPos($food, Round(Random(0, $clientsize[0]), -1), Round(Random(0, $clientsize[1]), -1))
            _ArrayRotate($label, 0, 1)
        EndIf
        If $pos[0] < $clientsize[0] Then
            GUICtrlSetPos($label[UBound($label) - 1], $pos[0] + 10, $pos[1])
            _ArrayRotate($label, 0, 1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1, 3, 1)
    If $test = 1 Then
        down()
    ElseIf $test = 2 Then
        left()
    Else
        up()
    EndIf
EndFunc  ;==>right
Func down()
    
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        $pos2 = ControlGetPos($gui, "", $food)
        If $pos[0] = $pos2[0] And $pos[1] = $pos2[1] Then
            _ArrayAdd($label, GUICtrlCreateLabel("X", $pos[0], $pos[1] + 10, 10, 10))
            GUICtrlSetPos($food, Round(Random(0, $clientsize[0]), -1), Round(Random(0, $clientsize[1]), -1))
            _ArrayRotate($label, 0, 1)
        EndIf
        If $pos[1] < $clientsize[1] Then
            GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] + 10)
            _ArrayRotate($label, 0, 1)
            Sleep($speedder)
        Else
            ExitLoop
        EndIf
    WEnd
    $test = Random(1, 3, 1)
    If $test = 1 Then
        up()
    ElseIf $test = 2 Then
        left()
    Else
        right()
    EndIf
EndFunc  ;==>down
#CS
    _ArrayRotate( $aArray [, $iDir [, $iSteps ] ] )
    By Rob Saunders
    Params:
    $aArray   The Array to rotate.
    $iDir    The direction to rotate.
    True (not 0) steps forward.
    False (0) steps backward.
    $iSteps   How many steps forward/backward
    to rotate the items.
#CE

Func _ArrayRotate(ByRef $aarray, $idir = 0, $isteps = 1)
    Local $ilen = UBound($aarray) - 1
    Local $vstore
    Local $isc; step counter
    Local $iac; array counter
    For $isc = 1 To $isteps
        If $idir Then
            $sstore = $aarray[0]
            For $iac = 0 To $ilen
                If $iac < $ilen Then
                    $aarray[$iac] = $aarray[$iac + 1]
                Else
                    $aarray[$iac] = $sstore
                EndIf
            Next
        Else
            $sstore = $aarray[$ilen]
            For $iac = $ilen To 0 Step - 1
                If $iac > 0 Then
                    $aarray[$iac] = $aarray[$iac - 1]
                Else
                    $aarray[$iac] = $sstore
                EndIf
            Next
        EndIf
    Next
EndFunc  ;==>_ArrayRotate

Func _Exit()
    Exit
EndFunc  ;==>_Exit
Func _sleep()
    While 1
        Sleep(10)
    WEnd
EndFunc  ;==>_sleep


.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Wohoo!!!!!!!! :

I got it fixed and got rid of all the bugs. The problem was the with the hot keys, with the _ispressed UDF it works great. I mean I can have it zipping and zapping across the screen very fast for a long time with lots of labels and have no errors. :(

So in a few days or more, there should be a new game in the scripts and scraps :( . All I have to do now is make some collision detection, msgboxs, line things up better and make notes so it useful for learning from. I just hope the collision detection is as easy as I think it will be.

Edited by quick_sliver007

.

Share this post


Link to post
Share on other sites

Wohoo!!!!!!!!  :)

I got it fixed and got rid of all the bugs. The problem was the with the hot keys, with the _ispressed UDF it works great. I mean I can have it zipping and zapping across the screen very fast for a long time with lots of labels and have no errors. :(

So in a few days or more, there should be a new game in the scripts and scraps : . All I have to do now is make some collision detection, msgboxs, line things up better and make notes so it useful for learning from. I just hope the collision detection is as easy as I think it will be.

<{POST_SNAPBACK}>

If you mean a collision detection for if it runs in the wall, just replace the ExitLoop statements in the code i did in my previous post with a function call that stops or substract a live :( (dont forget to stop your _ispressed function when your live's are gone :P )

Like to see the resulting game ;)

Share this post


Link to post
Share on other sites

Pong. Pong. Pong. :(

Looking forward to this too though. :(


601DisengageEnd Program

Share this post


Link to post
Share on other sites

Pong. Pong. Pong.  :(

Looking forward to this too though.  :(

<{POST_SNAPBACK}>

ROFL. Pong.

Mario!!! I want that one next!!!

Hehe.


Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Share this post


Link to post
Share on other sites

ROFL.  Pong.

Mario!!!  I want that one next!!!

Hehe.

<{POST_SNAPBACK}>

I believe it will be along time before I get around to Mario :( . I was thinking of doing that game where you bounce a ball around to bust blocks away.

.

Share this post


Link to post
Share on other sites

Breakout..... Pong &#% it. :( I posted a paddle version in the old thread. Don't make me beg! LOL!


601DisengageEnd Program

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0