Advertisement
Mesaif

Traffic Lights V1.0

May 13th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.23 KB | None | 0 0
  1. %Program that draws a street and works traffic lights
  2.  
  3.  
  4. %SCREEN SET UP%
  5. View.Set ("graphics:vga,offscreenonly")
  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. var xI : int := 10
  25. var xI2 : int := 85
  26. var yI : int := 360
  27. var yI2 : int := 390
  28.  
  29. var xII : int := 360
  30. var xII2 : int := 390
  31. var yII : int := 790
  32. var yII2 : int := 715
  33.  
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35.  
  36.  
  37.  
  38. procedure drawStreet %Draw the street
  39.  
  40. %Background
  41. drawbox (0, 0, 800, 800, black)
  42. drawfill (10, 10, green, black)
  43. %Horizontal Lines
  44. drawline (0, 350, 350, 350, black)
  45. drawline (0, 450, 350, 450, black)
  46.  
  47. drawline (450, 350, 800, 350, black)
  48. drawline (450, 450, 800, 450, black)
  49.  
  50.  
  51. %Vertical Lines
  52. drawline (350, 800, 350, 450, black)
  53. drawline (350, 0, 350, 350, black)
  54.  
  55. drawline (450, 800, 450, 450, black)
  56. drawline (450, 0, 450, 350, black)
  57.  
  58. %MIDDLE WHITE
  59. drawfillbox (350, 350, 450, 450, white)
  60. drawfill (400, 400, grey, black) %pavement
  61.  
  62. %Street Lines
  63. Draw.DashedLine (400, 800, 400, 0, drawDash, yellow)
  64. Draw.DashedLine (0, 400, 800, 400, drawDash, yellow)
  65.  
  66. drawfillbox (350, 350, 450, 450, grey)
  67.  
  68. end drawStreet
  69.  
  70. procedure drawLights %Procedure to draw ovals for lights that can take input to fill
  71.  
  72. %%%%SET I%%%%
  73.  
  74. drawbox (310, 460, 340, 530, black) %FRAME
  75. drawfill (316, 461, yellow, black) %FRAME
  76.  
  77. %TOP%
  78. drawoval (325, 515, 10, 10, black)
  79. if RstatusI = "on" then
  80. drawfill (325, 515, 12, black)
  81. elsif RstatusI = "off" then
  82. drawfill (325, 515, white, black)
  83. end if
  84.  
  85. drawoval (325, 495, 10, 10, black)
  86. if YstatusI = "on" then
  87. drawfill (325, 495, 43, black)
  88. elsif YstatusI = "off" then
  89. drawfill (325, 495, white, black)
  90. end if
  91.  
  92. drawoval (325, 475, 10, 10, black)
  93. if GstatusI = "on" then
  94. drawfill (325, 475, 10, black)
  95. elsif GstatusI = "off" then
  96. drawfill (325, 475, white, black)
  97. end if
  98.  
  99. %BOTTOM%
  100. drawbox (460, 270, 490, 340, black) %FRAME
  101. drawfill (461, 271, yellow, black) %FRAME
  102.  
  103. drawoval (475, 325, 10, 10, black)
  104. if RstatusI = "on" then
  105. drawfill (475, 325, 12, black)
  106. elsif RstatusI = "off" then
  107. drawfill (475, 325, white, black)
  108. end if
  109.  
  110. drawoval (475, 305, 10, 10, black)
  111. if YstatusI = "on" then
  112. drawfill (475, 305, 43, black)
  113. elsif YstatusI = "off" then
  114. drawfill (475, 305, white, black)
  115. end if
  116.  
  117. drawoval (475, 285, 10, 10, black)
  118. if GstatusI = "on" then
  119. drawfill (475, 285, 10, black)
  120. elsif GstatusI = "off" then
  121. drawfill (475, 285, white, black)
  122. end if
  123.  
  124. %%%%SET II%%%%
  125. %LEFT%
  126. drawbox (270, 310, 340, 340, black)
  127. drawfill (271, 311, yellow, black)
  128.  
  129. drawoval (325, 325, 10, 10, black)
  130. if RstatusII = "on" then
  131. drawfill (325, 325, 12, black)
  132. elsif RstatusII = "off" then
  133. drawfill (325, 325, white, black)
  134. end if
  135.  
  136. drawoval (305, 325, 10, 10, black)
  137. if YstatusII = "on" then
  138. drawfill (305, 325, 43, black)
  139. elsif YstatusII = "off" then
  140. drawfill (305, 325, white, black)
  141. end if
  142.  
  143. drawoval (285, 325, 10, 10, black)
  144. if GstatusII = "on" then
  145. drawfill (285, 325, 10, black)
  146. elsif GstatusII = "off" then
  147. drawfill (285, 325, white, black)
  148. end if
  149.  
  150. %RIGHT
  151.  
  152. drawbox (460, 460, 530, 490, black)
  153. drawfill (461, 461, yellow, black)
  154.  
  155. drawoval (475, 475, 10, 10, black)
  156. if RstatusII = "on" then
  157. drawfill (475, 475, 12, black)
  158. elsif RstatusII = "off" then
  159. drawfill (475, 475, white, black)
  160. end if
  161.  
  162. drawoval (495, 475, 10, 10, black)
  163. if YstatusII = "on" then
  164. drawfill (495, 475, 43, black)
  165. elsif YstatusII = "off" then
  166. drawfill (495, 475, white, black)
  167. end if
  168.  
  169. drawoval (515, 475, 10, 10, black)
  170. if GstatusII = "on" then
  171. drawfill (515, 475, 10, black)
  172. elsif GstatusII = "off" then
  173. drawfill (515, 475, white, black)
  174. end if
  175.  
  176. end drawLights
  177.  
  178. procedure runLightsI %procedure to understand input/set status (I)
  179.  
  180.  
  181. %if statement for section I lights
  182. if statusI = "green" then
  183. RstatusI := "off"
  184. YstatusI := "off"
  185. GstatusI := "on"
  186.  
  187. elsif statusI = "yellow" then
  188. RstatusI := "off"
  189. YstatusI := "on"
  190. GstatusI := "off"
  191.  
  192. elsif statusI = "red" then
  193. RstatusI := "on"
  194. YstatusI := "off"
  195. GstatusI := "off"
  196.  
  197. elsif statusI = "off" then
  198. RstatusI := "off"
  199. YstatusI := "off"
  200. GstatusI := "off"
  201.  
  202. end if
  203.  
  204. end runLightsI
  205. procedure runLightsII %procedure to understand input/set status (II)
  206.  
  207.  
  208. %if statement for section II lights
  209. if statusII = "green" then
  210. RstatusII := "off"
  211. YstatusII := "off"
  212. GstatusII := "on"
  213.  
  214. elsif statusII = "yellow" then
  215. RstatusII := "off"
  216. YstatusII := "on"
  217. GstatusII := "off"
  218.  
  219. elsif statusII = "red" then
  220. RstatusII := "on"
  221. YstatusII := "off"
  222. GstatusII := "off"
  223.  
  224. elsif statusII = "off" then
  225. RstatusII := "off"
  226. YstatusII := "off"
  227. GstatusII := "off"
  228.  
  229. end if
  230.  
  231. end runLightsII
  232.  
  233. procedure runLights %Calls the other two procedures (GENIUS)
  234. runLightsI
  235. runLightsII
  236. end runLights
  237.  
  238. procedure updateLights %Calls two more procedures
  239. runLights
  240. drawLights
  241. end updateLights
  242.  
  243. procedure cleartext %Procedure to clear text
  244. cls
  245. delay (500)
  246. drawStreet
  247. drawLights
  248. end cleartext
  249.  
  250. process Hcar () %The horizontal car
  251. loop
  252. drawfillbox ((xI - 5), yI, (xI2 - 5), yI2, grey) %Removes the previous drawn shape
  253. drawfillbox (xI, yI, xI2, yI2, blue)
  254. xI += 4
  255. xI2 += 4
  256.  
  257. if xI2 >= 350 and xI2 <= 360 then %When the car is near the end of the intersection
  258. if statusII = "red" or statusII = "yellow" then
  259. loop
  260. delay (50)
  261. if statusII = "green" then
  262. delay (50)
  263. exit
  264. end if
  265. end loop
  266. end if
  267. end if
  268. if xI2 >= 800 then
  269. drawfillbox (1, 355, 799, 395, grey)
  270. xI := 10
  271. xI2 := 85
  272. yI := 360
  273. yI2 := 390
  274. delay (50)
  275.  
  276. end if
  277.  
  278.  
  279. View.UpdateArea (0, 350, 800, 450)
  280. delay (50)
  281. %drawfillbox (1, 355, 799, 395, white) (OLD VERSION)
  282. end loop
  283. end Hcar
  284.  
  285. process Vcar () %The vertical car
  286. loop
  287.  
  288. drawfillbox (xII, (yII + 4), xII2, (yII2 + 4), grey)
  289. drawfillbox (xII, yII, xII2, yII2, red)
  290. yII -= 4
  291. yII2 -= 4
  292.  
  293. if yII2 <= 450 and yII2 >= 440 then %When the car is near the end of the intersection
  294. if statusI = "red" or statusI = "yellow" then
  295. loop
  296. delay (50)
  297. if statusI = "green" then
  298. delay (50)
  299. exit
  300. end if
  301. end loop
  302. end if
  303. end if
  304. if yII2 <= 0 then
  305. drawfillbox (351, 1, 390, 799, grey)
  306. xII := 360
  307. xII2 := 390
  308. yII := 790
  309. yII2 := 715
  310. delay (50)
  311. end if
  312. View.UpdateArea (350, 0, 450, 800)
  313. delay (50)
  314. %drawfillbox (351, 1, 390, 799, white) (OLD VERSION)
  315. end loop
  316. end Vcar
  317.  
  318.  
  319. procedure beginRoutine %Procedure to begin the routine
  320.  
  321. %Start with green/red lights
  322. statusI := "green"
  323. statusII := "red"
  324. updateLights
  325.  
  326.  
  327. delay (5000) %Delay 5 seconds
  328.  
  329. %Turn first light to yellow before turning to red
  330. statusI := "yellow"
  331. statusII := "red"
  332. updateLights
  333.  
  334.  
  335. delay (1000) %Delay 4 seconds
  336.  
  337. %Turn first light fully red then wait before turning II green
  338. statusI := "red"
  339. statusII := "red"
  340. updateLights
  341.  
  342. delay (2000) %Delay 3 seconds
  343.  
  344. %Reversal of initial state
  345. statusI := "red"
  346. statusII := "green"
  347. updateLights
  348.  
  349. delay (5000) %Delay 5 seconds
  350.  
  351. %Begin turning II to red
  352. statusI := "red"
  353. statusII := "yellow"
  354. updateLights
  355.  
  356. delay (1000) %Delay 4 seconds
  357.  
  358. %Turn both red to get ready for the next loop
  359. statusI := "red"
  360. statusII := "red"
  361. updateLights
  362.  
  363. delay (2000) %Delay 3 seconds
  364.  
  365. end beginRoutine
  366.  
  367.  
  368. procedure debugP %Procedure to debug/test the program
  369.  
  370. var input : string
  371. put "Input: " ..
  372. get input
  373.  
  374. %IF STATEMENT FOR CLS
  375. if input = "cls" then
  376.  
  377. cleartext %Reset status of lights
  378. statusI := "off"
  379. statusII := "off"
  380. drawLights
  381. runLights
  382. drawLights
  383.  
  384. %IF STATEMENT FOR TL
  385. elsif input = "tl" then
  386.  
  387. var IorII : string %Find out if user wants to test set I or set II
  388. put "I or II: " ..
  389. get IorII
  390. if IorII = "I" then
  391.  
  392. put "color: " ..
  393. get statusI
  394. drawLights
  395. runLightsI
  396. elsif IorII = "II" then
  397. put "color: " ..
  398. get statusII
  399. drawLights
  400. runLightsII
  401. end if
  402.  
  403. %IF STATEMENT FOR HELP
  404. elsif input = "help" then
  405. put commands
  406.  
  407. elsif input = "rtn" then
  408. cls
  409. cls
  410.  
  411. beginRoutine
  412.  
  413. else
  414. put "command not recognised"
  415. cleartext
  416.  
  417. end if
  418. end debugP
  419.  
  420.  
  421.  
  422.  
  423. %%%%%%%%%%%%%%%%%%%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%
  424.  
  425. %Main loop
  426. loop
  427. drawStreet
  428. drawLights
  429.  
  430. if debug = 1 then
  431. debugP
  432. end if
  433.  
  434. fork Hcar ()
  435. fork Vcar ()
  436. View.Update
  437. beginRoutine
  438.  
  439. end loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement