Jump to content

Snake Game!


Supericy
 Share

Recommended Posts

Posted Image

HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit 0
EndFunc

#include <StaticConstants.au3>
#include <GUIConstants.au3>

guicreate("Snake", 500,500)
guisetstate(@sw_show)

global $direction = 1, $bx = 10, $by = 485, $moves = 1, $ai, $up, $down, $left, $right
dim $bxold[2]
dim $byold[2]
global $direction1 = 1, $bx1 = 485, $by1 = 485, $moves1 = 1
dim $bxold1[2]
dim $byold1[2]
;$bxold[0] = 20
;$byold[0] = 470
;$bxold1[0] = 470
;$byold1[0] = 470

$direction = 1
$direction1 = 1

$inst = guictrlcreatelabel("Player vs Computer" & @crlf & @crlf & "You can go through the walls.", 200, 200, 100, 75, $SS_CENTER)
$start = guictrlcreatebutton("Start", 225,300,50,20)

guictrlcreatelabel("",$bx,$by,5,5)
guictrlsetbkcolor(-1, 0x0000FF)
guictrlcreatelabel("",$bx1,$by1,5,5)
guictrlsetbkcolor(-1, 0xFF0000)

for $i = 0 to 500 step 10
    guictrlcreatelabel("",$i,490,10,10)
    guictrlsetbkcolor(-1, 0x000000)
next
for $i = 0 to 500 step 10
        guictrlcreatelabel("",490, $i,10,10)
        guictrlsetbkcolor(-1, 0x000000)
next
for $i = 0 to 500 step 10
        guictrlcreatelabel("",$i,0,10,10)
        guictrlsetbkcolor(-1, 0x000000)
next
for $i = 0 to 500 step 10
    guictrlcreatelabel("",0, $i,10,10)
    guictrlsetbkcolor(-1, 0x000000)
next
guisetstate(@sw_show)

while 1
    $msg = guigetmsg()
    Select
        case $msg = $gui_event_close
            exit 0
        Case $msg = $start
            guictrlsetdata($inst, "Start in 3...")
                sleep(500)
            guictrlsetdata($inst, "Start in 2...")
                sleep(500)
            guictrlsetdata($inst, "Start in 1...")
                sleep(500)
            guictrlsetdata($inst, "GO!")
                sleep(500)
            guictrldelete($start)
            guictrldelete($inst)
            oneplayer()
    endselect
    sleep(25)
wend


func oneplayer() ;PLAYERS = 1------asdasdasd----------------------------------------------------
hotkeyset("{w}", "up")
hotkeyset("{s}", "down")
hotkeyset("{a}", "left")
hotkeyset("{d}", "right")
while 1
;blue BOX BELOW HERE------asdasdasd------------------------------------------------------------------

    select ;1=up 2=down 3=left 4=right
        case $direction = 1
            $by -= 5 ;$by = 270
        case $direction = 2
            $by += 5
        case $direction = 3
            $bx -= 5
        case $direction = 4
            $bx += 5
    endselect
    
    guictrlcreatelabel("",$bx,$by,5,5)
    guictrlsetbkcolor(-1, 0x0000FF)
    
    if ($bx > 490 or $bx < 10) or ($by > 490 or $by < 10) then
        select
            case $bx > 490
                $bx = 10
            case $bx < 10
                $bx = 490
            case $by > 490
                $by = 5
            case $by < 10
                $by = 490
        endselect
    endif
    for $i = 0 to ($moves-1) step 1
        if (($by = $byold[$i]) and ($bx = $bxold[$i])) or (($by = $byold1[$i]) and ($bx = $bxold1[$i])) then
            msgbox(0, "Lose", "Blue loses")
            exit 0
        endif
    next
    
    $moves += 1
    redim $bxold[$moves+1]
    redim $byold[$moves+1]
    $bxold[$moves] = $bx
    $byold[$moves] = $by

