Jump to content

Explain This To Me


Recommended Posts

I am trying to understand code better, not just knowing what a function does because ,or what to do if you get this syntax error or that glitch but a fundamental understanding of what you’re asking of your program

I personally spend 90% of my time on this forum looking for the meaning of certain scripts so why not make a topic about it. so here I am, with a shiny new account, asking for your help with informing this growing community and inform aspiring coders on what they are asking of their computer and in turn teaching them when it is better for certain code or another.

it is in a snake game and i dont know for sure if i need to put the entire script in for it to be relivent but what i got form it it was something along the lines that when it hits a wall or eats itself it makes a new head and i just dont know

Select
        Case $newhead[0] < 0 Or $newhead[0] > 39 Or $newhead[1] < 0 Or $newhead[1] > 39
            If $snakelength > 0 Then
                MsgBox(0, "Your snake ate the wall.", "Your snake was "& ($snakelength + 1) & " units long")
            Else
                MsgBox(0, "Your snake ate the wall.", "You should play with your snake more.")
            EndIf
            _cleanup()
            
        Case $board[$newhead[0]][$newhead[1]]  ; OK to go there
            $newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
            GUICtrlSetBkColor($newhead[2], 16777215)
            If $board[$newhead[0]][$newhead[1]] = 2 Then  ; Ate Food
                ; Move food
                Do
                    $a = Int(Random(0, 39))
                    $b = Int(Random(0, 39))
                Until $board[$a][$b] = 1  ; empty
                $board[$a][$b] = 2  ; food
                GUICtrlSetPos($food, $a * 10, $b * 10)
                $snakelength = $snakelength + 1
                ReDim $snake[$snakelength + 1][3]
            Else
                GUICtrlDelete($oldtail[2])
                $board[$oldtail[0]][$oldtail[1]] = 1
            EndIf
            $board[$newhead[0]][$newhead[1]] = 0
            For $i = $snakelength To 1 Step - 1 ; Array shift
                $snake[$i][0] = $snake[$i - 1][0]
                $snake[$i][1] = $snake[$i - 1][1]
                $snake[$i][2] = $snake[$i - 1][2]
            Next
            $snake[0][0] = $newhead[0]
            $snake[0][1] = $newhead[1]
            $snake[0][2] = $newhead[2]

p.s. sorry for the confusion first post's make me Skittish

Edited by friendlycyclone
Link to comment
Share on other sites

What's this all about :huh:

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

In the goodness of time, this topic will be relocated to a more suitable part of the forum ... providing it of course has merit enough to remain and eventually make sense.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

  • Moderators

friendlycyclone,

The previous posters have obviously never written a snake game - I understand perfectly what the code is trying to do! :D

Let me take a look at the code and I will see what I can do to better your "understanding" of what is happening (or perhaps not in this case). ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

friendlycyclone,

The previous posters have obviously never written a snake game - I understand perfectly what the code is trying to do! :D

Let me take a look at the code and I will see what I can do to better your "understanding" of what is happening (or perhaps not in this case). ;)

M23

There was no mention of snake game or indeed any code earlier  :P

I still must try to play with my snake more though.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Yep, the original version of the first post, was a little very confusing and sounded like it should probably end up in Chat ... though he did admit to clicking POST too soon. Who'd have thought it was about a Snake game.

Anyway, I never have liked snakes, so I'm with Indy on this one, and will leave you all to it.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

here is the full snake game

;left arrow                           25
;up arrow                             26
;right arrow                          27
;down arrow                           28
;space bar                            20
#include <GuiConstants.au3>
#include <Array.au3>
opt("GuiOnEventMode", 1)

MsgBox(0, "Directions", "Use the arrow keys to start the game, to guide the snake and unpause the game. Use the spacebar to pause the game.")

$gui = GUICreate("Snake", 400, 400)
GuiSetBkColor(0x000099) 
GUISetState()
GUISetOnEvent($gui_event_close, "_Exit")

Global $clientsize = WinGetClientSize($gui)
Global $speedder = 150
Global $food
Global $snakelength = 0
Dim $board[40][40]  ; tracks the board
Dim $snake[$snakelength + 1][3]  ; positions and parts of the snake
Dim $food_location[2]  ; not possible for multiple food locations?

DllOpen("user32.dll")
_start()
Global $dx = 1
Global $dy = 0
_sleep()
While 1
    Sleep($speedder)
    
    If _IsPressed(25) Then
        $dx = -1
        $dy = 0
    EndIf
    If _IsPressed(27) Then
        $dx = 1
        $dy = 0
    EndIf
    If _IsPressed(26) Then
        $dx = 0
        $dy = -1
    EndIf
    If _IsPressed(28) Then
        $dx = 0
        $dy = 1
    EndIf
    move()
    If _IsPressed(20) Then
        _sleep()
    EndIf
