Jump to content

Here it is, the snake game.


Recommended Posts

This game is the result of a combined effort of Smed and I. Feel free to leave comments. Here is V1.0.

;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

**EDIT** Edited msgboxs

**EDIT** I change the colors

snakeV1.0.Au3

Edited by quick_sliver007

.

Link to comment
Share on other sites

  • Replies 60
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Awsome, I played that so much.  Anychance you can make it the classic blue background and white snake?

<{POST_SNAPBACK}>

I never played it that way. I had always played it on the calculators and cell phones. It shouldn't be to hard to change the colors. Edit as you wish. :( Edited by quick_sliver007

.

Link to comment
Share on other sites

Awsome, I played that so much.  Anychance you can make it the classic blue background and white snake?

<{POST_SNAPBACK}>

White snake: Look for GUICtrlSetBkColor and change the color from 0 to 0xFFFF

Background: Try something like GuiSetBkColor(0x000099) after the GUI is created

"it's self" should be replaced with "itself"

@quick_sliver007 and Smed: Nice work for under 200 lines of code :(

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

White snake:  Look for GUICtrlSetBkColor and change the color from 0 to 0xFFFF

Background:  Try something like GuiSetBkColor(0x000099) after the GUI is created

"it's self" should be replaced with "itself"

@quick_sliver007 and Smed:  Nice work for under 200 lines of code :(

<{POST_SNAPBACK}>

Thank you. I just fixed that typo and changed one other msgbox to something Smed wanted. :(

.

Link to comment
Share on other sites

Very nice, I'm having far too much fun with this =p

One thing I found that I'm not 100% about is that if you are going down and you click the "up" arrow (or viseversa), you die. Now I don't know if that is how it is in the real game but I don't think you die when clicking the down/up arrow when going the opposite way on the cell phone version. If you do, my bad =/

Anyway, this is quite fun, good work! =)

Link to comment
Share on other sites

Very nice, I'm having far too much fun with this =p

One thing I found that I'm not 100% about is that if you are going down and you click the "up" arrow (or viseversa), you die. Now I don't know if that is how it is in the real game but I don't think you die when clicking the down/up arrow when going the opposite way on the cell phone version. If you do, my bad =/

Anyway, this is quite fun, good work! =)

<{POST_SNAPBACK}>

You die whenever the snake tries to move over a position where it already is. If the cell phone version handles this differently, its that version thats wrong. :( Glad you're enjoying it, thanks for the feedback.

EDIT:

P.S. @Ejoc: I never played the DOS version, I remember this game from waaay back in the era of Black&White monitors, so I'd have to differ with your opinion on the "Classic" color scheme. I think last time I helped implement this game we used a '&' to represent the food, and I can't even remember what language it was in. I do remember that it was a 4 terminal 'Mini', but even the brand escapes me. Misspent youth, and all that... :(

Edited by Smed

601DisengageEnd Program

Link to comment
Share on other sites

quick_sliver007 and I have been discussing how the next version of this should behave and we're not certain what the end user will want. We've agreed that starting game options are a good idea, and if things work out, there will be plenty.

I like the snake speeding up every time it eats (thats in my copy, not the public release). I think you should level-up around the time you eat 50 food. But thats just me.

So, we were wondering.. what would you want? There are so many choices.. more food per level, starting snake length based on level, starting speed based on level. Number of poisons placed per level (yes, thats definitely coming). Any other brilliant ideas?

If anybody likes the game enough to have an opinion, we'd like to hear it.

601DisengageEnd Program

Link to comment
Share on other sites

Smed, those selections all sound great. I wonder if you could make all these selections that can be set by the user, saved to an {.ini} file, then read the {.ini} before starting the game using the user settings..

What do you think?

Cheers.. :B

Link to comment
Share on other sites

Well I'm used to what I play on my 'mobile', cell-phone in Australia lol. And I have a old Nokia 3315 and the screen is black and green. You have the option of '9 levels'. The levels refer to the speed of the snake. Which means that the snake doesn't get any faster after eating X amount of food. Secondly, you have a 'Maze option', consisting of 5 mazes. When no maze is selected, its exactly how you have snake right now, except you are able to go 'through' the side of the window appearing out the otherside like a loop. The second maze has a wall around the outside like you have now. The rest of the mazes have objects, wall etc, all over the screen you have to work your way around.

Just some idea's :(.

qq

Link to comment
Share on other sites

Thank you two for the input for the next version. If any body has any ideals just post them.

Also take a look at my high score. :( It's the best I have done yet. I believe it can be beat easy but take a look anyway.

Edited by quick_sliver007

.

Link to comment
Share on other sites

Cool and fun game !  :(

Can we have a multiplayer version too ?  :(

<{POST_SNAPBACK}>

Thank you for the reply.

Multiplayer sounds interesting, I believe 2 player mode would be more realistic. I can see two snake on the same board racing for the food and dodging each other. I like it, I just have to see what my partner thinks. I just wonder how big of a group would take advantage of it and if a 2 play player mode would be worth the time to code it. Then again maybe, if Smed likes the ideal; in the next version we could try to make an AI and have a fight for food mode vs the computer. Now that would be something to see.

.

Link to comment
Share on other sites

Good job! Xenogis = Proud of you.

Suggestion: Make a score counter

You should be proud of yourself, you made me come out of my hiding spot and post again (I havn't posted on these forums for quite a while, i think about 2 & 1/2 months now)

[font="Times"] If anyone remembers me, I am back. Maybe to stay, maybe not.----------------------------------------------------------------------------------------------------------[/font][font="Times"]Things I am proud of: Pong! in AutoIt | SearchbarMy website: F.R.I.E.S.A little website that is trying to get started: http://thepiratelounge.net/ (not mine)[/font][font="Times"] ----------------------------------------------------------------------------------------------------------[/font][font="Arial"]The newbies need to stop stealing avatars!!! It is confusing!![/font]

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...