;RED COMPUTER BELOW ---asdasdasd-------------------------------------------------------------------

    $up = checkup()
    $down = checkdown()
    $left = checkleft()
    $right = checkright()
    
    $up1 = checkup1()
    $down1 = checkdown1()
    $left1 = checkleft1()
    $right1 = checkright1()
    
    ;msgbox(0,"hsf",$up & @crlf & $down & @crlf & $left & @crlf & $right & @crlf & $by1 & @crlf & $bx1 & @crlf)

    select ;1=up 2=down 3=left 4=right
        case ($by1-5 > 5) and $direction1 <> 2 and $up = 0 and $up1 = 0
            $direction1 = 1
        case ($bx1-5 > 5) and $direction1 <> 4 and $left = 0 and $left1 = 0
            $direction1 = 3
        case ($bx1+5 < 485) and $direction1 <> 3 and $right = 0 and $right1 = 0
            $direction1 = 4
        case ($by1+5 < 485) and $direction1 <> 1 and $down = 0 and $down1 = 0
            $direction1 = 2
    endselect
    
    select ;1=up 2=down 3=left 4=right
        case $direction1 = 1
            $by1 -= 5
        case $direction1 = 2
            $by1 += 5
        case $direction1 = 3
            $bx1 -= 5
        case $direction1 = 4
            $bx1 += 5
        case else
    endselect
    
    guictrlcreatelabel("",$bx1,$by1,5,5)
    guictrlsetbkcolor(-1, 0xFF0000)

    if ($bx1 > 490 or $bx1 < 10) or ($by1 > 490 or $by1 < 10) then
        select
            case $bx1 > 490
                $bx1 = 10
            case $bx1 < 10
                $bx1 = 490
            case $by1 > 490
                $by1 = 5
            case $by1 < 10
                $by1 = 490
        endselect
    endif
    for $i = 0 to ($moves1-1) step 1
        if (($by1 = $byold1[$i]) and ($bx1 = $bxold1[$i])) or (($by1 = $byold[$i]) and ($bx1 = $bxold[$i])) then
            msgbox(0, "Lose", "Red loses")
            exit 0
        endif
    next
    
    $moves1 += 1
    redim $bxold1[$moves1+1]
    redim $byold1[$moves1+1]
    $bxold1[$moves1] = $bx1
    $byold1[$moves1] = $by1
    sleep(25)
wend
endfunc

func up()
    $direction = 1
endfunc
func down()
    $direction = 2
endfunc
func left()
    $direction = 3
endfunc
func right()
    $direction = 4
endfunc


func checkup()
        for $i = 0 to $moves1 step 1
            if (($by1-5) = $byold[$i]) and ($bx1 = $bxold[$i]) then
                return 1
            endif
        next
endfunc

func checkdown()
        for $i = 0 to $moves1 step 1
            if (($by1+5) = $byold[$i]) and ($bx1 = $bxold[$i]) then
                return 1
            endif
        next
endfunc

func checkleft()
        for $i = 0 to $moves1 step 1
            if (($bx1-5) = $bxold[$i]) and ($by1 = $byold[$i]) then
                return 1
            endif
        next
endfunc

func checkright()
        for $i = 0 to $moves1 step 1
            if (($bx1+5) = $bxold[$i]) and ($by1 = $byold[$i]) then
                return 1
            endif
        next
    endfunc
    
func checkup1()
        for $i = 0 to $moves1 step 1
            if (($by1-5) = $byold1[$i]) and ($bx1 = $bxold1[$i]) then
                return 1
            endif
        next
endfunc

func checkdown1()
        for $i = 0 to $moves1 step 1
            if (($by1+5) = $byold1[$i]) and ($bx1 = $bxold1[$i]) then
                return 1
            endif
        next
endfunc

func checkleft1()
        for $i = 0 to $moves1 step 1
            if (($bx1-5) = $bxold1[$i]) and ($by1 = $byold1[$i]) then
                return 1
            endif
        next
endfunc

func checkright1()
        for $i = 0 to $moves1 step 1
            if (($bx1+5) = $bxold1[$i]) and ($by1 = $byold1[$i]) then
                return 1
            endif
        next
endfunc

Updated to no longer require image files.

I understand that this is probably the most ineffective way of making this type of game, but who cares, it works!

I also have a functional 2 player version, but i am planning on adding more options to it and combining this 1 player version with it.

Controls:

w - up

s - down

a - left

d - right

Edited by Supericy
Link to comment
Share on other sites

You could make this a bit easier for other people to use by just using labels and setting them to colors... black would be 0.. and red would be something like 0xFF0000 (I think).. Then just use GUICtrlSEtBkColor($label).

Yeah, i should have thought of using that in the first place. I updated it to use that, no longer need the image files.

Link to comment
Share on other sites

The game gets kinda screwy when the player or ai are in the walls, maybe you should just skip them and have the snake go out directly on the other side.

Link to comment
Share on other sites

I would suggest using bigger squares.. right now I can't really make precise turns.. it usually ends up being 2 or 3 off..

Also, if you're going to let people move through walls I would probably just leave the black wall off..

The AI seems to work pretty well.. I also like how the game starts, nice, simple and functional.

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
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...