Advertisement
Mesaif

Traffic Lights V1.6

May 13th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 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.  
  39. %SCREEN SET UP%
  40. View.Set ("graphics:vga,offscreenonly")
  41. View.Set ("graphics: 1000;1000")
  42. %SCREEN SET UP%
  43.  
  44. %VARIABLES%
  45. var statusI : string
  46. var RstatusI : string := "off"
  47. var YstatusI : string := "off"
  48. var GstatusI : string := "off"
  49.  
  50. var statusII : string
  51. var RstatusII : string := "off"
  52. var YstatusII : string := "off"
  53. var GstatusII : string := "off"
  54.  
  55. var debug : int := 0 %DEBUG VARIABLE%
  56.  
  57. var commands : string := "tl: test lights, cls: clear text, rtn: begin routine"
  58.  
  59. var xI : int := 10
  60. var xI2 : int := 85
  61. var yI : int := 360
  62. var yI2 : int := 390
  63.  
  64. var xII : int := 360
  65. var xII2 : int := 390
  66. var yII : int := 790
  67. var yII2 : int := 715
  68.  
  69. var speed : int := 4
  70.  
  71. var fillcolor : int := 72
  72.  
  73. %%%%%%%%%%%%%%%%%%%%%%%%%%%PROCEDURES%%%%%%%%%%%%%%%%%%%%%%%%%%%
  74.  
  75.  
  76.  
  77. procedure drawStreet %Draw the street
  78.  
  79. %Background
  80. drawbox (0, 0, 800, 800, black)
  81. drawfill (10, 10, green, black)
  82. %Horizontal Lines
  83. drawline (0, 350, 350, 350, black)
  84. drawline (0, 450, 350, 450, black)
  85.  
  86. drawline (450, 350, 800, 350, black)
  87. drawline (450, 450, 800, 450, black)
  88.  
  89.  
  90. %Vertical Lines
  91. drawline (350, 800, 350, 450, black)
  92. drawline (350, 0, 350, 350, black)
  93.  
  94. drawline (450, 800, 450, 450, black)
  95. drawline (450, 0, 450, 350, black)
  96.  
  97. %MIDDLE WHITE
  98. drawfillbox (350, 350, 450, 450, white)
  99. drawfill (400, 400, grey, black) %pavement
  100.  
  101. %Street Lines
  102. Draw.DashedLine (400, 800, 400, 0, drawDash, yellow)
  103. Draw.DashedLine (0, 400, 800, 400, drawDash, yellow)
  104.  
  105.  
  106.  
  107.  
  108. drawfillbox (350, 350, 450, 450, grey)
  109.  
  110. end drawStreet
  111. procedure drawCrosswalk %Automatically draw crosswalks
  112.  
  113.  
  114.  
  115. for iV : 355 .. 445 by 5 %For loop to draw vertical crosswalks
  116. drawline (iV, 465, iV, 450, white)
  117. drawline (iV, 350, iV, 335, white)
  118. end for
  119.  
  120. end drawCrosswalk
  121. procedure drawHwalk
  122. for decreasing iH : 445 .. 355 by 5 %For loop to automatically draw horizontal crosswalk lines
  123. drawline (335, iH, 350, iH, white)
  124. drawline (450, iH, 465, iH, white)
  125. end for
  126. end drawHwalk
  127.  
  128. procedure drawLights %Procedure to draw ovals for lights that can take input to fill
  129.  
  130. %%%%SET I%%%%
  131.  
  132. drawbox (310, 460, 340, 530, black) %FRAME
  133. drawfill (316, 461, yellow, black) %FRAME
  134.  
  135. %TOP%
  136. drawoval (325, 515, 10, 10, black)
  137. if RstatusI = "on" then
  138. drawfill (325, 515, 12, black)
  139. elsif RstatusI = "off" then
  140. drawfill (325, 515, white, black)
  141. end if
  142.  
  143. drawoval (325, 495, 10, 10, black)
  144. if YstatusI = "on" then
  145. drawfill (325, 495, 43, black)
  146. elsif YstatusI = "off" then
  147. drawfill (325, 495, white, black)
  148. end if
  149.  
  150. drawoval (325, 475, 10, 10, black)
  151. if GstatusI = "on" then
  152. drawfill (325, 475, 10, black)
  153. elsif GstatusI = "off" then
  154. drawfill (325, 475, white, black)
  155. end if
  156.  
  157. %BOTTOM%
  158. drawbox (460, 270, 490, 340, black) %FRAME
  159. drawfill (461, 271, yellow, black) %FRAME
  160.  
  161. drawoval (475, 325, 10, 10, black)
  162. if RstatusI = "on" then
  163. drawfill (475, 325, 12, black)
  164. elsif RstatusI = "off" then
  165. drawfill (475, 325, white, black)
  166. end if
  167.  
  168. drawoval (475, 305, 10, 10, black)
  169. if YstatusI = "on" then
  170. drawfill (475, 305, 43, black)
  171. elsif YstatusI = "off" then
  172. drawfill (475, 305, white, black)
  173. end if
  174.  
  175. drawoval (475, 285, 10, 10, black)
  176. if GstatusI = "on" then
  177. drawfill (475, 285, 10, black)
  178. elsif GstatusI = "off" then
  179. drawfill (475, 285, white, black)
  180. end if
  181.  
  182. %%%%SET II%%%%
  183. %LEFT%
  184. drawbox (270, 310, 340, 340, black)
  185. drawfill (271, 311, yellow, black)
  186.  
  187. drawoval (325, 325, 10, 10, black)
  188. if RstatusII = "on" then
  189. drawfill (325, 325, 12, black)
  190. elsif RstatusII = "off" then
  191. drawfill (325, 325, white, black)
  192. end if
  193.  
  194. drawoval (305, 325, 10, 10, black)
  195. if YstatusII = "on" then
  196. drawfill (305, 325, 43, black)
  197. elsif YstatusII = "off" then
  198. drawfill (305, 325, white, black)
  199. end if
  200.  
  201. drawoval (285, 325, 10, 10, black)
  202. if GstatusII = "on" then
  203. drawfill (285, 325, 10, black)
  204. elsif GstatusII = "off" then
  205. drawfill (285, 325, white, black)
  206. end if
  207.  
  208. %RIGHT
  209.  
  210. drawbox (460, 460, 530, 490, black)
  211. drawfill (461, 461, yellow, black)
  212.  
  213. drawoval (475, 475, 10, 10, black)
  214. if RstatusII = "on" then
  215. drawfill (475, 475, 12, black)
  216. elsif RstatusII = "off" then
  217. drawfill (475, 475, white, black)
  218. end if
  219.  
  220. drawoval (495, 475, 10, 10, black)
  221. if YstatusII = "on" then
  222. drawfill (495, 475, 43, black)
  223. elsif YstatusII = "off" then
  224. drawfill (495, 475, white, black)
  225. end if
  226.  
  227. drawoval (515, 475, 10, 10, black)
  228. if GstatusII = "on" then
  229. drawfill (515, 475, 10, black)
  230. elsif GstatusII = "off" then
  231. drawfill (515, 475, white, black)
  232. end if
  233.  
  234. end drawLights
  235.  
  236. procedure runLightsI %procedure to understand input/set status (I)
  237.  
  238.  
  239. %if statement for section I lights
  240. if statusI = "green" then
  241. RstatusI := "off"
  242. YstatusI := "off"
  243. GstatusI := "on"
  244.  
  245. elsif statusI = "yellow" then
  246. RstatusI := "off"
  247. YstatusI := "on"
  248. GstatusI := "off"
  249.  
  250. elsif statusI = "red" then
  251. RstatusI := "on"
  252. YstatusI := "off"
  253. GstatusI := "off"
  254.  
  255. elsif statusI = "off" then
  256. RstatusI := "off"
  257. YstatusI := "off"
  258. GstatusI := "off"
  259.  
  260. end if
  261.  
  262. end runLightsI
  263. procedure runLightsII %procedure to understand input/set status (II)
  264.  
  265.  
  266. %if statement for section II lights
  267. if statusII = "green" then
  268. RstatusII := "off"
  269. YstatusII := "off"
  270. GstatusII := "on"
  271.  
  272. elsif statusII = "yellow" then
  273. RstatusII := "off"
  274. YstatusII := "on"
  275. GstatusII := "off"
  276.  
  277. elsif statusII = "red" then
  278. RstatusII := "on"
  279. YstatusII := "off"
  280. GstatusII := "off"
  281.  
  282. elsif statusII = "off" then
  283. RstatusII := "off"
  284. YstatusII := "off"
  285. GstatusII := "off"
  286.  
  287. end if
  288.  
  289. end runLightsII
  290.  
  291. procedure runLights %Calls the other two procedures (GENIUS)
  292. runLightsI
  293. runLightsII
  294. end runLights
  295.  
  296. procedure updateLights %Calls two more procedures
  297. runLights
  298. drawLights
  299. end updateLights
  300.  
  301. procedure cleartext %Procedure to clear text
  302. cls
  303. delay (500)
  304. drawStreet
  305. drawLights
  306. end cleartext
  307.  
  308. process Hcar () %The horizontal car
  309. loop
  310. drawfillbox ((xI - 10), yI, (xI2 - 4), yI2, grey) %Removes the previous drawn shape
  311. drawfillbox (xI, yI, xI2, yI2, blue)
  312. xI := xI + speed
  313. xI2 := xI2 + speed
  314.  
  315. if xI2 >= 330 and xI2 <= 355 then %When the car is near the end of the intersection
  316. if statusII = "red" or statusII = "yellow" then
  317. loop
  318. delay (10)
  319. exit when statusII = "green" %exit when green
  320. end loop
  321. end if
  322. end if
  323. if xI2 >= 800 then
  324. drawfillbox (1, 355, 799, 395, grey)
  325. drawHwalk
  326. xI := 10
  327. xI2 := 85
  328. yI := 360
  329. yI2 := 390
  330. delay (50)
  331.  
  332. end if
  333.  
  334.  
  335. %View.UpdateArea (0, 350, 800, 450)
  336. delay (50)
  337. %drawfillbox (1, 355, 799, 395, white) (OLD VERSION)
  338. drawHwalk
  339. end loop
  340. end Hcar
  341.  
  342. process Vcar () %The vertical car
  343. loop
  344.  
  345. drawfillbox (xII, (yII + 10), xII2, (yII2 + 4), grey)
  346. drawfillbox (xII, yII, xII2, yII2, red)
  347. yII := yII - speed
  348. yII2 := yII2 - speed
  349.  
  350.  
  351. if yII2 <= 470 and yII2 >= 430 then %When the car is near the end of the intersection
  352. if statusI = "red" or statusI = "yellow" then
  353. loop
  354. delay (10)
  355. exit when statusI = "green" %exit when green
  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
Advertisement