Yjasl Examples

Introduction :: Reference Manual :: Internals :: Yjasl Examples :: Programming Guidelines :: Download/sf.net :: About/Contact

snake.yjasl :: A snake game (console)
mandel.yjasl :: Mandelbrot (console)
gali.yjasl :: Game Of Life using 1D arrays (console)
gali2.yjasl :: Game Of Life using 2D arrays (console)
graph_mandel.yjasl :: Mandelbrot (GDI bindings 640x480)
graph_plasma.yjasl :: Plasma (GDI bindings 640x480)

snake.yjasl
/*     A simple console mode snake game
    (w) by Neotec in 2006
    Uses: Jasl v2.00
*/

sub game_init();
sub game_exit();
sub init_pfield();
sub clear_pfield();
sub draw_pfield();
sub set(x, y, c);
sub make_border();
sub set_food(start);
sub make_level(nr);
sub cls();
sub update_level();
sub update_score();
sub center(y, s);
function move_snake();
function play_loop();

global pfield, old_cp, level, score, speed
global colors = {}
global snake = {}

game_init()

level = 0
score = 0
speed = 1

do
    cls()

    make_level(level)

    update_level()
    update_score()

    cfgcolor(14)
    cbgcolor(colors.bkg)

    center(10, "Level "..(level + 1))
    center(12, "Press any key when ready!")

    do until getkey() == null
    waitkey()

    draw_pfield()

    r = play_loop()

    if r == 1 then
        level = mod(level + 1, 5)
        speed = speed + 0.1
        if speed > 4 then
            speed = 4
        end
    end

until r < 0

if r == -2 then
    cfgcolor(14)
    cbgcolor(colors.bkg)
    center(10, "Game over!")
    center(12, "Press space to exit")
    do until getkey() == null
    do
        sleep(10)
    until char(getkey()) == " "
end

game_exit()

sub game_init()
    local i
    old_cp = cgetcp()
    ccpselect(1250)
    ccsrvisible(false)

    print("Initializing ")

    colors.bkg = 6
    colors.snake = 1
    colors.wall = 5
    colors.food = 8

    snake.tail = 0
    snake.head = 0
    snake.alength = 0
    snake.length = 0
    snake.dir = 0
    snake.pos = array(4000)
    snake.start = {}

    snake.start.dir = 3
    snake.start.x = 40
    snake.start.y = 24

    print(".")

    for i = 0, 3999 do
        snake.pos[i] = {}
        snake.pos[i].x = 0
        snake.pos[i].y = 0
        if mod(i, 1000) == 0 then
            print(".")
        end
    end

    print(".")

    init_pfield()

    cls()
end

sub game_exit()
    ccpselect(old_cp)
    cls()
    ccsrvisible(true)
end

sub init_pfield()
    local x, y
    pfield = array(80)

    for x = 0, 79 do
        pfield[x] = array(50)
        for y = 0, 49 do
            pfield[x][y] = {}
            pfield[x][y].r = floor(y / 2)
            pfield[x][y].n = -((y & 1) * 2 - 1)
            pfield[x][y].c = colors.bkg
        end
    end
end

sub clear_pfield()
    local x, y
    for x = 0, 79 do
        for y = 0, 49 do
            pfield[x][y].c = 0
        end
    end
end

sub draw_pfield()
    local x, y, c = char(223)
    local y1 = 0, y2 = 1
    chome()
    for y = 0, 24 do
        for x = 0, 79 do
            if pfield[x][y1].c == pfield[x][y2].c then
                cbgcolor(pfield[x][y1].c)
                print(" ")
            else
                cfgcolor(pfield[x][y1].c)
                cbgcolor(pfield[x][y2].c)
                print(c)
            end
        end
        y1 = y1 + 2
        y2 = y2 + 2
    end
end

sub make_border()
    local x, y
    for x = 0, 79 do
        pfield[x][0].c = colors.wall
        pfield[x][49].c = colors.wall
    end
    for y = 1, 48 do
        pfield[0][y].c = colors.wall
        pfield[79][y].c = colors.wall
    end
    for y = 1, 48 do
        for x = 1, 78 do
            pfield[x][y].c = colors.bkg
        end
    end
end

sub set(x, y, c)
    local y1
    pfield[x][y].c = c
    y1 = y + pfield[x][y].n

    ccsrpos(x, pfield[x][y].r)

    if pfield[x][y].c == pfield[x][y1].c then
        cbgcolor(c)
        print(" ")
    else
        if pfield[x][y].n == 1 then
            cfgcolor(pfield[x][y].c)
            cbgcolor(pfield[x][y1].c)
        else
            cfgcolor(pfield[x][y1].c)
            cbgcolor(pfield[x][y].c)
        end
        print(char(223))
    end
end

function play_loop()
    local time, c, e, ndir
    local running = true
    local food = 0
    local ret = 0
    local sval = (1 / speed) * 100

    snake.alength = 1
    snake.length = 2
    snake.head = 0
    snake.tail = 0
    snake.pos[0].x = snake.start.x
    snake.pos[0].y = snake.start.y
    snake.dir = snake.start.dir
    ndir = snake.dir
    time = timetick()

    while running do
        c = getkey()

        if c != null then
            if c == 27 then
                ret = -1
                running = false
            else
                switch char(c) do
                    case "8":
                        if snake.dir != 2 then
                            ndir = 0
                        end
                        break
                    case "6":
                        if snake.dir != 3 then
                            ndir = 1
                        end
                        break
                    case "2":
                        if snake.dir != 0 then
                            ndir = 2
                        end
                        break
                    case "4":
                        if snake.dir != 1 then
                            ndir = 3
                        end
                        break
                    case " ":
                        cfgcolor(14)
                        cbgcolor(colors.bkg)
                        center(12, "Paused")
                        do until getkey() == null
                        do until char(getkey()) == " "
                        draw_pfield()
                        time = timetick()
                        ndir = snake.dir
                        break
                end
            end
        end

        if timetick() - time > sval then
            snake.dir = ndir
            e = move_snake()

            if e == -1 then
                ret = -2
                running = false
            elseif e == 1 then
                food = food + 1
                set_food(false)
                snake.length = snake.length + food * 2
                score = score + 100 * speed
                update_score()
                if food == 20 then
                    running = false
                    ret = 1
                end
            end
            time = timetick()
            sleep(sval / 4)
        end
    end

    return ret
