Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Program ending abruptly.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JaredMezz




PostPosted: Thu May 14, 2015 5:53 pm   Post subject: Program ending abruptly.

What is it you are trying to achieve?
I am making a game for my class in which you are an astronaut attempting to get back into his ship. The game includes a title screen, help menu & game menu.


What is the problem you are having?
So far everything has been working smoothly but when I'm in my help menu and click return, the program says it has finished. I need it to just close the help menu and continue on it's normal path.


Describe what you have tried to solve this problem
I am honestly not sure what to do, I am ahead of my class working with the GUI stuff as we have not covered it quite yet.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
I can upload a file as well if you'd like.

Turing:

import GUI

%Variables
var userName : int
var xResolution, yResolution : int
var userResolution : int
var fontOne, fontTwo, fontThree : int
var startingWin, gameWin, helpWin : int %Variables to identify the windows
var swBackground : int := Pic.FileNew ("BG1.bmp")
var playBackground : int := Pic.FileNew ("PlayBackground.bmp")
var randXPos, randYPos : int
var helpMenu : int := Pic.FileNew ("HelpMenu.bmp")

%Game Variables
var background : int
var spaceship, spaceshipSprite : int
var astronaut, astronautSprite : int
var xPosition2 : int := 0
var yPosition2 : int := 720
var xPosition, yPosition : int := 0
var speed, astroSpeed : int := 8
var keys : array char of boolean
var counter : int := 0
var mouseX, mouseY, mouseButton : int

%Opening the starting window.
startingWin := Window.Open ("graphics:640;480,position:600;400,title:SpaceWalk")

%Font assigning
fontOne := Font.New ("Impact:20")
fontTwo := Font.New ("Impact:15")
fontThree := Font.New ("Impact:100")

%Spaceship
spaceship := Pic.FileNew ("spaceship.bmp")
spaceshipSprite := Sprite.New (spaceship)

%Astronaut
astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
astronautSprite := Sprite.New (astronaut)

%Spaceship
spaceship := Pic.FileNew ("spaceship.bmp")
spaceshipSprite := Sprite.New (spaceship)

%Astronaut
astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
astronautSprite := Sprite.New (astronaut)

%Starting Window
Pic.Draw (swBackground, 0, 0, picCopy)

Font.Draw ("Welcome to Space Walk!", 180, 400, fontOne, gray)
Font.Draw ("Welcome to Space Walk!", 182, 402, fontOne, blue)

Pic.Draw (astronaut, 80, 380, picMerge)
Pic.Draw (spaceship, 470, 390, picMerge)


%Creating the text box
Font.Draw ("Username", 263, 228, fontOne, gray)
Font.Draw ("Username", 265, 230, fontOne, 55)

var textbox : int
var name : string := ""

procedure setName (name : string)
end setName

textbox := GUI.CreateTextField (263, 200, 120, "", setName)


%This is where I draw the two buttons
%Procedure one, when someone clicks the 1920x1080 button
Font.Draw ("Resolution", 263, 130, fontOne, gray)
Font.Draw ("Resolution", 265, 128, fontOne, 55)

