Computer Science Canada Need help, my program doesnt go back to a proc properly after an object reaches a certain point. |
Author: | sakibur001 [ Mon Jan 16, 2017 5:46 pm ] |
Post subject: | Need help, my program doesnt go back to a proc properly after an object reaches a certain point. |
What is it you are trying to achieve? I want my program to go to a congrats proc and then back to mainmenu after it reaches the finish line. What is the problem you are having? I tried a few ways, but none seem to be working properly, i was able to open and close the congrats window, and open main menu, but for some reason, instead of the mainmenu content showing, it still shows the game content in the mainmenu window. Im kinda confused on whats happening. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) % Mr. Rosen % Final Culminating Activity % Second Version of Driving Simulator (V2.1) %Import the GUI program/commands import GUI %Global Variable Declarations var mainMenuButton : int := 0 var instructionButton : int := 0 var playGameButton : int := 0 var goodByeButton : int := 0 var mainMenuWindow : int := 0 var goodByeWin : int := 0 var instructWin : int := 0 var playGameWin : int := 0 var winOrLoseWind : int := 0 var crashWin : int := 0 var congratsWin : int := 0 var intfont : int var intfont2 : int var finished : boolean := false %Font type and size %Note: not being used for all text in program intfont := Font.New ("Comic Sans MS:12") % Procedure Forward Declaration Section forward procedure introduction forward procedure mainMenu forward procedure playGame forward procedure goodBye forward procedure instruction % Places a title at the top of the window procedure title cls Font.Draw ("Driving Simulator", 180, 480, intfont, black) end title procedure introAni %This procedure is apart of the intro, it will show a streetlight turning green %Then it will play a car start-up sound and a car will move from the left to the right of the window. %It will continue with the intro var picture : int Text.ColourBack (54) title %Welcome message Font.Draw ("Welcome! Are you ready to play? Lets go!", 120, 370, intfont, black) %Street light turning green %Black box drawfillbox (15, 200, 85, 400, 7) delay (200) %Red light drawfilloval (50, 350, 20, 20, brightred) delay (500) drawfilloval (50, 350, 20, 20, brightred) %Yellow light drawfilloval (50, 300, 20, 20, yellow) delay (500) drawfilloval (50, 300, 20, 20, yellow) %Green light drawfilloval (50, 250, 20, 20, brightgreen) delay (500) drawfilloval (50, 250, 20, 20, brightgreen) delay (300) %Car start-up sound Music.PlayFile ("carstart.wav") % A car will move from the left to the right of the screen % Body Draw.FillOval (100, 100, 100, 25, blue) Draw.FillOval (190, 100, 8, 5, yellow) Draw.FillOval (3, 108, 3, 10, blue) % Tires Draw.FillOval (50, 75, 22, 22, 0) Draw.FillOval (50, 75, 20, 20, 255) Draw.FillOval (50, 75, 2, 2, 236) Draw.FillOval (150, 75, 22, 22, 0) Draw.FillOval (150, 75, 20, 20, 255) Draw.FillOval (150, 75, 2, 2, 236) % Cab Draw.ThickLine (50, 120, 70, 150, 4, blue) Draw.ThickLine (150, 120, 130, 150, 4, blue) Draw.ThickLine (130, 150, 70, 150, 4, blue) Draw.ThickLine (100, 120, 100, 150, 4, blue) Draw.Line (99, 75, 99, 152, 0) Draw.FillOval (60, 115, 4, 1, 0) Draw.FillOval (110, 115, 4, 1, 0) picture := Pic.New (0, 0, 220, 199) Draw.Cls for i : 0 .. 300 by 10 Pic.Draw (picture, 0 + i, 0, picCopy) Time.Delay (35) cls end for delay (400) end introAni % Introduction of the program/game to the user body proc introduction mainMenuWindow := Window.Open ("position:top;left;graphics:500;500") % introAni %Change background colour to blue Text.ColourBack (54) title locate (4, 1) put "This program will test your driving skills. You will have to drive through a town with different cars and street lights appearing. Your goal is to reach the end of the street safely." mainMenuButton := GUI.CreateButton (200, 100, 0, "Main Menu", mainMenu) %Introductory noise of a car %Music.PlayFile ("carstart.wav") %Simple street light animation drawfillbox (15, 100, 85, 300, 7) delay (200) drawfilloval (50, 250, 20, 20, brightred) delay (500) drawfilloval (50, 250, 20, 20, brightred) drawfilloval (50, 200, 20, 20, yellow) delay (500) drawfilloval (50, 200, 20, 20, yellow) drawfilloval (50, 150, 20, 20, brightgreen) delay (500) drawfilloval (50, 150, 20, 20, brightgreen) end introduction % Main Menu for the user to make choices regarding game play body proc mainMenu %Menu background colour blue Text.ColourBack (54) title locate (3, 27) %put "Main Menu" Font.Draw ("Main Menu", 200, 460, intfont, black) Font.Draw ("Choose an option:", 10, 400, intfont, black) %put "Choose an option:" Window.Hide (defWinID) Window.Hide (instructWin) Window.Hide (playGameWin) Window.Show (mainMenuWindow) Window.SetActive (mainMenuWindow) %Create buttons to play, exit or get help instructionButton := GUI.CreateButton (290, 225, 0, "Help", instruction) playGameButton := GUI.CreateButton (145, 225, 0, "Play", playGame) goodByeButton := GUI.CreateButton (220, 225, 0, "Exit", goodBye) Window.Hide (playGameWin) %Music.PlayFile ("tweety.wav") end mainMenu procedure congrats %Window.Close (playGameWin) congratsWin := Window.Open ("position:400;200;graphics:500;500") Window.Show (congratsWin) Window.SetActive (congratsWin) put "Congrats" delay (2000) mainMenu Window.Hide (congratsWin) Window.Hide (playGameWin) finished := false end congrats % This is to provide instructions or help to the user body proc instruction Window.Hide (mainMenuWindow) instructWin := Window.Open ("position:400;200;graphics:500;500") %Instructions window background colour red Text.ColourBack (12) title %Put intructions Font.Draw ("Instructions:", 0, 440, intfont, black) locate (6, 1) put "This game is a basic Driving Simulator, it will feature a car which you will be driving on a street through the town." .. put " There will be streets and lights appearing, which you will have to obey." .. put "The goal of the game is to reach the end without hitting a building, if you do, then GAME OVER. To control the car, you have to use the arrow keys." %Picture of arrow keys var pictureID : int % pictureID := Pic.FileNew ("arrowkeys1.bmp") %Pic.Draw (pictureID, 169, 150, picMerge) %Button to return to the main menu mainMenuButton := GUI.CreateButton (166, 80, 0, "Return to the Main Menu", mainMenu) end instruction %if user crashes, then they will be redirected to this procedure procedure crash crashWin := Window.Open ("graphics:max;max") Window.SetActive (crashWin) cls GUI.SetBackgroundColor (black) var pictureID : int % pictureID := Pic.FileNew ("wastedgta.bmp") %Pic.Draw (pictureID, 530, 230, picMerge) % Music.PlayFile ("wasted.mp3") end crash % This is where the game is being played body proc playGame var x, y : int x := 115 y := 40 var chars : array char of boolean Window.Hide (mainMenuWindow) %Fits the screen playGameWin := Window.Open ("graphics:max;max") Window.SetActive (playGameWin) % put "" %put "This is it! Get ready to drive!" %delay (1000) %cls %Moves the car loop View.Set ("offscreenonly") Input.KeyDown (chars) if chars (KEY_UP_ARROW) then y := y + 5 end if if chars (KEY_RIGHT_ARROW) then x := x + 5 end if if chars (KEY_LEFT_ARROW) then x := x - 5 end if if chars (KEY_DOWN_ARROW) then y := y - 5 end if drawfillbox (x, y, x + 30, y + 20, brightred) % drawfillbox (x - 3, y - 3, x + 10, y + 10, 7) View.Update delay (15) %Draws the background and streets %background black drawfillbox (0, 0, maxx, maxy, 7) % Vertical roads drawfillbox (90, 0, 170, maxy, grey) drawfillbox (500, 0, 580, maxy, grey) drawfillbox (900, 0, 980, maxy, grey) %Horizontal Roads drawfillbox (0, 190, maxx, 290, grey) drawfillbox (0, 400, maxx, 500, grey) drawfillbox (980, 580, maxx, 650, grey) %person on a street % drawfilloval (200, 225, 7, 7, blue) %head %drawline (200, 210, 200, 225, blue) %body %drawline (200, 210, 196, 195, blue) %left leg %drawline (200, 210, 204, 195, blue) %right leg %drawline (195, 212, 205, 212, blue) %arms %Start and finish word placement Font.Draw ("START", 100, 25, intfont, green) Font.Draw ("FINISH", 1180, 610, intfont, brightred) %Checks if the user has reached the "Finish" line if (x >= 1180 and x >= 1180) and (y >= 610 and y >= 610) then finished := true %cls Window.Hide (playGameWin) congrats x := 115 y := 40 %Checks if the user crashes into a wall/building (the black stuff) %If they crash then the game resets after the crash proc %All of these statements include all of the buildings (black) and the end of the roads elsif (x >= 138 and x <= 500) and (y >= 0 and y <= 190) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 0 and x <= 94) and (y >= 0 and y <= 190) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 138 and x <= 500) and (y >= 270 and y <= 400) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 0 and x <= 94) and (y >= 270 and y <= 400) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 0 and x <= 1) and (y >= 190 and y <= 270) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 0 and x <= 1) and (y >= 400 and y <= 480) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 0 and x <= 94) and (y >= 480 and y <= 720) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 94 and x <= 190) and (y >= 660 and y <= 661) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 94 and x <= 190) and (y >= 0 and y <= 1) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 138 and x <= 500) and (y >= 480 and y <= 720) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 550 and x <= 900) and (y >= 0 and y <= 190) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 550 and x <= 900) and (y >= 270 and y <= 400) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 550 and x <= 900) and (y >= 480 and y <= 720) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 950 and x <= maxx) and (y >= 0 and y <= 190) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 950 and x <= maxx) and (y >= 270 and y <= 400) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 950 and x <= maxx) and (y >= 480 and y <= 580) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 950 and x <= maxx) and (y >= 630 and y <= 700) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 500 and x <= 580) and (y >= 0 and y <= 1) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 500 and x <= 580) and (y >= 660 and y <= 661) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 900 and x <= 980) and (y >= 0 and y <= 1) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 900 and x <= 980) and (y >= 660 and y <= 661) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 1310 and x <= 1311) and (y >= 190 and y <= 290) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 1310 and x <= 1311) and (y >= 400 and y <= 500) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame %This section checks if the car runs over people, if so then the game resets elsif (x >= 180 and x <= 210) and (y >= 195 and y <= 230) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame end if %Street Lights and pedestrians %Turns on specific street lights when the user is at specific areas and turns them off when user is away from that area if (x >= 90 and x <= 170) and (y >= 40 and y <= 70) then drawfilloval (70, 170, 7, 7, yellow) %"randomly" draws pedestrians onto the road elsif (x >= 150 and x <= 300) and (y >= 100 and y <= 275) then drawfilloval (220, 225, 7, 7, blue) %head drawline (220, 210, 220, 225, blue) %body drawline (220, 210, 216, 195, blue) %left leg drawline (220, 210, 224, 195, blue) %right leg drawline (215, 212, 225, 212, blue) %arms %Street lights elsif (x >= 90 and x <= 170) and (y >= 70 and y <= 125) then drawfilloval (70, 170, 7, 7, brightred) elsif (x >= 90 and x <= 170) and (y >= 125 and y <= 280) then drawfilloval (70, 170, 7, 7, brightgreen) drawfilloval (70, 305, 7, 7, yellow) elsif (x >= 90 and x <= 170) and (y >= 280 and y <= 350) then drawfilloval (70, 305, 7, 7, yellow) drawfilloval (70, 380, 7, 7, yellow) elsif (x >= 90 and x <= 170) and (y >= 350 and y <= 540) then drawfilloval (70, 380, 7, 7, yellow) drawfilloval (70, 515, 7, 7, yellow) elsif (x >= 130 and x <= 300) and (y >= 280 and y <= 540) then drawfilloval (190, 515, 7, 7, yellow) elsif (x >= 300 and x <= 500) and (y >= 280 and y <= 540) then drawfilloval (480, 515, 7, 7, yellow) elsif (x >= 500 and x <= 810) and (y >= 280 and y <= 540) then drawfilloval (480, 515, 7, 7, yellow) drawfilloval (600, 515, 7, 7, yellow) elsif (x >= 810 and x <= 920) and (y >= 280 and y <= 540) then drawfilloval (600, 515, 7, 7, yellow) drawfilloval (880, 515, 7, 7, yellow) elsif (x >= 920 and x <= 1300) and (y >= 280 and y <= 540) then drawfilloval (880, 515, 7, 7, yellow) drawfilloval (1005, 515, 7, 7, yellow) elsif (x >= 100 and x <= 300) and (y >= 190 and y <= 270) then drawfilloval (190, 170, 7, 7, yellow) elsif (x >= 300 and x <= 500) and (y >= 190 and y <= 270) then drawfilloval (480, 170, 7, 7, yellow) elsif (x >= 500 and x <= 810) and (y >= 190 and y <= 270) then drawfilloval (480, 170, 7, 7, yellow) drawfilloval (600, 170, 7, 7, yellow) elsif (x >= 810 and x <= 920) and (y >= 190 and y <= 270) then drawfilloval (600, 170, 7, 7, yellow) drawfilloval (880, 170, 7, 7, yellow) elsif (x >= 920 and x <= 1300) and (y >= 190 and y <= 290) then drawfilloval (880, 170, 7, 7, yellow) drawfilloval (1005, 170, 7, 7, yellow) elsif (x >= 920 and x <= 1300) and (y >= 540 and y <= 700) then drawfilloval (1005, 663, 7, 7, yellow) end if end loop end playGame % Goodbye/End program body proc goodBye goodByeWin := Window.Open ("position:top;right;graphics:500;500") %Goodbye window background colour yellow Text.ColourBack (14) title Font.Draw ("Thanks for using this program/game!", 0, 420, intfont, 12) locate (8, 1) put "Program designed and created by:" put "Sakibur M. Rahman" Window.Hide (mainMenuWindow) Window.Hide (playGameWin) Window.Hide (instructWin) %Someone saying goodbye %Music.PlayFile ("bugs_goodbye.wav") % After music plays, window waits for a short time before closing the window delay (1400) Window.Hide (goodByeWin) GUI.Quit end goodBye %Main Program introduction loop exit when GUI.ProcessEvent end loop %End Program |
Author: | Insectoid [ Tue Jan 17, 2017 10:15 am ] |
Post subject: | RE:Need help, my program doesnt go back to a proc properly after an object reaches a certain point. |
I haven't taken too close of a look, but I notice you have functions that call each other ie mainmenu calls instruction and instruction calls mainmenu. This is bad, and probably the cause of a number of bugs, and definitely the cause of some bugs you haven't discovered yet. Remember how, when a function/procedure ends, code execution goes back to where you called that function and resumes? Take advantage of that! Mainmenu calls instruction, right? That means, when instruction ends, mainmenu will resume automatically! But if you call mainmenu from instruction (like with a GUI button), now there's TWO copies of mainmenu in RAM. We don't want that. Besides taking up more RAM, it can also cause the program to crash eventually. It's just plain better to let the function exit- either with the 'exit' keyword, or by simply reaching the end of the function. Anyway, if that doesn't solve the problem, then it's probably due to the GUI stuff you've got there. I've never had anything but trouble with Turing's built-in GUI. Always found it easier to make my own buttons. |