Advertisement
Mesaif

Traffic Lights V1.7

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