procedure Res1080
    xResolution := 1920
    yResolution := 1080
    Font.Draw ("Your resolution has been set to 1080p", 178, 60, fontTwo, gray)
    Font.Draw ("Your resolution has been set to 1080p", 180, 60, fontTwo, 55)
    delay (1000)
    Window.Close (startingWin)
    gameWin := Window.Open ("graphics:1920;1080,position:maxx div 2;maxy div 2,title:SpaceWalk")
    randint (randXPos, 1, 1500)
    randint (randYPos, 1, 1500)
    GUI.Disable (textbox)
    Pic.Draw (playBackground, -randXPos, -randYPos, picCopy)
    %Spaceship
    spaceship := Pic.FileNew ("spaceship.bmp")
    spaceshipSprite := Sprite.New (spaceship)

    %Astronaut
    astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
    astronautSprite := Sprite.New (astronaut)

    %Spaceship
    spaceship := Pic.FileNew ("spaceship.bmp")
    spaceshipSprite := Sprite.New (spaceship)

    %Astronaut
    astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
    astronautSprite := Sprite.New (astronaut)

    %Showing Sprites
    Sprite.Show (spaceshipSprite)
    Sprite.Show (astronautSprite)

    %Top left lives
    Font.Draw ("Landings:", 10, 691, fontTwo, gray)
    Font.Draw ("Landings:", 10, 690, fontTwo, brightred)

    %Loop
    loop
        Mouse.Where (mouseX, mouseY, mouseButton)

        %Setting their starting point and the points after
        Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
        Sprite.SetPosition (astronautSprite, mouseX, mouseY, true)

        Input.KeyDown (keys)

        %Spaceship
        if keys ('w') then
            yPosition := yPosition + speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if yPosition >= 1080 then
                yPosition := yPosition - 8
            end if
        end if

        if keys ('s') then
            yPosition := yPosition - speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if yPosition <= 0 then
                yPosition := yPosition + 8
            end if
        end if

        if keys ('a') then
            xPosition := xPosition - speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if xPosition <= 0 then
                xPosition := xPosition + 8
            end if
        end if

        if keys ('d') then
            xPosition := xPosition + speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if xPosition >= 1920 then
                xPosition := xPosition - 8
            end if
        end if

        %Collision to end it
        if xPosition + Pic.Width (spaceship) > mouseX then
            if xPosition - Pic.Width (spaceship) < mouseX then
                if yPosition + Pic.Height (spaceship) > mouseY then
                    if yPosition - Pic.Height (spaceship) < mouseY then
                        counter := counter + 1
                        if counter = 5 then
                            Font.Draw (intstr (counter), 90 + (counter * 10), 691, fontTwo, gray)
                            Font.Draw (intstr (counter), 90 + (counter * 10), 690, fontTwo, brightred)

                            Font.Draw ("Game Over", 320, 310, fontThree, 8)
                            Font.Draw ("Game Over", 322, 312, fontThree, 54)

                            Font.Draw ("Thanks for playing!", 500, 275, fontOne, 8)
                            Font.Draw ("Thanks for playing!", 501, 276, fontOne, 54)

                            exit
                        end if

                        Font.Draw (intstr (counter), 90 + (counter * 10), 691, fontTwo, gray)
                        Font.Draw (intstr (counter), 90 + (counter * 10), 690, fontTwo, brightred)

                        delay (1000)

                        xPosition := 0
                        yPosition := 0
                        xPosition2 := 0
                        yPosition2 := 720
                    end if
                end if
            end if
        end if
        delay (10)
    end loop
end Res1080

