Advertisement
Mesaif

TrafficLights V1

May 12th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. %Program that draws a street and works traffic lights
  2.  
  3.  
  4. %SCREEN SET UP%
  5. setscreen ("graphics:vga")
  6. View.Set ("graphics: 1000;1000")
  7. %SCREEN SET UP%
  8.  
  9. %VARIABLES%
  10. var statusI : string
  11. var RstatusI : string := "off"
  12. var YstatusI : string := "off"
  13. var GstatusI : string := "off"
  14.  
  15. var statusII : string
  16. var RstatusII : string := "off"
  17. var YstatusII : string := "off"
  18. var GstatusII : string := "off"
  19.  
  20. var debug : int := 0 %DEBUG VARIABLE%
  21.  
  22. var commands : string := "tl: test lights, cls: clear text, rtn: begin routine"
  23.  
  24.  
  25.  
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27.  
  28.  
  29.  
  30. procedure drawStreet %Draw the street
  31.  
  32. %Background
  33. drawbox (0, 0, 800, 800, black)
  34.  
  35. %Horizontal Lines
  36. drawline (0, 350, 350, 350, black)
  37. drawline (0, 450, 350, 450, black)
  38.  
  39. drawline (450, 350, 800, 350, black)
  40. drawline (450, 450, 800, 450, black)
  41.  
  42.  
  43. %Vertical Lines
  44. drawline (350, 800, 350, 450, black)
  45. drawline (350, 0, 350, 350, black)
  46.  
  47. drawline (450, 800, 450, 450, black)
  48. drawline (450, 0, 450, 350, black)
  49.  
  50.  
  51. %Street Lines
  52. drawline (400, 800, 400, 0, yellow)
  53. drawline (0, 400, 800, 400, yellow)
  54.  
  55. %MIDDLE WHITE
  56. drawfillbox (350, 350, 450, 450, white)
  57.  
  58. end drawStreet
  59.  
  60. procedure drawLights %Procedure to draw ovals for lights that can take input to fill
  61.  
  62. %%%%SET I%%%%
  63. %TOP%
  64. drawoval (325, 515, 10, 10, black)
  65. if RstatusI = "on" then
  66. drawfill (325, 515, 12, black)
  67. elsif RstatusI = "off" then
  68. drawfill (325, 515, white, black)
  69. end if
  70.  
  71. drawoval (325, 495, 10, 10, black)
  72. if YstatusI = "on" then
  73. drawfill (325, 495, yellow, black)
  74. elsif YstatusI = "off" then
  75. drawfill (325, 495, white, black)
  76. end if
  77.  
  78. drawoval (325, 475, 10, 10, black)
  79. if GstatusI = "on" then
  80. drawfill (325, 475, 10, black)
  81. elsif GstatusI = "off" then
  82. drawfill (325, 475, white, black)
  83. end if
  84.  
  85. %BOTTOM%
  86. drawoval (475, 325, 10, 10, black)
  87. if RstatusI = "on" then
  88. drawfill (475, 325, 12, black)
  89. elsif RstatusI = "off" then
  90. drawfill (475, 325, white, black)
  91. end if
  92.  
  93. drawoval (475, 305, 10, 10, black)
  94. if YstatusI = "on" then
  95. drawfill (475, 305, yellow, black)
  96. elsif YstatusI = "off" then
  97. drawfill (475, 305, white, black)
  98. end if
  99.  
  100. drawoval (475, 285, 10, 10, black)
  101. if GstatusI = "on" then
  102. drawfill (475, 285, 10, black)
  103. elsif GstatusI = "off" then
  104. drawfill (475, 285, white, black)
  105. end if
  106.  
  107. %%%%SET II%%%%
  108. %LEFT%
  109. drawoval (325, 325, 10, 10, black)
  110. if RstatusII = "on" then
  111. drawfill (325, 325, 12, black)
  112. elsif RstatusII = "off" then
  113. drawfill (325, 325, white, black)
  114. end if
  115.  
  116. drawoval (305, 325, 10, 10, black)
  117. if YstatusII = "on" then
  118. drawfill (305, 325, yellow, black)
  119. elsif YstatusII = "off" then
  120. drawfill (305, 325, white, black)
  121. end if
  122.  
  123. drawoval (285, 325, 10, 10, black)
  124. if GstatusII = "on" then
  125. drawfill (285, 325, 10, black)
  126. elsif GstatusII = "off" then
  127. drawfill (285, 325, white, black)
  128. end if
  129.  
  130. %RIGHT
  131. drawoval (475, 475, 10, 10, black)
  132. if RstatusII = "on" then
  133. drawfill (475, 475, 12, black)
  134. elsif RstatusII = "off" then
  135. drawfill (475, 475, white, black)
  136. end if
  137.  
  138. drawoval (495, 475, 10, 10, black)
  139. if YstatusII = "on" then
  140. drawfill (495, 475, yellow, black)
  141. elsif YstatusII = "off" then
  142. drawfill (495, 475, white, black)
  143. end if
  144.  
  145. drawoval (515, 475, 10, 10, black)
  146. if GstatusII = "on" then
  147. drawfill (515, 475, 10, black)
  148. elsif GstatusII = "off" then
  149. drawfill (515, 475, white, black)
  150. end if
  151.  
  152. end drawLights
  153.  
  154. procedure runLightsI %procedure to understand input/set status (I)
  155.  
  156.  
  157. %if statement for section I lights
  158. if statusI = "green" then
  159. RstatusI := "off"
  160. YstatusI := "off"
  161. GstatusI := "on"
  162.  
  163. elsif statusI = "yellow" then
  164. RstatusI := "off"
  165. YstatusI := "on"
  166. GstatusI := "off"
  167.  
  168. elsif statusI = "red" then
  169. RstatusI := "on"
  170. YstatusI := "off"
  171. GstatusI := "off"
  172.  
  173. elsif statusI = "off" then
  174. RstatusI := "off"
  175. YstatusI := "off"
  176. GstatusI := "off"
  177.  
  178. end if
  179.  
  180. end runLightsI
  181. procedure runLightsII %procedure to understand input/set status (II)
  182.  
  183.  
  184. %if statement for section II lights
  185. if statusII = "green" then
  186. RstatusII := "off"
  187. YstatusII := "off"
  188. GstatusII := "on"
  189.  
  190. elsif statusII = "yellow" then
  191. RstatusII := "off"
  192. YstatusII := "on"
  193. GstatusII := "off"
  194.  
  195. elsif statusII = "red" then
  196. RstatusII := "on"
  197. YstatusII := "off"
  198. GstatusII := "off"
  199.  
  200. elsif statusII = "off" then
  201. RstatusII := "off"
  202. YstatusII := "off"
  203. GstatusII := "off"
  204.  
  205. end if
  206.  
  207. end runLightsII
  208.  
  209. procedure runLights %Calls the other two procedures (GENIUS)
  210. runLightsI
  211. runLightsII
  212. end runLights
  213.  
  214. procedure updateLights %Calls two more procedures
  215. runLights
  216. drawLights
  217. end updateLights
  218.  
  219. procedure cleartext %Procedure to clear text
  220. cls
  221. delay (500)
  222. drawStreet
  223. drawLights
  224. end cleartext
  225.  
  226. procedure beginRoutine %Procedure to begin the routine
  227.  
  228. %Start with green/red lights
  229. statusI := "green"
  230. statusII := "red"
  231. updateLights
  232.  
  233.  
  234. delay (5000) %Delay 5 seconds
  235.  
  236. %Turn first light to yellow before turning to red
  237. statusI := "yellow"
  238. statusII := "red"
  239. updateLights
  240.  
  241.  
  242. delay (4000) %Delay 4 seconds
  243.  
  244. %Turn first light fully red then wait before turning II green
  245. statusI := "red"
  246. statusII:= "red"
  247. updateLights
  248.  
  249. delay (3000) %Delay 3 seconds
  250.  
  251. %Reversal of initial state
  252. statusI:= "red"
  253. statusII := "green"
  254. updateLights
  255.  
  256. delay(5000) %Delay 5 seconds
  257.  
  258. %Begin turning II to red
  259. statusI:= "red"
  260. statusII:= "yellow"
  261. updateLights
  262.  
  263. delay(4000) %Delay 4 seconds
  264.  
  265. %Turn both red to get ready for the next loop
  266. statusI := "red"
  267. statusII:= "red"
  268. updateLights
  269.  
  270. delay(3000) %Delay 3 seconds
  271.  
  272. end beginRoutine
  273.  
  274. procedure debugP %Procedure to debug/test the program
  275.  
  276. var input : string
  277. put "Input: " ..
  278. get input
  279.  
  280. %IF STATEMENT FOR CLS
  281. if input = "cls" then
  282.  
  283. cleartext %Reset status of lights
  284. statusI := "off"
  285. statusII := "off"
  286. drawLights
  287. runLights
  288. drawLights
  289.  
  290. %IF STATEMENT FOR TL
  291. elsif input = "tl" then
  292.  
  293. var IorII : string %Find out if user wants to test set I or set II
  294. put "I or II: " ..
  295. get IorII
  296. if IorII = "I" then
  297.  
  298. put "color: " ..
  299. get statusI
  300. drawLights
  301. runLightsI
  302. elsif IorII = "II" then
  303. put "color: " ..
  304. get statusII
  305. drawLights
  306. runLightsII
  307. end if
  308.  
  309. %IF STATEMENT FOR HELP
  310. elsif input = "help" then
  311. put commands
  312.  
  313. elsif input = "rtn" then
  314. cls
  315. cls
  316. beginRoutine
  317.  
  318. else
  319. put "command not recognised"
  320. cleartext
  321.  
  322. end if
  323. end debugP
  324.  
  325.  
  326.  
  327. %%%%%%%%%%%%%%%%%%%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%
  328.  
  329. %Main loop
  330. loop
  331. drawStreet
  332. drawLights
  333.  
  334. if debug = 1 then
  335. debugP
  336. end if
  337.  
  338. beginRoutine
  339.  
  340. end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement