Mesaif

Traffic Lights V1.5

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