
-----------------------------------
sakibur001
Tue Jan 17, 2017 1:48 pm

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 = 590 and y = 138 and x = 0 and y = 0 and x = 0 and y = 138 and x = 270 and y = 0 and x = 270 and y = 0 and x = 190 and y = 0 and x = 400 and y = 0 and x = 480 and y = 94 and x = 660 and y = 94 and x = 0 and y = 138 and x = 480 and y = 550 and x = 0 and y = 550 and x = 270 and y = 550 and x = 480 and y = 950 and x = 0 and y = 950 and x = 270 and y = 950 and x = 480 and y = 950 and x = 630 and y = 500 and x = 0 and y = 500 and x = 660 and y = 900 and x = 0 and y = 900 and x = 660 and y = 1310 and x = 190 and y = 1310 and x = 400 and y = 180 and x = 195 and y = 180 and x = 405 and y = 650 and x = 227 and y = 90 and x = 40 and y = 140 and x = 100 and y = 140 and x = 400 and y = 570 and x = 100 and y = 90 and x = 70 and y = 90 and x = 125 and y = 90 and x = 350 and y = 130 and x = 280 and y = 300 and x = 280 and y = 500 and x = 280 and y = 650 and x = 280 and y = 730 and x = 280 and y = 800 and x = 280 and y = 100 and x = 190 and y = 300 and x = 190 and y = 500 and x = 190 and y = 810 and x = 190 and y = 920 and x = 190 and y = 920 and x = 540 and y 