Sign in to follow this  
Followers 0
qazwsx

Snake Game

4 posts in this topic

#1 ·  Posted (edited)

I am having trouble making the sections of my snake follow the head. I ahve looked at the other snake games on the site but none of them have the snake grow, they have the snake move faster instead. I have gotten the first section to follow put i need help with the logic to make each successive peace follow. Here is the code:

#include <GUIConstants.au3>
#include <misc.au3>
FileInstall("C:\Program Files\AIM95\aimalert.gif", @ScriptDir & "\aimalert.gif")
FileInstall("C:\Program Files\AIM95\stockalert.gif", @ScriptDir & "\stockalert.gif")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Hotkeys
HotKeySet("q", "_quit")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Vars
Global $x;y cords
Global $y;x cords
$move = 10;pixel movements per frame
$width = 360 - 10;width if the area
$height = 263 - 10;height of the area
Global $command;direction of the snake
Global $speed;speed of the snake
Global $food[2];coords of the food (array)
Global $pic2;food (handle of image)
Global $score = 0;score
Global $section[100];section of snake
Global $sectionposx
Global $sectionposy;pos of section
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Setup

$Form1 = GUICreate("Difficulty", 281, 116, 193, 115)
$Label1 = GUICtrlCreateLabel("Snake", 104, 16, 57, 28)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Easy", 16, 56, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Medium", 104, 56, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Hard", 192, 56, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $speed = 100
            GUIDelete()
            ExitLoop
        Case $Button2
            $speed = 50
            GUIDelete()
            ExitLoop
        Case $Button3
            $speed = 25
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;game screen
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Snake", 360, 263, 193, 115)
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\aimalert.gif", 0, 0, 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

SplashTextOn("Go", "Press an Arrow Key to Begin", 300, 20)
_food()


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Pic1
            
        Case Else
            If _IsPressed (25) Then _left ()
            If _IsPressed (26) Then _up()
            If _IsPressed (27) Then _right ()
            If _IsPressed (28) Then _down ()
            Sleep (1)
    EndSwitch
WEnd




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Funcs
Func _down()
    SplashOff()
    _turn()
    $command = "down"
    While $command = "down"
            If _IsPressed (25) Then _left ()
            If _IsPressed (26) Then _up()
            If _IsPressed (27) Then _right ()
        _eat()
        $y = $y + $move
        If $y > 253 Then
            $y = -10
            GUICtrlSetPos($Pic1, 0 + $x, $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followdown ()
        Sleep($speed)
    WEnd
    
EndFunc  ;==>_down

Func _left()
    SplashOff()
    _turn()
    $command = "left"
    While $command = "left"
            If _IsPressed (26) Then _up()
            If _IsPressed (27) Then _right ()
            If _IsPressed (28) Then _down ()
        _eat()
        $x = $x - $move
        If $x < -10 Then
            $x = 350
            GUICtrlSetPos($Pic1, $x, 0 + $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followleft ()
        Sleep($speed)
    WEnd
    
    
    
EndFunc  ;==>_left
Func _right()
    SplashOff()
    _turn()
    $command = "right"
    While $command = "right"
            If _IsPressed (25) Then _left ()
            If _IsPressed (26) Then _up()
            If _IsPressed (28) Then _down ()
        _eat()
        $x = $x + $move
        _followright ()
        If $x > 350 Then
            $x = -10
            GUICtrlSetPos($Pic1, $x, 0 + $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        Sleep($speed)
    WEnd
    
EndFunc  ;==>_right

Func _up()
    SplashOff()
    _turn()
    $command = "up"
    While $command = "up"
            If _IsPressed (25) Then _left ()
            If _IsPressed (27) Then _right ()
            If _IsPressed (28) Then _down ()
        _eat()
        $y = $y - $move
        If $y < -10 Then
            $y = 253
            GUICtrlSetPos($Pic1, 0 + $x, $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followup ()
        Sleep($speed)
    WEnd
EndFunc  ;==>_up

Func _food()
    $food[0] = Random(0, $width)
    $food[1] = Random(0, $height)
    $pic2 = GUICtrlCreatePic(@ScriptDir & "\stockalert.gif", $food[0], $food[1], 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
EndFunc  ;==>_food

Func _eat()
    If $x < $food[0] + 15 And $x > $food[0] - 15 And $y < $food[1] + 15 And $y > $food[1] - 15 Then
        GUICtrlDelete($pic2)
        $score = $score + 1
        _segment()
        _food()
        
    EndIf
EndFunc  ;==>_eat

Func _segment()
    $section[$score] = GUICtrlCreatePic(@ScriptDir & "\aimalert.gif", 0 + $x, 0 + $y, 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
    $sectionposx = $x
    $sectionposy = $y
EndFunc  ;==>_segment

Func _followright()
    If $score > 0 Then
        If $x < $sectionposx Then
            $sectionposx = $x - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        If $x > $sectionposx + 20 Then;follow right
            
            $sectionposx = $sectionposx + 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
    EndIf
EndFunc  ;==>_followright

Func _followleft()
    If $score > 0 Then
        If $x > $sectionposx Then
            $sectionposx = $x - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        If $x < $sectionposx - 20 Then;follow left
            $sectionposx = $sectionposx - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
    EndIf
EndFunc  ;==>_followleft

Func _followdown()
    If $score > 0 Then
        If $y < $sectionposy Then
            $sectionposy = $y - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        
        If $y > $sectionposy + 20 Then;follow down
            $sectionposy = $sectionposy + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc  ;==>_followdown

Func _followup()
    If $score > 0 Then
        If $y > $sectionposy Then
            $sectionposy = $y + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        If $y < $sectionposy - 20 Then;follow up
            $sectionposy = $sectionposy - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc  ;==>_followup

Func _turn()
    If $score > 0 Then
        If $x > $sectionposx + 5 Then;follow right
            $sectionposx = $sectionposx + 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        
        If $x < $sectionposx - 5 Then;follow left
            $sectionposx = $sectionposx - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        
        If $y > $sectionposy + 5 Then;follow down
            $sectionposy = $sectionposy + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        
        If $y < $sectionposy - 5 Then;follow down
            $sectionposy = $sectionposy - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc  ;==>_turn

Func _quit()
    MsgBox(0, "score", "Your score is: " & $score)
    Exit
EndFunc  ;==>_quit

If you dont want to read the whole code and just want a specific part like the creation of the segment i will gladly post that part.

Now with ispressed instead of hotkeys.

Edit: how do i make the code box scrollable?

Edited by sccrstvn93

Share this post


Link to post
Share on other sites



I am having trouble making the sections of my snake follow the head. I ahve looked at the other snake games on the site but none of them have the snake grow, they have the snake move faster instead. I have gotten the first section to follow put i need help with the logic to make each successive peace follow. Here is the code:

#include <GUIConstants.au3>
FileInstall("C:\Program Files\AIM95\aimalert.gif", @ScriptDir & "\aimalert.gif")
FileInstall("C:\Program Files\AIM95\stockalert.gif", @ScriptDir & "\stockalert.gif")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Hotkeys
HotKeySet("{up}", "_up")
HotKeySet("{left}", "_left")
HotKeySet("{right}", "_right")
HotKeySet("{down}", "_down")
HotKeySet("q", "_quit")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Vars
Global $x;y cords
Global $y;x cords
$move = 10;pixel movements per frame
$width = 360 - 10;width if the area
$height = 263 - 10;height of the area
Global $command;direction of the snake
Global $speed;speed of the snake
Global $food[2];coords of the food (array)
Global $pic2;food (handle of image)
Global $score = 0;score
Global $section[100];section of snake
Global $sectionposx
Global $sectionposy;pos of section
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Setup

$Form1 = GUICreate("Difficulty", 281, 116, 193, 115)
$Label1 = GUICtrlCreateLabel("Snake", 104, 16, 57, 28)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Easy", 16, 56, 75, 25, 0)
$Button2 = GUICtrlCreateButton("Medium", 104, 56, 75, 25, 0)
$Button3 = GUICtrlCreateButton("Hard", 192, 56, 75, 25, 0)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $speed = 100
            GUIDelete()
            ExitLoop
        Case $Button2
            $speed = 50
            GUIDelete()
            ExitLoop
        Case $Button3
            $speed = 25
            GUIDelete()
            ExitLoop
    EndSwitch
WEnd



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;game screen
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Snake", 360, 263, 193, 115)
$Pic1 = GUICtrlCreatePic(@ScriptDir & "\aimalert.gif", 0, 0, 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

SplashTextOn("Go", "Press an Arrow Key to Begin", 300, 20)
_food()


While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
            
        Case $Pic1
            

    EndSwitch
WEnd




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Funcs
Func _down()
    SplashOff()
    _turn()
    $command = "down"
    While $command = "down"
        _eat()
        $y = $y + $move
        If $y > 253 Then
            $y = -10
            GUICtrlSetPos($Pic1, 0 + $x, $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followdown ()
        Sleep($speed)
    WEnd
    
EndFunc ;==>_down

Func _left()
    SplashOff()
    _turn()
    $command = "left"
    While $command = "left"
        _eat()
        $x = $x - $move
        If $x < -10 Then
            $x = 350
            GUICtrlSetPos($Pic1, $x, 0 + $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followleft ()
        Sleep($speed)
    WEnd
    
    
    
EndFunc ;==>_left
Func _right()
    SplashOff()
    _turn()
    $command = "right"
    While $command = "right"
        _eat()
        $x = $x + $move
        _followright ()
        If $x > 350 Then
            $x = -10
            GUICtrlSetPos($Pic1, $x, 0 + $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        Sleep($speed)
    WEnd
    
EndFunc ;==>_right

Func _up()
    SplashOff()
    _turn()
    $command = "up"
    While $command = "up"
        _eat()
        $y = $y - $move
        If $y < -10 Then
            $y = 253
            GUICtrlSetPos($Pic1, 0 + $x, $y)
            
        Else
            GUICtrlSetPos($Pic1, 0 + $x, 0 + $y)
        EndIf
        _followup ()
        Sleep($speed)
    WEnd
EndFunc ;==>_up

Func _food()
    $food[0] = Random(0, $width)
    $food[1] = Random(0, $height)
    $pic2 = GUICtrlCreatePic(@ScriptDir & "\stockalert.gif", $food[0], $food[1], 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
EndFunc ;==>_food

Func _eat()
    If $x < $food[0] + 15 And $x > $food[0] - 15 And $y < $food[1] + 15 And $y > $food[1] - 15 Then
        GUICtrlDelete($pic2)
        $score = $score + 1
        _segment()
        _food()
        
    EndIf
EndFunc ;==>_eat

Func _segment()
    $section[$score] = GUICtrlCreatePic(@ScriptDir & "\aimalert.gif", 0 + $x, 0 + $y, 28, 28, BitOR($SS_NOTIFY, $WS_GROUP))
    $sectionposx = $x
    $sectionposy = $y
EndFunc ;==>_segment

Func _followright()
    If $score > 0 Then
        If $x < $sectionposx Then
            $sectionposx = $x - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        If $x > $sectionposx + 20 Then;follow right
            
            $sectionposx = $sectionposx + 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
    EndIf
EndFunc ;==>_followright

Func _followleft()
    If $score > 0 Then
        If $x > $sectionposx Then
            $sectionposx = $x - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        If $x < $sectionposx - 20 Then;follow left
            $sectionposx = $sectionposx - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
    EndIf
EndFunc ;==>_followleft

Func _followdown()
    If $score > 0 Then
        If $y < $sectionposy Then
            $sectionposy = $y - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        
        If $y > $sectionposy + 20 Then;follow down
            $sectionposy = $sectionposy + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc ;==>_followdown

Func _followup()
    If $score > 0 Then
        If $y > $sectionposy Then
            $sectionposy = $y + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        If $y < $sectionposy - 20 Then;follow up
            $sectionposy = $sectionposy - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc ;==>_followup

Func _turn()
    If $score > 0 Then
        If $x > $sectionposx + 5 Then;follow right
            $sectionposx = $sectionposx + 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        
        If $x < $sectionposx - 5 Then;follow left
            $sectionposx = $sectionposx - 10
            GUICtrlSetPos($section[$score], $sectionposx, $y)
        EndIf
        
        If $y > $sectionposy + 5 Then;follow down
            $sectionposy = $sectionposy + 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
        
        If $y < $sectionposy - 5 Then;follow down
            $sectionposy = $sectionposy - 10
            GUICtrlSetPos($section[$score], $x, $sectionposy)
        EndIf
    EndIf
EndFunc ;==>_turn

Func _quit()
    MsgBox(0, "score", "Your score is: " & $score)
    Exit
EndFunc ;==>_quit

If you dont want to read the whole code and just want a specific part like the creation of the segment i will gladly post that part.

Edit: how do i make the code box scrollable?

When I run this script, nothing appears on the GUI at all. You might want to look into _IsPressed instead of Hotkeys. Also, using _ArrayPush with coordinates, you could set each "section" of your snake to a different array variable.

Hope that helps.

Good luck

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

thx for the ideas, Did the icons install on your computer?

Edit: unless im not understanding _arraypush doesn't it delete a value everytime it adds one?

Edit2: maybe array add but even if i have all of the sections in an array how can i control them all?

Edited by sccrstvn93

Share this post


Link to post
Share on other sites

Ok its uses _ispressed now. Can anyone else tell me if this works on their computer?

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