yazdmich

Game Finished

Jan 22nd, 2013
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.45 KB | None | 0 0
  1. % Michael Yazdani
  2. % ICS 3U1 Final Project
  3. View.Set ("graphics:800,600") % Sets output window to graphics mode (no copy/paste functionality) with a size of 800 by 600 pixels
  4. type vector : % Creates a custom type (used in variables) outlines in lines below
  5. record % Puts multiple variables in the type, accessed with variable.subvariable
  6. x : int
  7. y : int
  8. x2 : int
  9. y2 : int
  10. b : boolean
  11. end record
  12. var padv, padv2 : array 1 .. 10 of vector % Variable declarations
  13. var block : array 14 .. 18 of vector
  14. var key : array 19 .. 23 of vector
  15. var numv, oldv, charv : vector
  16. var map : array 1 .. 15, 1 .. 20 of int
  17. var readsuccessful, check : boolean
  18. var keycheck : int
  19. const keyleft := chr (203)
  20. const keyright := chr (205)
  21. const keydown := chr (208)
  22. const keyup := chr (200)
  23. var chars : string (1)
  24.  
  25. function makev (y, x : int, b : boolean) : vector % Takes x and y values and an initial boolean value and creates a vector-type variable
  26. var v : vector
  27. v.y := y
  28. v.x := x
  29. v.b := b
  30. result v
  31. end makev
  32.  
  33. function makepadv (v, v2 : vector) : vector % Takes 2 primary vectors and combines them
  34. var a : vector
  35. a.x := v2.x
  36. a.y := v2.y
  37. a.x2 := v.x
  38. a.y2 := v.y
  39. a.b := v.b
  40. result a
  41. end makepadv
  42.  
  43. procedure readmap (filename : string, var okay : boolean) % Procedure to input a text file into an array to be read by drawmapsquare
  44. var numstream : int
  45. numv.y := 0
  46. numv.x := 0
  47. open : numstream, filename, get % Opens file
  48. if numstream > 0 then % Only continues if the file was able to be opened
  49. get : numstream, numv.y % Gets height and width
  50. get : numstream, numv.x
  51. for row : 1 .. numv.y
  52. for col : 1 .. numv.x
  53. get : numstream, map (row, col)
  54. for i : 1 .. 21 % If i matches the colour code, continue
  55. if i <= 5 then % First group of teleports
  56. if map (row, col) = i then
  57. padv (i) := makev (row, col, false)
  58. end if
  59. elsif i >= 9 and i <= 13 then % Second group of teleports
  60. if map (row, col) = i then
  61. padv (i - 3) := makev (row, col, false)
  62. end if
  63. elsif i > 13 then % Lock and key tiles
  64. if map (row, col) >= 14 and map (row, col) <= 18 then % Doors
  65. block (map (row, col)).y := row
  66. block (map (row, col)).x := col
  67. block (map (row, col)).b := false
  68.  
  69. elsif map (row, col) >= 19 then % Keys
  70. key (map (row, col)).y := row
  71. key (map (row, col)).x := col
  72. key (map (row, col)).b := false
  73. end if
  74. end if
  75. end for
  76. end for
  77. end for
  78. close : numstream
  79. okay := true
  80. else
  81. put "error, file not readable"
  82. okay := false
  83. end if
  84. end readmap
  85.  
  86. procedure drawmapsquare (v2 : vector, keynum : int) % draws a map tile based on the tile's colour code and various boolean variables
  87. var x, y : int
  88. x := (v2.x - 1) * 40
  89. y := (maxy - 40) - (v2.y - 1) * 40
  90. if keynum > 0 then
  91. 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
  92. Draw.FillBox (x, y, x + 39, y + 39, 000)
  93. drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
  94. 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
  95. Draw.FillBox (x, y, x + 39, y + 39, 000)
  96. 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
  97. map (v2.y, v2.x) := 007
  98. Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
  99. 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
  100. Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
  101. drawfillstar (x + 5, y + 5, x + 35, y + 35, 000)
  102. end if
  103. else
  104. if map (v2.y, v2.x) >= 19 and map (v2.y, v2.x) <= 23 and key(19).b = false then
  105. Draw.FillBox (x, y, x + 39, y + 39, 000)
  106. drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
  107. elsif map (v2.y, v2.x) >= 19 and map (v2.y, v2.x) <= 23 and key (19).b = true then
  108. Draw.FillBox (x, y, x + 39, y + 39, 000)
  109. elsif map (v2.y, v2.x) >= 14 and map (v2.y, v2.x) <= 18 and block (14).b = false then
  110. map (v2.y, v2.x) := 007
  111. Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
  112. elsif map (v2.y, v2.x) = 24 and block (14).b = true then
  113. Draw.FillBox (x, y, x + 39, y + 39, map (v2.y, v2.x))
  114. drawfillstar (x + 5, y + 5, x + 35, y + 35, 000)
  115. elsif map (v2.y, v2.x) = 7 then
  116. Pic.ScreenLoad ("wall.bmp", x, y, picCopy)
  117. elsif map (v2.y, v2.x) = 5 or map (v2.y, v2.x) = 13 then
  118. Pic.ScreenLoad ("purple.bmp", x, y, picCopy)
  119. elsif map (v2.y, v2.x) = 4 or map (v2.y, v2.x) = 12 then
  120. Pic.ScreenLoad ("red.bmp", x, y, picCopy)
  121. elsif map (v2.y, v2.x) = 3 or map (v2.y, v2.x) = 11 then
  122. Pic.ScreenLoad ("lblue.bmp", x, y, picCopy)
  123. elsif map (v2.y, v2.x) = 2 or map (v2.y, v2.x) = 10 then
  124. Pic.ScreenLoad ("green.bmp", x, y, picCopy)
  125. elsif map (v2.y, v2.x) = 1 or map (v2.y, v2.x) = 9 then
  126. Pic.ScreenLoad ("blue.bmp", x, y, picCopy)
  127. else
  128. Draw.FillBox (x, y, x + 39, y + 39, map (v2.y, v2.x))
  129. end if
  130. end if
  131. end drawmapsquare
  132.  
  133. procedure drawmap % Draws the map
  134. var v1, v4 : vector
  135.  
  136. v1.x := 0
  137. v1.y := maxy - 40
  138. for row : 1 .. numv.y
  139. for col : 1 .. numv.x
  140. v4.x := col
  141. v4.y := row
  142. drawmapsquare (v4, 0)
  143. v1.x := v1.x + 40
  144. end for
  145. v1.y := v1.y - 40
  146. v1.x := 0
  147. end for
  148.  
  149. end drawmap
  150.  
  151. procedure drawchar (row, col, keycheck : int) % Draws the character based on wether or not a key is active
  152. var x, y : int
  153. x := (col - 1) * 40
  154. y := (maxy - 40) - (row - 1) * 40
  155. if keycheck not= 0 then
  156. if key (keycheck + 5).b = true then
  157. drawfilloval (x + 19, y + 19, 19, 19, 8)
  158. drawfillstar (x + 5, y + 5, x + 35, y + 35, 44)
  159. end if
  160. else
  161. drawfilloval (x + 19, y + 19, 19, 19, 8)
  162. end if
  163. end drawchar
  164.  
  165. procedure teleport (pad : int) % Sets character position based on teleport array
  166. padv (pad).b := true
  167. if pad < 6 then
  168. charv := padv (pad + 5)
  169. elsif pad >= 6 then
  170. charv := padv (pad - 5)
  171. end if
  172. end teleport
  173.  
  174. readmap ("map.t", readsuccessful)
  175. if readsuccessful then
  176. for i : 1 .. 10 % Creates 2-point vectors for teleport pads
  177. if i <= 5 then
  178. padv2 (i) := makepadv (padv (i + 5), padv (i))
  179. elsif i >= 6 then
  180. padv2 (i) := makepadv (padv (i - 5), padv (i))
  181. end if
  182. end for
  183.  
  184. for i : 1 .. 10
  185. padv (i) := padv2 (i)
  186. padv (i).b := false % Sets all pads to false
  187. end for
  188.  
  189. for i : 19 .. 23
  190. key (i).b := false % Sets all keys to false
  191. end for
  192.  
  193. for i : 14 .. 18
  194. block (i).b := false % Sets all doors to false
  195. end for
  196.  
  197. drawmap
  198. charv.y := 2 % Sets player starting position
  199. charv.x := 2
  200. keycheck := 0
  201. oldv.b := true
  202. drawchar (charv.y, charv.x, 0) % Draws character
  203. loop % Main loop
  204. getch (chars)
  205. drawmapsquare (charv, 0) % Redraws square at previous location
  206. oldv := charv % Sets old vector to current vector
  207. if chars = keyleft then % Control structure
  208. charv.x := charv.x - 1
  209. if map (charv.y, charv.x) = 007 then
  210. charv.x := oldv.x
  211. end if
  212. elsif chars = keyright then
  213. charv.x := charv.x + 1
  214. if map (charv.y, charv.x) = 007 then
  215. charv.x := oldv.x
  216. end if
  217. elsif chars = keydown then
  218. charv.y := charv.y + 1
  219. if map (charv.y, charv.x) = 007 then
  220. charv.y := oldv.y
  221. end if
  222. elsif chars = keyup then
  223. charv.y := charv.y - 1
  224. if map (charv.y, charv.x) = 007 then
  225. charv.y := oldv.y
  226. end if
  227. end if
  228. for i : 1 .. 10
  229. if charv.x = padv (i).x and charv.y = padv (i).y and padv (i).b = false then
  230. teleport (i) % If current position matches a teleport pad, call teleport with appropriate pad ID
  231. exit
  232. else
  233. for o : 1 .. 10
  234. padv (o).b := false
  235. end for
  236. end if
  237. end for
  238.  
  239. for i : 14 .. 18
  240. 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
  241. block (i).b := true
  242. map (block (i).y, block (i).x) := 024
  243. drawmapsquare (block (i), i)
  244. drawmapsquare (charv, 0)
  245. key (i + 5).b := true
  246. keycheck := i
  247. exit
  248. 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
  249. block (i).b := false
  250. map (block (i).y, block (i).x) := 007
  251. drawmapsquare (block (i), i)
  252. key (i + 5).b := false
  253. keycheck := 0
  254. exit
  255. 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
  256. block (i).b := false
  257. map (block (i).y, block (i).x) := 007
  258. drawmapsquare (block (i), i)
  259. key (i + 5).b := false
  260. keycheck := 0
  261. drawmapsquare (key (i + 5), i)
  262. exit
  263. 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
  264. key (i + 5).b := true
  265. drawmapsquare (key (i + 5), i)
  266. end if
  267. end for
  268.  
  269. drawchar (charv.y, charv.x, keycheck) % Redraw character at new location
  270. end loop
  271. end if
Advertisement
Add Comment
Please, Sign In to add comment