Advertisement
Mesaif

Traffic Light - V1.9

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