end

function move_snake()
    local x, y
    local is_food = false

    x = snake.pos[snake.head].x
    y = snake.pos[snake.head].y

    snake.head = mod(snake.head + 1, 4000)

    switch snake.dir do
        case 0:
            y = y - 1
            break
        case 1:
            x = x + 1
            break
        case 2:
            y = y + 1
            break
        case 3:
            x = x - 1
            break
    end

    if x < 0 then
        x = x + 80
    elseif x > 79 then
        x = x - 80
    end

    if y < 0 then
        y = y + 50
    elseif y > 49 then
        y = y - 50
    end

    if snake.alength == snake.length then
        set(snake.pos[snake.tail].x, snake.pos[snake.tail].y, colors.bkg)
        snake.tail = mod(snake.tail + 1, 4000)
    else
        snake.alength = snake.alength + 1
    end

    if pfield[x][y].c == colors.wall then
        return -1
    elseif pfield[x][y].c == colors.snake then
        return -1
    end

    if pfield[x][y].c == colors.food then
        is_food = true
    end

    set(x, y, colors.snake)

    snake.pos[snake.head].x = x
    snake.pos[snake.head].y = y

    if is_food then
        return 1
    else
        return 0
    end
end

sub set_food(start)
    local x, y

    if start then
        do
            x = rand(1, 79)
            y = rand(1, 49)
        until pfield[x][y].c == colors.bkg && x != snake.start.x && y != snake.start.y
    else
        do
            x = rand(1, 79)
            y = rand(1, 49)
        until pfield[x][y].c == colors.bkg
    end

    set(x, y, colors.food)
end

sub make_level(nr)
    local x, y
    make_border()
    switch nr do
        case 0:
            snake.start.x = 40
            snake.start.y = 24
            snake.start.dir = 3

            for x = 37, 42 do
                set(x, 0, colors.bkg)
                set(x, 49, colors.bkg)
            end
            for y = 22, 27 do
                set(0, y, colors.bkg)
                set(79, y, colors.bkg)
            end
            break
        case 1:
            snake.start.x = 40
            snake.start.y = 24
            snake.start.dir = 3
            for x = 15, 64 do
                set(x, 12, colors.wall)
                set(x, 37, colors.wall)
            end
            for x = 37, 42 do
                set(x, 0, colors.bkg)
                set(x, 49, colors.bkg)
            end
            for y = 22, 27 do
                set(0, y, colors.bkg)
                set(79, y, colors.bkg)
            end
            break
        case 2:
            snake.start.x = 40
            snake.start.y = 44
            snake.start.dir = 3
            for y = 10, 39 do
                set(15, y, colors.wall)
                set(32, y, colors.wall)
                set(48, y, colors.wall)
                set(64, y, colors.wall)
            end
            for x = 37, 42 do
                set(x, 0, colors.bkg)
                set(x, 49, colors.bkg)
            end
            for y = 22, 27 do
                set(0, y, colors.bkg)
                set(79, y, colors.bkg)
            end
            break
        case 3:
            snake.start.x = 40
            snake.start.y = 44
            snake.start.dir = 3
            for y = 10, 48 do
                set(10, y, colors.wall)
                set(30, y, colors.wall)
                set(50, y, colors.wall)
                set(70, y, colors.wall)
            end
            for y = 1, 39 do
                set(20, y, colors.wall)
                set(40, y, colors.wall)
                set(60, y, colors.wall)
            end
            for y = 22, 27 do
                set(0, y, colors.bkg)
                set(79, y, colors.bkg)
            end
            break
        case 4:
            snake.start.x = 40
            snake.start.y = 24
            snake.start.dir = 3
            for x = 10, 36 do
                set(x, 8, colors.wall)
                set(x, 41, colors.wall)
            end
            for x = 43, 69 do
                set(x, 8, colors.wall)
                set(x, 41, colors.wall)
            end
            for y = 9, 21 do
                set(10, y, colors.wall)
                set(69, y, colors.wall)
            end
            for y = 28, 41 do
                set(10, y, colors.wall)
                set(69, y, colors.wall)
            end
            for x = 37, 42 do
                set(x, 0, colors.bkg)
                set(x, 49, colors.bkg)
            end
            for y = 22, 27 do
                set(0, y, colors.bkg)
                set(79, y, colors.bkg)
            end
            break
    end
    set_food(true)
    draw_pfield()
end

sub cls()
    crcolors()
    cclear()
end

sub update_level()
    cfgcolor(14)
    cbgcolor(0)
    ccsrpos(10, 25)
    print("Level: "..(level + 1))
end

sub update_score()
    cfgcolor(14)
    cbgcolor(0)
    ccsrpos(60, 25)
    print("Score: "..score)
end

sub center(y, s)
    ccsrpos(floor(40 - (strlen(s) / 2)), y)
    print(s)
end

© by René 'Neotec' Jeschke in 2006, 2007
Last updated: 14 JUN 2007 (under construction)
SourceForge.net Logo