%What happens when someone clicks the 1280x720 button
procedure Res720
    xResolution := 1280
    yResolution := 1080
    Font.Draw ("Your resolution has been set to 720p", 178, 60, fontTwo, gray)
    Font.Draw ("Your resolution has been set to 720p", 180, 60, fontTwo, 55)
    delay (1000)
    Window.Close (startingWin)
    gameWin := Window.Open ("graphics:1280;720,position:maxx div 2;maxy div 2,title:SpaceWalk")
    randint (randXPos, 1, 2000)
    randint (randYPos, 1, 2000)
    GUI.Disable (textbox)
    Pic.Draw (playBackground, -randXPos, -randYPos, picCopy)

    %Spaceship
    spaceship := Pic.FileNew ("spaceship.bmp")
    spaceshipSprite := Sprite.New (spaceship)

    %Astronaut
    astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
    astronautSprite := Sprite.New (astronaut)

    %Spaceship
    spaceship := Pic.FileNew ("spaceship.bmp")
    spaceshipSprite := Sprite.New (spaceship)

    %Astronaut
    astronaut := Pic.FileNew ("KSPAstronautFix.bmp")
    astronautSprite := Sprite.New (astronaut)

    %Showing Sprites
    Sprite.Show (spaceshipSprite)
    Sprite.Show (astronautSprite)

    %Top left lives
    Font.Draw ("Landings:", 10, 691, fontTwo, gray)
    Font.Draw ("Landings:", 10, 690, fontTwo, brightred)



    %Loop
    loop
        Mouse.Where (mouseX, mouseY, mouseButton)

        %Setting their starting point and the points after
        Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
        Sprite.SetPosition (astronautSprite, mouseX, mouseY, true)

        Input.KeyDown (keys)

        %Spaceship
        if keys ('w') then
            yPosition := yPosition + speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if yPosition >= 720 then
                yPosition := yPosition - 8
            end if
        end if

        if keys ('s') then
            yPosition := yPosition - speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if yPosition <= 0 then
                yPosition := yPosition + 8
            end if
        end if

        if keys ('a') then
            xPosition := xPosition - speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if xPosition <= 0 then
                xPosition := xPosition + 8
            end if
        end if

        if keys ('d') then
            xPosition := xPosition + speed
            Sprite.SetPosition (spaceshipSprite, xPosition, yPosition, true)
            if xPosition >= 1280 then
                xPosition := xPosition - 8
            end if
        end if

        %Collision to end it
        if xPosition + Pic.Width (spaceship) > mouseX then
            if xPosition - Pic.Width (spaceship) < mouseX then
                if yPosition + Pic.Height (spaceship) > mouseY then
                    if yPosition - Pic.Height (spaceship) < mouseY then
                        counter := counter + 1
                        if counter = 5 then
                            Font.Draw (intstr (counter), 90 + (counter * 10), 691, fontTwo, gray)
                            Font.Draw (intstr (counter), 90 + (counter * 10), 690, fontTwo, brightred)

                            Font.Draw ("Game Over", 320, 310, fontThree, 8)
                            Font.Draw ("Game Over", 322, 312, fontThree, 54)

                            Font.Draw ("Thanks for playing!", 500, 275, fontOne, 8)
                            Font.Draw ("Thanks for playing!", 501, 276, fontOne, 54)

                            exit
                        end if

                        Font.Draw (intstr (counter), 90 + (counter * 10), 691, fontTwo, gray)
                        Font.Draw (intstr (counter), 90 + (counter * 10), 690, fontTwo, brightred)

                        delay (1000)

                        xPosition := 0
                        yPosition := 0
                        xPosition2 := 0
                        yPosition2 := 720
                    end if
                end if
            end if
        end if
        delay (10)
    end loop
end Res720

%Returning to menu
procedure returnToMenu
    Window.Show (startingWin)
    Window.Close (helpWin)

end returnToMenu

%Help Menu Procedure
procedure helpWindow
    Window.Hide (startingWin)
    helpWin := Window.Open ("graphics:720;480,position:maxx div 2;maxy div 2,title:SpaceWalk | Help")

    Pic.Draw (helpMenu, 0, 0, picCopy)
    var buttonRes4 : int := GUI.CreateButton (325, 20, 0, "Return", returnToMenu)
end helpWindow

%Creating the buttons
var buttonRes1 : int := GUI.CreateButton (182, 95, 0, "1920x1080", Res1080)
var buttonRes2 : int := GUI.CreateButton (382, 95, 0, "1280x720", Res720)
var buttonRes3 : int := GUI.CreateButton (100, 95, 0, "Help", helpWindow)


loop
    name := GUI.GetText (textbox) %Getting what's in the username text box
    exit when GUI.ProcessEvent
end loop






Please specify what version of Turing you are using
4.1.1

Please excuse how messy this most likely is, we haven't covered procedures or GUI in class as of yet so I was just going off of the help menu's and stuff I've found here. Much appreciated (:
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri May 15, 2015 2:51 pm   Post subject: RE:Program ending abruptly.

At the bottom of your code you have a loop that exits when GUI.ProcessEvent returns true. This means that when you click a button, the associated procedure will run. At the end of that procedure it returns to the loop, which sees that GUI.ProcessEvent is true (since you clicked a button), so it exits the loop. After the loop there is no more code, so the program ends.

If you want it to keep running, you either need to give it more code to run, or tell it to run previous code again.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: