Advertisement
Mesaif

Traffic Lights V1.1

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