Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Snake Game.
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
araevin




PostPosted: Fri May 19, 2006 1:49 pm   Post subject: Snake Game.

The posison food is meant to be simmilar to normal food because there is only one and look for powerups. The blick is a wall. Very Happy

code:
%%%%%%%%%%%
%Variables%
%%%%%%%%%%%
%%%% Variables Used in This program %%%%
View.Set ("nocursor,noecho,graphics,msdos :500;500")
setscreen ("msdos")
var ch, oldch : string (1) := " "
var ara : int := 1000
var foodcount : int := 0
var lifese, face, tail, iflife, shadow, dfood, foodcol, foodrow, food, bar, powers, lvlpwr : int
var oldrow : int := 1
var oldcol : int := 1
var count : int := 5
var choice, name : string
var tailrow : array 1 .. ara of int
var tailcol : array 1 .. ara of int
var score2 : int := 1
var lol : string
var lvlrow, lvlcol, lirow, licol, row, col, poiscol, poisrow, barcol, barrow, powrow, powcol, tailrow2, tailcol2, shaderow, shadecol : int := Rand.Int (1, 24)
var score : int := foodcount
var font1, font4 : int
var level : int := 1
%%%% Defined Variables %%%%
shadow := 231
lvlpwr := 234
lifese := 251
row := maxrow div 2
col := maxcol div 2
powrow := 1
powcol := 1
bar := 219
face := 233
food := 203
tail := 236
dfood := 202
powers := 240
oldrow := row
oldcol := col
locate (row, col)
put chr (face)
for i : 1 .. ara
    tailrow (i) := 1
    tailcol (i) := 1
end for
font1 := Font.New ("serif:14:bold")
assert font1 > 0
font4 := Font.New ("Arial:24:bold,italic")
assert font4 > 0
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function shde : boolean
    if shaderow = row and shadecol = col then
        result true
    else
        result false
    end if
end shde
function lvlpwr2 : boolean
    if level = 3 and count < 2 and foodcount > 12 then
        result true
    else
        result false
    end if
end lvlpwr2
function lvl : boolean
    if foodcount = 15 then
        result true
    else
        result false
    end if
end lvl
function crash : boolean
    if row = 0 or col = 0 or row = maxrow or col = maxcol then
        result true
    else
        result false
    end if
end crash
function gain : boolean
    if lirow = row and licol = col then
        result true
    else
        result false
    end if
end gain
function poison : boolean
    if poisrow = row and poiscol = col then
        result true
    else
        result false
    end if
end poison
function powerup : boolean
    if powrow = row and powcol = col then
        result true
    else
        result false
    end if
end powerup

function barrier : boolean
    if row = barrow and col = barcol then
        result true
    else
        result false
    end if
end barrier
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% Procedures %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%Put chr procidures %%%%%
proc putlvlpwr
    locate (lvlrow, lvlcol)
    put chr (lvlpwr)
end putlvlpwr
proc putdeath
    locate (poisrow, poiscol)
    put chr (dfood) ..
end putdeath
proc putbar
    locate (barrow, barcol)
    put chr (bar) ..
end putbar
proc putfood
    locate (foodrow, foodcol)
    put chr (food) ..
end putfood
proc putlife
    locate (lirow, licol)
    put chr (lifese)
end putlife
proc putface
    locate (row, col)
    put chr (face) ..
end putface
%%%% Main Game procedures %%%%
proc shade
    if level = 2 and foodcount = 8 then
        locate (shaderow, shadecol)
        put chr (shadow)
        if shde = true then
            count := 1
            foodcount := 14
            locate (maxrow div 2, (maxcol div 2) div 2)
            cls
            put "You got the shadow upgrade life = 1 , score = 14"
            delay (800)
            cls
            shaderow := Rand.Int (0 + 2, maxrow - 2)
            shadecol := Rand.Int (1, 1)
        end if
    end if
end shade
proc levelcolour
    if level = 1 then
        Draw.FillBox (0, 0, maxx, maxy, black)
        colourback (black)
        put ""
    elsif level = 2 then
        Draw.FillBox (0, 0, maxx, maxy, red)
        colourback (red)
        put ""
    elsif level = 3 then
        Draw.FillBox (0, 0, maxx, maxy, green)
        colourback (green)
        put ""
    elsif level = 4 then
        Draw.FillBox (0, 0, maxx, maxy, blue)
        colourback (blue)
        put ""
    elsif level = 5 then
        Draw.FillBox (0, 0, maxx, maxy, purple)
        colourback (purple)
        put ""
    else
        Draw.FillBox (0, 0, maxx, maxy, 260)
        colourback (260)
        put ""
    end if
end levelcolour
proc lvl3
    if lvlpwr2 = true then
        putlvlpwr
        if lvlrow = row and lvlcol = col then
            count := count + 5
            foodcount := foodcount + 3
            cls
            locate (maxrow div 2, maxcol div 2)
            put "You got a secret lvl up!"
            delay (800)
            cls
        end if
    end if
end lvl3
proc levelup
    level := level + 1
    locate (maxrow div 2, maxcol div 2)
    delay (100)
    cls
    put "You beat this level!"
    delay (1000)
    cls
    locate (maxrow div 2, maxcol div 2)
    put name, " You got to level ", level, "!"
    locate (maxrow div 2 + 2, maxcol div 2)
    put "To continue press Y to Quit press N."
    get lol
    if lol = "n" then
        level := 1
        quit
    else
        foodcount := 0
        count := 5
        levelcolour
        cls
    end if
end levelup
proc tails
    colour (Rand.Int (34, 103))
    score := foodcount
    score := score * 2
    locate (tailrow (score2), tailcol (score2))
    put " " ..
    for decreasing i : score .. 2
        tailrow (i) := tailrow (i - 1)
        tailcol (i) := tailcol (i - 1)
    end for
    tailrow (1) := oldrow
    tailcol (1) := oldcol
    for i : 1 .. score
        locate (tailrow (i), tailcol (i))
        put chr (tail) ..
    end for
end tails
proc gains
    if count = 1 and foodcount > 5 then
        putlife
        if gain = true then
            count := count + 5
            locate (maxrow div 2, maxcol div 2)
            put "Life Up! +1 "
            delay (500)
            cls
            licol := Rand.Int (1, 3)
            lirow := Rand.Int (1, 3)
        end if
    end if
end gains
proc pwer
    if foodcount = 10 then
        locate (powrow, powcol)
        put chr (powers)
        if powerup = true and foodcount = 10 then
            foodcount := foodcount + 3
            locate (maxrow div 2, maxcol div 2)
            put name, " You found a Power Up!"
            delay (500)
            cls
        else
        end if
    end if
end pwer
proc endlife
    if level = 1 then
        put "Your score was ", foodcount
    elsif level = 1 then
        put "Your score was ", 15 + foodcount
    elsif level = 2 then
        put "Your score was ", 30 + foodcount
    elsif level = 3 then
        put "Your score was ", 45 + foodcount
    elsif level = 4 then
        put "Your score was ", 60 + foodcount
    elsif level = 5 then
        put "Your score was ", 75 + foodcount
    elsif level = 6 then
        put "Your score was ", 90 + foodcount
    end if
    put "Would you like to play again?"
    get choice
    cls
    if choice = "n" then
        put "Thank you for playing ", name
        delay (1000)
        quit
    else
        foodcount := 0
        level := 1
        count := 5
    end if
end endlife
proc poisonfood
    putdeath
    if poison = true then
        count := count - 1
        locate (maxrow div 2, maxcol div 2 - 5)
        put "You ate poison food! - 1 life"
        if count = 0 then
            cls
            endlife
        end if
        delay (500)
        cls
        poiscol := Rand.Int (5, maxcol - 5)
        poisrow := Rand.Int (5, maxrow - 5)
    else
    end if
end poisonfood
proc putabar
    if barrier = true then
        count := count - 1
        locate (maxrow div 2, maxcol div 2 - 12)
        put "You hit an obstacle! - 1 life."
        if count = 0 then
            cls
            endlife
        end if
        barrow := Rand.Int (3, maxrow - 3)
        barcol := Rand.Int (3, maxcol - 3)
        delay (1000)
        cls
        put chr (face) ..
        put chr (bar) ..
    end if
end putabar
proc tailcrash
    if foodcount = 1 then
        score := 2
    else
        score := foodcount * 2
    end if
    for i : 1 .. score
        if row = tailrow (i) and col = tailcol (i) then
            cls
            put "you hit your tail ", name, " you fool!"
            count := count - 1
            row := maxrow div 2
            col := maxcol div 2
            delay (800)
            cls
            if count = 0 then
            endlife
            end if
            end if
    end for
