Jump to content
Sign in to follow this  
quick_sliver007

I need help with _ArraySwap or....

Recommended Posts

quick_sliver007

I am trying to make a snake game. That game were you go around collecting the dots with a snake and for every dot you eat the snake gets bigger. I have ran into a problem with the snakes movement. I need to move the the very last piece of the snake to in front of the head of the snake and become the head over and over in a loop. The problem is that the last piece of the sank just moves in front of the snake and does not become the head. Look at the function "Up()" in my 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[5]
$gui = GUICreate("MyGUI", @DesktopWidth, @DesktopHeight - 10)
$label[0] = GUICtrlCreatePic("1.bmp", 200, 200, 10, 10)
GUICtrlSetBkColor($label[0], 0)
$label[1] = GUICtrlCreatePic("1.bmp", 200, 210, 10, 10)
GUICtrlSetBkColor($label[1], 0)
$label[2] = GUICtrlCreatePic("1.bmp", 200, 220, 10, 10)
GUICtrlSetBkColor($label[2], 0)
$label[3] = GUICtrlCreatePic("1.bmp", 200, 230, 10, 10)
GUICtrlSetBkColor($label[3], 0)
$label[4] = GUICtrlCreatePic("1.bmp", 200, 240, 10, 10)
GUICtrlSetBkColor($label[4], 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)
        $label[UBound($label) - 1] = $label[0]
        Sleep(1000)
    ;;;;;;;;;;;;;;;;help here;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        _ArraySwap( $label[0], $label[UBound($label) - 1] )
;;;;;;;;;;;;;;;;help here;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
        
    WEnd
EndFunc  ;==>up
Func left()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0] - 10, $pos[1])
        Sleep(1000)
    WEnd
EndFunc  ;==>left
Func right()
    While 1
        $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0] + 10, $pos[1])
        Sleep(1000)
    WEnd
EndFunc  ;==>right
Func down()
    
    While 1
        Dim $pos = ControlGetPos($gui, "", $label[0])
        GUICtrlSetPos($label[UBound($label) - 1], $pos[0], $pos[1] + 10)
        Sleep(1000)
    WEnd
EndFunc  ;==>down

As you can see I tried

_ArraySwap( $label[0], $label[UBound($label) - 1] )
and it is not working. So how do I get the tail(the last element in the array) to be the head(the first element in the array) after it has moved. Edited by quick_sliver007

.

Share this post


Link to post
Share on other sites
buzz44

_ArraySwap( $label[0], $label[4])
?

Edit: Ok that won't worklol, as the snake eats more, there will be more parts of it, and hence more indexes in the array.

Edit2: Try?

$Last = UBound($label) - 1
_ArraySwap( $label[0], $label[$Last])

Or

_ArraySwap( $label[0], $label[(UBound($label) - 1)])
Edited by Burrup

qq

Share this post


Link to post
Share on other sites
quick_sliver007

_ArraySwap( $label[0], $label[4])
?

Edit: Ok that won't worklol, as the snake eats more, there will be more parts of it, and hence more indexes in the array.

<{POST_SNAPBACK}>

It's a tricky one that I can't seem to figure out. Yep it will be a growing array.

.

Share this post


Link to post
Share on other sites
quick_sliver007

I believe I just thought of the real problem. I need to swap all of the elements. The last to front and every thing between back one. Because it is like a rotation of rear to the front. Let me try to figure out how in a min if you don't beat me to it.

**Edit**

More like an array shift after moving the tail to in front of the head

Edited by quick_sliver007

.

Share this post


Link to post
Share on other sites
buzz44

Perphaps look at the _Array() functions. _ArraySort/Reverse.


qq

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  

×