Advertisement
Mesaif

Traffic Lights V1.4

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