end tailcrash
%%Movement procedure%%
proc move
    delay (50)
    color (Rand.Int (35, 103))
    locate (row, col)
    color (Rand.Int (35, 103))
    put " " ..
    oldrow := row
    oldcol := col
    tails
    %tailcrash
    if ord (ch) = 200 then
        row := row - 1
        oldch := ch
    elsif ord (ch) = 208 then
        row := row + 1
        oldch := ch
    elsif ord (ch) = 205 then
        col := col + 1
        oldch := ch
    elsif ord (ch) = 203 then
        col := col - 1
        oldch := ch
    else
        ch := oldch
    end if
    levelcolour
    pwer
    putabar
    poisonfood
    gains
    tails
    tailcrash
    shade
    locate (1, 1)
    colour (Rand.Int (34, 103))
    put "Life left ", count, " Score ", foodcount, " level ", level
end move
%% The two central loops %%
proc main
    loop
        oldch := " "
        tails
        powrow := Rand.Int (1, 2)
        powcol := Rand.Int (maxcol - 2, maxcol - 1)
        poiscol := Rand.Int (5, maxcol - 5)
        poisrow := Rand.Int (5, maxrow - 5)
        licol := Rand.Int (1, 3)
        lirow := Rand.Int (1, 3)
        lvlrow := Rand.Int (2, maxrow - 2)
        lvlcol := Rand.Int (maxcol - 3, maxcol - 1)
        shaderow := Rand.Int (0 + 2, maxrow - 2)
        shadecol := Rand.Int (1, 1)
        loop
            putfood
            putbar
            color (Rand.Int (35, 103))
            setscreen ("graphics")
            if hasch then
                getch (ch)
            end if
            move
            if crash = true then
                cls
                count := count - 1
                locate (maxrow div 2, maxcol div 2 - 10)
                put "You crashed and lost a life. ", count, " lives left"
                row := maxrow div 2
                col := maxcol div 2
                delay (2000)
                cls
                if count = 0 then
                    cls
                    endlife
                end if
            elsif row = foodrow and col = foodcol then
                foodcount := foodcount + 1
                locate (1, 1)
                put "Score ", foodcount
                colourback (black)
                foodrow := Rand.Int (2, maxrow - 1)
                foodcol := Rand.Int (2, maxcol - 1)
                putfood
                putbar
                if lvl = true then
                    levelup
                end if
            else
                putface
            end if
        end loop
    end loop
    oldch := " "
end main
%% This is the secondary screen %%
proc menu
    cls
    colour (brightgreen)
    locate (2, (maxcol div 2) div 4)
    put "Here are the rules for Nibbles"
    delay (300)
    locate (3, (maxcol div 2) div 4)
    put " use your arrow keys to move "
    delay (300)
    locate (4, (maxcol div 2) div 4)
    put " dont hit the walls or you loose a life "
    delay (300)
    locate (5, (maxcol div 2) div 4)
    put " the thick bar is an obstacle Dont hit it !"
    delay (300)
    locate (6, (maxcol div 2) div 4)
    put " the T shape is food Eat that to get points "
    delay (300)
    locate (7, (maxcol div 2) div 4)
    put " the upside down T shape is poison food Dont eat it!"
    delay (300)
    locate (8, (maxcol div 2) div 4)
    put " the 3 dashes shape is a power up Try to get one!"
    delay (300)
    locate (9, (maxcol div 2) div 4)
    put " the V shape gives you 5 extra life ! find that!"
    delay (300)
    locate (10, (maxcol div 2) div 4)
    put " there are also alot of different powerups, look for wierd shapes!"
    delay (300)
    locate (11, (maxcol div 2) div 4)
    put " to continue to the game peress Y."
    put " "
    get lol
    if lol = "n" then
        quit
    else
    end if
end menu
%% first proc "start" %%
proc start
    foodrow := Rand.Int (2, maxrow - 1)
    foodcol := Rand.Int (2, maxcol - 1)
    shaderow := Rand.Int (0 + 2, maxrow - 2)
    shadecol := Rand.Int (1, 1)
    levelcolour
    color (Rand.Int (35, 200))
    Font.Draw ("Welcome to Nibbles!", 150, 300, font4, Rand.Int (35, 103))
    Font.Draw ("Press Y if you want to play N if you want to Quit", 150, 130, font1, Rand.Int (35, 103))
    locate (24, 25)
    get lol
    if lol = "n" then
        quit
    else
        cls
        Font.Draw ("Welcome to Nibbles!", 150, 300, font4, Rand.Int (35, 103))
        Font.Draw ("please enter your first name.", 150, 130, font1, Rand.Int (35, 103))
        get name
        menu
    end if
    cls
    cls
    color (Rand.Int (35, 103))
end start
%%%%%%%%%%
%MAINLINE%
%%%%%%%%%%
loop
    start
    putfood
    main
end loop
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: