Computer Science Canada Keyboard Input Not allowed? When i exit game it gives me an error. help please. |
Author: | sakibur001 [ Tue Jan 17, 2017 1:48 pm ] |
Post subject: | Keyboard Input Not allowed? When i exit game it gives me an error. help please. |
What is it you are trying to achieve? I got the game to finish now and go to the mainmenu but after i play the game atleast once and try to exit, it exits, but then gives me an error, keyboard input from invisible window is not allowed. Im not sure why, but could it be because im not closing a window properly. I tried Window.Close in a few areas but then it just crashes my game. 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.3) %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 ("Hello! 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 streets and pedestrians appearing. Your goal is to reach the finish line 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") loop exit when GUI.ProcessEvent end loop end mainMenu procedure congrats %Window.Close (playGameWin) congratsWin := Window.Open ("position:400;200;graphics:500;500") Window.Show (congratsWin) Window.SetActive (congratsWin) Text.ColourBack (yellow) title %Displays congratulatory message Font.Draw ("You reached the finish line!!! Congrats!", 100, 430, intfont, brightblue) delay (3500) Window.Hide (congratsWin) Window.Hide (playGameWin) finished := false mainMenu 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." .. put " The goal of the game is to reach the finish line without hitting a building or killing a person, if you do then the game resets. To control the car, you have to use the arrow keys." locate (15, 31) put "Up" locate (21, 17) put "Left" locate (21, 43) put "Right" locate (23, 30) put "Down" %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, 475, 330, 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 <= 1280) and (y >= 590 and y <= 700) 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 <= 220) and (y >= 195 and y <= 230) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 180 and x <= 220) and (y >= 405 and y <= 444) then finished := true Window.Hide (playGameWin) crash Window.Close (crashWin) playGame elsif (x >= 650 and x <= 700) and (y >= 227 and y <= 275) 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) %Draws pedestrians onto the road throughout the layout/map elsif (x >= 140 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 elsif (x >= 140 and x <= 270) and (y >= 400 and y <= 520) then drawfilloval (220, 437, 7, 7, 10) %head drawline (220, 438, 220, 418, 10) %body drawline (220, 418, 216, 410, 10) %left leg drawline (220, 418, 224, 410, 10) %right leg drawline (215, 422, 225, 422, 10) %arms drawfilloval (218, 439, 1, 1, 7) %left eye drawfilloval (223, 439, 1, 1, 7) %right eye drawline (218, 435, 222, 435, 7) %mouth elsif (x >= 570 and x <= 750) and (y >= 100 and y <= 275) then drawfilloval (690, 275, 7, 7, 42) %head drawline (690, 275, 690, 251, 42) %body drawline (690, 251, 686, 243, 42) %left leg drawline (690, 251, 694, 243, 42) %right leg drawline (680, 260, 700, 260, 42) %arms drawfilloval (688, 276, 1, 1, 7) %left eye drawfilloval (692, 276, 1, 1, 7) %right eye drawline (689, 273, 691, 273, 7) %mouth %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 >= 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 <= 650) and (y >= 280 and y <= 540) then drawfilloval (480, 515, 7, 7, yellow) drawfilloval (600, 515, 7, 7, yellow) elsif (x >= 650 and x <= 730) and (y >= 280 and y <= 540) then drawfilloval (600, 515, 7, 7, yellow) drawfilloval (880, 515, 7, 7, yellow) elsif (x >= 730 and x <= 800) and (y >= 280 and y <= 540) then drawfilloval (880, 515, 7, 7, brightred) elsif (x >= 800 and x <= 1200) and (y >= 280 and y <= 540) then drawfilloval (880, 515, 7, 7, brightgreen) drawfilloval (1005, 515, 7, 7, brightgreen) 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 |