WEnd
Func _start()
    $snakelength = 0
    ReDim $snake[$snakelength + 1][3]
    Local $x, $y, $a, $b, $pos
    For $x = 0 To 39
        For $y = 0 To 39
            $board[$x][$y] = 1  ; empty
        Next
    Next
    $a = Int(Random(3, 36))
    $b = Int(Random(3, 36))
    $snake[0][2] = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
    GUICtrlSetBkColor($snake[0][2], 16777215)
    $pos = ControlGetPos($gui, "", $snake[0][2])
    $snake[0][0] = Int($pos[0] / 10)
    $snake[0][1] = Int($pos[1] / 10)
    ;MsgBox (0, "Diagnostic", $snake[0][0] & ", " & $snake[0][1])
    $board[$a][$b] = 0  ; snake  simplifies the "can I go there?" logic
    
    Do
        $a = Int(Random(0, 39))
        $b = Int(Random(0, 39))
    Until $board[$a][$b] = 1  ; empty
    $food = GUICtrlCreateLabel("", $a * 10, $b * 10, 10, 10)
    $pos2 = ControlGetPos($gui, "", $food)
    $food_location[0] = $pos2[0] / 10
    $food_location[1] = $pos2[1] / 10
    GUICtrlSetBkColor($food, 39219)
    $board[$a][$b] = 2  ; food
EndFunc   ;==>_start

Func move()
    
    Local $newhead[3]
    $newhead[0] = $snake[0][0] + $dx
    $newhead[1] = $snake[0][1] + $dy
    
    Local $oldtail[3]
    $oldtail[0] = $snake[$snakelength][0]
    $oldtail[1] = $snake[$snakelength][1]
    $oldtail[2] = $snake[$snakelength][2]
    
    Select
        Case $newhead[0] < 0 Or $newhead[0] > 39 Or $newhead[1] < 0 Or $newhead[1] > 39
            If $snakelength > 0 Then
                MsgBox(0, "Your snake ate the wall.", "Your snake was "& ($snakelength + 1) & " units long")
            Else
                MsgBox(0, "Your snake ate the wall.", "You should play with your snake more.")
            EndIf
            _cleanup()
            
        Case $board[$newhead[0]][$newhead[1]]  ; OK to go there
            $newhead[2] = GUICtrlCreateLabel("", $newhead[0] * 10, $newhead[1] * 10, 10, 10)
            GUICtrlSetBkColor($newhead[2], 16777215)
            If $board[$newhead[0]][$newhead[1]] = 2 Then  ; Ate Food
                ; Move food
                Do
                    $a = Int(Random(0, 39))
                    $b = Int(Random(0, 39))
                Until $board[$a][$b] = 1  ; empty
                $board[$a][$b] = 2  ; food
                GUICtrlSetPos($food, $a * 10, $b * 10)
                $snakelength = $snakelength + 1
                ReDim $snake[$snakelength + 1][3]
            Else
                GUICtrlDelete($oldtail[2])
                $board[$oldtail[0]][$oldtail[1]] = 1
            EndIf
            $board[$newhead[0]][$newhead[1]] = 0
            For $i = $snakelength To 1 Step - 1 ; Array shift
                $snake[$i][0] = $snake[$i - 1][0]
                $snake[$i][1] = $snake[$i - 1][1]
                $snake[$i][2] = $snake[$i - 1][2]
            Next
            $snake[0][0] = $newhead[0]
            $snake[0][1] = $newhead[1]
            $snake[0][2] = $newhead[2]
        Case Else
            ;eat self
            MsgBox(0, "Yor snake ate itself", "You snake was "& ($snakelength + 1) & " units long")
            _cleanup()
    EndSelect
EndFunc   ;==>move

Func _cleanup()
    For $x = 0 To 39
        For $y = 0 To 39
            $board[$x][$y] = 1  ; empty
        Next
    Next
    GUICtrlDelete($food)
    For $i = 0 To $snakelength
        GUICtrlDelete($snake[$i][2])
    Next
    _start()   ; This is going to choke after 300 something calls
    _sleep()
EndFunc   ;==>_cleanup

Func _Exit()
    DllClose("user32.dll")
    Exit
EndFunc   ;==>_Exit
Func _sleep()
    Do
        Do
            Sleep(100)
        Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
        If _IsPressed(25) Then
            $dx = -1
            $dy = 0
        EndIf
        If _IsPressed(27) Then
            $dx = 1
            $dy = 0
        EndIf
        If _IsPressed(26) Then
            $dx = 0
            $dy = -1
        EndIf
        If _IsPressed(28) Then
            $dx = 0
            $dy = 1
        EndIf
    Until _IsPressed(25) Or _IsPressed(26) Or _IsPressed(27) Or _IsPressed(28)
EndFunc   ;==>_sleep
Func _IsPressed($hexkey)
    Local $ar, $brv
    $hexkey = '0x' & $hexkey
    $ar = DllCall("user32", "int", "GetAsyncKeyState", "int", $hexkey)
    
    If $ar[0] <> 0 Then
        $brv = 1
    Else
        $brv = 0
    EndIf
    
    Return $brv
EndFunc   ;==>_IsPressed

its long though so ill put a copy as an atatchment for the sake of ease... as soon as i figure out how -_-

Link to comment
Share on other sites

  • Moderators

friendlycyclone,

No problem - but I have house guests this weekend so I may not have too much time to look at it for a while. Just do not hold your breath - or if you do, hope that someone else steps in! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

a bit off topic but i also would like to know if there were any simple ways to make an object that you can manipulate

in the snake program it seemed like it was changing the color of the board arays were the "snake" would be as aposed to having the snake move over the board.

so i would like to know two things.

 1) is it possible to have an objects that moves independantly of eachother

 2) how do you set the color of  a certain place in an array (or something along those lines)

p.s. im trying to make it look like legend of zelda  were it will change when i get to the edge of the screen

so if that is impossible well then poop

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...