
-----------------------------------
sakibur001
Wed Dec 21, 2016 2:58 pm

How to make something loop in a program and let a user click a button to continue?
-----------------------------------
What is it you are trying to achieve?
I want to have a street light loop in my intro but I want it to loop and still allow the user to click the main menu button to continue. 


What is the problem you are having?
I can make it loop and the button appear but the button is not working when i make the street light loop. I need help making the street lights loop forever and allow the user to click on the menu button. Code below.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

% Final Culminating Activity
% First Version of Driving Simulator (V1.0)

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 intfont : int

%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
    %put "" : 22, "Driving Simulator"
    Font.Draw ("Driving Simulator", 180, 480, intfont, black)
end title

% Introduction of the program/game to the user
body proc introduction
    mainMenuWindow := Window.Open ("position:top;left;graphics:500;500")
    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 ("gt40takingOff.wav")
    drawfillbox (15, 100, 85, 300, 7)
    drawfilloval (50, 150, 20, 20, brightgreen)
    delay (500)
    drawfilloval (50, 150, 20, 20, brightgreen)
    drawfilloval (50, 200, 20, 20, yellow)
    delay (500)
    drawfilloval (50, 200, 20, 20, yellow)
    drawfilloval (50, 250, 20, 20, brightred)
    delay (500)
    drawfilloval (50, 250, 20, 20, brightred)

end introduction


% Main Menu for the user to make choices regarding game play
body proc mainMenu
    Text.ColourBack (54)
    title
    locate (3, 27)
    %put "Main Menu"
    Font.Draw ("Main Menu", 200, 460, intfont, black)
    locate (6, 1)
    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)
    %Music.PlayFile ("tweety.wav")
end mainMenu

% 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")
    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 random cars, streets and lights appearing, which you  will have to obey, if you do not, consequences may apply." ..
    put " The goal of the game is to reach the end without hitting another  car, 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

% This is where the game is played
body proc playGame
    Window.Hide (mainMenuWindow)
    playGameWin := Window.Open ("graphics:700;900")
    Window.SetActive (playGameWin)
    title
    put ""
    put "THIS IS PLAY"
    mainMenuButton := GUI.CreateButton (100, 100, 0, "Return to the Main Menu", mainMenu)
end playGame

% Goodbye/End program
body proc goodBye
    goodByeWin := Window.Open ("position:top;right;graphics:500;500")
    Text.ColourBack (14)
    title
    locate (5, 1)
    put "Thanks for using this program/game!"
    put ""
    put "Program designed and created by:"
    Window.Hide (mainMenuWindow)
    Window.Hide (playGameWin)
    Window.Hide (instructWin)
    %Someone saying goodbye
    Music.PlayFile ("bugs_goodbye.wav")
    delay (1400)          % waits for a short time before closing the window
    Window.Hide (goodByeWin)
    GUI.Quit
end goodBye

%Main Program
introduction
loop
    exit when GUI.ProcessEvent
end loop
%End Progra

-----------------------------------
Insectoid
Wed Dec 21, 2016 4:45 pm

RE:How to make something loop in a program and let a user click a button to continue?
-----------------------------------
You're gonna have to restructure your introduction procedure. Turing can only do one thing at a time. Your light animation is one thing, so Turing has to play the whole thing before it can do anything else. If you want anything to happen while the animation is going on, you need to break the animation up into smaller chunks that happen one after another. That way, you can slip 'other stuff' in between the chunks and it'll look simultaneous. 

The easiest way is to break up the animation into frames. Draw one frame, then do something else, then draw another frame, then do something else. Because everything has to sync up, you cannot use delays anywhere, except once in your main loop. Your main loop will look like this:

loop
    %draw one frame of animation
    delay(10)
    GUI.ProcessEvent
end loop

the 'draw one frame' part will probably be a procedure. In your case, all that procedure does is decide what color light to draw and where to draw it. Then it exits.

proc drawLights
    if (we want red!) then
        %draw red light
    elsif (we want yellow!) then
        %draw red
    elseif (we want green!) then
        %draw green
    end if
end drawLights

That's the function. It only draws one frame. It has no loops, it has no delays. This function gets called every frame and every frame it decides what to draw. How it decides is up to you. The Time.Elapsed() function will be very helpful, I suspect.

-----------------------------------
TWizard
Fri Jan 06, 2017 10:40 pm

Re: How to make something loop in a program and let a user click a button to continue?
-----------------------------------
This is somewhat doable

You can draw what you want at the end of everything else for example

[code]
var counter : int := 0
var position_x, position_y : int := 50
var file: array 0 .. 9 of int

for i : 1 .. 32
    file (i) := Pic.FileNew ("FileName_" + intstr (i) + ".FileType")
end for

loop
%do stuff up here

if counter = 10 then
counter := 0
end it

Pic.Draw (file(counter) ,position_x,position_y,0)
counter := counter 1
end loop
[/code]

First
we initialize the images into an array, then create a counter. Each time the the loop goes through, it will play the images in the order of the file names, in this case its file_#, this works pretty well, the downside is that, the images depending on what the menu has to load, for example if there isnt much then it will be sped up, if there is a lot then it will be slowed down.

I hope this is helpful. I havent tested this but you probably could use a process, however its also shouldnt be used for something like this.
