%DATEMADE:12/16/2017
%FUNCTION: To play snakes and ladders board game. There is your player tile and a cpu tile (yellow,red) first to the 100th tile wins, there are obstacles and power
%boosts on the way, ladders let you go up whille snakes push you down.
%COMPLETED PROJECTS: arrays, constants, integers, strings, graphics, booleen funtions, keyboard input, put, get, if/then/else, loop, exit when, for, procedure, bulletproof, header, 300 lines
%REMAINING PROJECTS: Real, comments, or, flickering, polishing
import GUI %GUI package
setscreen ("graphics:800;600;nobuttonbar") %screen 800*600 no buttonbar at the top
setscreen ("nocursor") %remove blinking cursor
%DECLARING VARIABLES AND ARRAYS-----------------------------------------------------------------------------------------------
var urname : string %varable where your name will be held
var font, font2, dice, newplayertile, newcputile, oldplayertile, oldcputile : int %font 1&2 are for graphics, newplayertile new cpu tile are for the array
newplayertile := 1 %set the players to square one
newcputile := 1 %set the players to square one
const radius := 10 %the radius of the player tile will always be 10 pixels
font := Font.New ("Palatino:35") %giving font a font to work with
font2 := Font.New ("serif:14") %another font for smaller things
var chars : array char of boolean %for keyboard input
var randomizer1, randomizer2 : int %for drawing the snakes on the board
type gamecoords : %a record for the array, keep track of the player and cpu x,y position at all times
record
playerx, playery, cpux, cpuy : int
end record
var board : array 1 .. 100 of gamecoords %the array for the board with all 100 squares
%END OF DECLARING VARIABLES AND ARRAYS----------------------------------------------------------------------------------------
%GREETING PLAYER, ASKING NAME--------------------------------------------------------------------------------------------------
var Playgame : int := GUI.CreateButtonFull (300, 300, 200, "Play Game", GUI.Quit, 100, 'p', true) %button to start the game
loop
exit when GUI.ProcessEvent
end loop
GUI.Dispose (Playgame)
put "Welcome to snakes and ladders! Press enter to move your player(yellow)" %rules of game
put "press escape to quit"
put "Land on a snake to slide down, land on a ladder to move up"
put "You are playing against the computer(red)"
put "First to land on tile 100 wins the game!"
put ""
put "What is your username?"
loop
get urname %get your name%get players username
exit when length (urname) <= 10 %username must be less than or eual to 10 characters
Draw.Text ("Name to long", 250, 375, font, 112) %if its not short enough tell the player to try again
end loop
cls %clear screen
for i : 1 .. length (urname) %from one to the amount of caracters your name is
locate (10 + i, 4 + (i * 4)) %start at (10,4) and move over 10 spaces for each letter, max of about 15 letters because of space constraints, preferable 1-7
put urname (i) %place one caracter of your name corresponding to the value of i in the located position
end for
Draw.Text ("WELCOME", 250, 375, font, 112) %welcome along with their name
delay (3000) %wait 3 seconds
cls %clear screen
%END OF GREETING PLAYER, ASKING NAME------------------------------------------------------------------------------------------
%DRAWBOARD AND ASSETS---------------------------------------------------------------------------------------------------------
procedure drawboard
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
drawfillbox (0, 0, maxx, maxy, 148) %drawing background
for i : 1 .. 10
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
drawfillbox (60 * i, 0, 60 * i + 5, maxy, black) %drawing vertical lines to seperate boxes, equation for x val is 60i while equation for x2 is 60i+5
end for
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
for i : 1 .. 10
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
drawfillbox (0, 60 * i, maxx, 60 * i + 5, black) %drawing horizontal lines to seperate tiles, y and y2 have the same equation as x and x2 (see above)
end for
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%SNAKES
drawfilloval (450, 250, 10, 18, black) %draw the heads of the snakes
drawfilloval (455, 259, 2, 2, 4)
drawfilloval (445, 259, 2, 2, 4)
drawfilloval (150, 550, 10, 18, black)
drawfilloval (155, 559, 2, 2, 4)
drawfilloval (145, 559, 2, 2, 4)
drawfilloval (150, 150, 10, 18, black)
drawfilloval (155, 159, 2, 2, 4)
drawfilloval (145, 159, 2, 2, 4)
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%SNAKE 48-30
for i : 0 .. 150
randint (randomizer1, 485, 495) %making the bodies of the snakes
randint (randomizer2, 250, 260)
drawarc (randomizer1, randomizer2, 40, 40, 195, 275, black) %making the bodies of the snakes
randint (randomizer1, 496, 510)
randint (randomizer2, 170, 180)
drawarc (randomizer1, randomizer2, 40, 40, 5, 90, black) %making the bodies of the snakes
randint (randomizer1, 535, 550)
drawline (555, 125, randomizer1, 180, black) %making the bodies of the snakes
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%SNAKE 93-65
randint (randomizer1, 185, 195)
randint (randomizer2, 550, 560)
drawarc (randomizer1, randomizer2, 40, 40, 195, 275, black) %making the bodies of the snakes
randint (randomizer1, 196, 210)
randint (randomizer2, 470, 480)
drawarc (randomizer1, randomizer2, 40, 40, 332, 90, black) %making the bodies of the snakes
randint (randomizer1, 225, 235)
randint (randomizer2, 410, 420)
drawarc (randomizer1, randomizer2, 40, 40, 75, 265, black) %making the bodies of the snakes
randint (randomizer2, 370, 380)
drawline (265, 375, 230, randomizer2, black) %making the bodies of the snakes
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%SNAKE 23-2
randint (randomizer1, 135, 145)
randint (randomizer2, 90, 100)
drawarc (randomizer1, randomizer2, 40, 40, 82, 195, black) %making the bodies of the snakes
randint (randomizer1, 60, 70)
randint (randomizer2, 66, 67)
drawarc (randomizer1, randomizer2, 40, 40, 276, 25, black) %making the bodies of the snakes
end for
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%PRINTING NUMBERS 1-100
for i : 0 .. 9 %numbers 1-10
Draw.Text (intstr (i + 1), 60 * i + 5, 45, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 10 .. 19 %numbers 11-20
Draw.Text (intstr (i + 1), 60 * (i - 10) + 5, 105, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 20 .. 29 %numbers 21-30
Draw.Text (intstr (i + 1), 60 * (i - 20) + 5, 165, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 30 .. 39 %numbers 31-40
Draw.Text (intstr (i + 1), 60 * (i - 30) + 5, 225, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 40 .. 49 %numbers 41-50
Draw.Text (intstr (i + 1), 60 * (i - 40) + 5, 285, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 50 .. 59 %numbers 51-60
Draw.Text (intstr (i + 1), 60 * (i - 50) + 5, 343, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 60 .. 69 %numbers 61-70
Draw.Text (intstr (i + 1), 60 * (i - 60) + 5, 405, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 70 .. 79 %numbers 71-80
Draw.Text (intstr (i + 1), 60 * (i - 70) + 5, 465, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 80 .. 89 %numbers 81-90
Draw.Text (intstr (i + 1), 60 * (i - 80) + 5, 525, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
for i : 90 .. 99 %numbers 91-100
Draw.Text (intstr (i + 1), 60 * (i - 90) + 5, 585, font2, 11) %put the number i once then move over (x-axis) 60 pixels each time
end for
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
View.Update %fix flickering (not working)
%LADDER 14-34
drawfillbox (205, 75, 210, 225, green) %make the boxes from square 14-34 to look like a ladder
drawfillbox (230, 75, 235, 225, green)
drawfillbox (205, 85, 235, 90, green)
drawfillbox (205, 115, 235, 120, green)
drawfillbox (205, 145, 235, 150, green)
drawfillbox (205, 175, 235, 180, green)
drawfillbox (205, 205, 235, 210, green) %make the boxes from square 14-34 to look like a ladder
View.Update %fix flickering (not working)
%LADDER 41-61
drawfillbox (25, 265, 30, 415, green) %make the boxes from square 41-61 to look like a ladder
drawfillbox (50, 265, 55, 415, green)
drawfillbox (25, 285, 55, 290, green)
drawfillbox (25, 315, 55, 320, green)
drawfillbox (25, 345, 55, 350, green)
drawfillbox (25, 375, 55, 380, green)
drawfillbox (25, 405, 55, 410, green) %make the boxes from square 41-61 to look like a ladder
View.Update %fix flickering (not working)
%LADDER 76-86
drawfillbox (325, 460, 330, 510, green) %make the boxes from square 76-86 to look like a ladder
drawfillbox (350, 460, 355, 510, green)
drawfillbox (325, 470, 355, 475, green)
drawfillbox (325, 490, 355, 495, green) %make the boxes from square 76-86 to look like a ladder
View.Update %fix flickering (not working)
end drawboard
%END OF DRAWING BOARD AND ASSETS--------------------------------------------------------
drawboard %draw the board
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
%INITIALIZING BOARD ARRAY---------------------------------------------------------------
for i : 1 .. 10
board (i).playerx := 60 * (i - 1) + 20 %setting the players x value for numbers 1-10 in board
board (i).playery := 25 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 1) + 35 %setting the cpu's x value for numbers 1-10 in board
board (i).cpuy := 30 %cpu's y value remains the same for each set of 10
end for
for i : 11 .. 20
board (i).playerx := 60 * (i - 11) + 20 %setting the players x value for numbers 11-20 in board
board (i).playery := 85 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 11) + 35 %setting the cpu's x value for numbers 11-20 in board
board (i).cpuy := 90 %cpu's y value remains the same for each set of 10
end for
for i : 21 .. 30
board (i).playerx := 60 * (i - 21) + 20 %setting the players x value for numbers 21-30 in board
board (i).playery := 145 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 21) + 35 %setting the cpu's x value for numbers 21-30 in board
board (i).cpuy := 150 %cpu's y value remains the same for each set of 10
end for
for i : 31 .. 40
board (i).playerx := 60 * (i - 31) + 20 %setting the players x value for numbers 31-40 in board
board (i).playery := 205 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 31) + 35 %setting the cpu's x value for numbers 31-40 in board
board (i).cpuy := 210 %cpu's y value remains the same for each set of 10
end for
for i : 41 .. 50
board (i).playerx := 60 * (i - 41) + 20 %setting the players x value for numbers 41-50 in board
board (i).playery := 265 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 41) + 35 %setting the cpu's x value for numbers 41-50 in board
board (i).cpuy := 270 %cpu's y value remains the same for each set of 10
end for
for i : 51 .. 60
board (i).playerx := 60 * (i - 51) + 20 %setting the players x value for numbers 51-60 in board
board (i).playery := 325 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 51) + 35 %setting the cpu's x value for numbers 51-60 in board
board (i).cpuy := 330 %cpu's y value remains the same for each set of 10
end for
for i : 61 .. 70
board (i).playerx := 60 * (i - 61) + 20 %setting the players x value for numbers 61-70 in board
board (i).playery := 385 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 61) + 35 %setting the cpu's x value for numbers 61-70 in board
board (i).cpuy := 390 %cpu's y value remains the same for each set of 10
end for
for i : 71 .. 80
board (i).playerx := 60 * (i - 71) + 20 %setting the players x value for numbers 71-81 in board
board (i).playery := 445 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 71) + 35 %setting the cpu's x value for numbers 71-80 in board
board (i).cpuy := 450 %cpu's y value remains the same for each set of 10
end for
for i : 81 .. 90
board (i).playerx := 60 * (i - 81) + 20 %setting the players x value for numbers 81-90 in board
board (i).playery := 505 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 81) + 35 %setting the cpu's x value for numbers 81-90 in board
board (i).cpuy := 510 %cpu's y value remains the same for each set of 10
end for
for i : 91 .. 100
board (i).playerx := 60 * (i - 91) + 20 %setting the players x value for numbers 91-100 in board
board (i).playery := 565 %players y value remains the same for each set of 10
board (i).cpux := 60 * (i - 91) + 35 %setting the cpu's x value for numbers 91-100 in board
board (i).cpuy := 570 %cpu's y value remains the same for each set of 10
end for
%END OF INITIALIZING BOARD ARRAY-----------------------------------------------------------------------------------------------------
%TELLING PLAYER THAT THEY WON------------------------------------------------------------------------------------------------------------------------
procedure win
cls %clear screen
Draw.Text ("YOU WON!", 200, 300, font, 148) %tell user they won
delay (3000) %wait
newcputile := 1 %reset game
newplayertile := 1
cls
drawboard %redraw board
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red) %redraw player tile
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow) %redraw cpu tile
end win
%END OF TELLING PLAYER THAT THEY WON-----------------------------------------------------------------------------------------------------------------
%TELLING PLAYER THAT THEY LOST----------------------------------------------------------------------------------------------------------------------
procedure loss
cls %clear screen
Draw.Text ("YOU LOST!", 200, 300, font, 148) %tell user they lost
delay (3000) %wait
newcputile := 1 %reset game
newplayertile := 1
cls
drawboard %redraw board
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red) %redraw player tile
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow) %redraw cpu tile
end loss
%END OF TELLING PLAYER THAT THEY LOST------------------------------------------------------------------------------------------------------------------
%MOVING THE CPU'S TILE---------------------------------------------------------------------------------------------------------------------------------
procedure movecpu
View.Update
drawfillbox (605, 0, maxx, maxy, 148)
cls
drawboard
View.Update
drawfillbox (605, 0, maxx, maxy, 148)
oldcputile := newcputile
randint (dice, 1, 6)
newcputile := newcputile + dice
if newcputile = 14 then
newcputile := 34
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 300, font, red)
delay (2000)
elsif newcputile = 23 then
newcputile := 2
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 300, font, red)
delay (2000)
elsif newcputile = 41 then
newcputile := 61
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 300, font, red)
delay (2000)
elsif newcputile = 48 then
newcputile := 30
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 300, font, red)
delay (2000)
elsif newcputile = 76 then
newcputile := 86
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 300, font, red)
delay (2000)
elsif newcputile = 93 then
newcputile := 65
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 300, font, red)
delay (2000)
elsif newcputile > 0 and newcputile > 100 then
newcputile := 100 - (dice - (100 - oldcputile))
end if
if newcputile = 100 then
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, 10, 10, yellow)
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, 10, 10, red)
delay (1000)
loss
end if
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
View.Update
drawfillbox (605, 0, maxx, maxy, 148)
Draw.Text ("CPU Rolled:", 605, 300, font2, red)
Draw.Text (intstr (dice), 620, 250, font, red)
View.Update
delay (1000)
end movecpu
%END OF MOVING CPU'S TILE----------------------------------------------------------------------------------------------------------------
%MOVING PLAYERS TILE----------------------------------------------------------------------------------------------------------------------
procedure moveplayer
cls
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
drawboard
View.Update
drawfillbox (605, 0, maxx, maxy, 148)
oldplayertile := newplayertile
randint (dice, 1, 6)
newplayertile := newplayertile + dice
if newplayertile = 14 then
newplayertile := 34
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile = 23 then
newplayertile := 2
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile = 41 then
newplayertile := 61
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile = 48 then
newplayertile := 30
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile = 76 then
newplayertile := 86
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("LADDER!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile = 93 then
newplayertile := 65
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
Draw.Text ("SNAKE!", 605, 100, font, yellow)
delay (2000)
elsif newplayertile > 0 and newplayertile > 100 then
newplayertile := 100 - (dice - (100 - oldplayertile))
end if
if newplayertile = 100 then
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, 10, 10, yellow)
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, 10, 10, red)
delay (1000)
win
end if
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, 10, 10, yellow)
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, 10, 10, red)
View.Update
drawfillbox (605, 0, maxx, maxy, 148)
Draw.Text (urname + " Rolled:", 605, 100, font2, yellow)
Draw.Text (intstr (dice), 620, 50, font, yellow)
delay (1000)
drawfillbox (605, 0, maxx, maxy, 148)
View.Update
movecpu
end moveplayer
%END OF MOVING PLAYERS TILE------------------------------------------------------------------------------------------------
%INITIALIZING PLAYER AND CPU POSITIONS-------------------------------------------------------------------------------------
drawfilloval (board (newcputile).cpux, board (newcputile).cpuy, radius, radius, red)
drawfilloval (board (newplayertile).playerx, board (newplayertile).playery, radius, radius, yellow)
%END OF INITIALIZING PLAYER AND CPU POSITIONS------------------------------------------------------------------------------
%QUIT THE GAME-------------------------------------------------------------------------------------------------------------
procedure leavegame
cls
Draw.Text ("GOODBYE, " + urname, 200, 300, font, 148)
delay (1000)
quit
end leavegame
%END OF QUITTING THE GAME--------------------------------------------------------------------------------------------------
%PROSSESING USER KEYBOARD INPUT-------------------------------------------------------------------------------------------
loop %when the user presses enter they will play a round, when they press escape they end the game
drawfillbox (605, 0, maxx, maxy, 148)
Input.KeyDown (chars)
if chars (KEY_ENTER) then
drawfillbox (605, 0, maxx, maxy, 148) %box covering the side
View.Update
moveplayer
elsif chars (KEY_ESC) then
leavegame
end if
end loop
%END OF PROSSESING USER KEYBOARD INPUT-----------------------------------------------------------------------------------
|