Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Michael Yazdani
- % ICS 3U1 Final Project
- View.Set ("graphics:800,600") % Sets output window to graphics mode (no copy/paste functionality) with a size of 800 by 600 pixels
- type vector : % Creates a custom type (used in variables) outlines in lines below
- record % Puts multiple variables in the type, accessed with variable.subvariable
- x : int
- y : int
- x2 : int
- y2 : int
- b : boolean
- end record
- var padv, padv2 : array 1 .. 10 of vector % Variable declarations
- var block : array 14 .. 18 of vector
- var key : array 19 .. 23 of vector
- var numv, oldv, charv : vector
- var map : array 1 .. 15, 1 .. 20 of int
- var readsuccessful, check : boolean
- var keycheck : int
- const keyleft := chr (203)
- const keyright := chr (205)
- const keydown := chr (208)
- const keyup := chr (200)
- var chars : string (1)
- function makev (y, x : int, b : boolean) : vector % Takes x and y values and an initial boolean value and creates a vector-type variable
- var v : vector
- v.y := y
- v.x := x
- v.b := b
- result v
- end makev
- function makepadv (v, v2 : vector) : vector % Takes 2 primary vectors and combines them
- var a : vector
- a.x := v2.x
- a.y := v2.y
- a.x2 := v.x
- a.y2 := v.y
- a.b := v.b
- result a
- end makepadv
- procedure readmap (filename : string, var okay : boolean) % Procedure to input a text file into an array to be read by drawmapsquare
- var numstream : int
- numv.y := 0
- numv.x := 0
- open : numstream, filename, get % Opens file
- if numstream > 0 then % Only continues if the file was able to be opened
- get : numstream, numv.y % Gets height and width
- get : numstream, numv.x
- for row : 1 .. numv.y
- for col : 1 .. numv.x
- get : numstream, map (row, col)
- for i : 1 .. 21 % If i matches the colour code, continue
- if i <= 5 then % First group of teleports
- if map (row, col) = i then
- padv (i) := makev (row, col, false)
- end if
- elsif i >= 9 and i <= 13 then % Second group of teleports
- if map (row, col) = i then
- padv (i - 3) := makev (row, col, false)
- end if
- elsif i > 13 then % Lock and key tiles
- if map (row, col) >= 14 and map (row, col) <= 18 then % Doors
- block (map (row, col)).y := row
- block (map (row, col)).x := col
- block (map (row, col)).b := false
- elsif map (row, col) >= 19 then % Keys
- key (map (row, col)).y := row
- key (map (row, col)).x := col
- key (map (row, col)).b := false
- end if
- end if
- end for
- end for
- end for
- close : numstream
- okay := true
- else
- put "error, file not readable"
- okay := false
- end if
- end readmap
- procedure drawmapsquare (v2 : vector, keynum : int) % draws a map tile based on the tile's colour code and various boolean variables
- var x, y : int
- x := (v2.x - 1) * 40
- y := (maxy - 40) - (v2.y - 1) * 40
- if keynum > 0 then
- if map (v2.y, v2.x) = keynum + 5 and key(keynum + 5).b = false then % if the tile is a key and that key is not picked up
- Draw.FillBox (x, y, x + 39, y + 39, 000)
- drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
- elsif map (v2.y, v2.x) = keynum + 5 and key (keynum + 5).b = true then % if the tile is a key and that key is picked up
- Draw.FillBox (x, y, x + 39, y + 39, 000)
- elsif map (v2.y, v2.x) = 007 and block (keynum).b = false then % if the tile is a block and that block's key is not picked up
- map (v2.y, v2.x) := 007
- Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 24 and block (keynum).b = true then % if the tile is a block and that blocks' key is picked up
- Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
- drawfillstar (x + 5, y + 5, x + 35, y + 35, 000)
- end if
- else
- if map (v2.y, v2.x) >= 19 and map (v2.y, v2.x) <= 23 and key(19).b = false then
- Draw.FillBox (x, y, x + 39, y + 39, 000)
- drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
- elsif map (v2.y, v2.x) >= 19 and map (v2.y, v2.x) <= 23 and key (19).b = true then
- Draw.FillBox (x, y, x + 39, y + 39, 000)
- elsif map (v2.y, v2.x) >= 14 and map (v2.y, v2.x) <= 18 and block (14).b = false then
- map (v2.y, v2.x) := 007
- Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 24 and block (14).b = true then
- Draw.FillBox (x, y, x + 39, y + 39, map (v2.y, v2.x))
- drawfillstar (x + 5, y + 5, x + 35, y + 35, 000)
- elsif map (v2.y, v2.x) = 7 then
- Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 5 or map (v2.y, v2.x) = 13 then
- Pic.ScreenLoad ("purple.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 4 or map (v2.y, v2.x) = 12 then
- Pic.ScreenLoad ("red.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 3 or map (v2.y, v2.x) = 11 then
- Pic.ScreenLoad ("lblue.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 2 or map (v2.y, v2.x) = 10 then
- Pic.ScreenLoad ("green.bmp", x, y, picCopy)
- elsif map (v2.y, v2.x) = 1 or map (v2.y, v2.x) = 9 then
- Pic.ScreenLoad ("blue.bmp", x, y, picCopy)
- else
- Draw.FillBox (x, y, x + 39, y + 39, map (v2.y, v2.x))
- end if
- end if
- end drawmapsquare
- procedure drawmap % Draws the map
- var v1, v4 : vector
- v1.x := 0
- v1.y := maxy - 40
- for row : 1 .. numv.y
- for col : 1 .. numv.x
- v4.x := col
- v4.y := row
- drawmapsquare (v4, 0)
- v1.x := v1.x + 40
- end for
- v1.y := v1.y - 40
- v1.x := 0
- end for
- end drawmap
- procedure drawchar (row, col, keycheck : int) % Draws the character based on wether or not a key is active
- var x, y : int
- x := (col - 1) * 40
- y := (maxy - 40) - (row - 1) * 40
- if keycheck not= 0 then
- if key (keycheck + 5).b = true then
- drawfilloval (x + 19, y + 19, 19, 19, 8)
- drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
- end if
- else
- drawfilloval (x + 19, y + 19, 19, 19, 8)
- end if
- end drawchar
- procedure teleport (pad : int) % Sets character position based on teleport array
- padv (pad).b := true
- if pad < 6 then
- charv := padv (pad + 5)
- elsif pad >= 6 then
- charv := padv (pad - 5)
- end if
- end teleport
- readmap ("map.t", readsuccessful)
- if readsuccessful then
- for i : 1 .. 10 % Creates 2-point vectors for teleport pads
- if i <= 5 then
- padv2 (i) := makepadv (padv (i + 5), padv (i))
- elsif i >= 6 then
- padv2 (i) := makepadv (padv (i - 5), padv (i))
- end if
- end for
- for i : 1 .. 10
- padv (i) := padv2 (i)
- padv (i).b := false % Sets all pads to false
- end for
- for i : 19 .. 23
- key (i).b := false % Sets all keys to false
- end for
- for i : 14 .. 18
- block (i).b := false % Sets all doors to false
- end for
- drawmap
- charv.y := 2 % Sets player starting position
- charv.x := 2
- keycheck := 0
- oldv.b := true
- drawchar (charv.y, charv.x, 0) % Draws character
- loop % Main loop
- getch (chars)
- drawmapsquare (charv, 0) % Redraws square at previous location
- oldv := charv % Sets old vector to current vector
- if chars = keyleft then % Control structure
- charv.x := charv.x - 1
- if map (charv.y, charv.x) = 007 then
- charv.x := oldv.x
- end if
- elsif chars = keyright then
- charv.x := charv.x + 1
- if map (charv.y, charv.x) = 007 then
- charv.x := oldv.x
- end if
- elsif chars = keydown then
- charv.y := charv.y + 1
- if map (charv.y, charv.x) = 007 then
- charv.y := oldv.y
- end if
- elsif chars = keyup then
- charv.y := charv.y - 1
- if map (charv.y, charv.x) = 007 then
- charv.y := oldv.y
- end if
- end if
- for i : 1 .. 10
- if charv.x = padv (i).x and charv.y = padv (i).y and padv (i).b = false then
- teleport (i) % If current position matches a teleport pad, call teleport with appropriate pad ID
- exit
- else
- for o : 1 .. 10
- padv (o).b := false
- end for
- end if
- end for
- for i : 14 .. 18
- if map (charv.y, charv.x) = i + 5 and key (i + 5).b = false then % If current location is a key and not active, pick up key and set to active, and redraw associated door as open
- block (i).b := true
- map (block (i).y, block (i).x) := 024
- drawmapsquare (block (i), i)
- drawmapsquare (charv, 0)
- key (i + 5).b := true
- keycheck := i
- exit
- elsif map (charv.y, charv.x) = i + 5 and key (i + 5).b = true then % If current location is a key and active, drop key and set to inactive, and redraw associated door as closed
- block (i).b := false
- map (block (i).y, block (i).x) := 007
- drawmapsquare (block (i), i)
- key (i + 5).b := false
- keycheck := 0
- exit
- elsif map (oldv.y, oldv.x) = 024 and key (i + 5).b = true then % If previous location is a block and was open, redraw as closed and reset key to inactive
- block (i).b := false
- map (block (i).y, block (i).x) := 007
- drawmapsquare (block (i), i)
- key (i + 5).b := false
- keycheck := 0
- drawmapsquare (key (i + 5), i)
- exit
- elsif map (oldv.y, oldv.x) = i + 5 and key (i + 5).b = true then % If previous location is that of an active key, do not draw it
- key (i + 5).b := true
- drawmapsquare (key (i + 5), i)
- end if
- end for
- drawchar (charv.y, charv.x, keycheck) % Redraw character at new location
- end loop
- end if
Advertisement
Add Comment
Please, Sign In to add comment