All pastes #36985 Raw Edit

Snake

public text v1 · immutable
#36985 ·published 2006-01-16 17:41 UTC
rendered paste body
View.Set ("graphics:550,400, offscreenonly, nobuttonbar, title:Snake")
var x, y, dir, font, score, foodx, foody, speed, speedfactor, bodycount : int
var chars : array char of boolean
var moving, pauser, gameOver, setfood : boolean
var bodyx, bodyy : flexible array 0 .. 0 of int
x := 28
y := 18
dir := 1
score := 0
moving := false
pauser := false
gameOver := false
setfood := false
speed := 0
speedfactor := 10
bodycount := 0
font := Font.New ("Impact:14")
% lets say 1 in terms of direction = up
proc drawgrid
    % Draw a 10x10 grid background
    for i : 1 .. maxx by 10
        for j : 1 .. maxy by 10
            drawbox (i - 1, j - 1, i + 9, j + 9, black)
        end for
    end for
    drawfillbox (0, maxy - 49, maxx, maxy, grey)
end drawgrid

proc controls
    % This is the main game loop
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) and dir not= 2 then
        dir := 1
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_DOWN_ARROW) and dir not= 1 then
        dir := 2
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_LEFT_ARROW) and dir not= 4 then
        dir := 3
        if moving = false then
            moving := true
        end if
    elsif chars (KEY_RIGHT_ARROW) and dir not= 3 then
        dir := 4
        if moving = false then
            moving := true
        end if
    end if
    if chars (KEY_ENTER) then
        if pauser = false then
            pauser := true
        elsif pauser = true then
            pauser := false
        end if
    end if

end controls

proc move
    % controls should go first and then check if moving has begun then
    % Snake Shifting code here
    if bodycount > 0 then
        bodyx (1) := x
        bodyy (1) := y
        if bodycount > 1 then
            for decreasing i : bodycount .. 2
                bodyx (i) := bodyx (i - 1)
                bodyy (i) := bodyy (i - 1)
            end for
        end if
    end if


    if moving = true then
        speed += speedfactor
        if speed mod 10 = 0 then
            if dir = 1 then
                y += 1
            elsif dir = 2 then
                y -= 1
            elsif dir = 3 then
                x -= 1
            elsif dir = 4 then
                x += 1
            end if
            speed := 0

        end if
    end if
end move

proc ohnoes
    % Handles Game Over/ Restart
    if x <= 0 or x * 10 >= maxx or y <= 0 or y * 10 >= maxy - 50 then
        x := 28
        y := 18
        moving := false
        %pauser := true
        gameOver := true
        score := 0
        dir := 0
        bodycount := 0
        new bodyx, bodycount
        new bodyy, bodycount
    end if
end ohnoes

proc otherside
    if x < 0 then
        x := maxx div 10
    end if
    if x * 10 >= maxx then
        x := 0
    end if
    if y < 0 then
        y := maxy div 10 - 5
    end if
    if y * 10 >= maxy - 50 then
        y := 0
    end if
end otherside

proc HUD
    Font.Draw ("SCORE :" + intstr (score), 20, maxy - 40, font, black)
    if pauser = true then
        Font.Draw ("PAUSED", maxx div 2 - 100, maxy - 40, font, red)
    end if
end HUD

proc food
    % Food Code
    if setfood = false then
        setfood := true
        randint (foodx, 1, maxx div 10)
        randint (foody, 1, maxy div 10 - 5)
    end if
    drawfilloval (foodx * 10 + 5, foody * 10 + 5, 5, 5, red)
    if x = foodx and y = foody then
        bodycount += 1
        new bodyx, bodycount
        new bodyy, bodycount
        score += 1
        setfood := false
    end if
end food





loop
    %ohnoes
    otherside
    drawgrid
    HUD
    controls

    food
    if pauser = false then
        move
    end if

    locate (3, 18)
    put x, " ", y, " = ", foodx, " ", foody, " ", bodycount, " ", upper (bodyx), " " ..
    View.Update
    cls
    delay (5)
    drawfillbox (x * 10, y * 10, x * 10 + 10, y * 10 + 10, 1)
    for decreasing i : bodycount .. 1
        drawfillbox (bodyx (i) * 10, bodyy (i) * 10, bodyx (i) * 10 + 10, bodyy (i) * 10 + 10, i)
    end for
end loop